#!/usr/bin/env python # coding: utf-8 # In[1]: import folium import geopandas as gpd import requests # Function to query OpenStreetMap using Overpass API def query_osm_overpass(geojson_geometry, osm_query): overpass_url = "http://overpass-api.de/api/interpreter" payload = { "data": f'[out:json];(way{osm_query}(poly:"{geojson_geometry}"););out;' } response = requests.post(overpass_url, data=payload) data = response.json() return data # Load GeoJSON file def load_geojson(file_path): gdf = gpd.read_file(file_path) return gdf # Main function def visualize_water_infrastructure(geojson_file, osm_query): gdf = load_geojson(geojson_file) m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15) geojson_data = gdf.to_json() folium.GeoJson(geojson_data).add_to(m) for feature in gdf.iterrows(): geometry = feature[1]['geometry'] geojson_geometry = geometry.__geo_interface__ osm_data = query_osm_overpass(geojson_geometry, osm_query) for element in osm_data['elements']: lat = element['lat'] lon = element['lon'] popup = folium.Popup(element.get('tags', {}), max_width=400) folium.Marker([lat, lon], popup=popup, icon=folium.Icon(color='blue')).add_to(m) m.save('water_infrastructure_map.html') print("Map saved as 'water_infrastructure_map.html'") if __name__ == "__main__": geojson_file = "C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson" osm_query = '"[waterway]"' visualize_water_infrastructure(geojson_file, osm_query) # In[1]: import folium import geopandas as gpd import requests from json import JSONDecodeError # Function to query OpenStreetMap using Overpass API def query_osm_overpass(geojson_geometry, osm_query): overpass_url = "http://overpass-api.de/api/interpreter" payload = { "data": f'[out:json];(way{osm_query}(poly:"{geojson_geometry}"););out;' } try: response = requests.post(overpass_url, data=payload) response.raise_for_status() # Raise an error for unsuccessful responses data = response.json() return data except requests.exceptions.RequestException as e: print("Error querying Overpass API:", e) return None except JSONDecodeError as e: print("Error decoding JSON response from Overpass API:", e) return None # Load GeoJSON file def load_geojson(file_path): gdf = gpd.read_file(file_path) return gdf # Main function def visualize_water_infrastructure(geojson_file, osm_query): gdf = load_geojson(geojson_file) m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15) geojson_data = gdf.to_json() folium.GeoJson(geojson_data).add_to(m) for feature in gdf.iterrows(): geometry = feature[1]['geometry'] geojson_geometry = geometry.__geo_interface__ osm_data = query_osm_overpass(geojson_geometry, osm_query) for element in osm_data['elements']: lat = element['lat'] lon = element['lon'] popup = folium.Popup(element.get('tags', {}), max_width=400) folium.Marker([lat, lon], popup=popup, icon=folium.Icon(color='blue')).add_to(m) m.save('water_infrastructure_map.html') print("Map saved as 'water_infrastructure_map.html'") if __name__ == "__main__": geojson_file = "C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson" osm_query = '"waterway"' visualize_water_infrastructure(geojson_file, osm_query)