forked from MPAS-Dev/geometric_features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_features.py
executable file
·73 lines (61 loc) · 2.53 KB
/
tag_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
"""
This script takes a file containing a collection of one or more feature
definitions, that is pointed to by the -f flag and a tag name pointed to by
the -t flag. The tag is added to each feature (if that feature does not
already have that tag. Results are written back to the same feature file.
"""
import os.path
import json
import argparse
from collections import defaultdict
from utils.feature_write_utils import write_all_features
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--feature_file", dest="feature_file",
help="Features collection file to be tagged", metavar="FILE",
required=True)
parser.add_argument("-t", "--tag", dest="tag", help="Tag to add to all features",
metavar="TAG", required=True)
parser.add_argument("-r", "--remove", dest="remove", action='store_true',
help="Use this flag to signal removing a tag instead of adding")
args = parser.parse_args()
out_file_name = args.feature_file
all_features = defaultdict(list)
if os.path.exists(out_file_name):
with open(out_file_name) as f:
appended_file = json.load(f)
for feature in appended_file['features']:
try:
feature_tags = feature['properties']['tags']
except:
feature_tags = ''
feature_tag_list = feature_tags.split(';')
if args.remove:
if args.tag in feature_tag_list:
feature_tag_list.remove(args.tag)
if(len(feature_tag_list) == 0):
feature['properties']['tags'] = ''
else:
feature_tags = feature_tag_list[0]
for tag in feature_tag_list[1:len(feature_tag_list)]:
feature_tags = '%s;%s'%(feature_tags,tag)
feature['properties']['tags'] = feature_tags
else:
if args.tag not in feature_tag_list:
if(feature_tags == ''):
feature['properties']['tags'] = args.tag
else:
feature['properties']['tags'] = '%s;%s'%(feature_tags,args.tag)
all_features['features'].append(feature)
del appended_file
out_file = open(out_file_name, 'w')
out_file.write('{"type": "FeatureCollection",\n')
out_file.write(' "groupName": "enterNameHere",\n')
out_file.write(' "features":\n')
out_file.write('\t[\n')
write_all_features(all_features, out_file, '\t\t')
out_file.write('\n')
out_file.write('\t]\n')
out_file.write('}\n')
# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python