Emr*_*ürk 5 sql-server asp.net-mvc geometry spatial-query geojson
我想将geom(geometry)数据类型转换为GeoJSON。我该怎么办?
例如,WKT中的几何:
POLYGON((455216.346127297 4288433.28426224,455203.386722146 4288427.76317716,455207.791765017 4288417.51116228,455220.784166744 4288423.30230044,455216.346127297 4288433.28426224))
Run Code Online (Sandbox Code Playgroud)
转到以下GeoJSON:
{ "type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ],
[ [100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2] ]
]
}
Run Code Online (Sandbox Code Playgroud)
小智 7
我也有同样的需求。我们有一个大型数据库,其中包含 SQL Server 中带有 GEOMETRY 列的表。我觉得能够从包含 GeoJson 的存储过程中获取单个字符串对象是更可取的。我编写了一个函数,它将几何实例作为对象并返回一个 GeoJson 字符串。
CREATE FUNCTION [dbo].[geomToGeoJSON] (@geom GEOMETRY)
RETURNS VARCHAR(MAX)
AS
BEGIN
-- Declare the return variable here
DECLARE @geoJSON VARCHAR(MAX)
DECLARE @Ngeom GEOMETRY
DECLARE @ptCounter INT
DECLARE @numPt INT
DECLARE @ringCounter INT
DECLARE @numRing INT
DECLARE @gCounter INT
DECLARE @numGeom INT
DECLARE @handled BIT = 0
DECLARE @extRing GEOMETRY
DECLARE @intRing GEOMETRY
-- fix bad geometries and enforce ring orientation
SET @geom = @geom.STUnion(@geom.STPointN(1)).MakeValid()
-- Point ----------------------------
IF (@geom.STGeometryType() = 'Point')
BEGIN
SET @geoJSON = '{ "type": "Point", "coordinates": [' + LTRIM(RTRIM(STR(@geom.STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@geom.STY, 38, 8))) + '] }'
SET @handled = 1
END
-- MultiPoint ---------------------------------------------
IF (
@handled = 0
AND @geom.STGeometryType() = 'MultiPoint'
)
BEGIN
SET @gCounter = 1
SET @numGeom = @geom.STNumGeometries()
SET @geoJSON = '{ "type": "MultiPoint", "coordinates": ['
WHILE @gCounter <= @numGeom
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@geom.STGeometryN(@gCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@geom.STGeometryN(@gCounter).STY, 38, 8))) + '], '
SET @gCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + '] }'
SET @handled = 1
END
-- LineString ---------------------------------------------
IF (
@handled = 0
AND @geom.STGeometryType() = 'LineString'
)
BEGIN
SET @ptCounter = 1
SET @numPt = @geom.STNumPoints()
SET @geoJSON = '{ "type": "LineString", "coordinates": ['
WHILE @ptCounter <= @numPt
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@geom.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@geom.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ' ] }'
SET @handled = 1
END
-- MultiLineString ---------------------------------------------
IF (
@handled = 0
AND @geom.STGeometryType() = 'MultiLineString'
)
BEGIN
SET @gCounter = 1
SET @numGeom = @geom.STNumGeometries()
SET @geoJSON = '{ "type": "MultiLineString", "coordinates": ['
WHILE @gCounter <= @numGeom
BEGIN
SET @Ngeom = @geom.STGeometryN(@gCounter)
SET @geoJSON += '['
SELECT
@ptCounter = 1
,@numPt = @Ngeom.STNumPoints()
WHILE @ptCounter <= @numPt
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@Ngeom.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@Ngeom.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + '],'
SET @gCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + '] }'
SET @handled = 1
END
-- Polygon ---------------------------------------------
IF (
@handled = 0
AND @geom.STGeometryType() = 'Polygon'
)
BEGIN
SET @extRing = @geom.STExteriorRing()
SET @geoJSON = '{ "type": "Polygon", "coordinates": [['
SELECT
@ptCounter = 1
,@numPt = @extRing.STNumPoints()
WHILE @ptCounter <= @numPt
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@extRing.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@extRing.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ']'
SET @ringCounter = 1
SET @numRing = @geom.STNumInteriorRing()
WHILE @ringCounter <= @numRing
BEGIN
SET @geoJSON += ',['
SET @intRing = @geom.STInteriorRingN(@ringCounter)
-- set the ring orientation so that they are consistent
SET @intRing = @intRing.STUnion(@intRing.STPointN(1)).MakeValid()
SELECT
@ptCounter = @intRing.STNumPoints()
WHILE @ptCounter > 0
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@intRing.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@intRing.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter -= 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ']'
SET @ringCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ']] }'
SET @handled = 1
END
-- MultiPolygon ---------------------------------------------
IF (
@handled = 0
AND @geom.STGeometryType() = 'MultiPolygon'
)
BEGIN
SELECT
@gCounter = 1
,@numGeom = @geom.STNumGeometries()
SET @geoJSON = '{ "type": "MultiPolygon", "coordinates": ['
WHILE @gCounter <= @numGeom
BEGIN
SET @Ngeom = @geom.STGeometryN(@gCounter)
SET @extRing = @Ngeom.STExteriorRing()
SET @geoJSON += '[['
SELECT
@ptCounter = 1
,@numPt = @extRing.STNumPoints()
-- add the exterior ring points to the json
WHILE @ptCounter <= @numPt
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@extRing.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@extRing.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ']'
SET @ringCounter = 1
SET @numRing = @Ngeom.STNumInteriorRing()
-- add any internal ring points to the json
WHILE @ringCounter <= @numRing
BEGIN
SET @geoJSON += ',['
SET @intRing = @Ngeom.STInteriorRingN(@ringCounter)
-- make sure the ring orientation is the same every time
SET @intRing = @intRing.STUnion(@intRing.STPointN(1)).MakeValid()
SELECT
@ptCounter = @intRing.STNumPoints()
WHILE @ptCounter > 0
BEGIN
SET @geoJSON += '[' + LTRIM(RTRIM(STR(@intRing.STPointN(@ptCounter).STX, 38, 8))) + ', ' + LTRIM(RTRIM(STR(@intRing.STPointN(@ptCounter).STY, 38, 8))) + '], '
SET @ptCounter -= 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + ']'
SET @ringCounter += 1
END
SET @geoJSON += '],'
SET @gCounter += 1
END
SET @geoJSON = LEFT(@geoJSON, LEN(@geoJSON) - 1) + '] }'
SET @handled = 1
END
IF (@handled = 0)
BEGIN
SET @geoJSON = '{"type": "' + @geom.STGeometryType() + '", "coordinates": []}'
END
RETURN @geoJSON
END
Run Code Online (Sandbox Code Playgroud)
然后我可以像这样选择一个单独的 GeoJSON 对象:
SELECT dbo.geomToGeoJSON(GEOMCOLNAME) FROM DB.gis.PARCEL WHERE PARCEL = 'R1525750900'
Run Code Online (Sandbox Code Playgroud)
并得到一个看起来像的结果
{
"type": "Polygon",
"coordinates": [
[
[-116.27593761, 43.62939598],
[-116.27558219, 43.62939633],
[-116.27558253, 43.62955520],
[-116.27582493, 43.62955445],
[-116.27582534, 43.62963010],
[-116.27593893, 43.62962975],
[-116.27593761, 43.62939598]
]
]
}
Run Code Online (Sandbox Code Playgroud)
或者我可以像这样将一整套对象打包到一个 FeatureCollection 中:
DECLARE @GeoJSON VARCHAR(MAX)
SET @GeoJSON = '{"type": "FeatureCollection", "features": ['
SELECT
@GeoJSON += '{"type": "Feature", "geometry": ' + sde_apps.dbo.geomToGeoJSON(SHAPE) + ', "properties": { "Parcel": "' + PARCEL + '"}},'
FROM
db.gis.PARCEL
WHERE
SUBNM LIKE @subnm
SET @GeoJSON = LEFT(@GeoJSON, LEN(@GeoJSON) - 1) + ']}'
SELECT
@GeoJSON
Run Code Online (Sandbox Code Playgroud)
查询性能取决于几何的复杂性和数量,但我通常在大约十分之二秒内得到结果。
我已经使用 MSDN 中的示例几何图形进行了验证,然后将生成的 GeoJSON 输入到http://geojsonlint.com/ 中。我知道这已经一年了,但我仍然有需求,我怀疑没有地图服务器的任何人都可以使用类似的东西在 Bing 地图上绘制图层等生成自己的简单地图服务器。
tho*_*hor -2
如果您有权访问 PostgreSQL/PostGIS,则可以使用ST_GeomFromText读取几何图形并将ST_AsGeoJSON几何图形保存为 GeoJSON:
SELECT ST_AsGeoJSON(ST_GeomFromText('POLYGON((455216.346127297
4288433.28426224,455203.386722146 4288427.76317716,455207.791765017
4288417.51116228,455220.784166744 4288423.30230044,455216.346127297
4288433.28426224))'));
Run Code Online (Sandbox Code Playgroud)
,生成:
-------------------------------------------------------------------------------
{"type":"Polygon","coordinates":[[[455216.346127297,4288433.28426224],
[455203.386722146,4288427.76317716],[455207.791765017,4288417.51116228],
[455220.784166744,4288423.30230044],[455216.346127297,4288433.28426224]]]}
(1 row)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2702 次 |
| 最近记录: |