将数据插入PostGIS中的空间数据库
我创建了一个具有3个字段的表:ID(整数),名称(char)和Surface(Polygon)。当我尝试通过执行在此表中插入新行时:
插入公共场所。“区域” (“ id”,“ name”,“ surface”) 值(0,'myarea',polygon((0 0,1 0,1 1,0 1,0 1,0 0)));
我得到这个错误:
SQL错误[42601]:错误:“ 0”或接近的语法错误 位置:84
我在PostGIS等文档中搜索,但我没有发现如何定义此字段(对于点和Linestring相同)。
我也尝试了这样的事情:
插入公共场所。“区域” (“ id”,“ name”,“ surface”) 值(1,'myarea',st_geomfromtext('polygon((8 4,10.5 4,10.5 1.5 1.5,8 1.5,8 4)))'));
但结果是:
SQL错误[42804]:错误:列“ Surface”是多边形类型,但表达式是类型的几何形状 提示:您需要重写或施放表达。 位置:76
您能帮我找出我的错误吗?
非常感谢您的宝贵时间!
I have create a table which has 3 fields: id(integer), name(char) and surface(polygon). When i try to insert a new row in this table by executing:
INSERT INTO public."Area"
("ID", "Name", "Surface")
VALUES(0, 'myArea', POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)));
I get this error:
SQL Error [42601]: ERROR: syntax error at or near "0"
Position: 84
I searched in the documentation of postgis etc but i didnt find out how to define this field (same for points and linestring).
I also tried something like this:
INSERT INTO public."Area"
("ID", "Name", "Surface")
VALUES(1, 'myArea', ST_GeomFromText('POLYGON((8 4, 10.5 4, 10.5 1.5, 8 1.5, 8 4))'));
but the result was :
SQL Error [42804]: ERROR: column "Surface" is of type polygon but expression is of type geometry
Hint: You will need to rewrite or cast the expression.
Position: 76
Could you please help me to find out which is my mistake?
Thank you a lot for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误消息告诉列是类型
polygon
的列,即本机邮政类型。使用PostGIS及其几何
类型,可以使用这种类型的方法非常有限,如果您正在使用空间数据。要创建一个包含多边形类型的几何形状的列,使用投影
4326
,您可以使用
st_geomfromtext
加载数据。由于您的列是
几何
类型的,因此有一个自动铸件从text
到几何
,或者您可以强制它。与您的第一个查询不同,请注意,多边形定义必须在引号之间包含。最后,从技术上讲,可以创建一个postgis
几何
并将其施放给Postgres本机polygon
type(select st_geomfromtext('polygon('polygon((0 0,0,0,0)) 1,1 1,1 0,0 0)))):: polygon;
),但是这样做几乎没有好处计算交叉口等)The error message tells the column is of type
polygon
, which is a native postgres type. What one can do with this type is very limited and if you are working with spatial data, using Postgis and itsgeometry
type is indicated.To create a column containing geometries of the type polygon, with the projection
4326
, you would doYou can then load data using
ST_GeomFromText
.Since your column is of
geometry
type, there is an automatic cast fromtext
togeometry
, or you can force it. Unlike in your 1st query, note that the polygon definition must be enclosed between quotes.At last, it is technically possible to create a Postgis
geometry
and to cast it to Postgres nativepolygon
type (SELECT ST_GeomFromText('POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))')::polygon;
), but doing so brings very little benefit since you still won't have access to most of Postgis functions (compute distances, reproject, compute intersections etc)尝试 st_polygonfromtext
而不是st_geomfromtext。
或将列声明为通用几何形状,而不是多边形类型。
Try ST_PolygonFromText
instead of ST_GeomFromText.
Or declare the column as generic GEOMETRY, rather than POLYGON type.