Skip to content

Commit 4cc3cbf

Browse files
committed
builds broken due to stopped service; now fixed
1 parent 7dbb5b5 commit 4cc3cbf

1 file changed

Lines changed: 40 additions & 16 deletions

File tree

‎geo-intro.ipynb‎

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -942,17 +942,26 @@
942942
"metadata": {},
943943
"outputs": [],
944944
"source": [
945-
"# If you don't have it, install geopy using 'pip install geopy'\n",
946-
"from geopy.geocoders import Nominatim\n",
947-
"\n",
948-
"# Create a geolocator using Open Street Map (aka Nominatim)\n",
949-
"# Use your own user agent identifier here\n",
950-
"geolocator = Nominatim(user_agent=\"codeforecon\")\n",
951-
"\n",
952-
"# Pass an address to retrieve full location information:\n",
953-
"location = geolocator.geocode(\"Bank of England\")\n",
954-
"\n",
955-
"location.address"
945+
"geolocator = Nominatim(user_agent=\"coding-for-economists/1.0\")\n",
946+
"\n",
947+
"# Function to safely geocode a place name\n",
948+
"# We use a local loop here because the Photon API (photon.komoot.io) frequently times out.\n",
949+
"# Using Nominatim directly gives us more control over timeouts and error handling.\n",
950+
"def get_geo(point_name):\n",
951+
" try:\n",
952+
" loc = geolocator.geocode(f\"{point_name}, NY\", timeout=10)\n",
953+
" return Point(loc.latitude, loc.longitude) if loc else None\n",
954+
" except Exception:\n",
955+
" return None\n",
956+
"\n",
957+
"boros_df['geometry'] = boros_df.boro_name.apply(get_geo)\n",
958+
"# Drop any failed geocode attempts before converting to GeoDataFrame\n",
959+
"boroughs_gdf = gpd.GeoDataFrame(\n",
960+
" boros_df.dropna(subset=['geometry']), \n",
961+
" geometry='geometry', \n",
962+
" crs='EPSG:4326'\n",
963+
")\n",
964+
"boroughs_gdf\n"
956965
]
957966
},
958967
{
@@ -1025,11 +1034,26 @@
10251034
"metadata": {},
10261035
"outputs": [],
10271036
"source": [
1028-
"boros_df[[\"geometry\", \"address\"]] = gpd.tools.geocode(\n",
1029-
" boros_df.boro_name,\n",
1030-
" provider=\"photon\",\n",
1031-
").set_geometry(\"geometry\")\n",
1032-
"boros_df"
1037+
"geolocator = Nominatim(user_agent=\"coding-for-economists/1.0\")\n",
1038+
"\n",
1039+
"# Function to safely geocode a place name\n",
1040+
"# We use a local loop here because the Photon API (photon.komoot.io) frequently times out.\n",
1041+
"# Using Nominatim directly gives us more control over timeouts and error handling.\n",
1042+
"def get_geo(point_name):\n",
1043+
" try:\n",
1044+
" loc = geolocator.geocode(f\"{point_name}, NY\", timeout=10)\n",
1045+
" return Point(loc.latitude, loc.longitude) if loc else None\n",
1046+
" except Exception:\n",
1047+
" return None\n",
1048+
"\n",
1049+
"boros_df['geometry'] = boros_df.boro_name.apply(get_geo)\n",
1050+
"# Drop any failed geocode attempts before converting to GeoDataFrame\n",
1051+
"boroughs_gdf = gpd.GeoDataFrame(\n",
1052+
" boros_df.dropna(subset=['geometry']), \n",
1053+
" geometry='geometry', \n",
1054+
" crs='EPSG:4326'\n",
1055+
")\n",
1056+
"boroughs_gdf\n"
10331057
]
10341058
},
10351059
{

0 commit comments

Comments
 (0)