How to convert point text to geometry

[亡魂溺海] 提交于 2020-01-15 12:18:22

问题


I have installed postgis in my DB. Now i have 1 regions in my DB like ((-79.4609576808001,43.9726680183837)) I want this region to convert to geometry type. I have searched on google and found that St_geomfromText will convert text to geometry type.

my query is as follows:

SELECT ST_GeomFromText(region,4326) from "erpAssets";

But it is giving error it is saying that no function matches for st_geomfromtext


回答1:


You can also use ST_MakePoint which is probably cleaner as you don't have to concatenate latitude and longitude values as text. Use it in conjunction with ST_SetSrid to set the coordinate reference system to 4326, eg,

Select ST_SetSrid(ST_MakePoint(lon, lat),4326) from sometable;

will return a geometry type. Note the order is lon/lat (x/y), a cause of lots of confusion, due to people saying lat/lon in everyday speech.

ST_GeomFromText is generally more useful when you have a geometry in Well-known text (WKT) format, eg,

Select ST_GeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))', 4326);

If your data is actually in the form ((-79.4609576808001,43.9726680183837)) and you don't want to split it up as I suggested above, the correct format to use use with ST_GeomFromText for a point is:

Select ST_GeomFromText('POINT(-79.4609576808001 43.9726680183837)', 4326)

where the SRID is optional, but recommended.

See http://en.wikipedia.org/wiki/Well_Known_Text for more information.




回答2:


It looks like ST_GeomFromText is expecting a string, not a variable. Is ST_GeomFromText better than providing direct geometry? mentions the function is immutable.

This question gives one way of building a string from variables. Insert PostGIS Object (e.g. ST_GeomFromText) from row variables in plpgsql script

So it looks like Chris Traver's solution should work for you:

ST_GeomFromText('POINT(' || row_data.longitude || ' ' || row_data.latitude || 
 ' ' || row_data.altitude || ')', 4326)

So you just need to access the x and y portion of the point. ST_X(point) and ST_Y(point) will respectively return the x and y coordinate of a point in PostGIS, but I don't know what data type region is, and I don't think it a point type.



来源:https://stackoverflow.com/questions/23509690/how-to-convert-point-text-to-geometry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!