【问题标题】:Saving javascript array into Postgres polygon field将 javascript 数组保存到 Postgres 多边形字段中
【发布时间】:2020-05-06 12:16:45
【问题描述】:

我在尝试保存已格式化为 GeoJSON 的多边形时遇到问题,问题是我将多边形作为坐标数组的数组,但 postgres 多边形字段需要一个元组数组,但 javascript 没有'不支持元组,因此我不知道如何将数据插入 Postgres。

postgres 如何获取数据的示例:

INSERT INTO table VALUES(default, '[(x,y), (x,y)]');

我拥有的数据示例:

"coordinates": [
          [
            [
              49.5703125,
              59.5343180010956
            ],
            [
              54.84375,
              54.77534585936447
            ],
            [
              63.28125,
              59.5343180010956
            ],
            [
              54.84375,
              61.77312286453146
            ],
            [
              49.5703125,
              60.930432202923335
            ],
            [
              49.5703125,
              59.5343180010956
            ]
          ]
        ]

尝试将数组保存到 postgres 时遇到的错误:

{
    "message": "invalid input syntax for type polygon: \"{{\"-64.1892249612655\",\"-31.4212119274207\"},{\"-64.1896863245919\",\"-31.4223122073094\"},{\"-64.1900957427429\",\"-31.423283040535\"},{\"-64.1901970936061\",\"-31.4235231632172\"},{\"-64.190677427225\",\"-31.4246610035708\"},{\"-64.1892249612655\",\"-31.4212119274207\"}}\"",
    "name": "QueryFailedError",
    "length": 353,
    "severity": "ERROR",
    "code": "22P02",
    "file": "float.c",
    "line": "542",
    "routine": "float8in_internal",
    "query": "INSERT INTO \"zones\"(\"title\", \"boundary_points\", \"created_at\", \"updated_at\", \"iconFileId\", \"backgroundFileId\") VALUES ($1, $2, DEFAULT, DEFAULT, $3, $4) RETURNING \"id\", \"created_at\", \"updated_at\"",
    "parameters": [
        "BAJO GENERAL PAZ",
        [
            [
                -64.1892249612655,
                -31.4212119274207
            ],
            [
                -64.1896863245919,
                -31.4223122073094
            ],
            [
                -64.1900957427429,
                -31.423283040535
            ],
            [
                -64.1901970936061,
                -31.4235231632172
            ],
            [
                -64.190677427225,
                -31.4246610035708
            ],
            [
                -64.1892249612655,
                -31.4212119274207
            ]
        ],
        null,
        null
    ]
}

【问题讨论】:

  • 这个数组的每个点都应该成为你表中的一条记录?
  • 不,这只是一条记录,有一个我称之为'boundary_points'的字段,它是多边形类型,为了在postgres中定义一个多边形,你需要传递一个元组数组(x ,y) 的坐标。

标签: javascript postgresql geometry postgis typeorm


【解决方案1】:

您的 GeoJSON 多边形无效 - 它缺少类型:"type":"Polygon"。如果要将 GeoJSON 多边形存储到 GEOMETRY 列中,则应在 INSERT INTO 中使用 ST_GeomFromGeoJson()

CREATE TEMPORARY TABLE t (the_geom GEOMETRY);

INSERT INTO t (the_geom) VALUES (ST_GeomFromGeoJSON(
        '{"type":"Polygon",
          "coordinates": [
          [
            [49.5703125,59.5343180010956],
            [54.84375,54.77534585936447],
            [63.28125,59.5343180010956],
            [54.84375,61.77312286453146],
            [49.5703125,60.930432202923335],
            [49.5703125,59.5343180010956]
          ]
        ]}'::json));

SELECT ST_AsText(the_geom,2) FROM t;

                                     st_astext                                      
------------------------------------------------------------------------------------
 POLYGON((49.57 59.53,54.84 54.78,63.28 59.53,54.84 61.77,49.57 60.93,49.57 59.53))
(1 Zeile)

进一步阅读:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 2021-06-25
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2012-08-25
    • 2021-06-13
    • 2012-08-05
    相关资源
    最近更新 更多