how to iterate through geojson elements(如何循环访问Geojson元素)
本文介绍了如何循环访问Geojson元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行此问题中的代码https://gis.stackexchange.com/questions/142391/storing-geojson-featurecollection-to-postgresql-with-postgis/142479#142479 但当我运行该应用程序时,收到以下错误:query="""
KeyError: ' "type"'
请告诉我如何修复它。
编码:
def exeGeoFromGeoJSONToWKT(self):
query="""
WITH data AS (
SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
},
{ "type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0], [100.0, 0.0] ]
]
},
"properties": {
"prop0": "value0",
"prop1": {"this": "that"}
}
}
]}'::json AS featuresCollection)
SELECT
LIDARDataPolygonsAsGeometry
FROM (
SELECT
ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON(feature->>'geometry'),4326),25832) AS LIDARDataPolygonsAsGeometry
FROM (SELECT json_array_elements(featuresCollection->'features') AS feature
FROM data) AS f
"""
print(query)
data = self.connection.query(query,[])
# print(data)
return data
尝试:
query="""
WITH data AS (
SELECT $${ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
}
]}$$::json AS featuresCollection)
SELECT
LIDARDataPolygonsAsGeometry
FROM (
SELECT
ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON(feature->>'geometry'),4326),25832) AS LIDARDataPolygonsAsGeometry
FROM (SELECT json_array_elements(featuresCollection->'features') AS feature
FROM data) AS f
"""
推荐答案
从数据库的角度来看,查询运行良好,但问题似乎出在查询构建上。您的查询有一个包含多个"
双引号的JSON文档,因此您可以转义它们("
),或者尝试使用另一个answer中所述的参数将JSON添加到查询中。
无关:您不需要这3个嵌套子查询。一个查询就可以了:
WITH data AS (
SELECT '{ "type": "FeatureCollection",
...
]}'::json AS mygeojson
)
SELECT
ST_Transform(
ST_SetSRID(
ST_GeomFromGeoJSON(json_array_elements(
mygeojson->'features')->>'geometry'),
4326),
25832) AS feature
FROM data
这篇关于如何循环访问Geojson元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何循环访问Geojson元素
猜你喜欢
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01