Skip to content

Commit

Permalink
Merge pull request #72 from edgi-govdata-archiving/county_names
Browse files Browse the repository at this point in the history
County names
  • Loading branch information
ericnost committed Feb 5, 2024
2 parents 89edf31 + 077a600 commit 5e7c545
Show file tree
Hide file tree
Showing 11 changed files with 13,041 additions and 13,095 deletions.
33 changes: 18 additions & 15 deletions ECHO_modules/DataSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import geographies
from .DataSetResults import DataSetResults
from .get_data import get_echo_data
from .utilities import get_facs_in_counties

class DataSet:
'''
Expand Down Expand Up @@ -64,7 +65,7 @@ def store_results( self, region_type, region_value, state=None ):
df = self.get_data( region_type, region_value, state )
result.store( df )
value = region_value
if ( type( value ) == list ):
if type(value) == list:
value = ''.join( map( str, value ))
self.results[ (region_type, value, state) ] = result
return result
Expand Down Expand Up @@ -95,7 +96,12 @@ def get_data( self, region_type, region_value, state=None,
x_sql = self.sql + ' where ' + filter
program_data = get_echo_data( x_sql, self.idx_field )
except pd.errors.EmptyDataError:
print( "No program records were found." )
print( "No program records were found.")

if (region_type == 'County'):
if (type(region_value) == str):
region_value = [region_value,]
program_data = get_facs_in_counties(program_data, region_value)

if ( program_data is not None ):
print( "There were {} program records found".format( str( len( program_data ))))
Expand Down Expand Up @@ -322,18 +328,12 @@ def _has_echo_flag( self, echo_type, echo_data ):
def _set_facility_filter( self, region_type, region_value=None, state=None ):
if ( region_type == 'State' ):
region_value = state
filter = '"' + geographies.region_field[region_type]['field'] + '"'
if ( region_type == 'County' ):
filter = '('
for county in region_value:
filter += '"' + geographies.region_field[region_type]['field'] + '"'
filter += ' like \'' + county + '%\' or '
filter = filter[:-3]
filter += ')'
elif ( region_type == 'State' ) :
if ( region_type == 'County' or region_type == 'State' ):
filter = '"' + geographies.region_field['State']['field'] + '"'
filter += ' = \'' + state + '\''
else:
# region_value will be an list of values
filter = '"' + geographies.region_field[region_type]['field'] + '"'
# region_value will be an list of values
id_string = ""
value_type = type(region_value)
if ( value_type == list or value_type == tuple ):
Expand All @@ -345,7 +345,10 @@ def _set_facility_filter( self, region_type, region_value=None, state=None ):
# Remove trailing comma from id_string
filter += ' in (' + id_string[:-1] + ')'
elif ( type(region_value) == str ):
filter += ' = \'' + region_value + '\''
if ( region_type == 'Congressional District' or region_type == 'County' ):
filter += ' and "FAC_STATE" = \'' + state + '\''
region_value = ''.join(region_value.split())
region_value = ",".join(map(lambda x: "\'" + str(x) + "\'", region_value.split(',')))
filter += ' in (' + region_value + ')'
if ( region_type == 'Congressional District' ):
filter += ' and "' + geographies.region_field['State']['field']
filter += '" = \'' + state + '\''
return filter
2 changes: 1 addition & 1 deletion ECHO_modules/DataSetResults.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def show_chart( self ):
'FAC_PERCENT_MINORITY', 'FAC_POP_DEN', 'FAC_DERIVED_HUC',
'FAC_SIC_CODES', 'FAC_NAICS_CODES'])
d = data.groupby(pd.to_datetime(data['YEARQTR'],
format="%Y", errors='coerce').dt.to_period("Y")).sum()
format="%Y", errors='coerce').dt.to_period("Y")).sum(numeric_only=True)
d.index = d.index.strftime('%Y')
d = d[ d.index > '2000' ]

Expand Down
2 changes: 1 addition & 1 deletion ECHO_modules/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.0"
__version__ = "0.1.2"
74 changes: 11 additions & 63 deletions ECHO_modules/geographies.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,67 +85,15 @@
# 'Census Block': {"field": 'FAC_DERIVED_CB2010'} # We don't have this in the spatial database because there are too many CBs - too much data!
# 'EPA Region': { "field": 'FAC_EPA_REGION'} # Possibly too large to handle in Colab environment

states = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DC", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD",
"MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
"SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]

fips = {
"AK": "02",
"AL": "01",
"AR": "05",
"AS": "60",
"AZ": "04",
"CA": "06",
"CO": "08",
"CT": "09",
"DC": "11",
"DE": "10",
"FL": "12",
"GA": "13",
"GU": "66",
"HI": "15",
"IA": "19",
"ID": "16",
"IL": "17",
"IN": "18",
"KS": "20",
"KY": "21",
"LA": "22",
"MA": "25",
"MD": "24",
"ME": "23",
"MI": "26",
"MN": "27",
"MO": "29",
"MS": "28",
"MT": "30",
"NC": "37",
"ND": "38",
"NE": "31",
"NH": "33",
"NJ": "34",
"NM": "35",
"NV": "32",
"NY": "36",
"OH": "39",
"OK": "40",
"OR": "41",
"PA": "42",
"PR": "72",
"RI": "44",
"SC": "45",
"SD": "46",
"TN": "47",
"TX": "48",
"UT": "49",
"VA": "51",
"VI": "78",
"VT": "50",
"WA": "53",
"WI": "55",
"WV": "54",
"WY": "56"
}
states = ['AK','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL',
'IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE',
'NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT',
'VA','VT','WA','WI','WV','WY']

fips = { "AK": "02", "AL": "01", "AR": "05", "AS": "60", "AZ": "04", "CA": "06", "CO": "08",
"CT": "09", "DC": "11", "DE": "10", "FL": "12", "GA": "13", "GU": "66", "HI": "15", "IA": "19",
"ID": "16", "IL": "17", "IN": "18", "KS": "20", "KY": "21", "LA": "22", "MA": "25", "MD": "24",
"ME": "23", "MI": "26", "MN": "27", "MO": "29", "MS": "28", "MT": "30", "NC": "37", "ND": "38",
"NE": "31", "NH": "33", "NJ": "34", "NM": "35", "NV": "32", "NY": "36", "OH": "39", "OK": "40",
"OR": "41", "PA": "42", "PR": "72", "RI": "44", "SC": "45", "SD": "46", "TN": "47", "TX": "48",
"UT": "49", "VA": "51", "VI": "78", "VT": "50", "WA": "53", "WI": "55", "WV": "54", "WY": "56"}
14 changes: 7 additions & 7 deletions ECHO_modules/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def get_active_facilities( state, region_type, regions_selected ):
Dataframe
The active facilities returned from the database query
'''

try:
if ( region_type == 'State' or region_type == 'County'):
sql = 'select * from "ECHO_EXPORTER" where "FAC_STATE" = \'{}\''
Expand All @@ -359,18 +359,18 @@ def get_active_facilities( state, region_type, regions_selected ):
sql = sql.format( state, cd_str )
df_active = get_echo_data( sql, 'REGISTRY_ID' )
elif ( region_type == 'Zip Code' ):
zc_str = ",".join( map( lambda x: "\'"+str(x)+"\'", regions_selected ))
regions_selected = ''.join(regions_selected.split())
zc_str = ",".join( map( lambda x: "\'"+str(x)+"\'", regions_selected.split(',') ))
sql = 'select * from "ECHO_EXPORTER" where "FAC_ZIP" in ({})'
sql += ' and "FAC_ACTIVE_FLAG" = \'Y\''
sql = sql.format( zc_str )
#print(sql) # debugging
df_active = get_echo_data( sql, 'REGISTRY_ID' )
elif ( region_type == 'Watershed' ):
ws_str = ",".join( map( lambda x: "\'"+str(x)+"\'", regions_selected ))
regions_selected = ''.join(regions_selected.split())
ws_str = ",".join( map( lambda x: "\'"+str(x)+"\'", regions_selected.split(',') ))
sql = 'select * from "ECHO_EXPORTER" where "FAC_DERIVED_HUC" in ({})'
sql += ' and "FAC_ACTIVE_FLAG" = \'Y\''
sql = sql.format( ws_str )
#print(sql) # debugging
df_active = get_echo_data( sql, 'REGISTRY_ID' )
else:
df_active = None
Expand Down Expand Up @@ -558,13 +558,13 @@ def mapper(df, bounds=None, no_text=False):

# Initialize the map
m = folium.Map(
location = [df.mean()["FAC_LAT"], df.mean()["FAC_LONG"]]
location = [df.mean(numeric_only=True)["FAC_LAT"], df.mean(numeric_only=True)["FAC_LONG"]]
)

# Create the Marker Cluster array
#kwargs={"disableClusteringAtZoom": 10, "showCoverageOnHover": False}
mc = FastMarkerCluster("")

# Add a clickable marker for each facility
for index, row in df.iterrows():
if ( bounds is not None ):
Expand Down
Loading

0 comments on commit 5e7c545

Please sign in to comment.