Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import json
- def get_output_file_name(path):
- current_file_name = path.split('/')[-1]
- return '%s.json' % current_file_name.split('.')[0]
- file = sys.argv[1]
- file_name = get_output_file_name(file)
- indent = int(sys.argv[2]) if len(sys.argv) >= 3 else None
- if len(sys.argv) == 3:
- country_osm_id = sys.argv[2]
- else:
- country_osm_id = None
- with open(file) as data_file:
- data = json.load(data_file)
- features = data.get('features')
- new_features = []
- for feature in features:
- properties = feature.get('properties')
- name = properties.get('name')
- osm_id = properties.get('id')
- admin_level = properties.get('admin_level')
- try:
- is_in_country = int(properties.get('is_in_country', country_osm_id))
- except:
- is_in_country = properties.get('is_in_country', country_osm_id)
- try:
- parent_id = int(properties.get('parent_id'))
- except:
- parent_id = properties.get('parent_id')
- geometry = feature.get('geometry')
- new_feature = {
- 'type': 'Feature',
- 'properties': {
- 'osm_id': osm_id,
- 'admin_level': admin_level,
- 'is_in_country': is_in_country,
- 'name': name,
- 'name_en': name,
- 'parent_id': parent_id,
- 'is_simplified': True
- },
- 'geometry': geometry
- }
- new_features.append(new_feature)
- data['features'] = new_features
- output_directory = './output'
- if not os.path.exists(output_directory):
- os.mkdir(output_directory);
- file = open('%s/%s' % (output_directory, file_name), 'w')
- if indent:
- data_json = json.dumps(data, indent=2)
- else:
- data_json = json.dumps(data)
- file.write(data_json)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement