SQL Select records where Lat/Long fall within Polygon

Posted on 2011-05-01
Last Modified: 2012-05-11
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:

Thanks for any assistance

Question by:grantballantyne
    LVL 40

    Accepted 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

    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.
    LVL 40

    Expert Comment

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now