{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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.\n", "\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n", "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.\n", "\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n" ] }, { "ename": "JSONDecodeError", "evalue": "Expecting value: line 1 column 1 (char 0)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mJSONDecodeError\u001b[0m Traceback (most recent call last)", "File \u001b[1;32mc:\\Users\\jtrum\\miniconda3\\envs\\wash_scan\\lib\\site-packages\\requests\\models.py:971\u001b[0m, in \u001b[0;36mResponse.json\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m 970\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m--> 971\u001b[0m \u001b[39mreturn\u001b[39;00m complexjson\u001b[39m.\u001b[39;49mloads(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtext, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[0;32m 972\u001b[0m \u001b[39mexcept\u001b[39;00m JSONDecodeError \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 973\u001b[0m \u001b[39m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[0;32m 974\u001b[0m \u001b[39m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n", "File \u001b[1;32mc:\\Users\\jtrum\\miniconda3\\envs\\wash_scan\\lib\\json\\__init__.py:357\u001b[0m, in \u001b[0;36mloads\u001b[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m 354\u001b[0m \u001b[39mif\u001b[39;00m (\u001b[39mcls\u001b[39m \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m object_hook \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m\n\u001b[0;32m 355\u001b[0m parse_int \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m parse_float \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m\n\u001b[0;32m 356\u001b[0m parse_constant \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m object_pairs_hook \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m kw):\n\u001b[1;32m--> 357\u001b[0m \u001b[39mreturn\u001b[39;00m _default_decoder\u001b[39m.\u001b[39;49mdecode(s)\n\u001b[0;32m 358\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mcls\u001b[39m \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n", "File \u001b[1;32mc:\\Users\\jtrum\\miniconda3\\envs\\wash_scan\\lib\\json\\decoder.py:337\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m 333\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001b[39;00m\n\u001b[0;32m 334\u001b[0m \u001b[39mcontaining a JSON document).\u001b[39;00m\n\u001b[0;32m 335\u001b[0m \n\u001b[0;32m 336\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m--> 337\u001b[0m obj, end \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mraw_decode(s, idx\u001b[39m=\u001b[39;49m_w(s, \u001b[39m0\u001b[39;49m)\u001b[39m.\u001b[39;49mend())\n\u001b[0;32m 338\u001b[0m end \u001b[39m=\u001b[39m _w(s, end)\u001b[39m.\u001b[39mend()\n", "File \u001b[1;32mc:\\Users\\jtrum\\miniconda3\\envs\\wash_scan\\lib\\json\\decoder.py:355\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[1;34m(self, s, idx)\u001b[0m\n\u001b[0;32m 354\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mStopIteration\u001b[39;00m \u001b[39mas\u001b[39;00m err:\n\u001b[1;32m--> 355\u001b[0m \u001b[39mraise\u001b[39;00m JSONDecodeError(\u001b[39m\"\u001b[39m\u001b[39mExpecting value\u001b[39m\u001b[39m\"\u001b[39m, s, err\u001b[39m.\u001b[39mvalue) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[0;32m 356\u001b[0m \u001b[39mreturn\u001b[39;00m obj, end\n", "\u001b[1;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[1;31mJSONDecodeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[1], line 54\u001b[0m\n\u001b[0;32m 52\u001b[0m geojson_file \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mC:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 53\u001b[0m osm_query \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m[waterway]\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m---> 54\u001b[0m visualize_water_infrastructure(geojson_file, osm_query)\n", "Cell \u001b[1;32mIn[1], line 38\u001b[0m, in \u001b[0;36mvisualize_water_infrastructure\u001b[1;34m(geojson_file, osm_query)\u001b[0m\n\u001b[0;32m 36\u001b[0m geometry \u001b[39m=\u001b[39m feature[\u001b[39m1\u001b[39m][\u001b[39m'\u001b[39m\u001b[39mgeometry\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[0;32m 37\u001b[0m geojson_geometry \u001b[39m=\u001b[39m geometry\u001b[39m.\u001b[39m__geo_interface__\n\u001b[1;32m---> 38\u001b[0m osm_data \u001b[39m=\u001b[39m query_osm_overpass(geojson_geometry, osm_query)\n\u001b[0;32m 40\u001b[0m \u001b[39m# Create markers for each water infrastructure element\u001b[39;00m\n\u001b[0;32m 41\u001b[0m \u001b[39mfor\u001b[39;00m element \u001b[39min\u001b[39;00m osm_data[\u001b[39m'\u001b[39m\u001b[39melements\u001b[39m\u001b[39m'\u001b[39m]:\n", "Cell \u001b[1;32mIn[1], line 12\u001b[0m, in \u001b[0;36mquery_osm_overpass\u001b[1;34m(geojson_geometry, osm_query)\u001b[0m\n\u001b[0;32m 8\u001b[0m payload \u001b[39m=\u001b[39m {\n\u001b[0;32m 9\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mdata\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mf\u001b[39m\u001b[39m'\u001b[39m\u001b[39m[out:json];(way\u001b[39m\u001b[39m{\u001b[39;00mosm_query\u001b[39m}\u001b[39;00m\u001b[39m(poly:\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mgeojson_geometry\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\u001b[39m););out;\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m 10\u001b[0m }\n\u001b[0;32m 11\u001b[0m response \u001b[39m=\u001b[39m requests\u001b[39m.\u001b[39mpost(overpass_url, data\u001b[39m=\u001b[39mpayload)\n\u001b[1;32m---> 12\u001b[0m data \u001b[39m=\u001b[39m response\u001b[39m.\u001b[39;49mjson()\n\u001b[0;32m 13\u001b[0m \u001b[39mreturn\u001b[39;00m data\n", "File \u001b[1;32mc:\\Users\\jtrum\\miniconda3\\envs\\wash_scan\\lib\\site-packages\\requests\\models.py:975\u001b[0m, in \u001b[0;36mResponse.json\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m 971\u001b[0m \u001b[39mreturn\u001b[39;00m complexjson\u001b[39m.\u001b[39mloads(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mtext, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs)\n\u001b[0;32m 972\u001b[0m \u001b[39mexcept\u001b[39;00m JSONDecodeError \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 973\u001b[0m \u001b[39m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[0;32m 974\u001b[0m \u001b[39m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n\u001b[1;32m--> 975\u001b[0m \u001b[39mraise\u001b[39;00m RequestsJSONDecodeError(e\u001b[39m.\u001b[39mmsg, e\u001b[39m.\u001b[39mdoc, e\u001b[39m.\u001b[39mpos)\n", "\u001b[1;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)" ] } ], "source": [ "import folium\n", "import geopandas as gpd\n", "import requests\n", "\n", "# Function to query OpenStreetMap using Overpass API\n", "def query_osm_overpass(geojson_geometry, osm_query):\n", " overpass_url = \"http://overpass-api.de/api/interpreter\"\n", " payload = {\n", " \"data\": f'[out:json];(way{osm_query}(poly:\"{geojson_geometry}\"););out;'\n", " }\n", " response = requests.post(overpass_url, data=payload)\n", " data = response.json()\n", " return data\n", "\n", "# Load GeoJSON file\n", "def load_geojson(file_path):\n", " gdf = gpd.read_file(file_path)\n", " return gdf\n", "\n", "# Main function\n", "def visualize_water_infrastructure(geojson_file, osm_query):\n", " gdf = load_geojson(geojson_file)\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n", " geojson_data = gdf.to_json()\n", " folium.GeoJson(geojson_data).add_to(m)\n", " for feature in gdf.iterrows():\n", " geometry = feature[1]['geometry']\n", " geojson_geometry = geometry.__geo_interface__\n", " osm_data = query_osm_overpass(geojson_geometry, osm_query)\n", " for element in osm_data['elements']:\n", " lat = element['lat']\n", " lon = element['lon']\n", " popup = folium.Popup(element.get('tags', {}), max_width=400)\n", " folium.Marker([lat, lon], popup=popup, icon=folium.Icon(color='blue')).add_to(m)\n", "\n", " m.save('water_infrastructure_map.html')\n", " print(\"Map saved as 'water_infrastructure_map.html'\")\n", "\n", "if __name__ == \"__main__\":\n", " geojson_file = \"C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson\"\n", " osm_query = '\"[waterway]\"'\n", " visualize_water_infrastructure(geojson_file, osm_query)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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.\n", "\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n", "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.\n", "\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Error querying Overpass API: 400 Client Error: Bad Request for url: http://overpass-api.de/api/interpreter\n" ] }, { "ename": "TypeError", "evalue": "'NoneType' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[1], line 53\u001b[0m\n\u001b[0;32m 51\u001b[0m geojson_file \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mC:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 52\u001b[0m osm_query \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mwaterway\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m---> 53\u001b[0m visualize_water_infrastructure(geojson_file, osm_query)\n", "Cell \u001b[1;32mIn[1], line 40\u001b[0m, in \u001b[0;36mvisualize_water_infrastructure\u001b[1;34m(geojson_file, osm_query)\u001b[0m\n\u001b[0;32m 38\u001b[0m geojson_geometry \u001b[39m=\u001b[39m geometry\u001b[39m.\u001b[39m__geo_interface__\n\u001b[0;32m 39\u001b[0m osm_data \u001b[39m=\u001b[39m query_osm_overpass(geojson_geometry, osm_query)\n\u001b[1;32m---> 40\u001b[0m \u001b[39mfor\u001b[39;00m element \u001b[39min\u001b[39;00m osm_data[\u001b[39m'\u001b[39;49m\u001b[39melements\u001b[39;49m\u001b[39m'\u001b[39;49m]:\n\u001b[0;32m 41\u001b[0m lat \u001b[39m=\u001b[39m element[\u001b[39m'\u001b[39m\u001b[39mlat\u001b[39m\u001b[39m'\u001b[39m]\n\u001b[0;32m 42\u001b[0m lon \u001b[39m=\u001b[39m element[\u001b[39m'\u001b[39m\u001b[39mlon\u001b[39m\u001b[39m'\u001b[39m]\n", "\u001b[1;31mTypeError\u001b[0m: 'NoneType' object is not subscriptable" ] } ], "source": [ "import folium\n", "import geopandas as gpd\n", "import requests\n", "from json import JSONDecodeError\n", "\n", "\n", "# Function to query OpenStreetMap using Overpass API\n", "def query_osm_overpass(geojson_geometry, osm_query):\n", " overpass_url = \"http://overpass-api.de/api/interpreter\"\n", " payload = {\n", " \"data\": f'[out:json];(way{osm_query}(poly:\"{geojson_geometry}\"););out;'\n", " }\n", " try:\n", " response = requests.post(overpass_url, data=payload)\n", " response.raise_for_status() # Raise an error for unsuccessful responses\n", " data = response.json()\n", " return data\n", " except requests.exceptions.RequestException as e:\n", " print(\"Error querying Overpass API:\", e)\n", " return None\n", " except JSONDecodeError as e:\n", " print(\"Error decoding JSON response from Overpass API:\", e)\n", " return None\n", "\n", "# Load GeoJSON file\n", "def load_geojson(file_path):\n", " gdf = gpd.read_file(file_path)\n", " return gdf\n", "\n", "# Main function\n", "def visualize_water_infrastructure(geojson_file, osm_query):\n", " gdf = load_geojson(geojson_file)\n", " m = folium.Map(location=[gdf.centroid.y.mean(), gdf.centroid.x.mean()], zoom_start=15)\n", " geojson_data = gdf.to_json()\n", " folium.GeoJson(geojson_data).add_to(m)\n", " for feature in gdf.iterrows():\n", " geometry = feature[1]['geometry']\n", " geojson_geometry = geometry.__geo_interface__\n", " osm_data = query_osm_overpass(geojson_geometry, osm_query)\n", " for element in osm_data['elements']:\n", " lat = element['lat']\n", " lon = element['lon']\n", " popup = folium.Popup(element.get('tags', {}), max_width=400)\n", " folium.Marker([lat, lon], popup=popup, icon=folium.Icon(color='blue')).add_to(m)\n", "\n", " m.save('water_infrastructure_map.html')\n", " print(\"Map saved as 'water_infrastructure_map.html'\")\n", "\n", "\n", "if __name__ == \"__main__\":\n", " geojson_file = \"C:/Users/jtrum/world_bank/luanda_angola/exports/luanda2clean.geojson\"\n", " osm_query = '\"waterway\"'\n", " visualize_water_infrastructure(geojson_file, osm_query)\n" ] } ], "metadata": { "kernelspec": { "display_name": "wash_scan", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.17" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }