import geopandas
# Pull in example dataset of NYC Boroughs
path_to_data = geopandas.datasets.get_path("nybb")
gdf = geopandas.read_file(path_to_data)
gdf
BoroCode | BoroName | Shape_Leng | Shape_Area | geometry | |
---|---|---|---|---|---|
0 | 5 | Staten Island | 330470.010332 | 1.623820e+09 | MULTIPOLYGON (((970217.022 145643.332, 970227.... |
1 | 4 | Queens | 896344.047763 | 3.045213e+09 | MULTIPOLYGON (((1029606.077 156073.814, 102957... |
2 | 3 | Brooklyn | 741080.523166 | 1.937479e+09 | MULTIPOLYGON (((1021176.479 151374.797, 102100... |
3 | 1 | Manhattan | 359299.096471 | 6.364715e+08 | MULTIPOLYGON (((981219.056 188655.316, 980940.... |
4 | 2 | Bronx | 464392.991824 | 1.186925e+09 | MULTIPOLYGON (((1012821.806 229228.265, 101278... |
geopandas.datasets.available
['naturalearth_cities', 'naturalearth_lowres', 'nybb']
# Writing files (defaults to shapefile but can be changed, e.g., driver="GeoJSON")
gdf.to_file("Data/myfile.shp")
gdf.to_file("Data/my_file.geojson", driver="GeoJSON")
gdf.plot();
gdf = gdf.set_index("BoroName")
gdf["area"] = gdf.area
gdf["area"]
BoroName Staten Island 1.623822e+09 Queens 3.045214e+09 Brooklyn 1.937478e+09 Manhattan 6.364712e+08 Bronx 1.186926e+09 Name: area, dtype: float64
gdf.plot("area", legend=True);
# Interactive maps
gdf.explore("area", legend=False)
import pandas as pd
ft = pd.read_csv("Data/freedom_trail.csv")
ft.head()
id | name | address | city | state | zip | |
---|---|---|---|---|---|---|
0 | 1 | Boston Common | 139 Tremont St | Boston | MA | 2111 |
1 | 2 | Massachusetts State House | 24 Beacon St | Boston | MA | 2133 |
2 | 3 | Park Street Church | 121 Tremont St | Boston | MA | 2108 |
3 | 4 | Granary Burying Ground | 83 Tremont St | Boston | MA | 2108 |
4 | 5 | King’s Chapel/King’s Chapel Burying Ground | 58 Tremont St | Boston | MA | 2108 |
# Load in CSV dataset with geocoded point coordinates
go = pd.read_csv("Data/geocode_output.csv")
go = go.drop(columns=["Unnamed: 0"])
go.head()
id | name | address | city | state | zip | latitude | longitude | |
---|---|---|---|---|---|---|---|---|
0 | 1 | Boston Common | 139 Tremont St | Boston | MA | 2111 | 42.354274 | -71.063835 |
1 | 2 | Massachusetts State House | 24 Beacon St | Boston | MA | 2133 | 42.359244 | -71.063870 |
2 | 3 | Park Street Church | 121 Tremont St | Boston | MA | 2108 | 42.356641 | -71.061984 |
3 | 4 | Granary Burying Ground | 83 Tremont St | Boston | MA | 2108 | 42.357599 | -71.060977 |
4 | 5 | King’s Chapel/King’s Chapel Burying Ground | 58 Tremont St | Boston | MA | 2108 | 42.358013 | -71.060027 |
# Read in latitude/longitude into a GeoPandas dataframe
# crs - optional input for coordinate system, EPSG:4326 = WGS 1984
gdf2 = geopandas.GeoDataFrame(geometry=geopandas.points_from_xy(go.longitude, go.latitude, crs="EPSG:4326"), data=go)
gdf2.plot()
<AxesSubplot:>
# Plot coordinates and exclude empty geometries (there's a couple of no-hits in this data)
# ["OpenStreetMap", "Stamen Terrain", "Stamen Toner", "Stamen Watercolor", "CartoDB positron", "CartoDB dark_matter"]
gdf2.loc[~gdf2.geometry.is_empty, :].explore(
tiles="OpenStreetMap", cmap=["Blues"], popup=True, marker_kwds={"radius":10},
highlight=True, legend=True, legend_kwds={"caption":"A test caption", "colorbar":True, "scale":True})
mass_towns = geopandas.read_file("Data/townssurvey_shp/TOWNSSURVEY_POLY.shp")
town_halls = geopandas.read_file("Data/townhalls_pt/TOWNHALLS_PT_MEMA.shp")
# Set background layer and axis information
ax = mass_towns.plot(color="white", edgecolor="black")
ax.set_title("Town Halls of Massachusetts")
town_halls.plot(ax=ax, color="red", markersize=5);
# plt.show()
boston = mass_towns.query('TOWN == "BOSTON"')
boston_th = town_halls.query('CITY == "BOSTON"')
ax = boston.plot(color="white", edgecolor="black")
ax.set_title("Boston, Mass.")
boston_th.plot(ax=ax, markersize=20, column="NAME", legend=True, legend_kwds={"loc": "lower right"});
mass_towns.head()
TOWN | TOWN_ID | POP1980 | POP1990 | POP2000 | POPCH90_00 | TYPE | ISLAND | COASTAL_PO | FOURCOLOR | ... | FIPS_MCD | FIPS_COUNT | ACRES | SQUARE_MIL | POP2010 | POPCH00_10 | POPCH80_90 | SHAPE_AREA | SHAPE_LEN | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | WESTPORT | 334 | 13861 | 13852 | 14183 | 331 | T | 1 | NO | 1 | ... | 25005100 | 5 | 0.942 | 0.002 | 15532 | 1349 | -9 | 3810.93430 | 310.959400 | POLYGON ((236386.252 808384.495, 236380.123 80... |
1 | FALMOUTH | 96 | 25007 | 27960 | 32660 | 4700 | T | 1 | NO | 2 | ... | 25001035 | 1 | 3.695 | 0.006 | 31531 | -1129 | 2953 | 14953.45170 | 498.058255 | POLYGON ((268360.782 808386.365, 268347.943 80... |
2 | WESTPORT | 334 | 13861 | 13852 | 14183 | 331 | T | 1 | NO | 1 | ... | 25005100 | 5 | 0.433 | 0.001 | 15532 | 1349 | -9 | 1753.45595 | 278.276059 | POLYGON ((233335.013 809394.755, 233340.333 80... |
3 | FALMOUTH | 96 | 25007 | 27960 | 32660 | 4700 | T | 1 | NO | 2 | ... | 25001035 | 1 | 0.157 | 0.000 | 31531 | -1129 | 2953 | 635.88115 | 109.461733 | POLYGON ((277887.593 814023.755, 277886.782 81... |
4 | DARTMOUTH | 72 | 24840 | 27244 | 30666 | 3422 | T | 1 | NO | 4 | ... | 25005020 | 5 | 0.260 | 0.000 | 34032 | 3366 | 2404 | 1053.60360 | 131.624579 | POLYGON ((245000.002 815602.875, 245003.153 81... |
5 rows × 23 columns
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.colors as colors
fig, ax = plt.subplots(1, figsize=(10, 6))
ax.axis('off')
ax.set_title("Population of Massachusetts (2010), City/Town", fontdict={'fontsize': '15', 'fontweight': '3'})
mass_towns.plot(column="POP2010", cmap="summer", scheme='natural_breaks', linewidth=0.9, ax=ax, edgecolor="1", legend=True,
missing_kwds={"color": "lightgrey", "label": "Missing values",},
legend_kwds={"loc": "lower left"});
fig.savefig("mass_population_2010.png", dpi=300)
fig, ax = plt.subplots(1, figsize=(10, 6))
ax.axis('off')
ax.set_title("Population of Massachusetts (2010)", fontdict={'fontsize': '15', 'fontweight': '3'})
mass_towns.plot(column="POP2010", cmap="summer", linewidth=0.9, ax=ax, edgecolor="1", legend=True,
missing_kwds={"color": "lightgrey", "label": "Missing values",},);
fig.savefig("mass_population_2020_cmap.png", dpi=300)