request on a query to capture the cell area a given x,y coordinate would be found in?

  • Hi,

    I have a problem I would like to solve that I believe is possible with SQL query although I don't know if it would require use of advanced functions.

    If we look at the diagram below (pardon me for the poor translation to pure text). We can see that I have attempted to show 4 cells: A1,A2,B1,B2.

    If each cell is a square with the four points of a square each having an x,y coordinate it would be depicted below. All 4 cells are touching each other exactly adjacently.

    x,yx,y

    0,0100,0 101,0200,0

    A1 A2

    0,100100,100101,100200,100

    0,101100,101101,101200,101

    B1 B2

    0,200100,200101,200200,200

    With this in place if I translate this to a database table as such:(please excuse the poor off setting of the field headers.)

    Upperleft_X Upperleft_Y Upperright XUpperright_YLowerleft_X Lowerleft_YLowerright_XLowerright_YCell

    0010000100100100A1

    01011001010200100200B1

    10102000101100200100A2

    101101200101101200200200B2

    This table shows that record 1 for cell A1 has 4 x,y points.

    My question is if I were to provide an X input and a Y input where X is between 0 and 200 and the Y input is between 0 and 200 what would the query be to be able to return the value of the Cell Field:

    for example: x= 25 and y = 130 would return a result of : A2

    another example: x= 105 and Y = 40 would return a result of : B1

    Is this even possible in just SQL server? How would this query scale to say a table with 10,000 records

    The parameters would be to about 8 precision points so that we are looking at using > or >= operators to get the X and Y parameters to fall within the contrained cell areas.

    If not possible with SQL server, can you suggest another programming framework to use or combination there of?

    Any response or even the actual working SQL query would be greatly appreciated.

    thanks

    Haggisns

  • Hi,

    Please read this article and post some sample data that can easily be used.

    You will get help much quicker with this information.

    Forum Etiquette: How to post data/code on a forum to get the best help

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • 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]

  • thanks The SQL Guy,

    I am still installing SQL Server 2008 and SSMS to see how this all works out.

    I will endeavor to post properly in the future.

  • My apologies for posting a Spatial answer in the 2005 forum. I must learn to read properly 🙂

    To answer your question for the 2005 platform I must ask a question. Are the shapes always square or are they polygons?

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Yes, they are always squares.

  • If they're always squares, perhaps something like this?

    WITH Squares

    (

    Upperleft_X, Upperleft_Y, Upperright_X, Upperright_Y

    ,Lowerleft_X, Lowerleft_Y, Lowerright_X, Lowerright_Y, Cell

    ) AS (

    SELECT 0, 0, 100, 0, 0, 100, 100, 100, 'A1'

    UNION ALL SELECT 0, 101, 100, 101, 0, 200, 100, 200, 'B1'

    UNION ALL SELECT 101, 0, 200, 0, 101, 100, 200, 100, 'A2'

    UNION ALL SELECT 101, 101, 200, 101, 101, 200, 200, 200, 'B2')

    ,Points (n, x, y) AS (

    SELECT 1, 25, 130

    UNION ALL SELECT 2, 105, 40)

    SELECT a.n, b.Cell, x, y, Upperleft_X, Upperright_X, Lowerleft_X, Lowerright_X

    , Upperleft_Y, Upperright_Y, Lowerleft_Y, Lowerright_Y

    FROM Points a

    JOIN Squares b

    ON (x BETWEEN Upperleft_X AND Lowerright_X AND

    y BETWEEN Upperleft_Y AND Lowerright_Y);


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Hi,

    If you are only dealing with squares it is quite easy.

    Firstly you only need the top left and bottom right co-ordinates:

    CREATE TABLE Squares

    (CellNo CHAR(2) NOT NULL,

    TopLeftX INT NOT NULL,

    TopLeftY INT NOT NULL,

    BottomRightX INT NOT NULL,

    BottomRightY INT NOT NULL);

    INSERT INTO Squares VALUES

    ('A1', 0, 0, 100, 100),

    ('A2', 0, 101, 100, 200),

    ('B1', 101, 0, 200, 100),

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

    Secondly, to make it scale to 10,000+ records you need to index the four co-ordinates. I would cluster the table on these columns:

    ALTER TABLE Squares

    ADD CONSTRAINT SquaresCoordinates UNIQUE CLUSTERED (TopLeftX, TopLeftY, BottomRightX, BottomRightY);

    Lastly, you just need to query for any values where the X and the Y each falls between the upper left and bottom right range. You can use a join as Dwain showed you above or you can specify the points in a where clause.

    SELECT

    *

    FROM

    Squares

    WHERE

    (25 BETWEEN TopLeftX AND BottomRightX

    AND 130 BETWEEN TopLeftY AND BottomRightY)

    OR

    (105 BETWEEN TopLeftX AND BottomRightX

    AND 40 BETWEEN TopLeftY AND BottomRightY);

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Viewing 8 posts - 1 through 7 (of 7 total)

    You must be logged in to reply to this topic. Login to reply