Generate view with X and Y from geometry type

让人想犯罪 __ 提交于 2019-12-12 05:04:04

问题


In sql-server-2012 I want to generate view with all points from geometry type. How can I do this? View example

GeomKey | X  | Y
--------+----+-----
 1      | X1 | Y1
 1      | x2 | Y2
 2      | x1 | Y1

Example in Oracle with sys.SDO

select c.ngeometrykey,  z.* 
from gis_map.mp_geometry c ,    
table(sdo_util.getvertices(c.geometry)) z 

回答1:


I don't think you can do this in a view but you can create a table-valued user defined function (a function that returns a table) to get what you want.

This example uses a table defined as

CREATE TABLE GeoTable (GeomKey int, vector GEOMETRY)

which stores different geometry types (in the example I linked below I used POINT, MULTIPOINT, LINESTRING and POLYGON).

CREATE FUNCTION dbo.GetVertices()
RETURNS @ret TABLE (GeomKey INT, X INT, Y INT, PointNo INT)
AS
BEGIN
    DECLARE @max INT
    SET @max = (SELECT MAX(vector.STNumPoints()) FROM GeoTable) 

    ;WITH Sequence(Number) AS
    (
        SELECT 1 AS Number
        UNION ALL
        SELECT Number + 1
        FROM Sequence
        WHERE Number < @max
    )
    INSERT INTO @ret 
    SELECT
        gt.GeomKey
        ,gt.vector.STPointN(nums.number).STX AS X
        ,gt.vector.STPointN(nums.number).STY AS Y
        ,nums.number AS PointNo
    FROM GeoTable gt, Sequence nums
    WHERE nums.number <= gt.vector.STNumPoints()
    RETURN
END;

See this sample SQL Fiddle for a complete working example.



来源:https://stackoverflow.com/questions/25640832/generate-view-with-x-and-y-from-geometry-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!