gpt4 book ai didi

database - 如何将 POINTS(LANG, LAT) 存储到 PostGIS 中的几何类型列中?

转载 作者:太空狗 更新时间:2023-10-30 01:55:57 30 4
gpt4 key购买 nike

我在 PostGIS 中创建了一个表 itapp_cities 来存储城市的数据。我添加了一个数据类型为 geometrylocation 列来存储城市的 longitudelatitude 。当我运行以下 INSERT 查询时,出现如下所示的错误。

INSERT 查询:

INSERT INTO itapp_cities(city_id, city_name, city_code, state_id, location) 
VALUES (DEFAULT,'Ada', 'ada-ok',37,POINT(34.774531000000003, -96.678344899999999));

表定义:

CREATE TABLE itapp_cities
(
city_id bigserial NOT NULL,
city_name character varying(100) NOT NULL,
city_code character varying(5) NOT NULL DEFAULT ''::character varying,
state_id bigint NOT NULL,
location geometry,
CONSTRAINT itapp_cities_pkey PRIMARY KEY (city_id),
CONSTRAINT fk_states FOREIGN KEY (city_id)
REFERENCES itapp_states (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)

错误:

ERROR:  column "location" is of type geometry but expression is of type point
LINE 2: VALUES (DEFAULT,'Ada', 'ada-ok',37,POINT(34.77453100000000...
^
HINT: You will need to rewrite or cast the expression.
********** Error **********

ERROR: column "location" is of type geometry but expression is of type point
SQL state: 42804

如何在此列中存储点值?我是 PostGIS 的新手所以请原谅我这个愚蠢的问题

最佳答案

您可以使用函数 ST_MakePoint()并设置 SRID (Spatial Reference System Identifier)ST_SetSRID() :

SELECT ST_SetSRID(ST_MakePoint(longitude, latitude),4326)

或者当您输入文字值时,将字符串表示形式提供给 ST_GeomFromText() :

SELECT ST_GeomFromText('SRID=4326;POINT(34.774531 -96.6783449)')

dba.SE 上的相关答案以及更多详细信息和链接:

关于database - 如何将 POINTS(LANG, LAT) 存储到 PostGIS 中的几何类型列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33207930/

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