# ---
# 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
# ---

# # Creating Shapefiles for Web App - HUC8
#
# Inputs: 
# - Pagg_ReportingUnit.csv.  Contains reportinug unit info from WaDE database.
# - HUC8 source shapefile.

#Needed Libararies
import os
import numpy as np
import pandas as pd
from datetime import datetime
import geopandas as gpd # the library that lets us read in shapefiles
pd.set_option('display.max_columns', 999)  # How to display all columns of a Pandas DataFrame in Jupyter Notebook

# +
# Set working directory
workingDir = "C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape"
os.chdir(workingDir)

# Grab AggreagatedAmounts ReportingUnit.csv file.
reportingunits_input = pd.read_csv('SourceFiles/Pagg_ReportingUnit.csv')
df_1RU = pd.DataFrame(reportingunits_input)
df_1RU.head(3)
# -

# Grab the HUC8 Shapefile.
shapefile_input = gpd.read_file('SourceFiles/HUC8/HUC8_US.shp')
dfs_1HUC8 = pd.DataFrame(shapefile_input)
dfs_1HUC8.head(3)

# +
# HUC8
# State: Utah
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
# dfs_1HUC8_UT = dfs_1HUC8[(dfs_1HUC8.STATES == 'UT')]
dfs_1HUC8_UT = dfs_1HUC8
df_1RU_HUC8_UT = df_1RU[(df_1RU.ReportingUnitTypeCV == 'HUC8') & ((df_1RU.StateCV == 'UT'))]

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_HUC8_UT.ReportingUnitUUID.values, index = df_1RU_HUC8_UT.ReportingUnitNativeID).to_dict()
def retrieveHUC8ID(colrowValue):
    if colrowValue == '' or pd.isnull(colrowValue):
        outList = ''
    else:
        String1 = colrowValue.strip()
        try:
            outList = ReportingUnitUUIDdict[String1]
        except:
            outList = ''
    return outList
dfs_1HUC8_UT['ReportingUnitUUID'] = dfs_1HUC8_UT.apply(lambda row: retrieveHUC8ID(row['HUC8']), axis=1)

# Merging temporal dataframes into one, using left-join.
dfs_1HUC8_UT = pd.merge(dfs_1HUC8_UT, df_1RU_HUC8_UT, left_on='ReportingUnitUUID', right_on='ReportingUnitUUID', how='left')

# Creating new output state specific dataframe with fields of interest.
dfs_2HUC8_UT = pd.DataFrame() #empty dataframe
dfs_2HUC8_UT['OBJECTID'] = dfs_1HUC8_UT.index
dfs_2HUC8_UT['Shape'] = 'Polygon'
dfs_2HUC8_UT['UnitID'] = dfs_1HUC8_UT['ReportingUnitID']
dfs_2HUC8_UT['UnitUUID'] = dfs_1HUC8_UT['ReportingUnitUUID']
dfs_2HUC8_UT['NativeID'] = dfs_1HUC8_UT['ReportingUnitNativeID']
dfs_2HUC8_UT['Name'] = dfs_1HUC8_UT['ReportingUnitName']
dfs_2HUC8_UT['TypeCV'] = dfs_1HUC8_UT['ReportingUnitTypeCV']
dfs_2HUC8_UT['StateCV'] = dfs_1HUC8_UT['StateCV']
dfs_2HUC8_UT['Shape_Length'] = dfs_1HUC8_UT['Shape_Leng']
dfs_2HUC8_UT['Shape_Area'] = dfs_1HUC8_UT['Shape_Area']
dfs_2HUC8_UT['geometry'] = dfs_1HUC8_UT['geometry']

# view output
dfs_2HUC8_UT

# +
# HUC8
# State: CO
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
# dfs_1HUC8_CO = dfs_1HUC8[(dfs_1HUC8.STATES == 'CO')]
dfs_1HUC8_CO = dfs_1HUC8
df_1RU_HUC8_CO = df_1RU[(df_1RU.ReportingUnitTypeCV == 'HUC8') & ((df_1RU.StateCV == 'CO'))]

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_HUC8_CO.ReportingUnitUUID.values, index = df_1RU_HUC8_CO.ReportingUnitNativeID).to_dict()
def retrieveCountyName(colrowValue):
    if colrowValue == '' or pd.isnull(colrowValue):
        outList = ''
    else:
        String1 = colrowValue
        try:
            outList = ReportingUnitUUIDdict[String1]
        except:
            outList = ''
    return outList
dfs_1HUC8_CO['ReportingUnitUUID'] = dfs_1HUC8_CO.apply(lambda row: retrieveCountyName(row['HUC8']), axis=1)

# Merging temporal dataframes into one, using left-join.
dfs_1HUC8_CO = pd.merge(dfs_1HUC8_CO, df_1RU_HUC8_CO, left_on='ReportingUnitUUID', right_on='ReportingUnitUUID', how='left')

# Creating new output state specific dataframe with fields of interest.
dfs_2HUC8_CO = pd.DataFrame() #empty dataframe
dfs_2HUC8_CO['OBJECTID'] = dfs_1HUC8_CO.index
dfs_2HUC8_CO['Shape'] = 'Polygon'
dfs_2HUC8_CO['UnitID'] = dfs_1HUC8_CO['ReportingUnitID']
dfs_2HUC8_CO['UnitUUID'] = dfs_1HUC8_CO['ReportingUnitUUID']
dfs_2HUC8_CO['NativeID'] = dfs_1HUC8_CO['ReportingUnitNativeID']
dfs_2HUC8_CO['Name'] = dfs_1HUC8_CO['ReportingUnitName']
dfs_2HUC8_CO['TypeCV'] = dfs_1HUC8_CO['ReportingUnitTypeCV']
dfs_2HUC8_CO['StateCV'] = dfs_1HUC8_CO['StateCV']
dfs_2HUC8_CO['Shape_Length'] = dfs_1HUC8_CO['Shape_Leng']
dfs_2HUC8_CO['Shape_Area'] = dfs_1HUC8_CO['Shape_Area']
dfs_2HUC8_CO['geometry'] = dfs_1HUC8_CO['geometry']

# view output
dfs_2HUC8_CO.head(3)
# -

# ### Concatenate and Export

# Merge dataframes
frames = [dfs_2HUC8_UT, dfs_2HUC8_CO]
outdf = pd.concat(frames)
outdf.head(3)

# drop NA rows
outdf = outdf.dropna(subset=['UnitID'])
outdf

# Export the dataframe to a shapefile.
# Note: rememeber that shapefiles autofil in fields names, will need to fix field names in app upload.
dfsOut = gpd.GeoDataFrame(outdf,  crs="EPSG:4326", geometry='geometry') # covert to geodataframe
dfsOut.to_file("Processed_Shapefiles/HUC8.shp") # export shape file

StarcoderdataJupyterScriptsDedupFiltered