【问题标题】:I have two tables, 1st country 2nd is state queried using ST_contains the state lies within the country or not我有两张表,第一个国家第二个是使用 ST_contains 查询的国家,该国家是否在国内
【发布时间】:2013-07-20 19:41:53
【问题描述】:

我是 postGIS 新手,对这些查询感到头疼......我需要一些帮助

我有两个表,第一个是country(cid int,countryname text,coutrycoordinates geometry),另一个是state(sid int,statename text,statecoordinates geometry),其中我必须使用国内的ST_contains 写一个查询,但是我的查询不起作用

现在假设我已经插入

INSERT into country  VALUES (
1,'country1',
'POLYGON ((1 5,4 5,4 7,1 7,1 5))');

INSERT into state  VALUES (
1,'state1',
'POLYGON ((2 5,3 5,3 6,2 6,2 5))');

工作并被插入,但是为几何列存储的值是这种类型的

01030000000100000005000000000000000000F03F000000000000F03F000000000000F03F0000000000001040000000000000104000000000000010400000000000001040000000000000F03F000000000000F03F000000000000F03F for country and 

010300000001000000050000000000000000000040000000000000004000000000000000400000000000000840000000000000084000000000000008400000000000000840000000000000004000000000000000400000000000000040  for state in the postreSQL db 

我有一个带有 2 个文本字段的网页和一个带有 ST_ContainsST_Intersects 的下拉菜单,单击时提交按钮应显示状态是否位于该国家/地区。

select c.cid  from country as c, state as s  where ST_Contains('POLYGON ((1 1,1 4,4 4,4 1,1 1))', 'POLYGON ((2 2,2 3, 3 3, 3 2, 2 2))') 

上述方法有效,但选择了两个表中交叉连接的所有行,而不仅仅是c.cid.

select c.cid from country as c, state as s  where ST_Contains(
01030000000100000005000000000000000000F03F000000000000F03F000000000000F03F0000000000001040000000000000104000000000000010400000000000001040000000000000F03F000000000000F03F000000000000F03F, 010300000001000000050000000000000000000040000000000000004000000000000000400000000000000840000000000000084000000000000008400000000000000840000000000000004000000000000000400000000000000040)

如果这是查询则返回错误

NOTICE:  identifier "f03f000000000000f03f000000000000f03f0000000000001040000000000000104000000000000010400000000000001040000000000000f03f000000000000f03f000000000000f03f" will be truncated to "f03f000000000000f03f000000000000f03f000000000000104000000000000"
ERROR:  syntax error at or near "F03F000000000000F03F000000000000F03F0000000000001040000000000000104000000000000010400000000000001040000000000000F03F000000000000F03F000000000000F03F"
LINE 2: 01030000000100000005000000000000000000F03F000000000000F03F00...
                                              ^

这是在我编写此代码的 php 页面中执行的那个

请问我哪里出错了。

【问题讨论】:

标签: php postgresql postgis pgadmin


【解决方案1】:

你快到了,只是缺少引号:

select c.cid from country as c, state as s  where ST_Contains(
'01030000000100000005000000000000000000F03F000000000000F03F000000000000F03F0000000000001040000000000000104000000000000010400000000000001040000000000000F03F000000000000F03F000000000000F03F', '010300000001000000050000000000000000000040000000000000004000000000000000400000000000000840000000000000084000000000000008400000000000000840000000000000004000000000000000400000000000000040')

“奇怪”的几何形状是因为 Postgres 将 Polygon 转换为 geometry。您可以使用ST_AsText()再次将它们转换为文本

SELECT ST_AsText( state1 ) FROM state;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多