This time, we'll work a little more on manipulating and exploring the attributes. This is akin to working with the attribute table in desktop GIS software.
Start by importing pandas, geopandas, matplotlib.pyplot, and os:
>>> import pandas as pd
>>> import geopandas as gpd
>>> import matplotlib.pyplot as plt
>>> import os
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import os
>>> os.chdir('../')
>>> os.getcwd()
os.chdir('../')
os.getcwd()
>>> trails = gpd.read_file('workshopdata/OSMP_Trails.shp')
trails = gpd.read_file('workshopata/OSMP_Trails.shp')
>>> trails.head()
trails.head()
>>> trails.columns
trails.columns
>>> trails.TRAILTYPE
trails.TRAILTYPE
>>> trails.plot()
trails.plot()
>>> bikeTrails = trails.loc[trails['BICYCLES']=='Yes']
>>> bikeTrails
bikeTrails = trails.loc[trails['BICYCLES']=='Yes']
bikeTrails
>>> bikeTrails.plot()
bikeTrails.plot()
>>> hikeTrails = trails.loc[trails['TRAILTYPE']=='Hiking Trail']
>>> len(hikeTrails)
hikeTrails = trails.loc[trails['TRAILTYPE']=='Hiking Trail']
len(hikeTrails)
>>> horseTrails = trails.loc[trails['HORSES']=='Yes']
>>> len(horseTrails)
horseTrails = trails.loc[trails['HORSES']=='Yes']
len(horseTrails)
>>> trails.loc[trails['MILEAGE']>=2]
trails.loc[trails['MILEAGE']>=2]
trails.TRAILNAME.unique()
>>> len(trails.TRAILNAME.unique())
len(trails.TRAILNAME.unique())
Sounds hard, but not really!
>>> trailLengths = trails.groupby('TRAILNAME')['MILEAGE'].sum()
>>> trailLengths
trailLengths = trails.groupby('TRAILNAME')['MILEAGE'].sum()
trailLengths
>>> trailLengths = trailLengths.reset_index()
>>> trailLengths
trailLengths = trailLengths.reset_index()
trailLengths
>>> trailLengths.to_csv('trailLengths.csv')
trailLengths.to_csv('trailLengths.csv')
osmp = gpd.read_file('workshopdata/OSMP.shp')
county = gpd.read_file('workshopdata/Boulder_Co.shp')
>>> county = county.to_crs(osmp.crs)
county = county.to_crs(osmp.crs)
>>> fig, ax = plt.subplots(1, 1, figsize = (15,15))
>>> county.plot(ax=ax, color = '#F0E68C', edgecolor='black')
>>> osmp.plot(ax=ax, color = 'green', alpha=0.5)
>>> hikeTrails.plot(ax=ax, color='red')
>>> bikeTrails.plot(ax=ax, color='yellow')
fig, ax = plt.subplots(1, 1, figsize = (15,15))
county.plot(ax=ax, color = '#F0E68C', edgecolor='black')
osmp.plot(ax=ax, color='green', alpha=0.5)
hikeTrails.plot(ax=ax, color='red')
bikeTrails.plot(ax=ax, color='yellow')