如何遍历 geojson 元素
how to iterate through geojson elements
我想执行这道题的代码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 文档,因此您要么转义它们 (\"
),要么尝试使用参数将 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
我想执行这道题的代码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 文档,因此您要么转义它们 (\"
),要么尝试使用参数将 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