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)
C:\Users\jtrum\AppData\Local\Temp\ipykernel_19848\3958551895.py:26: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15) C:\Users\jtrum\AppData\Local\Temp\ipykernel_19848\3958551895.py:26: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)
--------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) File c:\Users\jtrum\miniconda3\envs\wash_scan\lib\site-packages\requests\models.py:971, in Response.json(self, **kwargs) 970 try: --> 971 return complexjson.loads(self.text, **kwargs) 972 except JSONDecodeError as e: 973 # Catch JSON-related errors and raise as requests.JSONDecodeError 974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError File c:\Users\jtrum\miniconda3\envs\wash_scan\lib\json\__init__.py:357, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 354 if (cls is None and object_hook is None and 355 parse_int is None and parse_float is None and 356 parse_constant is None and object_pairs_hook is None and not kw): --> 357 return _default_decoder.decode(s) 358 if cls is None: File c:\Users\jtrum\miniconda3\envs\wash_scan\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w) 333 """Return the Python representation of ``s`` (a ``str`` instance 334 containing a JSON document). 335 336 """ --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 338 end = _w(s, end).end() File c:\Users\jtrum\miniconda3\envs\wash_scan\lib\json\decoder.py:355, in JSONDecoder.raw_decode(self, s, idx) 354 except StopIteration as err: --> 355 raise JSONDecodeError("Expecting value", s, err.value) from None 356 return obj, end JSONDecodeError: Expecting value: line 1 column 1 (char 0) During handling of the above exception, another exception occurred: JSONDecodeError Traceback (most recent call last) Cell In[1], line 54 52 geojson_file = "C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson" 53 osm_query = '"[waterway]"' ---> 54 visualize_water_infrastructure(geojson_file, osm_query) Cell In[1], line 38, in visualize_water_infrastructure(geojson_file, osm_query) 36 geometry = feature[1]['geometry'] 37 geojson_geometry = geometry.__geo_interface__ ---> 38 osm_data = query_osm_overpass(geojson_geometry, osm_query) 40 # Create markers for each water infrastructure element 41 for element in osm_data['elements']: Cell In[1], line 12, in query_osm_overpass(geojson_geometry, osm_query) 8 payload = { 9 "data": f'[out:json];(way{osm_query}(poly:"{geojson_geometry}"););out;' 10 } 11 response = requests.post(overpass_url, data=payload) ---> 12 data = response.json() 13 return data File c:\Users\jtrum\miniconda3\envs\wash_scan\lib\site-packages\requests\models.py:975, in Response.json(self, **kwargs) 971 return complexjson.loads(self.text, **kwargs) 972 except JSONDecodeError as e: 973 # Catch JSON-related errors and raise as requests.JSONDecodeError 974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError --> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos) JSONDecodeError: Expecting value: line 1 column 1 (char 0)
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)
C:\Users\jtrum\AppData\Local\Temp\ipykernel_10936\3089025524.py:33: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15) C:\Users\jtrum\AppData\Local\Temp\ipykernel_10936\3089025524.py:33: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)
Error querying Overpass API: 400 Client Error: Bad Request for url: http://overpass-api.de/api/interpreter
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[1], line 53 51 geojson_file = "C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson" 52 osm_query = '"waterway"' ---> 53 visualize_water_infrastructure(geojson_file, osm_query) Cell In[1], line 40, in visualize_water_infrastructure(geojson_file, osm_query) 38 geojson_geometry = geometry.__geo_interface__ 39 osm_data = query_osm_overpass(geojson_geometry, osm_query) ---> 40 for element in osm_data['elements']: 41 lat = element['lat'] 42 lon = element['lon'] TypeError: 'NoneType' object is not subscriptable