Home Forums SQL Server 2005 T-SQL (SS2K5) request on a query to capture the cell area a given x,y coordinate would be found in? RE: request on a query to capture the cell area a given x,y coordinate would be found in?

  • This sounds like a job for the Geometry data type and spatial functions. The following query works for your example.

    IF OBJECT_ID ('dbo.SpatialTable') IS NOT NULL

    DROP TABLE dbo.SpatialTable;

    GO

    CREATE TABLE SpatialTable

    (Cell CHAR(2),

    GeomCol GEOMETRY);

    GO

    INSERT INTO SpatialTable (Cell, GeomCol)

    VALUES ('A1', 'POLYGON ((0 0, 100 0, 100 100, 0 100, 0 0))');

    GO

    INSERT INTO SpatialTable (Cell, GeomCol)

    VALUES ('A2', 'POLYGON ((101 0, 200 0, 200 100, 101 100, 101 0))');

    GO

    INSERT INTO SpatialTable (Cell, GeomCol)

    VALUES ('B1', 'POLYGON ((0 101, 100 101, 100 200, 0 200, 0 101))');

    GO

    INSERT INTO SpatialTable (Cell, GeomCol)

    VALUES ('B2', 'POLYGON ((101 101, 200 101, 200 200, 101 200, 101 101))');

    GO

    DECLARE @Point GEOMETRY;

    SET @Point = 'POINT (25 130)'

    SELECT

    Cell

    FROM

    SpatialTable

    WHERE

    GeomCol.STIntersects(@Point) = 1;

    SET @Point = 'POINT (105 40)'

    SELECT

    Cell

    FROM

    SpatialTable

    WHERE

    GeomCol.STIntersects(@Point) = 1;

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]