简体   繁体   English

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

[英]How to store POINTS(LANG, LAT) into geometry type column in PostGIS?

I have created a table itapp_cities in PostGIS which stores the data of cities.我在 PostGIS 中创建了一个表itapp_cities来存储城市的数据。 I have added a column location with datatype geometry to store longitude and latitude of a city.我添加了一个带有geometry数据类型的列location来存储城市的longitudelatitude When I run the following INSERT query I get the error shown below.当我运行以下INSERT查询时,出现如下所示的错误。

INSERT query: 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)); 

Table definition:表定义:

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:错误:

 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

How can I store point values inside this column?如何在此列中存储点值? I am new to PostGIS so forgive me for this silly question我是 PostGIS 的新手,所以请原谅我这个愚蠢的问题

Try this SQL and match it with your insert sql query 尝试使用此SQL并将其与insert sql查询匹配

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

for more details go through this link 有关详细信息,请通过此链接

You can use the function ST_MakePoint() and the set the SRID (Spatial Reference System Identifier) with ST_SetSRID() : 您可以使用函数ST_MakePoint()并使用ST_MakePoint()设置SRID(空间参考系统标识符 ST_SetSRID()

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

Or while you enter literal values anyway, feed the string representation to ST_GeomFromText() : 或者无论如何输入文字值,请将字符串表示提供给ST_GeomFromText()

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

Related answer on dba.SE with more details and links: 关于dba.SE的相关回答以及更多细节和链接:

According to the postGIS docs, your original insert was close.根据 postGIS 文档,您的原始插入很接近。 Just add ' around the POINT expression.只需在 POINT 表达式周围添加 '。

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

http://postgis.net/workshops/postgis-intro/geometries.html http://postgis.net/workshops/postgis-intro/geometries.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM