我有一个 JSON 文件,其中包含几个公园的属性以及作为一个点的位置(几何)。我想知道如何将 JSON 转换为 postgreSQL 格式。确实,我尝试了几种方法,例如 SQLizer 和 MapForce,但我无法转换它们。有没有办法将这个具有几何形状的 JSON 转换为 postgreSQL 格式?
我很感激任何帮助。
您可以在下面找到脚本。
var lenneparks = {
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"place": "Aachen Kurpark",
"year": "1853 (Hi)",
"text": "Elisengarten, kleine Parkanlage in der Innenstadt, rückwärtig vom Elisenbrunnen"
},
"geometry": {
"type": "Point",
"coordinates": [
6.086027,
50.774247
]
}
},
{
"type": "Feature",
"properties": {
"place": "Aachen",
"year": "ca. 1862 (Hi)",
"text": "Staatsprokurator Dubusc"
},
"geometry": {
"type": "Point",
"coordinates": [
6.0838868,
50.7653455
]
}
}
]
};
最佳答案
编辑 1:更正 SQL 注释
您是否尝试过 PostGIS 扩展?它带有非常方便的功能来导入此类数据,例如:
-- To create a geometry object from your GeoJSON
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}') As geometry;
-- To see the WKT of your GeoJSON
SELECT ST_AsText(ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}')) As geometry;
编辑 2:为每个几何体创建记录
此函数将为数组 features
中的每个 json 元素创建一个包含一条记录的表,从那里您可以开始解析创建表所需的数据...希望它有所帮助:
CREATE TEMPORARY TABLE features AS
SELECT json_array_elements('{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"place": "Aachen Kurpark",
"year": "1853 (Hi)",
"text": "Elisengarten, kleine Parkanlage in der Innenstadt, rückwärtig vom Elisenbrunnen"
},
"geometry": {
"type": "Point",
"coordinates": [
6.086027,
50.774247
]
}
},
{
"type": "Feature",
"properties": {
"place": "Aachen",
"year": "ca. 1862 (Hi)",
"text": "Staatsprokurator Dubusc"
},
"geometry": {
"type": "Point",
"coordinates": [
6.0838868,
50.7653455
]
}
}
]
}'::JSON -> 'features') as features;
SELECT * FROM features;
编辑 3:查询以从 json 表中提取信息
SELECT
features -> 'geometry' -> 'coordinates' -> 0 AS lat,
features -> 'geometry' -> 'coordinates' -> 1 AS lon,
features -> 'properties' -> 'place'::TEXT,
features -> 'properties' -> 'year'::TEXT,
features -> 'properties' -> 'text'::TEXT
FROM features;
编辑 4:从 json 表中提取几何图形并将它们转换为 WKT 和几何图形
SELECT ST_GeomFromGeoJSON((features -> 'geometry')::text)
FROM features;
SELECT ST_AsText(ST_GeomFromGeoJSON((features -> 'geometry')::TEXT))
FROM features;
关于json - 将包含几何的 JSON 转换为 postgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48788771/