简体   繁体   中英

How to return more than one field using a STIntersection query in SQL Server

I am calculating polygon intersections within SQL Server and have a table as shown below based on the example here: http://msdn.microsoft.com/en-gb/library/cc280766.aspx where 'Coordinates_OGC_WKT' is computed.

+--------+----------------------+------------------------------------+------------+
| ZoneId |     Coordinates      |        Coordinates_OGC_WKT         |  ZoneName  |
+--------+----------------------+------------------------------------+------------+
|      1 | 0xE61000000 [etc...] | POLYGON ((-122.358 47.653 [etc...] | Polygon    |
|      2 | 0xE61000000 [etc...] | LINESTRING (-122.36 47 [etc...]    | Linestring |
+--------+----------------------+------------------------------------+------------+

I have tested the proposed SP as below and it works however I am unsure how to also return the 'ZoneName' field - which is important.

DECLARE @geocoords geography;
DECLARE @geoline geography;
DECLARE @result geography;

SELECT @geocoords = Coordinates FROM SpatialZonePolygons WHERE ZonePolygonId = 1;
SELECT @geoline = Coordinates FROM SpatialZonePolygons WHERE ZonePolygonId = 2;
SELECT @result = @geoline.STIntersection(@geocoords);
SELECT @result.STAsText() AS WKT;

The result shows the intersection coordinates but doesn't indicate the row in the database that holds the polygon coordinates. I'm trying to retrieve the 'ZoneName' where the intersection occurs (which is required):

+---------------------------------------------------------------+
|                              WKT                              |
+---------------------------------------------------------------+
| LINESTRING (-122.35800000000017 47.656000130337446, [etc...]) |
+---------------------------------------------------------------+

This must be simple but I'm struggling.

---- EDIT ----

This looks as though it should work and kind of does... but I'm getting zones that are no where near the polyline:

SELECT *
FROM dbo.SpatialZonePolygons
WHERE Coordinates.STIntersects(geography::STGeomFromText('LINESTRING(51.46276 -0.106, 51.46275 -0.10604, 51.46248 -0.10672, 51.46262 -0.10687, etc...)', 4326))>0

I have checked the polyline is correct by manually plotting it on a Google map. I have also checked the polygon coordinates are correct by doing the same so why would the database return zones that are 50-odd miles away from the polyline with definitely no intersections?

As a test, the polyline runs through London so I thought I'd see if a zone in Brussels was also returned... It wasn't. Could this therefore be an accuracy issue? I doubt it as the plotted polyline renders accurately and goes no where near the zones.

I'm a little desperate now so I am also wondering if I am storing the coordinates in the right order (lat lng) or should it be lng lat?

This should do it:

CREATE PROCEDURE FindPolygons(
    @geoline geography
) as begin
SELECT
     Coordinates
    ,ZoneName 
    ,WKT
FROM (
    SELECT
         Coordinates
        ,ZoneName 
        ,@geoline.STIntersection(Coordinates).STAsText() AS WKT
    FROM SpatialZonePolygons        
) data
WHERE data.WKT is not null
end

go

I'm taking your comment of "returning the polygons from a table of such that intersect a given line" as the requirement.

create procedure findPolygons (
   @line geography
)
as
begin

    select ZoneId, Coordinates
    from SpatialZonePolygons 
    where Coordinates.STIntersects( @line ) = 1

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM