从 .osm 文件绘制未排序的方式/区域的节点引用



所以我试图从.osm节点引用中定义建筑物的确切形状(外层(,因为我需要根据一些假设在其内部(房间,墙壁(创建更详细的结构。

到目前为止,我已经使用 pyosmium 从其引用的"building:part"中提取节点的坐标,将节点的坐标存储到元组列表中,使用 shapely 的多边形函数重建它,并使用 mplleaflet 绘制它。但不知何故,引用中的节点没有排序,当我尝试绘制它时,显示了很多交叉点。

我目前解决此排序问题的方法是:

def distance(current_data, x_point):
# find the shortest distance between x_point and all points in current data
# check if it's perpendicular to the line built before
return current_data[x] # which perpendicular and has shortest distance to x_point
def sort_nodes(nodes):
temp = []
for node in nodes:
if len(temp) < 2: # adding first 2 points as a starting line 
temp.append(node)
else:
n = distance(temp, node)
# find index of current_data[x] in temp
return temp.insert(min_index, node)

仅按距离(最短(对坐标元组进行排序仍然不能解决问题。即使根据其程度对其进行排序也可能导致另一个问题,并非所有建筑物都是矩形的。

所以这就是我如何通过基于距离进行排序来达到这一步。

有没有更好的方法可以做到这一点?还是我做错了?我已经连续尝试了 2 天。如果这是微不足道的,我很抱歉,但我对编码真的很陌生,需要完成这项工作。感谢您的帮助。

编辑:回答SCAI

这是我提取节点的以下方法:

import osmium as osm
def way_filter():
class WayFilter(osm.SimpleHandler):
def __init__(self):
super(WayFilter, self).__init__()
self.nodes = []
def way(self, w):
if 'building:part' in w.tags and w.tags['building:part'] == 'hospital':
temp = []
for n in w.nodes:
temp.append(n.ref)
self.nodes.append(temp)
ways = WayFilter()
ways.apply_file(map)
return ways.nodes
def get_node(ref_node):
class ObjectCounterHandler(osm.SimpleHandler):
def __init__(self):
osm.SimpleHandler.__init__(self)
self.location = []
self.ref = ref_node
def write_object(self, lon, lat):
self.location.append([lon, lat])
def node(self, n):
try:
if any(n.id in sublist for sublist in self.ref):
self.write_object(n.location.lon, n.location.lat)
except TypeError:
if n.id in self.ref:
self.write_object(n.location.lon, n.location.lat)
h = ObjectCounterHandler()
h.apply_file(map)
return h.location

主程序

a = way_filter()
for ref in a:
b = get_node(ref)
c = next(colors)
loc = []
for x in b:
loc.append(tuple(x))
# plot the points
polygons = Polygon(loc)
x,y = polygons.exterior.xy
plt.plot(x,y, zorder=1) 
mplleaflet.show()

这是没有排序的结果。 没有排序的绘图图像

节点以正确的顺序引用,即它们彼此相邻。如果正确读取引用的节点 ID 列表,则无需执行任何手动排序。只有关系元素才需要手动排序。

不幸的是,我不熟悉pyosmium,所以我不能告诉你你的代码有什么问题。

最新更新