<jupyter_start><jupyter_text>08. Pulling it all TogetherFor this last lesson, we'll practice going through a full workflow! We'll answer the question: What is the total grocery-store sales volume of each census tract? WORKFLOW:Here's a set of steps that we will implement in the labeled cells below: 8.1 Read in and Prep Data- read in tracts acs joined data- read our grocery-data CSV into a Pandas DataFrame (it lives at `'notebook_data/other/ca_grocery_stores_2019_wgs84.csv`)- coerce it to a GeoDataFrame- define its CRS (EPSG:4326)- transform it to match the CRS of `tracts_acs_gdf_ac`- take a peek8.2 Spatial Join and Dissolve- join the two datasets in such a way that you can then...- group by tract and calculate the total grocery-store sales volume- don't forget to check the dimensions, contents, and any other relevant aspects of your results8.3 Plot and Review- plot the tracts, coloring them by total grocery-store sales volume- plot the grocery stores on top- bonus points for devising a nice visualization scheme that helps you heuristically check your results! INSTRUCTIONS:**We've written out some of the code for you, but you'll need to replace the ellipses with the correctcontent.***You can check your answers by double-clicking on the Markdown cells where indicated.***Instructor Notes**- Datasets used    - Census tracts in Alameda County joined to American Community Survey data ('outdata/tracts_acs_gdf_ac.json')    - California 2019 grocery store data ('notebook_data/other/ca_grocery_stores_2019_wgs84.csv')- Expected time to complete    - Lecture and questions: N/A    - Exercises: 30 minutes----------------- --------------------------------------- Install Packages<jupyter_code>import pandas as pd
import geopandas as gpd

import matplotlib # base python plotting library
import matplotlib.pyplot as plt # submodule of matplotlib

# To display plots, maps, charts etc in the notebook
%matplotlib inline<jupyter_output><empty_output><jupyter_text>------------------ 8.1 Read the Prep DataWe first need to prepare our data by loading both our tracts/acs and grocery data, and conduct our usual steps to make there they have the same CRS.- read in our tracts acs joined data - read our grocery-data CSV into a Pandas DataFrame (it lives at `'notebook_data/other/ca_grocery_stores_2019_wgs84.csv`)- coerce it to a GeoDataFrame- define its CRS (EPSG:4326)- transform it to match the CRS of `tracts_acs_gdf_ac`- take a peek<jupyter_code># read in tracts acs data

tracts_acs_gdf_ac = gpd.read_file(...)
# read our grocery-data CSV into a Pandas DataFrame

grocery_pts_df = pd.read_csv(...)
# coerce it to a GeoDataFrame

grocery_pts_gdf = gpd.GeoDataFrame(grocery_pts_df, 
                                   geometry=gpd.points_from_xy(...,...))
# define its CRS (NOTE: Use EPSG:4326)

grocery_pts_gdf.crs = ...
# transform it to match the CRS of tracts_acs_gdf_ac

grocery_pts_gdf.to_crs(..., inplace=...)
grocery_pts_gdf.crs
# take a peek

print(grocery_pts_gdf.head())<jupyter_output><empty_output><jupyter_text>Double-click here to see solution!<!-- SOLUTION: read in and prep data:----------------------- read in tracts acs datatracts_acs_gdf_ac = gpd.read_file('outdata/tracts_acs_gdf_ac.json') read in the grocery-store datagrocery_pts_df = pd.read_csv('notebook_data/other/ca_grocery_stores_2019_wgs84.csv') coerce to a GeoDataFramegrocery_pts_gdf = gpd.GeoDataFrame(grocery_pts_df,                                geometry=gpd.points_from_xy(grocery_pts_df.X, grocery_pts_df.Y)) define the CRSgrocery_pts_gdf.crs = "epsg:4326" transform to our census-tract CRSgrocery_pts_gdf.to_crs(tracts_acs_gdf_ac.crs, inplace=True)print(grocery_pts_gdf.head())-->-----------------------  8.2 Spatial Join and DissolveNow that we have our data and they're in the same projection, we're going to conduct an *attribute join* to bring together the two datasets. From there we'll be able to actually *aggregate* our data to count the total sales volume.- join the two datasets in such a way that you can then...- group by tract and calculate the total grocery-store sales volume- don't forget to check the dimensions, contents, and any other relevant aspects of your results<jupyter_code># join the two datasets in such a way that you can then...

tracts_joingrocery = gpd.sjoin(..., ..., how= ...)
# group by tract and calculate the total grocery-store sales volume

tracts_totsalesvol = tracts_joingrocery[['GEOID','geometry','SALESVOL']].dissolve(by= ...,
                                                                                  aggfunc=..., as_index=False)
# don't forget to check the dimensions, contents, and any other relevant aspects of your results

# check the dimensions
print('Dimensions of result:', ...)
print('Dimesions of census tracts:', ...)
# check the result
print(tracts_totsalesvol.head())<jupyter_output><empty_output><jupyter_text>Double-click here to see solution!<!-- SOLUTION: spatial join and dissolve:--------------------------- join the grocery stores onto the tractstracts_joingrocery = gpd.sjoin(tracts_acs_gdf_ac, grocery_pts_gdf, how='left') dissolve the joined object, summing the SALESVOL column by GEOIDtracts_totsalesvol = tracts_joingrocery[['GEOID','geometry','SALESVOL']].dissolve(by='GEOID',                                                                                  aggfunc="sum",as_index=False) check the dimensions print('Dimensions of result:', tracts_totsalesvol.shape) print('Dimesions of census tracts:', tracts_acs_gdf_ac.shape) check the resultprint(tracts_totsalesvol.head())-->----------------------  8.3 Plot and ReviewWith any time of geospatial analysis you do, it's always nice to plot and visualize your results to check your work and start to understand the full story of your analysis.- Plot the tracts, coloring them by total grocery-store sales volume- Plot the grocery stores on top- Bonus points for devising a nice visualization scheme that helps you heuristically check your results!<jupyter_code># create the figure and axes

fig, ax = plt.subplots(figsize = (20,20)) 

# plot the tracts, coloring by total SALESVOL

tracts_totsalesvol.plot(ax=ax, column= ..., scheme="quantiles", cmap="autumn", edgecolor="grey",
                        legend=True, legend_kwds={'title':...})
# subset the stores for only those within our tracts, to keep map within region of interest

grocery_pts_gdf_ac = grocery_pts_gdf.loc[..., ]
# add the grocery stores, coloring by SALESVOL, for a visual check

grocery_pts_gdf_ac.plot(ax=ax, column= ... , cmap= ..., linewidth= ..., markersize= ...,
                        legend=True, legend_kwds={'label': ... , 'orientation': "horizontal"})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10724421209858102, 'fraction_numerical': 0.009559372666168783, 'mean_word_length': 3.6307053941908713, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '313912', 'license': 'MIT', 'n_tokens_mistral': 2063, 'n_tokens_neox': 1905, 'n_words': 728, 'path': '08_Pulling_It_All_Together.ipynb', 'repo_name': 'katherinerosewolf/Geospatial-Fundamentals-in-Python', 'size': '6695'}