Skip to content

Commit

Permalink
Merge pull request #13 from MPAS-Dev/sort_features
Browse files Browse the repository at this point in the history
Add sorting, and catch some parsing errors with try blocks
  • Loading branch information
douglasjacobsen committed Feb 5, 2016
2 parents e65e346 + 3ca1320 commit c066672
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions merge_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,49 @@
first_feature = True
if os.path.exists(file_to_append):
new_file = False
with open(file_to_append) as f:
appended_file = json.load(f)
for feature in appended_file['features']:
all_features['features'].append(feature)
del appended_file
try:
with open(file_to_append) as f:
appended_file = json.load(f)
for feature in appended_file['features']:
all_features['features'].append(feature)
del appended_file
except:
new_file = True

out_file = open(file_to_append, 'w')

if args.feature_file:
with open(args.feature_file) as f:
feature_file = json.load(f)
try:
with open(args.feature_file) as f:
feature_file = json.load(f)

for feature in feature_file['features']:
if match_tag_list(feature, master_tag_list):
if not feature_already_exists(all_features, feature):
all_features['features'].append(feature)
for feature in feature_file['features']:
if match_tag_list(feature, master_tag_list):
if not feature_already_exists(all_features, feature):
all_features['features'].append(feature)

del feature_file
del feature_file
except:
print "Error parsing geojson file: %s"%(args.feature_file)

if args.features_dir:
paths = []
for (dirpath, dirnames, filenames) in os.walk(args.features_dir):
for filename in sorted(filenames):
with open('%s/%s'%(dirpath, filename), 'r') as f:
for filename in filenames:
paths.append('%s/%s'%(dirpath, filename))

for path in sorted(paths):
try:
with open('%s'%(path), 'r') as f:
feature_file = json.load(f)
for feature in feature_file['features']:
if match_tag_list(feature, master_tag_list):
if not feature_already_exists(all_features, feature):
all_features['features'].append(feature)
del feature_file
except:
print "Error parsing geojson file: %s"%(path)
del paths

out_file.write('{"type": "FeatureCollection",\n')
out_file.write(' "groupName": "enterNameHere",\n')
Expand Down

0 comments on commit c066672

Please sign in to comment.