json - 将包含几何的 JSON 转换为 postgreSQL

标签 json postgresql postgis

我有一个 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/

相关文章:

ruby-on-rails - 在Rails API服务中访问未定义的路由时呈现JSON格式的响应

SQL 选择 team_id 所在的所有项目(1 和 2 和 3 和 4 以及 .. 列表中的其余值)

javascript - 如何 JSON.parse 对象使对象成为类的实例?

json - 如何在内存中存储 2 - 3 GB 的树并让 nodejs 可以访问它?

json - Laravel 在正文中发送 JSON header

ruby-on-rails - 我对控制台中收到的模型(调用 'Model.connection' 建立连接)消息感到困惑

postgresql - PostgreSQL 中的小日期时间

leaflet - 如何处理 800k 点作为 MVT

postgresql - osm2pgsql : Function AddGeometryColumn doesn't exist

ruby-on-rails - 从 Rails 访问 PostGIS 空间数据