Skip to content

Commit

Permalink
Merge pull request #78 from geometalab/CrosswalksConverterExtended
Browse files Browse the repository at this point in the history
extended converter to convert from geojson files too
  • Loading branch information
marcelhuberfoo authored Jun 20, 2016
2 parents 5c5938c + 455b7af commit 9e83c79
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[style]
based_on_style = pep8
column_limit = 80
split_before_logical_operator = true
89 changes: 59 additions & 30 deletions src/challenge/CrosswalksConverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = \
{
Expand All @@ -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 = \
{
Expand All @@ -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()
Expand All @@ -122,5 +150,6 @@ def mainfunc():
else:
parser.print_help()


if __name__ == "__main__":
mainfunc()

0 comments on commit 9e83c79

Please sign in to comment.