forked from MPAS-Dev/geometric_features
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_features.py
executable file
·50 lines (35 loc) · 1.64 KB
/
split_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
#!/usr/bin/env python
"""
This script takes a file containing one or more feature definitions, that is
pointed to by the -f flag. It then writes each feature defition out to it's own
independent file in an autogenerated directory tree.
"""
import sys, os, glob, shutil, numpy
import json
import argparse
from utils.feature_write_utils import *
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--features_file", dest="features_file", help="File containing features to split up", metavar="FILE", required=True)
args = parser.parse_args()
if args.features_file:
if not os.path.exists(args.features_file):
parser.error('The file %s does not exist.'%(args.features_file))
with open(args.features_file) as f:
features_file = json.load(f)
for feature in features_file['features']:
feature_name = feature['properties']['name']
component = feature['properties']['component']
object_type = feature['properties']['object']
dir_name = feature_name.strip().replace(' ','_').strip('\'').strip('.',)
if not os.path.exists('%s/%s/%s'%(component, object_type, dir_name)):
os.makedirs('%s/%s/%s'%(component, object_type, dir_name))
out_file = open('%s/%s/%s/%s.geojson'%(component, object_type, dir_name, object_type), 'w')
out_file.write('{"type": "FeatureCollection",\n')
out_file.write(' "features":\n')
out_file.write('\t[\n')
write_single_feature(feature, out_file, '\t\t')
out_file.write('\n')
out_file.write('\t]\n')
out_file.write('}\n')
out_file.close()
# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python