Visualizing Amazon Warehouse Locations in the US using Python, Google Maps Geocoding API, and Folium

Sohail Hosseini
5 min readApr 6, 2023

--

Introduction

In this tutorial, we’ll learn how to visualize the locations of Amazon warehouses in the United States on an interactive map. To achieve this, we’ll scrape the warehouse data from a website, use the Google Maps Geocoding API to obtain latitude and longitude coordinates for each warehouse, and finally create an interactive map using the Folium library.

This tutorial assumes you have some basic knowledge of Python and web scraping. If you’re not familiar with these topics, you may want to brush up on them before proceeding.

We’ll be covering the following steps:

  1. Setting up the environment
  2. Web scraping to obtain warehouse data
  3. Geocoding addresses using the Google Maps Geocoding API
  4. Creating an interactive map using Folium

Let’s get started!

  1. Setting up the environment

First, make sure you have Python 3 installed on your computer. You can check your Python version by running the following command in your terminal or command prompt:

python --version

If you don’t have Python 3 installed, you can download it from the official Python website: https://www.python.org/downloads/

Next, you’ll need to install the following Python packages:

  • requests: A library for making HTTP requests
  • beautifulsoup4: A library for parsing HTML and XML documents
  • folium: A library for creating interactive maps
  • googlemaps: A library for using Google Maps APIs

You can install these packages using pip:

pip install requests beautifulsoup4 folium googlemaps

2. Web scraping to obtain warehouse data

We’ll be scraping warehouse data from the following website: https://www.bestforworld.com/the-us-amazon-warehouse-selection-the-amazon-fulfillment-center-locations-updated-2022

The data is presented in an HTML table, which we’ll parse using BeautifulSoup, a popular Python library for web scraping.

Here’s the code to fetch the warehouse data:

import requests
from bs4 import BeautifulSoup

url = 'https://www.bestforworld.com/the-us-amazon-warehouse-selection-the-amazon-fulfillment-center-locations-updated-2022'

response = requests.get(url)
html_content = response.text

soup = BeautifulSoup(html_content, 'html.parser')

table = soup.find('table')
rows = table.find_all('tr')

warehouses = []

for row in rows[1:]:
cells = row.find_all('td')
if len(cells) >= 5:
warehouse = {
'FBA Code': cells[0].text,
'US Amazon warehouse location': cells[1].text,
'City': cells[2].text,
'State': cells[3].text,
'Zip': cells[4].text
}
warehouses.append(warehouse)

# Remove rows with empty 'City' value
warehouses = [warehouse for warehouse in warehouses if warehouse['City'].strip() != '' and warehouse['City'].strip() != 'City']

In this code snippet, we first import the requests and BeautifulSoup libraries. We then send an HTTP GET request to the website using requests.get(), and parse the response text using BeautifulSoup. Next, we find the table containing the warehouse data and extract the information from each row using the find_all() method.

The resulting warehouses list contains dictionaries with the following keys:

  • FBA Code: The fulfillment center code
  • US Amazon warehouse location: The warehouse location description
  • City: The city where the warehouse is located
  • State: The state where the warehouse is located
  • Zip: The ZIP code of the warehouse location

Finally, we remove any rows with empty ‘City’ values or rows containing the header ‘City’.

3. Geocoding addresses using the Google Maps Geocoding API

With the warehouse data in hand, we now need to obtain the latitude and longitude coordinates for each warehouse. We’ll use the Google Maps Geocoding API for this task.

First, you’ll need to obtain an API key for the Google Maps Geocoding API. You can get one from the Google Cloud Platform Console.

Once you have your API key, we’ll use the googlemaps package to interact with the API. Here's a function that takes a gmaps client object and an address string as input and returns the latitude and longitude coordinates:

import googlemaps

google_api_key = "YOUR_API_KEY" # Replace with your actual API key
gmaps = googlemaps.Client(key=google_api_key)

def get_coordinates(gmaps, address):
try:
geocode_result = gmaps.geocode(address)
if geocode_result:
location = geocode_result[0]["geometry"]["location"]
return location["lat"], location["lng"]
else:
return None, None
except Exception as e:
print(f"Error occurred while geocoding: {e}")
return None, None

Remember to replace "YOUR_API_KEY" with your actual API key. The get_coordinates() function uses the Google Maps Geocoding API to obtain the latitude and longitude for the given address.

Now, let’s add latitude and longitude coordinates to our warehouses list:

for warehouse in warehouses:
address = f"{warehouse['City']}, {warehouse['State']}, {warehouse['Zip']}, USA"
latitude, longitude = get_coordinates(gmaps, address)
if latitude and longitude:
warehouse['latitude'] = latitude
warehouse['longitude'] = longitude
else:
print(f"Failed to geocode address: {address}")

This code iterates through the warehouses list and calls the get_coordinates() function for each warehouse's address, adding the latitude and longitude to the warehouse dictionary.

4. Creating an interactive map using Folium

Now that we have the warehouse locations, we can create an interactive map using the Folium library. Folium allows us to create Leaflet.js maps using Python.

Here’s how to create a map and add markers for each warehouse:

import folium

# Remove warehouses without coordinates
warehouses = [warehouse for warehouse in warehouses if 'latitude' in warehouse and 'longitude' in warehouse]

map_warehouses = folium.Map(location=[39.8283, -98.5795], zoom_start=4) # Centered on the US

for warehouse in warehouses:
folium.Marker(
location=[warehouse['latitude'], warehouse['longitude']],
popup=f"{warehouse['FBA Code']} - {warehouse['US Amazon warehouse location']} - {warehouse['City']}, {warehouse['State']}"
).add_to(map_warehouses)

map_warehouses.save("warehouses_map.html")

First, we import the folium library and remove any warehouses without latitude and longitude coordinates. Next, we create a folium.Map object centered on the United States with a zoom level of 4.

Then, we loop through the warehouses list and add a marker to the map for each warehouse, using the folium.Marker() function. We set the location parameter to the warehouse's latitude and longitude, and create a popup containing the warehouse's FBA code, location description, city, and state. We add the marker to the map using the add_to() method.

Finally, we save the map as an HTML file called “warehouses_map.html” using the save() method. You can open this file in your web browser to view the interactive map with all the warehouse locations.

If you’re using Jupyter Notebook or JupyterLab, you can display the map inline by simply returning the map_warehouses object at the end of the code:

map_warehouses

Add this line to the end of your code, and the map should be displayed within your Jupyter Notebook or JupyterLab environment.

Conclusion

In this tutorial, we learned how to visualize Amazon warehouse locations in the US using Python, the Google Maps Geocoding API, and Folium. We started by setting up our environment, then scraped warehouse data from a website using BeautifulSoup. We geocoded the warehouse addresses using the Google Maps Geocoding API and finally created an interactive map with Folium.

By following this tutorial, you should now have a better understanding of how to work with web scraping, geocoding, and mapping in Python. You can adapt these techniques to visualize other types of location data, such as store locations, distribution centers, or points of interest.

BECOME a WRITER at MLearning.ai // AI Factory XR Super Cheap AI

--

--