如何在一个查询中包含三个几何图形 - PostGIS
How to include three geometries in one query - PostGIS
我是 SQL 的新手,我在创建包含三个几何图形的查询时遇到困难。
我有一个点层(建筑物),我想从中计算所有点建筑物,它们与某个多边形(市政单位)中的线(道路)的最大距离为 50 米,使用两个额外的标准三个 tables.
这是 table 结构:
Table 1: building (id_building, address_name,color_tagged,point)
Table 2: roads (id_road, line)
Table 3: munic_units (id_munic, munic_name, polygon)
我尝试了下面的代码并做了很多更改..但是它给我错误。
我很乐意听到任何建议。谢谢你。
SELECT address_name, count(*) AS Frequency,munic_name,color_tagged
FROM building,roads,munic_units
WHERE ST_CONTAINS((SELECT polygon FROM munic_units WHERE munic_name=''),(ST_DWithin((SELECT point FROM building WHERE color_tagged=''),line,50)))
GROUP BY address_name,munic_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;
首先,我尝试了一个具有两个几何形状的更简单的版本:
SELECT address_name, count(*) AS Frequency,color_tagged
FROM building,roads,loc_munic_units
WHERE ST_Dwithin(point,roads_geom,50) AND color_tagged='YELLOW'
GROUP BY address_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;
和returns...enter image description here ..这个阶段的预期结果是找到距离道路50米以内的所有建筑物color_tagged作为'Yellow'。最终想要的结果是 运行 之前只在特定区域搜索 - tables 的一个 polygon.The 结构如下所示。
您已接近解决方案。第一个查询失败,因为您没有正确加入 tables。第二个查询可能 returns 高估了计数,因为您与城市 table.
进行了交叉连接
使用第二个查询,您可以添加缺少的连接条件(并正确编写连接)
SELECT address_name, count(*) AS Frequency,color_tagged
FROM building b --selects from buildings
JOIN roads r ON ST_Dwithin(b.point,r.roads_geom,50) -- when building is within 50 of a road
JOIN loc_munic_units m ON ST_WITHIN(r.roads_geom, m.polygon) -- and when the road is within a municipality polygon
WHERE b.color_tagged='YELLOW' AND m.munic_name = 'abc' -- restrain to a specific municipality (or >1) and color
GROUP BY address_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;
我是 SQL 的新手,我在创建包含三个几何图形的查询时遇到困难。 我有一个点层(建筑物),我想从中计算所有点建筑物,它们与某个多边形(市政单位)中的线(道路)的最大距离为 50 米,使用两个额外的标准三个 tables.
这是 table 结构:
Table 1: building (id_building, address_name,color_tagged,point)
Table 2: roads (id_road, line)
Table 3: munic_units (id_munic, munic_name, polygon)
我尝试了下面的代码并做了很多更改..但是它给我错误。 我很乐意听到任何建议。谢谢你。
SELECT address_name, count(*) AS Frequency,munic_name,color_tagged
FROM building,roads,munic_units
WHERE ST_CONTAINS((SELECT polygon FROM munic_units WHERE munic_name=''),(ST_DWithin((SELECT point FROM building WHERE color_tagged=''),line,50)))
GROUP BY address_name,munic_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;
首先,我尝试了一个具有两个几何形状的更简单的版本:
SELECT address_name, count(*) AS Frequency,color_tagged
FROM building,roads,loc_munic_units
WHERE ST_Dwithin(point,roads_geom,50) AND color_tagged='YELLOW'
GROUP BY address_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;
和returns...enter image description here ..这个阶段的预期结果是找到距离道路50米以内的所有建筑物color_tagged作为'Yellow'。最终想要的结果是 运行 之前只在特定区域搜索 - tables 的一个 polygon.The 结构如下所示。
您已接近解决方案。第一个查询失败,因为您没有正确加入 tables。第二个查询可能 returns 高估了计数,因为您与城市 table.
进行了交叉连接使用第二个查询,您可以添加缺少的连接条件(并正确编写连接)
SELECT address_name, count(*) AS Frequency,color_tagged
FROM building b --selects from buildings
JOIN roads r ON ST_Dwithin(b.point,r.roads_geom,50) -- when building is within 50 of a road
JOIN loc_munic_units m ON ST_WITHIN(r.roads_geom, m.polygon) -- and when the road is within a municipality polygon
WHERE b.color_tagged='YELLOW' AND m.munic_name = 'abc' -- restrain to a specific municipality (or >1) and color
GROUP BY address_name,color_tagged
ORDER BY Frequency DESC,address_name DESC;