的方法如下:
import osmium
import math
class FacilityHandler(osmium.SimpleHandler):
def __init__(self, center_lat, center_lon, radius):
super(FacilityHandler, self).__init__()
self.facilities = []
self.center_lat = center_lat
self.center_lon = center_lon
self.radius = radius
def way(self, w):
for tag in w.tags:
if tag.k == "amenity":
lat = w.center().lat
lon = w.center().lon
distance = self.calculate_distance(lat, lon)
if distance <= self.radius:
facility_info = {
"name": tag.v,
"lat": lat,
"lon": lon,
"distance": distance
}
self.facilities.append(facility_info)
break
def calculate_distance(self, lat, lon):
lat1 = math.radians(self.center_lat)
lon1 = math.radians(self.center_lon)
lat2 = math.radians(lat)
lon2 = math.radians(lon)
radius = 6371 # approximate radius of the Earth in kilometers
dlat = lat2 - lat1
dlon = lon2 - lon1
a = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2) * math.sin(dlon/2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = radius * c
return distance
def get_facilities(center_lat, center_lon, radius, pbf_file_path):
facility_handler = FacilityHandler(center_lat, center_lon, radius)
# 创建Osmium Reader并使用处理器处理PBF文件
reader = osmium.io.Reader(pbf_file_path)
osmium.apply(reader, facility_handler)
reader.close()
return facility_handler.facilities
center_lat = 40.7128
center_lon = -74.0060
radius = 10 # 单位:千米
pbf_file_path = "path/to/pbf/file.pbf"
facilities = get_facilities(center_lat, center_lon, radius, pbf_file_path)
for facility in facilities:
print(facility)
注意:这里的代码示例假设你已经安装了 esy-osm-pbf
和相关的库,并且已经下载了 OpenStreetMap 的 PBF 数据文件。另外,你还需要根据实际情况修改代码中的经纬度、半径和 PBF 文件路径。
领取专属 10元无门槛券
手把手带您无忧上云