我用 Python 编写了一个脚本,给定纬度/经度,生成包含道路边缘、人行横道和车道的 geojson。看起来像这样:
def fetch_intersection_data(lat, lon, radius: int = 20):
"""
Fetch intersection data from OpenStreetMap using Overpass API and convert to GeoJSON.
:param lat: Latitude of the center of the intersection
:param lon: Longitude of the center of the intersection
:param radius: Radius around the point to search (in meters)
:return: GeoJSON data as a dictionary
"""
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = f"""
[out:json];
(
way["highway"](around:{radius},{lat},{lon});
node["highway"="crossing"](around:{radius},{lat},{lon});
way["lanes"](around:{radius},{lat},{lon});
way["lanes:forward"](around:{radius},{lat},{lon});
way["lanes:backward"](around:{radius},{lat},{lon});
);
out geom;
"""
print(overpass_query)
response = requests.get(overpass_url, params={'data': overpass_query})
osm_data = response.json()
with open('raw_data.json', 'w') as f:
json.dump(osm_data, f)
try:
features = []
for element in osm_data['elements']:
feature = {
"type": "Feature",
"properties": element.get('tags', {}),
"geometry": None
}
if element['type'] == 'way':
if 'geometry' in element:
feature['geometry'] = {
"type": "LineString",
"coordinates": [[point['lon'], point['lat']] for point in element['geometry']]
}
feature['properties']['id'] = element['id']
elif element['type'] == 'node':
feature['geometry'] = {
"type": "Point",
"coordinates": [element['lon'], element['lat']]
}
feature['properties']['id'] = element['id']
if feature['geometry']:
features.append(feature)
geojson = {"type": "FeatureCollection","features": features}
except Exception as e:
print(e)
return geojson
到目前为止一切顺利:它几乎完成了我需要的一切。例如,这就是我在
geojson.io
上绘制时 geojson 的样子:
现在,缺少的是道路内的车道。我的意思是我希望能够获取几何图形/坐标来绘制这样的东西(红色):
API返回的JSON文件似乎没有车道坐标。我不确定我的查询是否缺少某些内容或者此信息是否不可用。
有什么想法吗?
OSM中的车道没有自己的坐标。车道是通过相应高速公路的属性指定的。它们主要由车道数和方向(相对于它们所属路线的方向)组成,还有一些很少出现的其他属性。所以你必须自己估计他们的位置。