Answers:
关于我的测试数据:
解决方案一
如果有两个假设:
道路是住宅区。
您正在使用公制系统。
这个想法是增加/减少点的X和Y坐标。如果您在公制系统内工作,则可以向您的点以东1m处,创建一个新点,并用原始点创建一条线。您将向东走,直到线与一条道路相交。要在西部寻找相交点,必须从原始X坐标减去1m。Y坐标相同。如果在北/东/南/西没有路,则计数器在1000(m)处停止。当您知道一条距离超过1000m的道路时,您必须更改此值。
您可以使用以下代码解决该任务:
已编辑
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "point":
startpoint = lyr
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "roads":
roads = lyr
startpoint_iter = startpoint.getFeatures()
for feature in startpoint_iter:
geom = feature.geometry()
if geom.type() == QGis.Point:
xy = geom.asPoint()
x,y = xy[0], xy[1]
line_start = QgsPoint(x,y)
def reached(direction, count_m):
road_reached = None
road = None
count=1
while road_reached < 1 and count <=count_m:
count += 1
if direction == 'N':
line_end = QgsPoint(x, y+count)
if direction == 'E':
line_end = QgsPoint(x+count,y)
if direction == 'S':
line_end = QgsPoint(x,y-count)
if direction == 'W':
line_end = QgsPoint(x-count,y)
line = QgsGeometry.fromPolyline([line_start,line_end])
for f in roads.getFeatures():
if line.intersects(f.geometry()):
road_reached = 1
road = f['name']
print road
reached('N', 1000)
reached('E', 1000)
reached('S', 1000)
reached('W', 1000)
另一个例子表明,东部的道路e没有被识别为该点附近的道路。
如何调用函数和输出:
>>>>reached('N', 1000)
road a
>>>>reached('E', 1000)
road b
>>>>reached('S', 1000)
road c
>>>>reached('W', 1000)
road d
如果封闭点的道路超过4条,则您必须朝更多方向看(同时更改X和Y)。或者,您可以更改线的方位角,这意味着您可以在0-360°范围内将其旋转1度。
解决方案二
从评论中得到启发,您也Polygonize
可以先走道路。因此,您可以使用QGIS中的工具:Processing > Toolbox > QGIS geoalgorithms > Vector geometry tools > Polygonize
。重命名临时层为polygon
。假设您只想拥有完全被道路包围的点的道路名称。否则,您必须使用解我。仅在所有道路都已连接(捕捉)的情况下才有效!
首先,该点必须与多边形相交。现在的想法是,AND
封闭线的两个起点都必须与多边形相交。
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "point":
startpoint = lyr
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "polygon":
poly = lyr
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "roads":
roads = lyr
for h in startpoint.getFeatures():
for g in poly.getFeatures():
if h.geometry().intersects(g.geometry()):
poly_geom = g.geometry()
for f in roads.getFeatures():
geom = f.geometry().asPolyline()
start_point = QgsGeometry.fromPoint(QgsPoint(geom[0]))
end_point = QgsGeometry.fromPoint(QgsPoint(geom[-1]))
if poly_geom.intersects(start_point) and poly_geom.intersects(end_point):
print f['name']
输出:
road c
road b
road e
road f