如何从Postgis的多点平面中选择特定区域?

时间:2018-11-30 00:39:22

标签: python sql postgresql gis postgis

以下是一些具有x y和z值的测试数据(应该代表物理世界中的图像像素位置,但这是一个处理的小示例)。

-- create a table wtih geom as the geometry type column    
CREATE TABLE spatial_table (geom geometry);
     -- insert 3d points
    INSERT INTO spatial_table VALUES ('MULTIPOINT(1 0 800, 2 3 152, 1 2 300, 1 5 234234, 5 3 123123)')

现在,我可以使用以下查询从中提取所有点:

      SELECT * from st_x(st_geomfromewkt(SELECT ST_AsText( (ST_Dump(geom)).geom)
FROM spatial_table))

但是可以说我想要一个特定的区域,我知道该区域的x和y边界。如何做到这一点?

1 个答案:

答案 0 :(得分:2)

使用st_makeenvelope(xmin, ymin, xmax, ymax, srid)

对于以下示例:

enter image description here

SQL查询:

select p.id
from (values (st_geomfromtext('Point (34.5430 48.3706)'),'C'),
(st_geomfromtext('Point (34.0136 48.4661)'),'B'),
(st_geomfromtext('Point (33.6983 48.2173)'),'A'),
(st_geomfromtext('Point (34.0628 48.1450)'),'G'),
(st_geomfromtext('Point (33.7562 47.8586)'),'F'),
(st_geomfromtext('Point (34.0686 47.6388)'),'E'),
(st_geomfromtext('Point (34.4562 47.9599)'),'D'),
(st_geomfromtext('Point (34.1235 47.8528)'),'H')) p(geom, id)
where st_within(p.geom, st_makeenvelope(33.9327, 47.7892, 34.6645, 48.2434))

返回G,D,H