• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 824
  • Last Modified:

SQL Select records where Lat/Long fall within Polygon

Dear Experts

I have a page where users are able to draw a polygon on a google map.  I am posting the following variables through to the results page via a form:

minX  (minimum longitude)
maxX (maximum longitude)
minY  (minimum latitude)
maxY (maximum latitude)

The records in my DB table hold the latitude and longitude values and I would like to be able to select the records where the latidude and longitude fall within the area selected.

Can anyone advise of the sql syntax to be able to achieve this?

See following page as example of where I am at with this:  http://www.bspc.co.uk/forsalemap.asp

Thanks for any assistance

  • 2
1 Solution
We've done this for several GPS navigation apps. What you normally do to support polygons is to use "Point in Polygon" algorithms, such as http://en.wikipedia.org/wiki/Point_in_polygon 

We usually use the line intersection algorithm.

If you just want to support square regions, then SQL is fine, but polygons are a different issue.
You may want to look at using the Spatial support in SQL Server. There are builtin functions for checking whether a polygon contains a point, etc. I was not aware of them at the time I posted the above.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now