# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %matplotlib inline

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import geoplot as gplt
import geopandas as gp
import geoplot.crs as gcrs
# converts coordinate pairs into points that can be interpreted by geopandas
from shapely.geometry import Point

# +
# reading the shapefile
sftracts = gp.read_file("/Users/daviderickson/projects/datasf/data/Analysis Neighborhoods - 2010 census tracts assigned to neighborhoods/geo_export_729f30ed-0972-44ec-b06d-34a8c3d7abe2.dbf")

# setting the census tract ID as index will make map creation easier later on!
sftracts.set_index("tractce10", inplace=True)

sftracts.head()
# -

df = pd.read_csv('/Users/daviderickson/projects/datasf/data/Assessor_Historical_Secured_Property_Tax_Rolls.csv')

df.columns[25]

df.shape

df.head(5)

df.describe()

df.columns

# +
# Create two lists for the loop results to be placed
lat = []
lon = []

# For each row in a varible,
for row in df['the_geom']:
    # Try to,
    try:
        # Split the row by comma, convert to float, and append
        # everything before the comma to lat
        lon.append(float(row.split(',')[0].split('(')[1]))
        # Split the row by comma, convert to float, and append
        # everything after the comma to lon
        lat.append(float(row.split(',')[1].split(')')[0]))
    # But if you get an error
    except:
        # append a missing value to lat
        lat.append(np.NaN)
        # append a missing value to lon
        lon.append(np.NaN)

# Create two new columns from lat and lon
df['latitude'] = lat
df['longitude'] = lon
# -

# convert coords to points
df["Coordinates"] = (df[["latitude", "longitude"]]
                               .apply(tuple, axis=1)
                               .apply(Point)
                              ) 
# convert dataframe to geodataframe
df = gp.GeoDataFrame(df, geometry="Coordinates")

df.head()

# +
# 
df.crs = sftracts.crs # Making sure the map projections of both geodataframes are the same
df_joined = gp.sjoin(df, sftracts) # joining the geodataframes on its spatial geometries
df_joined.rename(columns={"index_right":"tractce10"}, inplace=True)
df_joined.iloc[:,::-1].head()
df_joined.head()


# -

land_value = df['Assessed Land Value']
plt.scatter('latitude', 'longitude', data=df, c=np.log(land_value))
plt.colorbar()
'lat, lon backwards?'

df['Assessed Land Value'].describe()

df['Closed Roll Year'].describe()

ax = gplt.polyplot(sftracts.geometry, projection = gcrs.LambertConformal(),
                   figsize=(12,8), edgecolor="k")
ax.set_title("Census Tract Boundaries in San Francisco", fontweight="bold", fontsize=16)
plt.show()

df_joined.columns

# +
# land_value_by_tract = gp.GeoDataFrame((df
#                                      .tractce10.value_counts()
#                                      .to_frame()
#                                      .rename(columns={"tractce10":"Incidents"})
#                                     ).merge(sftracts.geometry.to_frame(),
#                                             left_index=True, right_index=True)
#                                    )
# drug_inc_by_tract.head()
# -

df_groupby_nhood = df_joined.groupby(by='Analysis Neighborhood').mean()

df_groupby_nhood.head()

# convert coords to points
df_groupby_nhood["Coordinates"] = (df_groupby_nhood[["latitude", "longitude"]]
                               .apply(tuple, axis=1)
                               .apply(Point)
                              ) 
# convert dataframe to geodataframe
df_groupby_nhood = gp.GeoDataFrame(df_groupby_nhood, geometry="Coordinates")

# +

df_joined.crs = sftracts.crs # Making sure the map projections of both geodataframes are the same
drug_inc_by_tract = gp.sjoin(df_joined, sftracts) # joining the geodataframes on its spatial geometries
drug_inc_by_tract.rename(columns={"index_right":"tractce10"}, inplace=True)
drug_inc_by_tract.iloc[:,::-1].head()
# -

drug_inc_by_tract.head()

# I think This block should be the answer, but it never finishes running
"""

ax = gplt.choropleth(drug_inc_by_tract, projection=gcrs.LambertConformal(), hue="Assessed Land Value",
                     cmap="coolwarm", k=None, linewidth=0, figsize=(10,8), legend=True)
gplt.polyplot(sftracts.geometry, projection = gcrs.LambertConformal(), edgecolor="k",
              linewidth=1, ax=ax)
ax.set_title("Mean Assesed Land Value by Tract",
             fontweight="bold", fontsize=16)
plt.show()

"""

drug_inc_by_tract = gp.GeoDataFrame((df_joined
                                    ).merge(sftracts.geometry.to_frame(),
                                            left_index=True, right_index=True)
                                   )
drug_inc_by_tract.head()


# +
# 
df_groupby_nhood.crs = sftracts.crs # Making sure the map projections of both geodataframes are the same
df_agg_tract = gp.sjoin(df_groupby_nhood, sftracts) # joining the geodataframes on its spatial geometries
df_agg_tract.rename(columns={"index_right":"tractce10"}, inplace=True)
df_agg_tract.iloc[:,::-1].head()
df_agg_tract.head()


# -

df_agg_tract['Assessed Land Value'].hist()

# +


ax = gplt.choropleth(df_agg_tract, projection=gcrs.LambertConformal(), hue="Assessed Land Value",
                     cmap="coolwarm", k=None, linewidth=0, figsize=(10,8), legend=True)
gplt.polyplot(sftracts.geometry, projection = gcrs.LambertConformal(), edgecolor="k",
              linewidth=1, ax=ax)
ax.set_title("Mean Assesed Land Value by Tract",
             fontweight="bold", fontsize=16)
plt.show()


# -



StarcoderdataJupyterScriptsDedupFiltered