-
Notifications
You must be signed in to change notification settings - Fork 0
/
names.py
112 lines (93 loc) · 3.62 KB
/
names.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# mapv/names.py
"""Parse the directory structure of the dds.cr.usgs.gov site and extract the
list of all the names of places with their states. """
import os
import re
import json
import requests
# import geocoder
from storage import mapnames
#-------------------------------------------------------------------------------
# I want stdout to be unbuffered, always
#-------------------------------------------------------------------------------
class Unbuffered(object):
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)
import sys
sys.stdout = Unbuffered(sys.stdout)
#-------------------------------------------------------------------------------
# count_names
#-------------------------------------------------------------------------------
def count_names(states):
cnt = 0
for state, names in states.items():
cnt += len(names)
return cnt
#-------------------------------------------------------------------------------
# filesystem_names
#-------------------------------------------------------------------------------
def filesystem_names():
"""Return a 2-level hierarchy of state/place name dictionaries."""
states = {}
for state, name in mapnames():
# state, name corresponds to a quadrangle, with two halves
# (east/west). Each half represents a 30-minute area.
if state not in states:
states[state] = {}
states[state][name] = True
return states
#-------------------------------------------------------------------------------
# clean_names
#-------------------------------------------------------------------------------
def cleanup_names(names):
"""Cleanup a list of names to avoid query misses and duplicates
when calling geocoding. WARNING: this generates duplicates."""
cnt = 0
for state, name in names:
m = re.match(f"([a-z'-_.]+)_(east|west|north|south|island|mountain)",
name)
if not m:
yield(state, name)
else:
cnt += 1
print(f'unclean={cnt}')
def clean_names():
states = {}
for state, name in cleanup_names(mapnames()):
# state, name corresponds to a quadrangle, with two halves
# (east/west). Each half represents a 30-minute area.
if state not in states:
states[state] = {}
states[state][name] = True
return states
#-------------------------------------------------------------------------------
# geocode_names
#-------------------------------------------------------------------------------
def geocode_names(states):
d = {}
with requests.Session() as session:
for state in sorted(states.keys()):
d[state] = {}
for name in states[state].keys():
# FIXME underscore in the name breaks the query
query = f'{name}, {state}, USA'
g = geocoder.osm(query, session=session)
if g.ok:
d[state][name] = g.latlng
print(f'{name}, {state}: {g.latlng}.')
else:
d[state][name] = True
print(f'{name}, {state}: not found.')
return d
#===============================================================================
# main
#===============================================================================
if __name__ == '__main__':
nm = filesystem_names()
clean_nm = clean_names()
print(f'Names: total={count_names(nm)}, clean={count_names(clean_nm)}')