简体   繁体   中英

Extract Polygon Lat/Longs from Geometry Point

Let me preface this by stating, I have looked at nearly all the Stackoverflow questions that I have came across, and I can't seem to find anything that works. I'm starting to feel like I'm going about this the wrong way.

I'm have shapes stored in a Geometry type. The shape should look like this in my WPF app -

在此处输入图像描述

The problem is that a lot of my Geometry points are of GEOMETRYCOLLECTION . In my app, I need the polygon lat/long points. The geometry collection contains linestrings too, which I don't need. I only need the polygon points.

Example -

在此处输入图像描述

Is there a way for me to extract the polygon lat/long points via SQL Server? We previously have used the Geom.STConvexHull() function, however I need more defined points than what that provides.

You could use a little helper function like this:

create or alter function GetGeometries(@geo geometry, @geometry_type varchar(200) = null)
returns @geometries table (i int primary key, geo geometry)
as
begin
  declare @i int = 1;
  while (1=1)
  begin
    declare @g geometry = @geo.STGeometryN(@i);
    if @g is null break;

    if (@geometry_type is null or @g.InstanceOf(@geometry_type)=1 )
    begin
      insert into @geometries(i,geo)values (@i, @g);
    end
    set @i += 1;
  end

  return;
end

go

create or alter function GetPoints(@geo geometry)
returns @geometries table (i int primary key, point geometry)
as
begin
  declare @i int = 1;
  while (1=1)
  begin
    declare @g geometry = @geo.STPointN(@i);
    if @g is null break;

    insert into @geometries(i,point)values (@i, @g);

    set @i += 1;
  end

  return;
end

go


DECLARE @g geometry = 'GEOMETRYCOLLECTION(LINESTRING(1 1, 3 5),POLYGON((-1 -1, -1 -5, -5 -5, -5 -1, -1 -1)))';

SELECT g.i geometry_index, p.i point_index, p.point.STX X, p.point.STY Y
from dbo.GetGeometries(@g,'POLYGON') g
outer apply dbo.GetPoints(g.geo) p

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