diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 00000000..10484096 --- /dev/null +++ b/.style.yapf @@ -0,0 +1,4 @@ +[style] +based_on_style = pep8 +column_limit = 80 +split_before_logical_operator = true diff --git a/src/challenge/CrosswalksConverter.py b/src/challenge/CrosswalksConverter.py index bc4b10aa..4dacf134 100644 --- a/src/challenge/CrosswalksConverter.py +++ b/src/challenge/CrosswalksConverter.py @@ -4,20 +4,34 @@ import os -def addcsvelt(json_data, input_filename): +def lat_lon_from_geojson_or_json(entry): + lat = entry.get('latitude', None) + lon = entry.get('longitude', None) + if lat is None and lon is None: + try: + coordinates = entry['geometry']['coordinates'] + lat = coordinates[1] + lon = coordinates[0] + except KeyError: + pass + return dict(latitude=lat, longitude=lon) + + +def convert_csv(json_data, name, ext='csv'): def build_element(crosswalk): - return str( - crosswalk['latitude']) + ',' + str(crosswalk['longitude']) + os.linesep + return str(crosswalk['latitude']) + ',' + str(crosswalk[ + 'longitude']) + os.linesep - elements = [build_element(crosswalk) - for crosswalk in json_data['crosswalks']] + elements = [build_element(lat_lon_from_geojson_or_json(crosswalk)) + for crosswalk in json_data.get('crosswalks', json_data.get( + 'features', []))] value = 'latitude,longitude' + os.linesep + ''.join(elements) - output_filename = os.path.splitext(input_filename)[0] + '.csv' + output_filename = '.'.join([name, ext]) with open(output_filename, 'w') as f: f.write(value) -def convert_maproulette(json_data, input_filename): +def convert_maproulette(json_data, name, ext='tasks.json'): def build_element(crosswalk): element = \ { @@ -42,15 +56,16 @@ def build_element(crosswalk): } return element - elements = [build_element(crosswalk) - for crosswalk in json_data['crosswalks']] + elements = [build_element(lat_lon_from_geojson_or_json(crosswalk)) + for crosswalk in json_data.get('crosswalks', json_data.get( + 'features', []))] value = str(elements).replace("'", '"') - output_filename = 'tasks.json' + output_filename = '.'.join([name, ext]) with open(output_filename, 'w') as f: f.write(value) -def convert_geojson(json_data, input_filename): +def convert_geojson(json_data, name, ext='geo.json'): def build_element(crosswalk): element = \ { @@ -73,47 +88,60 @@ def build_element(crosswalk): "features": [ ] } - outer['features'] = [build_element(crosswalk) - for crosswalk in json_data['crosswalks']] + outer['features'] = [build_element(lat_lon_from_geojson_or_json(crosswalk)) + for crosswalk in json_data.get( + 'crosswalks', json_data.get('features', []))] value = str(outer).replace("'", '"') - output_filename = os.path.splitext(input_filename)[0] + '.geo.json' + output_filename = '.'.join([name, ext]) with open(output_filename, 'w') as f: f.write(value) def convert(args): data = json.load(args.input_file) - data_filename = args.input_file.name + conv_kw = {} + if args.outputfile is not None: + file_args = args.outputfile.split('.') + conv_kw['name'] = file_args[0] + if len(file_args) > 1: + conv_kw['ext'] = '.'.join(file_args[1:]) + else: + file_args = args.input_file.name.split('.') + conv_kw['name'] = file_args[0] + # do not supply extension to use defaults for conv_func in args.conversion_funcs: - conv_func(data, data_filename) + conv_func(data, **conv_kw) def mainfunc(): parser = argparse.ArgumentParser( - description='Convert crosswalks json to other formats', - ) + description='Convert crosswalks json to other formats', ) parser.add_argument( '--csv', action='append_const', dest='conversion_funcs', - const=addcsvelt, - help='convert to csv format with two columns [latitude,longitude]') - parser.add_argument( - '--geojson', - action='append_const', - dest='conversion_funcs', - const=convert_geojson, - help='convert to geojson format') + const=convert_csv, + help='convert to csv format with two columns [latitude,longitude], extension .csv') + parser.add_argument('--geojson', + action='append_const', + dest='conversion_funcs', + const=convert_geojson, + help='convert to geojson format, extension .geo.json') parser.add_argument( '--maproulette', action='append_const', dest='conversion_funcs', const=convert_maproulette, - help='convert to maproulette.org challenge format') + help='convert to maproulette.org challenge format, extension .tasks.json') parser.add_argument( - 'input_file', - type=file, - help='name of crosswalks json file') + '--outputfile', + action='store', + dest='outputfile', + default=None, + help='explicit output filename if input filename is not wanted as name part. extension if present will be used instead of defaults') + parser.add_argument('input_file', + type=file, + help='name of crosswalks json file') parser.set_defaults(func=convert) args = parser.parse_args() @@ -122,5 +150,6 @@ def mainfunc(): else: parser.print_help() + if __name__ == "__main__": mainfunc()