forked from MPAS-Dev/geometric_features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_features.py
executable file
·115 lines (94 loc) · 4.23 KB
/
merge_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
"""
This script has two modes of usage:
1. To create a new file (features.geojson) containing one or more features that
are pointed to using the -f or -d flags.
2. To append one or more features on an already existing features.geojson file,
again defined by the -f and -d flags.
The usage mode is automatically detected for you, depending on if the
output file exists or not before calling this script. The output file
is pointed to using the -o flag (features.geojson by default).
When using this script, you can optionally give a list of tags in a semicolon
delimited list (e.g. "tag1;tag2;tag3"). Features are only added to
features.geojson if their tags property contains all of the tags listed on the
input line.
Authors: Douglas Jacobsen
Last Modified: 02/11/2016
"""
import sys, os, glob, shutil, numpy, fnmatch
import json
import argparse
from collections import defaultdict
from utils.feature_write_utils import *
from utils.feature_test_utils import *
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--feature_file", dest="feature_file", help="Single feature file to append to features.geojson", metavar="FILE")
parser.add_argument("-d", "--features_directory", dest="features_dir", help="Directory containing multiple feature files, each will be appended to features.geojson", metavar="PATH")
parser.add_argument("-t", "--tags", dest="tags", help="Semicolon separated list of tags to match features against.", metavar='"TAG1;TAG2;TAG3"')
parser.add_argument("-o", "--output", dest="output_file_name", help="Output file, e.g., features.geojson.", metavar="PATH", default="features.geojson")
args = parser.parse_args()
if not args.feature_file and not args.features_dir:
parser.error('Either a feature file (-f) or a feature directory (-d) is required.')
if args.features_dir:
if not os.path.exists(args.features_dir):
parser.error('The path %s does not exist.'%(args.features_dir))
if args.feature_file:
if not os.path.exists(args.feature_file):
parser.error('The file %s does not exist.'%(args.feature_file))
master_tag_list = []
if args.tags:
for tag in args.tags.split(';'):
master_tag_list.append(tag)
file_to_append = args.output_file_name
all_features = defaultdict(list)
new_file = True
first_feature = True
if os.path.exists(file_to_append):
new_file = False
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:
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)
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 filenames:
if fnmatch.fnmatch(filename, '*.geojson'):
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')
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