forked from Gene-Weaver/VoucherVisionEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_geonames.py
More file actions
63 lines (52 loc) · 2.75 KB
/
utils_geonames.py
File metadata and controls
63 lines (52 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import geocoder
def search_location(query, GEONAMES_USERNAME):
"""Search for the location using geocoder and GeoNames, excluding feature class 'S'."""
# Exclude feature class 'S' (Spot, Building, Farm, etc.)
g = geocoder.geonames(query, key=GEONAMES_USERNAME, maxRows=10, featureClass=['A', 'P', 'T', 'H', 'L', 'R', 'V', 'U'])
if g:
if len(g) == 1: # If there's exactly one result
print(f"Single result found: {g[0].address} (GeoNames ID: {g[0].geonames_id})")
return g[0].geonames_id
elif len(g) > 1: # If there are multiple results
print("Multiple results found. Possible locations:")
for result in g:
print(f"- {result.address} (GeoNames ID: {result.geonames_id})")
return None # No single result to show the hierarchy for
else:
print(f"No results found for: {query}")
return None
def get_hierarchy(geoname_id, GEONAMES_USERNAME):
"""Fetch the hierarchy for a given GeoNames ID."""
h = geocoder.geonames(geoname_id, key=GEONAMES_USERNAME, method='hierarchy')
if h:
print("Location hierarchy:")
for place in h:
print(f"{place.address} ({place.description})")
else:
print(f"No hierarchy found for GeoNames ID: {geoname_id}")
def get_location_hierarchy(stated_country, stated_stateProvince, stated_county, stated_locality, GEONAMES_USERNAME):
"""
Get the location hierarchy by progressively reducing the query string if no results are found.
"""
# Build the full query string, starting with the most detailed (locality, county, stateProvince, country)
query_parts = [stated_locality, stated_county, stated_stateProvince, stated_country]
# Keep trying by removing the smallest (left-most) part of the hierarchy until something works
for i in range(len(query_parts)):
# Create query by joining non-empty parts
query = ", ".join([part for part in query_parts[i:] if part])
if query:
print(f"Searching for location: {query}")
geoname_id = search_location(query, GEONAMES_USERNAME)
if geoname_id:
get_hierarchy(geoname_id, GEONAMES_USERNAME)
return # Exit after the first successful search and hierarchy fetch
# If no data found at any level, return None
print("No valid region found for the specified inputs.")
# Example usage
stated_country = "USA"
stated_stateProvince = "Colorado"
# stated_county = "El Paso"
stated_county = ""
# stated_locality = "Garden of the Gods, Colorado Springs"
stated_locality = "Pike National Forest"
get_location_hierarchy(stated_country, stated_stateProvince, stated_county, stated_locality, GEONAMES_USERNAME='vouchervision')