Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

City and State Lookup #31

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions pgeocode.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,75 @@ def query_postal_code(self, x, y):
return dist[0]
else:
return dist

class StateLookup(Nominatim):

def __init__(self, country='us'):
super().__init__(country=country, unique=False)
self._data_frame = self._index_states()

def _index_states(self):
""" Create a dataframe with unique postal codes """
data_path_states = self._data_path.replace('.txt', '-states.txt')

if os.path.exists(data_path_states):
data_unique = pd.read_csv(data_path_states, dtype={'state_code': str})
else:
# group together places with the same state code
df_unique_cp_group = self._data.groupby('state_code')
data_unique = df_unique_cp_group[['postal_code']].first()
valid_keys = set(DATA_FIELDS).difference(['state_code'])
for key in valid_keys:
data_unique[key] = df_unique_cp_group[key].first()
data_unique = data_unique.reset_index()[['state_name', 'state_code']].dropna()
data_unique.to_csv(data_path_states, index=None, columns=['state_name', 'state_code'])
return data_unique

def query_state(self, query):
"""
Get a list of states matching a query.
:return:
"""
data = self._data_frame
bool_series = data["state_name"].str.lower().str.startswith(query.lower(), na=False)
return data[bool_series]


class CityLookup(StateLookup):

def __init__(self, country='us', state='NY'):
super().__init__(country=country)
self._data_frame = self._index_cities(state)

def _index_cities(self, state):
""" Create a dataframe with unique postal codes """
data_path_states = self._data_path.replace('.txt', '-{}-cities.txt'.format(state))

if os.path.exists(data_path_states):
data_unique = pd.read_csv(data_path_states, dtype={'county_code': str})
else:
# group together places with the same place name
df_unique_cp_group = self._data[self._data['state_code'] == state].groupby('place_name')
data_unique = df_unique_cp_group[['postal_code', 'county_name']].first()
valid_keys = set(DATA_FIELDS).difference(['place_name'])
for key in valid_keys:
data_unique[key] = df_unique_cp_group[key].first()
data_unique = data_unique.reset_index()[['place_name', 'county_name', 'county_code', 'postal_code']]\
.fillna(value='')
data_unique.to_csv(data_path_states, index=None)
return data_unique

def query_city(self, query):
"""
Get a list of cities matching a query.
:param state:
:param query:
:return:
"""
data = self._data_frame
bool_series = data["place_name"].str.lower().str.startswith(query.lower(), na=False)
return data[bool_series]



# Copied from geopy
Expand Down