簡體   English   中英

將 javascript 數組保存到 Postgres 多邊形字段中

[英]Saving javascript array into Postgres polygon field

我在嘗試保存已格式化為 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列中,則應在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)

在此處輸入圖片說明

進一步閱讀:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM