/
randomcoords.py
121 lines (96 loc) · 3.57 KB
/
randomcoords.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
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
"""
Program to generate random coordinates in the contiguous UNited States.
This program will generate a user-specified number of random geographic
coordinates in the contiguous United States, reverse geocode them, then
write them to the specified file.
Example:
$ python randomcoords.py number_of_points output_file
The following conventions were used to ensure the coordinates were in the
contiguous U.S.
US Geographic Information
Northernmost - (49.384472, -95.153389)
Southernmost - (24.433333, -81.918333)
Easternmost - (44.815278. -65.949722)
Westernmost - (48.164167. -124.733056)
Geographic Center - (39.833333. -98.583333)
"""
import argparse
import os
import random
from pygeocoder import Geocoder, GeocoderError
__version__ = '0.1.0'
NORTHERNMOST = 49.
SOUTHERNMOST = 25.
EASTERNMOST = -66.
WESTERNMOST = -124.
def coordinate_generator(number_of_points):
"""
Generate a number of random geographical points and then geocode them.
:param number_of_points: number of points to generate
:type number_of_points: int
:return: list of geographic point tuples
"""
coordinate_list = []
counter = 0
geocoder = Geocoder()
while counter < number_of_points:
lat = round(random.uniform(SOUTHERNMOST, NORTHERNMOST), 6)
lng = round(random.uniform(EASTERNMOST, WESTERNMOST), 6)
try:
gcode = geocoder.reverse_geocode(lat, lng)
if gcode[0].data[0]['formatted_address'][-6:] in ('Canada', 'Mexico'):
continue
elif 'unnamed road' in gcode[0].data[0]['formatted_address']:
continue
elif 'Unnamed Road' in gcode[0].data[0]['formatted_address']:
continue
else:
counter += 1
coordinate_list.append((gcode[0].coordinates, gcode[0].formatted_address))
# output_file.write(fullstring.format(gcode.x, gcode.y, gcode.address))
except GeocoderError:
continue
print 'Finished generating %d coordinate points' % counter
return coordinate_list
def main(points, fname):
"""
write list of coordinates to file
:param points: number of points to generate
:param fname: name of output file
"""
fullstring = '{0}, {1}, "{2}"\n'
coordinates = []
if os.path.isfile(fname):
proceed_message = "File {} exists, proceed(y or n)? "
proceed_prompt = raw_input(proceed_message.format(fname))
if proceed_prompt.lower() == 'y':
fout = open(fname, 'a')
number_of_points = points
coordinates = coordinate_generator(number_of_points)
# fout.close()
for loc in coordinates:
fout.write(fullstring.format(loc[0][1], loc[0][0], loc[1]))
else:
print 'Aborting . . .'
exit()
else:
fout = open(fname, 'a')
number_of_points = points
coordinates = coordinate_generator(number_of_points)
for loc in coordinates:
fout.write(fullstring.format(loc[0][1], loc[0][0], loc[1]))
fout.close()
if __name__ == '__main__':
#pylint: disable=invalid-name
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--points", type=int,
help="number of coordinate points to generate"
)
parser.add_argument("-o", "--output_file",
help="name of output file",
default="output.csv")
args = parser.parse_args()
points = args.points
fname = args.output_file
main(points, fname)