Test whether or not a coord exisits in polygon defined by geocodes

Posted on 2010-08-31
Last Modified: 2012-05-10
I have a list of geocodes that define a polygon on a map and I want to find if a particular geocode lands within the values. The polygon shape in this case is like a 7 but changes in nearly all cases. We are using SQL 2008 and I can define the table any way needed to take a single geocode and return true or false.

Question by:jgoodale
  • 3
  • 2
LVL 14

Expert Comment

by:Daniel Junges
ID: 33565661
first transform you coords in one 2d coord system like utm, then you can use the follow function:

public bool isPointInside( Ponto ponto )
    int i, j;
    bool res = false;
    for ( i = 0, j = Count - 1; i < Count; j = i++ ) {
        if ( (((polygono[i].Y <= ponto.Y) && (ponto.Y < polygono[j].Y)) ||
        ((polygono[j].Y <= ponto.Y) && (ponto.Y < polygono[i].Y))) &&
        (ponto.X < (polygono[j].X - polygono[i].X) * (ponto.Y - polygono[i].Y) / (polygono[j].Y - polygono[i].Y) + polygono[i].X) )
            res = !res;
    return res;

Author Comment

ID: 33568268
Can you clarify you answer more? I'm not sure what a 2d coord system like utm is..

LVL 14

Expert Comment

by:Daniel Junges
ID: 33569246

You cannot use this function if you coord are in a form of degrees

Accepted Solution

jgoodale earned 0 total points
ID: 33569427
Ok, I found my own solution using spacial data in MSSQL server 2008. This allows my to take a simple geocode string and test to find if the coords fit inside a polygon using MSSQL.

First off I created a table that contains defined polygons of the areas I want to segment:

CREATE TABLE [dbo].[lku_subdivision](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [coords] [geometry] NULL,
      [subdivision] [varchar](50) NULL,
      [id] ASC

Then I insert a polygon I defined using this Google map tool:
I copied the coords from the list in that tool and formatted them for the insert below.

INSERT INTO lku_subdivision (coords,subdivision) VALUES (geometry::STGeomFromText ('POLYGON ((-117.215066 33.094867 ,-117.211483 33.094903 ,-117.210753 33.094112 ,-117.210538 33.093213 ,-117.210538 33.092458 ,-117.211611 33.091775 ,-117.213328 33.091506 ,-117.215302 33.092153 ,-117.216675 33.09307 ,-117.216611 33.093897 ,-117.216311 33.094508 ,-117.21586 33.094885 ,-117.215323 33.094903 ,-117.215066 33.094867 ,-117.215066 33.094867))', 0),'Mahogany');

Now I have a table with my defined polygon and I want to find if a geocoded point fits within the table, If so update a field in my database with the associated name of the subdivision from the table where my polygon is defined:

-- Description:      <Description,,>
-- =============================================
CREATE PROCEDURE updatePropertySubdivision
@pointStr varchar(100),
@sysid int
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      DECLARE @newSubDivision Varchar(100)
      DECLARE @point1 AS GEOMETRY = 'POINT(' + @pointStr + ')'
    -- Insert statements for procedure here
      SELECT @newSubDivision=subdivision from lku_subdivision WHERE coords.STContains((@point1))=1;
      If @newSubDivision IS NOT NULL
            UPDATE properties SET subdivisionManual=@newSubDivision WHERE sysid=@sysid;

The simple test to see if the coords are inside the polygon is below (note that the STContains needs a point and not a string):
SELECT subdivision from lku_subdivision WHERE coords.STContains((@point1))=1


Author Closing Comment

ID: 33569437

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSIS Conditional Split 7 31
best counters for cpu high usage 3 25
replicated - directional or bidirectional? 3 29
SQL Server 2012 - Merge Replication Issue 1 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

813 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

12 Experts available now in Live!

Get 1:1 Help Now