1st November, 2023

Continuing from last time I am trying to see if we can somehow correlate our geographical data with how far or how close we are from densely populated urban areas.

This work involved a lot of mapping data from different sources to make sure that they are all aligned to be able to plot the grographical data in accordance.

This is a big hurdle for me at the moment because the data sources and libraries of python that I have found for using US state wise data do not have the required granularity of being able to show population density. There is a way to map it by county level but that would require boundaries in terms of coorrdinates to follow which is immensely difficult to find for a county.

So for now I am trying to show density by creating my own dataset of cities with populations greater than 1 million and finding their geographical coordinates manually and mapping them to the city names

This will help me process the data efficiently and be able to overlay the densely populated cities over the data of the shootings.

I wish I could show the output for the overlay however the code for the data cleaning still has some minor issues which I am trying to fix for the time being because it does not always translate as 1:1 when creating a new data set and trying to match it with an existing one.

This is in fact just a huge lookup proocess that needs to complete properly before the plotting functions can work without errors.

import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import us
# Specify the full path to your Excel file using a raw string
excel_file_path = r’C:\Users\91766\Desktop\fatal-police-shootings-data.xlsx’
# Read data from Excel file
df = pd.read_excel(excel_file_path)
# Create a map of the USA using Cartopy
fig, ax = plt.subplots(subplot_kw={‘projection’: ccrs.PlateCarree()}, figsize=(12, 9))
ax.set_extent([-125, -66, 24, 49])  # USA bounding box
capitals = [“Washington, D.C.”, “Montgomery”, “Juneau”, “Phoenix”, “Little Rock”, “Sacramento”, “Denver”, “Hartford”, “Dover”, “Tallahassee”, “Atlanta”, “Honolulu”, “Boise”, “Springfield”, “Indianapolis”, “Des Moines”, “Topeka”, “Frankfort”, “Baton Rouge”, “Augusta”, “Annapolis”, “Boston”, “Lansing”, “St. Paul”, “Jackson”, “Jefferson City”, “Helena”, “Lincoln”, “Carson City”, “Concord”, “Trenton”, “Santa Fe”, “Albany”, “Raleigh”, “Bismarck”, “Columbus”, “Oklahoma City”, “Salem”, “Harrisburg”, “Providence”, “Columbia”, “Pierre”, “Nashville”, “Austin”, “Salt Lake City”, “Montpelier”, “Richmond”, “Olympia”, “Charleston”, “Madison”, “Cheyenne”]
geolocator = Nominatim(user_agent=”my_geocoder”)
coordinates = []
for capital in capitals:
    location = geolocator.geocode(capital)
    if location:
        coordinates.append((capital, location.latitude, location.longitude))
    else:
        coordinates.append((capital, “Not found”, “Not found”))
# Print the results
for data in coordinates:
    print(data)
# Extract latitude and longitude columns
latitude_column = ‘latitude’  # Replace with your actual column name
longitude_column = ‘longitude’  # Replace with your actual column name
latitudes = df[latitude_column].tolist()
longitudes = df[longitude_column].tolist()
# Plotting the coordinates
ax.scatter(longitudes, latitudes, s=10, c=’red’, marker=’o’, alpha=0.7, edgecolor=’k’, transform=ccrs.Geodetic())
# Add map features
ax.coastlines(resolution=’10m’, color=’black’, linewidth=1)
ax.add_feature(cfeature.BORDERS, linestyle=’:’)
# Get and plot capital cities using the us library
for state in us.STATES:
    capital = us.states.lookup(state.capital)
    ax.text(capital.longitude, capital.latitude, state.capital, transform=ccrs.PlateCarree(), fontsize=8, ha=’right’, va=’bottom’, color=’blue’)
# Draw state lines
ax.add_feature(cfeature.STATES, linestyle=’-‘, edgecolor=’black’)
# Show the plot
plt.title(‘Coordinates Plot on the Map of the USA with State Capitals Highlighted’)
plt.show()

 

Leave a Reply

Your email address will not be published. Required fields are marked *