We have a spatial table (dbo.Map) with eleven polygons representing eleven regions. This table has unique IDs, names, and other meta data associated with it, as well as a geography data-type column called geo. We also have a separate table (dbo.points) with a list of points with their lat/longs that are in the regions defined by the first table. The two tables don't have anything to join them on, unless we are able to join a lat/long to a geography column. How can we return a list of all the points in a given region?
Asked
Active
Viewed 1,502 times
0
-
Assuming you are using an MS-SQLServer this might help: http://stackoverflow.com/questions/11054149/sql-server-2008-spatial-find-a-point-in-polygon – Peter Paul Kiefer May 22 '15 at 21:42
1 Answers
0
Here's how I'd do it:
First, I have to infer your schema. I'll choose something like this:
create table dbo.Map (
MapID int identity not null,
geo geography not null
);
create table dbo.Points (
PointID int identity not null,
Latitude decimal(9, 7),
Longitude decimal(10, 7),
geo as geography::Point(Latitude, Longitude, 4236) persisted
);
Next, the select:
select PointID, MapID
from dbo.Points as p
left join dbo.Map as m
on p.geo.STIntersects(m.geo) = 1
You'd probably want to add some sort of where clause, but that's the bare bones "how do I find which region my point is in?".
Also note that the points table has a persisted computed column that is a geography instance that represents the point given by the Longitude and Latitude columns. This is good for a couple of reasons:
- You can't put junk (e.g.
abs(Latitude) > 90
) data into Latitude and Longitude. - You get the geography instance stored in-row so you don't have to calculate it every time

Ben Thul
- 31,080
- 4
- 45
- 68