postgresql 如何将点文本转换为几何图形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23509690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:41:36  来源:igfitidea点击:

How to convert point text to geometry

postgresqlpostgis

提问by Muneem Habib

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.

我已经在我的数据库中安装了 postgis。现在我的数据库中有 1 个区域,例如 ((-79.4609576808001,43.9726680183837)) 我希望这个区域转换为几何类型。我在谷歌上搜索过,发现 St_geomfromText 会将文本转换为几何类型。

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

但它给出的错误是没有函数与 st_geomfromtext 匹配

回答by John Powell

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,

您还可以使用 ST_MakePoint,它可能更简洁,因为您不必将纬度和经度值连接为文本。与 ST_SetSrid 结合使用将坐标参考系设置为 4326,例如,

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.

将返回几何类型。请注意,顺序是 lon/lat (x/y),由于人们在日常讲话中使用 lat/lon,因此会引起很多混淆。

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

ST_GeomFromText 通常在您拥有 Well-known text (WKT) 格式的几何图形时更有用,例如,

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:

如果您的数据实际上是 ((-79.4609576808001,43.9726680183837)) 形式,并且您不想按照我上面的建议将其拆分,那么与 ST_GeomFromText 一起使用的正确格式是:

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

where the SRID is optional, but recommended.

其中 SRID 是可选的,但建议使用。

See http://en.wikipedia.org/wiki/Well_Known_Textfor more information.

有关更多信息,请参阅http://en.wikipedia.org/wiki/Well_Known_Text

回答by midfield99

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.

看起来 ST_GeomFromText 需要一个字符串,而不是一个变量。ST_GeomFromText 比提供直接几何更好吗?提到函数是不可变的。

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

这个问题给出了一种从变量构建字符串的方法。 从 plpgsql 脚本中的行变量插入 PostGIS 对象(例如 ST_GeomFromText)

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

所以看起来 Chris Traver 的解决方案应该适合你:

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 regionis, and I don't think it a point type.

所以你只需要访问点的 x 和 y 部分。 ST_X(point)并且ST_Y(point)会分别返回PostGIS中一个点的x和y坐标,但是我不知道数据类型region是什么,我认为它不是点类型。