Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

request on a query to capture the cell area a given x,y coordinate would be found in? Expand / Collapse
Author
Message
Posted Monday, September 9, 2013 1:41 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, September 12, 2013 7:29 AM
Points: 14, Visits: 41
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,y x,y
0,0 100,0 101,0 200,0
A1 A2
0,100 100,100 101,100 200,100

0,101 100,101 101,101 200,101
B1 B2
0,200 100,200 101,200 200,200

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

Upperleft_X Upperleft_Y Upperright X Upperright_Y Lowerleft_X Lowerleft_Y Lowerright_X Lowerright_Y Cell
0 0 100 0 0 100 100 100 A1
0 101 100 101 0 200 100 200 B1
101 0 200 0 101 100 200 100 A2
101 101 200 101 101 200 200 200 B2

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
Post #1492908
Posted Monday, September 9, 2013 3:46 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 5:24 PM
Points: 1,805, Visits: 5,867
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


  • MMGrid Addin
  • MMNose Addin


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

  • Post #1492933
    Posted Tuesday, September 10, 2013 5:13 AM


    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: Today @ 5:09 AM
    Points: 971, Visits: 3,009
    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

    @SeanPearceSQL

    About Me
    Post #1493107
    Posted Tuesday, September 10, 2013 8:32 AM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Thursday, September 12, 2013 7:29 AM
    Points: 14, Visits: 41
    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.

    Post #1493213
    Posted Wednesday, September 11, 2013 7:14 AM


    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: Today @ 5:09 AM
    Points: 971, Visits: 3,009
    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

    @SeanPearceSQL

    About Me
    Post #1493661
    Posted Wednesday, September 11, 2013 12:15 PM
    Grasshopper

    GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

    Group: General Forum Members
    Last Login: Thursday, September 12, 2013 7:29 AM
    Points: 14, Visits: 41
    Yes, they are always squares.
    Post #1493841
    Posted Wednesday, September 11, 2013 9:12 PM


    Hall of Fame

    Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

    Group: General Forum Members
    Last Login: Today @ 4:53 AM
    Points: 3,422, Visits: 5,368
    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!

    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?
    Since random numbers are too important to be left to chance, let's generate some!
    Learn to understand recursive CTEs by example.
    Splitting strings based on patterns can be fast!
    Post #1493981
    Posted Thursday, September 12, 2013 2:22 AM


    SSC Eights!

    SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

    Group: General Forum Members
    Last Login: Today @ 5:09 AM
    Points: 971, Visits: 3,009
    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

    @SeanPearceSQL

    About Me
    Post #1494048
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse