|
942 | 942 | "metadata": {}, |
943 | 943 | "outputs": [], |
944 | 944 | "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" |
956 | 965 | ] |
957 | 966 | }, |
958 | 967 | { |
|
1025 | 1034 | "metadata": {}, |
1026 | 1035 | "outputs": [], |
1027 | 1036 | "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" |
1033 | 1057 | ] |
1034 | 1058 | }, |
1035 | 1059 | { |
|
0 commit comments