<jupyter_start><jupyter_text>Creating Shapefiles for Web App - Custom-BasinInputs: - Pagg_ReportingUnit.csv.  Contains reportinug unit info from WaDE database.- Texas Basins source shapefile.- Wyoming Basins source shapefile.- California Planning Area source shapefile.- Airizona Active Management Area source shapefile.- Utah Subarea source shapefile.<jupyter_code>#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)<jupyter_output><empty_output><jupyter_text>Texas - Basin<jupyter_code># Grab the TX Basin Shapefile.
# Paring RU_Name to ReportingUnitName

TXBshapefile_input = gpd.read_file('C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape/SourceFiles/Basins/WaDEBasins.shp')
dfs_TXB = pd.DataFrame(TXBshapefile_input)
dfs_TXB.head(3)
# Custom
# State: TX, Basin
# Notes: Need to change the string case of ReportingUnitName to title
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
df_1RU_Custom_TX = df_1RU[(df_1RU.ReportingUnitTypeCV == 'Basin') & ((df_1RU.StateCV == 'TX'))]

df_1RU_Custom_TX['ReportingUnitName'] = df_1RU_Custom_TX['ReportingUnitName'].str.title()

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_Custom_TX.ReportingUnitUUID.values, index = df_1RU_Custom_TX.ReportingUnitName).to_dict()
def retrieveUUID(colrowValue):
    if colrowValue == '' or pd.isnull(colrowValue):
        outList = ''
    else:
        String1 = colrowValue.strip()
        try:
            outList = ReportingUnitUUIDdict[String1]
        except:
            outList = ''
    return outList
dfs_TXB['ReportingUnitUUID'] = dfs_TXB.apply(lambda row: retrieveUUID(row['RU_Name']), axis=1)

# Merging temporal dataframes into one, using left-join.
dfs_TXB = pd.merge(dfs_TXB, df_1RU_Custom_TX, left_on='ReportingUnitUUID', right_on='ReportingUnitUUID', how='left')
dfs_TXB = dfs_TXB.dropna().reset_index()

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

#view output
dfs_2TXB<jupyter_output><ipython-input-4-0db9958bc91f>:9: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  df_1RU_Custom_TX['ReportingUnitName'] = df_1RU_Custom_TX['ReportingUnitName'].str.title()<jupyter_text>Wyoming - Basin<jupyter_code># Grab the WY Basin Shapefile.
# Paring RU_Name to ReportingUnitName

WYBshapefile_input = gpd.read_file('C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape/SourceFiles/Basins/WaDEBasins.shp')
dfs_WYB = pd.DataFrame(WYBshapefile_input)
dfs_WYB.head(3)
# Custom
# State: WY, Basin
# # Notes: Need to change the string case of ReportingUnitName to title
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
df_1RU_Custom_WY = df_1RU[(df_1RU.ReportingUnitTypeCV == 'Basin') & ((df_1RU.StateCV == 'WY'))]

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_Custom_WY.ReportingUnitUUID.values, index = df_1RU_Custom_WY.ReportingUnitName).to_dict()
def retrieveUUID(colrowValue):
    if colrowValue == '' or pd.isnull(colrowValue):
        outList = ''
    else:
        String1 = colrowValue.strip()
        try:
            outList = ReportingUnitUUIDdict[String1]
        except:
            outList = ''
    return outList
dfs_WYB['ReportingUnitUUID'] = dfs_WYB.apply(lambda row: retrieveUUID(row['RU_Name']), axis=1)

# Merging temporal dataframes into one, using left-join.
dfs_WYB = pd.merge(dfs_WYB, df_1RU_Custom_WY, left_on='ReportingUnitUUID', right_on='ReportingUnitUUID', how='left')
dfs_WYB = dfs_WYB.dropna().reset_index()

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

#view output
dfs_2WYB<jupyter_output><empty_output><jupyter_text>California - Planning Area<jupyter_code># Grab the CA Planning Area Shapefile.
# Paring PA_NO to ReportingUnitNativeID

CAPAshapefile_input = gpd.read_file('C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape/SourceFiles/Custom/CA/Water_Plan_Planning_Areas.shp')
dfs_CAPA = pd.DataFrame(CAPAshapefile_input)
dfs_CAPA.head(3)
# Custom
# State: CA, Planning Area
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
df_1RU_Custom_CA = df_1RU[(df_1RU.ReportingUnitTypeCV == 'Planning Area') & ((df_1RU.StateCV == 'CA'))]

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_Custom_CA.ReportingUnitUUID.values, index = df_1RU_Custom_CA.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_CAPA['ReportingUnitUUID'] = dfs_CAPA.apply(lambda row: retrieveCountyName(row['PA_NO']), axis=1)

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

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

# view output
dfs_2CAPA.head(3)<jupyter_output><empty_output><jupyter_text>AZ - Active Management Area<jupyter_code># Grab the AZ Active Management Area Shapefile.
# Paring BASIN_NAME to ReportingUnitName
AZ_AMAshapefile_input = gpd.read_file('C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape/SourceFiles/Custom/AZ/AZ_AMA.shp')
dfs_AZAMA = pd.DataFrame(AZ_AMAshapefile_input)
dfs_AZAMA.head(3)
# Custom
# State: AZ, Active Management Area
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
df_1RU_Custom_AZ = df_1RU[(df_1RU.ReportingUnitTypeCV == 'Active Management Area') & ((df_1RU.StateCV == 'AZ'))]

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

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

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

# view output
dfs_2AZAMA.head(3)<jupyter_output><empty_output><jupyter_text>Utah - Subarea<jupyter_code># Grab the UT custom Subarea Shapefile.
# Paring RU_Name to ReportingUnitName

UTSubshapefile_input = gpd.read_file('C:/Users/rjame/Documents/RShinyAppPractice/CreateAppShapefiles/App2_AggregatedShape/SourceFiles/Custom/UT/UT_Subarea.shp')
dfs_UTsub = pd.DataFrame(UTSubshapefile_input)
dfs_UTsub
# Custom
# State: UT, Subarea
###########################################################################

# Create temporal dataframes for state specific and reportingunit type storage
df_1RU_Custom_UT = df_1RU[(df_1RU.ReportingUnitTypeCV == 'Subarea') & ((df_1RU.StateCV == 'UT'))]

# retreive ReportingUnitUUID.
ReportingUnitUUIDdict = pd.Series(df_1RU_Custom_UT.ReportingUnitUUID.values, index = df_1RU_Custom_UT.ReportingUnitName).to_dict()
def retrieveUUID(colrowValue):
    if colrowValue == '' or pd.isnull(colrowValue):
        outList = ''
    else:
        String1 = colrowValue.strip()
        try:
            outList = ReportingUnitUUIDdict[String1]
        except:
            outList = ''
    return outList
dfs_UTsub['ReportingUnitUUID'] = dfs_UTsub.apply(lambda row: retrieveUUID(row['RU_Name']), axis=1)

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

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

#view output
dfs_2UTsub.head(3)<jupyter_output><empty_output><jupyter_text>Concatenate and Export<jupyter_code># Merge dataframes
frames = [dfs_2TXB, dfs_2WYB, dfs_2CAPA, dfs_2AZAMA, dfs_2UTsub]
outdf = pd.concat(frames)
outdf.head(3)
# drop NA rows
outdf = outdf.dropna(subset=['UnitID'])
outdf
# Export the dataframe to a shapefile.
dfsOut = gpd.GeoDataFrame(outdf,  crs="EPSG:4326", geometry='geometry') # covert to geodataframe
dfsOut.to_file("Processed_Shapefiles/Custom-Basin.shp") # export shape file<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14215235387574213, 'fraction_numerical': 0.012291997658667113, 'mean_word_length': 6.603305785123967, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '275121', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 4509, 'n_tokens_neox': 4305, 'n_words': 751, 'path': 'CreateAppShapefiles/App2_AggregatedShape/App2ShapeFiles_Custom-Basin.ipynb', 'repo_name': 'WSWCWaterDataExchange/WaDE2.0Apps', 'size': '11959'}