javascript - 将 javascript 数组保存到 Postgres 多边形字段中

标签 javascript postgresql geometry postgis typeorm

我在尝试保存像 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
    ]
}

最佳答案

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

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)

enter image description here

进一步阅读:

关于javascript - 将 javascript 数组保存到 Postgres 多边形字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59824117/

相关文章:

python - 给定一组点 xi=(xi,yi) 在 Python 中拟合一个椭圆

javascript - jquery fadeout 不是解决方案

javascript - Javascript Promises - If 语句返回最佳实践

javascript - 不使用Javascript将标题移动到div上方

python - SQLAlchemy 中的复杂外键约束

javascript - 94个字符一行任务,圆交点

javascript - 显示、隐藏,然后重新显示布局中断事件

postgresql - PostgreSQL 中基于游标的记录

sql - 如何使用 SQL 查看序列详细信息

javascript - 你如何从 CSS transform rotate(deg) 规则中找到变换矩阵?