Skip to content

Commit 9e83c79

Browse files
Merge pull request #78 from geometalab/CrosswalksConverterExtended
extended converter to convert from geojson files too
2 parents 5c5938c + 455b7af commit 9e83c79

File tree

2 files changed

+63
-30
lines changed

2 files changed

+63
-30
lines changed

.style.yapf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[style]
2+
based_on_style = pep8
3+
column_limit = 80
4+
split_before_logical_operator = true

src/challenge/CrosswalksConverter.py

+59-30
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,34 @@
44
import os
55

66

7-
def addcsvelt(json_data, input_filename):
7+
def lat_lon_from_geojson_or_json(entry):
8+
lat = entry.get('latitude', None)
9+
lon = entry.get('longitude', None)
10+
if lat is None and lon is None:
11+
try:
12+
coordinates = entry['geometry']['coordinates']
13+
lat = coordinates[1]
14+
lon = coordinates[0]
15+
except KeyError:
16+
pass
17+
return dict(latitude=lat, longitude=lon)
18+
19+
20+
def convert_csv(json_data, name, ext='csv'):
821
def build_element(crosswalk):
9-
return str(
10-
crosswalk['latitude']) + ',' + str(crosswalk['longitude']) + os.linesep
22+
return str(crosswalk['latitude']) + ',' + str(crosswalk[
23+
'longitude']) + os.linesep
1124

12-
elements = [build_element(crosswalk)
13-
for crosswalk in json_data['crosswalks']]
25+
elements = [build_element(lat_lon_from_geojson_or_json(crosswalk))
26+
for crosswalk in json_data.get('crosswalks', json_data.get(
27+
'features', []))]
1428
value = 'latitude,longitude' + os.linesep + ''.join(elements)
15-
output_filename = os.path.splitext(input_filename)[0] + '.csv'
29+
output_filename = '.'.join([name, ext])
1630
with open(output_filename, 'w') as f:
1731
f.write(value)
1832

1933

20-
def convert_maproulette(json_data, input_filename):
34+
def convert_maproulette(json_data, name, ext='tasks.json'):
2135
def build_element(crosswalk):
2236
element = \
2337
{
@@ -42,15 +56,16 @@ def build_element(crosswalk):
4256
}
4357
return element
4458

45-
elements = [build_element(crosswalk)
46-
for crosswalk in json_data['crosswalks']]
59+
elements = [build_element(lat_lon_from_geojson_or_json(crosswalk))
60+
for crosswalk in json_data.get('crosswalks', json_data.get(
61+
'features', []))]
4762
value = str(elements).replace("'", '"')
48-
output_filename = 'tasks.json'
63+
output_filename = '.'.join([name, ext])
4964
with open(output_filename, 'w') as f:
5065
f.write(value)
5166

5267

53-
def convert_geojson(json_data, input_filename):
68+
def convert_geojson(json_data, name, ext='geo.json'):
5469
def build_element(crosswalk):
5570
element = \
5671
{
@@ -73,47 +88,60 @@ def build_element(crosswalk):
7388
"features": [
7489
]
7590
}
76-
outer['features'] = [build_element(crosswalk)
77-
for crosswalk in json_data['crosswalks']]
91+
outer['features'] = [build_element(lat_lon_from_geojson_or_json(crosswalk))
92+
for crosswalk in json_data.get(
93+
'crosswalks', json_data.get('features', []))]
7894
value = str(outer).replace("'", '"')
79-
output_filename = os.path.splitext(input_filename)[0] + '.geo.json'
95+
output_filename = '.'.join([name, ext])
8096
with open(output_filename, 'w') as f:
8197
f.write(value)
8298

8399

84100
def convert(args):
85101
data = json.load(args.input_file)
86-
data_filename = args.input_file.name
102+
conv_kw = {}
103+
if args.outputfile is not None:
104+
file_args = args.outputfile.split('.')
105+
conv_kw['name'] = file_args[0]
106+
if len(file_args) > 1:
107+
conv_kw['ext'] = '.'.join(file_args[1:])
108+
else:
109+
file_args = args.input_file.name.split('.')
110+
conv_kw['name'] = file_args[0]
111+
# do not supply extension to use defaults
87112
for conv_func in args.conversion_funcs:
88-
conv_func(data, data_filename)
113+
conv_func(data, **conv_kw)
89114

90115

91116
def mainfunc():
92117
parser = argparse.ArgumentParser(
93-
description='Convert crosswalks json to other formats',
94-
)
118+
description='Convert crosswalks json to other formats', )
95119
parser.add_argument(
96120
'--csv',
97121
action='append_const',
98122
dest='conversion_funcs',
99-
const=addcsvelt,
100-
help='convert to csv format with two columns [latitude,longitude]')
101-
parser.add_argument(
102-
'--geojson',
103-
action='append_const',
104-
dest='conversion_funcs',
105-
const=convert_geojson,
106-
help='convert to geojson format')
123+
const=convert_csv,
124+
help='convert to csv format with two columns [latitude,longitude], extension .csv')
125+
parser.add_argument('--geojson',
126+
action='append_const',
127+
dest='conversion_funcs',
128+
const=convert_geojson,
129+
help='convert to geojson format, extension .geo.json')
107130
parser.add_argument(
108131
'--maproulette',
109132
action='append_const',
110133
dest='conversion_funcs',
111134
const=convert_maproulette,
112-
help='convert to maproulette.org challenge format')
135+
help='convert to maproulette.org challenge format, extension .tasks.json')
113136
parser.add_argument(
114-
'input_file',
115-
type=file,
116-
help='name of crosswalks json file')
137+
'--outputfile',
138+
action='store',
139+
dest='outputfile',
140+
default=None,
141+
help='explicit output filename if input filename is not wanted as name part. extension if present will be used instead of defaults')
142+
parser.add_argument('input_file',
143+
type=file,
144+
help='name of crosswalks json file')
117145
parser.set_defaults(func=convert)
118146

119147
args = parser.parse_args()
@@ -122,5 +150,6 @@ def mainfunc():
122150
else:
123151
parser.print_help()
124152

153+
125154
if __name__ == "__main__":
126155
mainfunc()

0 commit comments

Comments
 (0)