gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:15:02 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com