Skip to content

Instantly share code, notes, and snippets.

@rbanick
Created March 11, 2016 12:41
Show Gist options
  • Save rbanick/0eeaa6006d8e60f1ead7 to your computer and use it in GitHub Desktop.
Save rbanick/0eeaa6006d8e60f1ead7 to your computer and use it in GitHub Desktop.
Matching OSM files to corresponding OpenMapKit surveys
import os
import fnmatch
import json
import shutil
source_folder = 'your/survey/folder/directory'
osm_files = '/your/osm/files/folder'
fixed_folders = '/where/to/put/rescued/survey/folders'
def out(f, arr, name):
#output values from the 4 arrays
f.write(name + '\n')
f.write('num: ' + str(len(arr)) + '\n')
for v in arr:
f.write(v + '\n')
f.write('\n')
def searchthing(location, searchterm):
good = []
fixed = []
missing_file_name = []
no_file = []
for dirName, subdirList, fileList in os.walk(location):
for fname in fileList:
if fname.lower().endswith((".json")):
with open(dirName + '/' + fname, 'r+') as cf:
parsed_json = json.loads(cf.read().decode("utf-8"))
#is the current foldering missing an osm file?
if not [v for v in fileList if '.osm' in v]:
bad = None
#check to see if it has osm tags at one or two levels
if parsed_json.has_key('osm_building'):
#check to see if entry is a dict
bad = None
if isinstance(parsed_json['osm_building'], dict):
bad = parsed_json['osm_building']['originalFilename']
else:
bad = parsed_json['osm_building']
if bad:
try:
source_file = osm_files + bad
destination_file = dirName + '/' + bad
shutil.copy(source_file,destination_file)
fixed.append(dirName)
this_folder=dirName.split('/')[-1]
fixed_destination = fixed_folders + '/' + dirName
shutil.copytree(dirName,fixed_destination)
except IOError:
no_file.append(dirName)
else:
missing_file_name.append(dirName)
else:
good.append(dirName)
with open(location + 'output.txt', 'w') as f:
out(f,fixed,'fixxed')
#out(f,good,'good')
out(f,missing_file_name,'missing_file_name')
out(f,no_file,'no_file')
searchthing(source_folder, '.osm')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment