SqlServer中Geography的使用


地理空間數據類型 geography 是作為 SQL Server 中的 .NET 公共語言運行時 (CLR) 數據類型實現的。 此類型表示圓形地球坐標系中的數據。 geography 數據類型存儲橢球體(圓形地球)數據,如 GPS 緯度和經度坐標SQL Server 。 SQL Server 支持 geography 空間數據類型的一組方法。 這些方法包括開放地理空間信息聯盟 (OGC) 標准和對該標准的一組 Microsoft 擴展所定義的 geography 方法。

--總表面積
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STArea()

--二進制(WKB)表示形式
SELECT geography::STGeomFromText('LINESTRING( -122.360 47.656, -122.343 47.656)', 4326).STAsBinary()

--WKT表示形式
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STAsText()

--緩沖區
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STBuffer(1)

--返回指定的曲線
SELECT geography::STGeomFromText('COMPOUNDCURVE (CIRCULARSTRING (-122.358 47.653, -122.348 47.649, -122.348 47.658), CIRCULARSTRING(-122.348 47.658, -122.358 47.658, -122.358 47.653))',4326).STCurveN(2)

--圓弧線段的多邊形近似值
SELECT geography::STGeomFromText('CIRCULARSTRING(-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653)',4326).STCurveToLine()

--差集
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STDifference(geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326))

--最大維度
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STDimension()

--是否不相聯
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STDisjoint(geography::STGeomFromText('POINT( -122.343 47.656)', 4326))

--距離
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STDistance(geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326))

--終點
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STEndPoint()

--判斷是否相等
SELECT geography::STGeomFromText('GEOMETRYCOLLECTION(POLYGON((-122.368 47.658, -122.338 47.649, -122.338 47.658, -122.368 47.658, -122.368 47.658)), LINESTRING(-122.360 47.656, -122.343 47.656), POINT (-122.35 47.656))', 4326).STEquals(geography::STGeomFromText('POLYGON((-122.368 47.658, -122.338 47.649, -122.338 47.658, -122.368 47.658, -122.368 47.658))', 4326))

--查找GeometryCollection中第n個實例
SELECT geography::STGeomFromText('MULTIPOINT(-122.360 47.656, -122.343 47.656)', 4326).STGeometryN(2)

--類型
SELECT geometry::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STGeometryType()

--交集
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STIntersection(geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326))

--判斷是否相交
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STIntersects(geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326))

--判斷是否閉合
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STIsClosed()

--判斷是否為空
SELECT geography::STGeomFromText('POLYGON EMPTY', 4326).STIsEmpty()

--判斷是否為有效geography格式
SELECT geography::STGeomFromText('LINESTRING(0 0, 2 2, 1 0)', 4326).STIsValid()

--邊長總長度
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STLength()

--一維geography曲線數
SELECT geography::STGeomFromText('COMPOUNDCURVE(CIRCULARSTRING(-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))',4326).STNumCurves()

--geometry數量
SELECT geography::STGeomFromText('MULTIPOINT((-122.360 47.656), (-122.343 47.656))', 4326).STNumGeometries()

--點數量
SELECT geography::STGeomFromText('GEOMETRYCOLLECTION(CIRCULARSTRING(-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653) ,CURVEPOLYGON(CIRCULARSTRING(-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653)))', 4326).STNumPoints()

--指定點
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STPointN(2)

--空間參考
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STSrid

--起點
SELECT geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656)', 4326).STStartPoint()

--余集
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STSymDifference(geography::STGeomFromText('POLYGON((-122.351 47.656, -122.341 47.656, -122.341 47.661, -122.351 47.661, -122.351 47.656))', 4326))

--並集
SELECT geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326).STUnion(geography::STGeomFromText('POLYGON((-122.351 47.656, -122.341 47.656, -122.341 47.661, -122.351 47.661, -122.351 47.656))', 4326))

 

官方資料:

https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2008/bb933960(v=sql.100)

https://docs.microsoft.com/zh-cn/sql/t-sql/spatial-geography/spatial-types-geography?view=sql-server-ver15

https://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/using-sql-with-gdbs/a-quick-tour-of-sql-functions-used-with-st-geometry.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM