Guest User

2024 New Jersey UAP Flap Location Visualization Code (Python)

a guest
Dec 6th, 2024
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. import geopandas as gpd
  2. import matplotlib.pyplot as plt
  3. from geopy.geocoders import Nominatim
  4. from shapely.geometry import Point
  5.  
  6. # List of locations in New Jersey
  7. locations = [
  8.     "Hillsboro, NJ", "Morristown, NJ", "Morris County, NJ", "Union County, NJ", "Scotch Plains, NJ",
  9.     "Westfield, NJ", "Cranford, NJ", "Garwood, NJ", "North Plainfield, NJ", "Rutherford, NJ",
  10.     "Sparta, NJ", "Sussex County, NJ", "Florham Park, NJ", "Burlington County, NJ", "Somerset County, NJ",
  11.     "Marlton, NJ", "Glassboro, NJ", "Flemington, Hunterdon County, NJ", "Branchburg, NJ", "Readington, NJ",
  12.     "Monmouth County, NJ", "East Brunswick, NJ", "Passaic County, NJ", "Warren County, NJ", "Wyckoff, NJ",
  13.     "Ocean County, NJ", "Old Bridge, NJ", "Hazlet, NJ", "Matawan, NJ"
  14. ]
  15.  
  16. # Geolocate using Nominatim
  17. geolocator = Nominatim(user_agent="nj_location_mapper")
  18. geo_data = []
  19. for location in locations:
  20.     try:
  21.         loc = geolocator.geocode(location)
  22.         if loc:
  23.             geo_data.append((location, loc.latitude, loc.longitude))
  24.     except:
  25.         geo_data.append((location, None, None))
  26.  
  27. # Create a GeoDataFrame
  28. geometry = [Point(xy) for _, lat, lon in geo_data if lat and lon for xy in [(lon, lat)]]
  29. valid_geo_data = [row for row in geo_data if row[1] and row[2]]
  30. gdf = gpd.GeoDataFrame(valid_geo_data, columns=["Location", "Latitude", "Longitude"], geometry=geometry, crs="EPSG:4326")
  31.  
  32. # Load a New Jersey county shapefile, taken from here: https://catalog.data.gov/dataset/tiger-line-shapefile-current-state-new-jersey-county-subdivision
  33. nj_counties_path = "./tl_2023_34_cousub/tl_2023_34_cousub.shp"  # Replace with the path to your shapefile
  34. nj_counties = gpd.read_file(nj_counties_path)
  35.  
  36. # Plot New Jersey counties and locations
  37. fig, ax = plt.subplots(figsize=(12, 10))
  38. nj_counties.boundary.plot(ax=ax, linewidth=0.5, color='black')
  39. nj_counties.plot(ax=ax, color="lightgray", edgecolor="gray", alpha=0.05)
  40. gdf.plot(ax=ax, color='red', markersize=50)
  41.  
  42. # Annotate locations
  43. #for x, y, label in zip(gdf.geometry.x, gdf.geometry.y, gdf['Location']):
  44. #    ax.text(x, y, label, fontsize=8, ha='right')
  45.  
  46. plt.title("New Jersey Counties with Locations", fontsize=14)
  47. plt.show()
  48.  
Advertisement
Add Comment
Please, Sign In to add comment