-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtext2geojson.py
executable file
·60 lines (48 loc) · 2.07 KB
/
text2geojson.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
#!/usr/bin/env python
""" Convert tab-separated geo census output to GeoJSON.
Run with --help flag for usage instructions.
"""
from sys import stdin, stdout, stderr
from csv import DictReader
from os.path import basename
from optparse import OptionParser
from json import JSONEncoder
from re import compile
def make_feature(row, precision=None):
return {'properties': row,
'geometry':
{'type': 'Point',
'coordinates': (float(row['Longitude']),
float(row['Latitude']))}}
if __name__ == '__main__':
parser = OptionParser(usage="""%prog [options]
""")
parser.add_option('-o', '--output', dest='output',
help='Optional output filename, stdout if omitted.')
parser.add_option('-i', '--indent', dest='indent',
type='int', help='Optional number of spaces to indent.')
parser.add_option('-p', '--precision', dest='precision', type='int',
help='Optional decimal precision for degree values.')
parser.add_option('-q', '--quiet', dest='verbose',
help='Be quieter than normal',
action='store_false')
parser.add_option('-v', '--verbose', dest='verbose',
help='Be louder than normal',
action='store_true')
parser.set_defaults(indent=None, precision=5)
options, args = parser.parse_args()
input = len(args) and open(args.pop(0), 'r') or stdin
output = options.output and open(options.output, 'w') or stdout
indent = options.indent
rows = DictReader(input, dialect='excel-tab')
features = [make_feature(row, options.precision) for row in rows]
collection = {'type': 'FeatureCollection',
'features': features}
float_pat = compile(r'^-?\d+\.\d+$')
float_fmt = '%%.%df' % options.precision
encoded = JSONEncoder(indent=indent).iterencode(collection)
for atom in encoded:
if float_pat.match(atom):
output.write(float_fmt % float(atom))
else:
output.write(atom)