将数据插入PostGIS中的空间数据库

发布于 2025-02-02 07:04:22 字数 669 浏览 4 评论 0原文

我创建了一个具有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2025-02-09 07:04:23

错误消息告诉列是类型polygon的列,即本机邮政类型。使用PostGIS及其几何类型,可以使用这种类型的方法非常有限,如果您正在使用空间数据。

要创建一个包含多边形类型的几何形状的列,使用投影4326,您可以

CREATE TABLE test (my_poly geometry(polygon,4326));

使用st_geomfromtext加载数据。

由于您的列是几何类型的,因此有一个自动铸件从text几何 ,或者您可以强制它。与您的第一个查询不同,请注意,多边形定义必须在引号之间包含。

insert into test values ('polygon((0 0, 0 1, 1 1, 1 0, 0 0))'::geometry)

最后,从技术上讲,可以创建一个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 its geometry type is indicated.

To create a column containing geometries of the type polygon, with the projection 4326, you would do

CREATE TABLE test (my_poly geometry(polygon,4326));

You can then load data using ST_GeomFromText.

Since your column is of geometry type, there is an automatic cast from text to geometry, or you can force it. Unlike in your 1st query, note that the polygon definition must be enclosed between quotes.

insert into test values ('polygon((0 0, 0 1, 1 1, 1 0, 0 0))'::geometry)

At last, it is technically possible to create a Postgis geometry and to cast it to Postgres native polygon 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)

时光沙漏 2025-02-09 07:04:22

尝试 st_polygonfromtext
而不是st_geomfromtext。

或将列声明为通用几何形状,而不是多边形类型。

Try ST_PolygonFromText
instead of ST_GeomFromText.

Or declare the column as generic GEOMETRY, rather than POLYGON type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文