Spatial SQL for the Geographer – STDistance Example

When I put together the Spatial SQL for the Geographer posts I never wrote anything about measuring distance.  Geographers often need to know the distance between locations, as distance decay is one of those things we find important.  Fortunately for us, measuring distance between points is pretty easy using Spatial SQL (and pretty fast).  The following query demonstrates an example of measuring distance between a set of defined points from the populated places data available in the sample data.

select 
t1.Id as ID, 
t1.NAMEASCII as t1_Name, 
t1.SOV0NAME as t1_SOV0NAME, 
t1.Latitude as t1_Latitude, 
t1.Longitude as t1_Longitude, 
t2.ID as t2_ID, t2.NAMEASCII as t2_Name, 
t2.SOV0NAME as t2_SOV0NAME, 
t2.LATITUDE as t2_Latitide, 
t2.LONGITUDE as t2_Longitude,
Geography::Point(t1.Latitude, t1.Longitude, 4326).STDistance(Geography::Point(t2.LATITUDE, t2.LONGITUDE, 4326)) as Distance_Meters
from dbo.Populated_Places t1 cross join dbo.Populated_Places t2
where t1.SOV0NAME = 'United States' and  t2.SOV0NAME = 'United States'

You will notice that in the query a few different things are happening:

  • A self cross join is used to take each record and join it to every other record.  This is done to get the lat/long combo for each city and matched up with every other city.  There is probably a better way to do this, but for this example it works.
  • In order to calculate distance two points are generated on the fly from the columns by calculating the point with Geography::Point(LAT, LONG, SRID).  Setting the SRID is important, as that will impact the measurement.  More often than not the user will be using an SRID of 4326 (WGS84), unless they are using projected data.  For more info on SRIDs check out this handy Wikipedia article.
  • You could replace Geography with Geometry when generating the points.  Doing so would lead to vastly different measurement results because of the way distance is calculated on the plane or on the spheroid.  When using the Geography data type the distance results are returned in meters while with the Geometry data type the results are reported in decimal degrees.
  • Once the points are created on either side of the STDistance method the query can be run.  The documentation provides a couple notes about using STDistance. The SRIDs need to match between the points and there is some error involved when measuring distance.  The distance measurement error is important to understand if one has tight tolerance they need to adhere to.

The results of this query will return a set of columns with the measurement in meters from every record to every other record for US cities.  With some additional coding you can generate a distance matrix, find the nearest locations, or create line features between points pretty quickly.  This query can also go pretty fast, if the proper indexes are generated and called in the query it should take less than a minute.  Not to bad when returning over 600k records.

Spatial SQL Tools from Codeplex

When I wrote the Spatial SQL (or SQL Spatial, whatever you want to call it, I use both interchangeably for better or worse) blog posts I never mentioned a set of tools that are available which greatly expand the capabilities of the spatial analysis within SQL.

http://sqlspatialtools.codeplex.com/

If you are working with spatial data within SQL Server I encourage you to check this out.  I know this works in SQL Server 2008 R2, but I cannot vouch for its usefulness in SQL Server 2012, as I have not used it yet.  For a complete list of the functionality available on Codeplex click here.

Until next time, keep on geo-ing!

GISDoctor.com Updates

I made a few updates to a number of pages on the site. The GIS Blogs page is now called Geo Blogs and I added a few more blogs to the list. Also, I made sure the mash-ups available on the map mash-up page all work.  I try to keep up with the mash-up examples, but sometimes data sources change and I need to update the code.

Thanks for reading!

Sunday Morning Geo-Fun

Why is it that the only time I have to blog is on Sunday mornings?  Here are a few quick geo-items that tickled my fancy from the previous week.

  • Check out the article, The New Cartographers: OpenStreetMap’s World Takeover, from Carl Franzen at Talking Points Memo.  The first two parts of this story have been tweeted a lot this last week and I can see why.  The article provides a fairly good overview of OSM, including some background on the project, the nuances of licensing OSM data, and adoption in the tech industry.  Part three of the article comes out on Sunday.  Makes me feel good about calling 2012 the year of OSM.
  • Years ago I used to pump out Google Map Mash-Ups on a regular basis, some of which were developed during my time at the Map and Geographic Center at the University of Connecticut.  Well, after nearly two years one of those mash-ups got some press!  Check out the article in the Atlantic, Pre-Sprawl Aerial Images:’Next Best Thing to a Time Machine. The article discusses the dual-map mash-up that I developed for the On the Line Project that is used to compare the drastic changes in Connecticut’s landscape using current and historical aerial photography.  Pretty cool.
  • The guys at Google’s NC data center, which just got the indoors street view treatment, definitely Rickrolled streetview (Check out the image on the screens, also, why didn’t they blur out Rick Astley’s face too?).
  • Brian Flood has been doing a lot of great things for the online mapping and spatial data communities for a while now.  This video and post on the MapBox blog is the latest example.  Using Arc2Earth Sync to integrate with MapBox and ArcGIS appears smooth and simple.  Awesome.  There is a lot of great work happening in “spatial” and it’s only going to make what we do as geo-professionals better.
  • Speaking of MapBox, when does Esri try to scoop them up (if they haven’t already), like they just did with GeoLoqi?
  • Avid Geo Boston had their October meet-up this past week.  The video is here. Since I am a horrible member and missed the meet-up for the third straight month I cannot comment on the talks, but I’m sure everyone had a good time.
  • Avid Geo will be hosting their wildly successful annual Ignite Spatial event on November 14th at the Center for Geographic Analysis at Harvard.  Tickets are available here, and they are currently looking for presenters.
  • Don’t forget to take the totally unscientific GISDoctor.com ArcGIS 10.1 survey!
  • Finally, I’ll be updating some pages on my site this week, including the blogs page and some of the mash-ups.
  • As always, follow me on twitter @GISDoctor, and hopefully I’ll blog more this week.  I have tons of ideas!

 

Sunday Morning Geo-Fun

I use Weather Underground for the my weather needs, especially the handy-dandy web-mapping application for “Is it going to rain on my run?” weather report.  Just recently, I was checking the radar before heading out and noticed what appeared to be a technical malfunction, or the opening scenes to an epic movie.

What is happening?  Were there massive, freak storms happening throughout the eastern US?  No.

What you are probably seeing are radar blobs/radar blooms.  It took a couple minutes of Googling, but here is a 2007 article from AccuWeather.com explaining what you see on the map.  Apparently I observed a phenomena actually known as Ground Clutter.  I’m neither a radar or weather expert so I won’t comment on what is technically happening but it’s still pretty neat to see on a map.

Whaddya think of 10.1?

ArcGIS 10.1 has been out for a few months now and I am curious as to what the GIS public thinks of the newest release.  I haven’t upgraded any of my Esri products to the newest release yet, and probably won’t for a while, but I want to gauge the public’s reaction to the most recent “dot” release.

If you have any other thoughts or comments on ArcGIS for Desktop (ArcMap), ArcGIS Online, Esri Maps for Excel, or anything else spatial leave a message!

I’ll post the results in a couple weeks.

Here is the awesome, non-scientific, one question survey.  Take it!

Create your free online surveys with SurveyMonkey, the world’s leading questionnaire tool.

Avid Geo September Meet-Up! [UPDATE!]

Avid Geo September Meet-Up is coming up this week. Check it out…

http://meetu.ps/jLlFg

And, again, I can’t make it. Why am I always busy on the third Thursday of the month…

UPDATE – Guido Stein, leader of the Avid Geo group, sent out an update about this month’s meet-up:

Just wanted to give you a heads up that the meeting is coming up on Thursday and we are going to be hosted by the wonderful people at Applied Geographics also know to me as my company.

This month Lars will be giving us a little demo on basic OSM editing and I will be sharing a little python code for data for geometric manipulation.

We will be trying to record and push this meeting onto youtube at the avidgeo to share the meeting with the rest of the world…

Want more info?  Check out (and join) the Avid Geo meet-up group.

Report Bugs! Do it!

Like all of you, I run into the famous (infamous?) Esri error report, more often than I like.  Ever wonder what happens when you actually report the error (which I do when I have the patience)?  Check this out this blog post from Esri’s Resource Center.  One item that I took away from the article is that I need to add more information to my error report than just my frustrated babbling, as Esri’s error reporting algorithms evaluate the available information before it is passed on to the engineers.

If you are a heavy Esri user and run into the error report often you should take the five minutes to read the post.

Avid Geo Meet-Up – Thursday, 8/16/2012!

Avid Geo will be holding it’s monthly meeting for August this coming Thursday at Dogpatch   Labs in Cambridge.  Unfortunately I can’t make this month’s meeting but it looks to another good one.  David Zwarg, from Azavea will be talking about PostGIS raster formats and supporting GDAL drivers.  I’ve seen David speak a couple times now and its always about some pretty interesting stuff.  So, if you are into PostGIS,  GDAL, and like “talking shop” with some people in the know then head over to Cambridge this Thursday!