ST_Within 不 return 正确
ST_Within does not return true
我使用 ogr2ogr
工具从 GeoPackage 导入了一些数据。我假设导入操作没有任何故障,因为导入的多边形(geom
列)ST_IsValidReason()
returns Valid Geometry
。
nzstat=# \d sa2_2020
Table "public.sa2_2020"
Column | Type | Collation | Nullable | Default
-----------------+-----------------------------+-----------+----------+---------------------------------------
gid | integer | | not null | nextval('sa2_2020_gid_seq'::regclass)
sa22018_v1_00 | character varying | | |
sa22018_v1_name | character varying | | |
land_area_sq_km | double precision | | |
area_sq_km | double precision | | |
shape_length | double precision | | |
geom | geometry(MultiPolygon,2193) | | |
Indexes:
"sa2_2020_pkey" PRIMARY KEY, btree (gid)
"sa2_2020_geom_geom_idx" gist (geom)
nzstat=# select ST_IsValidReason(geom) from sa2_2020 where gid=4;
st_isvalidreason
------------------
Valid Geometry
(1 row)
nzstat=# select ST_IsValidReason(ST_SetSRID(ST_MakePoint(174.77632, -41.28671), 2193));
st_isvalidreason
------------------
Valid Geometry
(1 row)
nzstat=# select sa22018_v1_name from sa2_2020 where ST_Within(ST_SetSRID(ST_MakePoint(174.82726, -41.16671), 2193), geom);
sa22018_v1_name
-----------------
(0 rows)
nzstat=#
我用于 ST_MakePoint()
的坐标来自另一个数据库,该数据库在 NZGD2000 坐标系 (SRID = 2193)
中提供 x
和 y
值
full_address_ascii | gd2000_xcoord | gd2000_ycoord
-------------------------------------------------+---------------+---------------
1 Willis Street, Wellington Central, Wellington | 174.77632 | -41.28671
坐标应位于多个多边形之一,但我的查询 returns 没有结果。我在这里遗漏了什么吗?
我使用 PostGIS 3.2 和 PostgeSQL 13.5。
谢谢
EPSG:2193 的坐标以米为单位,因此您的值 (174.82726, -41.16671) 不太可能在该投影中,它们更有可能以度为单位 (WGS84, EPGS:4326 ).所以你需要转换你的点以将它们与 EPSG:2193 中的多边形进行比较,因此你的 SQL 应该是这样的:
select sa22018_v1_name from sa2_2020 where ST_Within(ST_TRANSFORM(ST_MakePoint(174.82726, -41.16671), 2193), geom);
ST_SetSRID
only changes the metadata of the point (setting the CRS), to actually change the values of a point's coordinates you need to reproject the point (transform it from one projection to another) and so must use ST_Transform
.
我使用 ogr2ogr
工具从 GeoPackage 导入了一些数据。我假设导入操作没有任何故障,因为导入的多边形(geom
列)ST_IsValidReason()
returns Valid Geometry
。
nzstat=# \d sa2_2020
Table "public.sa2_2020"
Column | Type | Collation | Nullable | Default
-----------------+-----------------------------+-----------+----------+---------------------------------------
gid | integer | | not null | nextval('sa2_2020_gid_seq'::regclass)
sa22018_v1_00 | character varying | | |
sa22018_v1_name | character varying | | |
land_area_sq_km | double precision | | |
area_sq_km | double precision | | |
shape_length | double precision | | |
geom | geometry(MultiPolygon,2193) | | |
Indexes:
"sa2_2020_pkey" PRIMARY KEY, btree (gid)
"sa2_2020_geom_geom_idx" gist (geom)
nzstat=# select ST_IsValidReason(geom) from sa2_2020 where gid=4;
st_isvalidreason
------------------
Valid Geometry
(1 row)
nzstat=# select ST_IsValidReason(ST_SetSRID(ST_MakePoint(174.77632, -41.28671), 2193));
st_isvalidreason
------------------
Valid Geometry
(1 row)
nzstat=# select sa22018_v1_name from sa2_2020 where ST_Within(ST_SetSRID(ST_MakePoint(174.82726, -41.16671), 2193), geom);
sa22018_v1_name
-----------------
(0 rows)
nzstat=#
我用于 ST_MakePoint()
的坐标来自另一个数据库,该数据库在 NZGD2000 坐标系 (SRID = 2193)
x
和 y
值
full_address_ascii | gd2000_xcoord | gd2000_ycoord
-------------------------------------------------+---------------+---------------
1 Willis Street, Wellington Central, Wellington | 174.77632 | -41.28671
坐标应位于多个多边形之一,但我的查询 returns 没有结果。我在这里遗漏了什么吗?
我使用 PostGIS 3.2 和 PostgeSQL 13.5。
谢谢
EPSG:2193 的坐标以米为单位,因此您的值 (174.82726, -41.16671) 不太可能在该投影中,它们更有可能以度为单位 (WGS84, EPGS:4326 ).所以你需要转换你的点以将它们与 EPSG:2193 中的多边形进行比较,因此你的 SQL 应该是这样的:
select sa22018_v1_name from sa2_2020 where ST_Within(ST_TRANSFORM(ST_MakePoint(174.82726, -41.16671), 2193), geom);
ST_SetSRID
only changes the metadata of the point (setting the CRS), to actually change the values of a point's coordinates you need to reproject the point (transform it from one projection to another) and so must use ST_Transform
.