4
4
import os
5
5
6
6
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' ):
8
21
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
11
24
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' , []))]
14
28
value = 'latitude,longitude' + os .linesep + '' .join (elements )
15
- output_filename = os . path . splitext ( input_filename )[ 0 ] + '.csv'
29
+ output_filename = '.' . join ([ name , ext ])
16
30
with open (output_filename , 'w' ) as f :
17
31
f .write (value )
18
32
19
33
20
- def convert_maproulette (json_data , input_filename ):
34
+ def convert_maproulette (json_data , name , ext = 'tasks.json' ):
21
35
def build_element (crosswalk ):
22
36
element = \
23
37
{
@@ -42,15 +56,16 @@ def build_element(crosswalk):
42
56
}
43
57
return element
44
58
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' , []))]
47
62
value = str (elements ).replace ("'" , '"' )
48
- output_filename = 'tasks.json'
63
+ output_filename = '.' . join ([ name , ext ])
49
64
with open (output_filename , 'w' ) as f :
50
65
f .write (value )
51
66
52
67
53
- def convert_geojson (json_data , input_filename ):
68
+ def convert_geojson (json_data , name , ext = 'geo.json' ):
54
69
def build_element (crosswalk ):
55
70
element = \
56
71
{
@@ -73,47 +88,60 @@ def build_element(crosswalk):
73
88
"features" : [
74
89
]
75
90
}
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' , []))]
78
94
value = str (outer ).replace ("'" , '"' )
79
- output_filename = os . path . splitext ( input_filename )[ 0 ] + '.geo.json'
95
+ output_filename = '.' . join ([ name , ext ])
80
96
with open (output_filename , 'w' ) as f :
81
97
f .write (value )
82
98
83
99
84
100
def convert (args ):
85
101
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
87
112
for conv_func in args .conversion_funcs :
88
- conv_func (data , data_filename )
113
+ conv_func (data , ** conv_kw )
89
114
90
115
91
116
def mainfunc ():
92
117
parser = argparse .ArgumentParser (
93
- description = 'Convert crosswalks json to other formats' ,
94
- )
118
+ description = 'Convert crosswalks json to other formats' , )
95
119
parser .add_argument (
96
120
'--csv' ,
97
121
action = 'append_const' ,
98
122
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' )
107
130
parser .add_argument (
108
131
'--maproulette' ,
109
132
action = 'append_const' ,
110
133
dest = 'conversion_funcs' ,
111
134
const = convert_maproulette ,
112
- help = 'convert to maproulette.org challenge format' )
135
+ help = 'convert to maproulette.org challenge format, extension .tasks.json ' )
113
136
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' )
117
145
parser .set_defaults (func = convert )
118
146
119
147
args = parser .parse_args ()
@@ -122,5 +150,6 @@ def mainfunc():
122
150
else :
123
151
parser .print_help ()
124
152
153
+
125
154
if __name__ == "__main__" :
126
155
mainfunc ()
0 commit comments