def write_group_name(feature_file, groupName): # {{{ """ Adds groupName property to a given feature file. Authors: Xylar Asay-Davis, Phillip J. Wolfram Last Modified: 03/16/2017 """ if not os.path.exists(feature_file): raise ValueError('File {} does not exist.' .format(feature_file)) all_features = defaultdict(list) with open(feature_file) as f: filevals = json.load(f) for feature in filevals['features']: if not feature_already_exists(all_features, feature): all_features['features'].append(feature) all_features['groupName'] = groupName write_all_features(all_features, feature_file, indent=4) return # }}}
def remove_small_polygons(inFileName, outFileName, minArea): # {{{ features = defaultdict(list) if os.path.exists(outFileName): try: with open(outFileName) as f: appended_file = json.load(f) for feature in appended_file['features']: features['features'].append(feature) del appended_file except: pass inFeatures = defaultdict(list) try: with open(inFileName) as f: feature_file = json.load(f) for feature in feature_file['features']: inFeatures['features'].append(feature) del feature_file except: print "Error parsing geojson file: %s" % (inFileName) raise for feature in inFeatures['features']: name = feature['properties']['name'] if feature_already_exists(features, feature): print "Warning: feature %s already in features.geojson. " \ "Skipping..." % name continue geom = feature['geometry'] if geom['type'] not in ['Polygon', 'MultiPolygon']: # no area to check, so just add it features['features'].append(feature) else: add = False featureShape = shapely.geometry.shape(geom) if featureShape.type == 'Polygon': if featureShape.area > minArea: add = True else: # a MultiPolygon outPolygons = [] for polygon in featureShape: if polygon.area > minArea: outPolygons.append(polygon) if len(outPolygons) > 0: outShape = shapely.ops.cascaded_union(outPolygons) feature['geometry'] = shapely.geometry.mapping(outShape) add = True if add: features['features'].append(feature) else: print "%s has been removed." % name write_all_features(features, outFileName, indent=4) # }}}
def remove_small_polygons(inFileName, outFileName, minArea): # {{{ features = defaultdict(list) if os.path.exists(outFileName): try: with open(outFileName) as f: appended_file = json.load(f) for feature in appended_file['features']: features['features'].append(feature) del appended_file except: pass inFeatures = defaultdict(list) try: with open(inFileName) as f: feature_file = json.load(f) for feature in feature_file['features']: inFeatures['features'].append(feature) del feature_file except: print "Error parsing geojson file: %s" % (inFileName) raise for feature in inFeatures['features']: name = feature['properties']['name'] if feature_already_exists(features, feature): print "Warning: feature %s already in features.geojson. " \ "Skipping..." % name continue geom = feature['geometry'] if geom['type'] not in ['Polygon', 'MultiPolygon']: # no area to check, so just add it features['features'].append(feature) else: add = False featureShape = shapely.geometry.shape(geom) if featureShape.type == 'Polygon': if featureShape.area > minArea: add = True else: # a MultiPolygon outPolygons = [] for polygon in featureShape: if polygon.area > minArea: outPolygons.append(polygon) if len(outPolygons) > 0: outShape = shapely.ops.cascaded_union(outPolygons) feature['geometry'] = shapely.geometry.mapping(outShape) add = True if add: features['features'].append(feature) else: print "%s has been removed." % name write_all_features(features, outFileName, indent=4) # }}}
if os.path.exists(out_file_name): try: with open(out_file_name) as f: appended_file = json.load(f) for feature in appended_file['features']: features['features'].append(feature) del appended_file except: pass try: with open(args.feature_file) as f: feature_file = json.load(f) for feature in feature_file['features']: if not feature_already_exists(features, feature): features['features'].append(feature) del feature_file except: print "Error parsing geojson file: %s"%(args.feature_file) raise mask = defaultdict(list) try: with open(args.mask_file) as f: feature_file = json.load(f) for feature in feature_file['features']: if not feature_already_exists(mask, feature): mask['features'].append(feature)
pass featuresToSimplify = defaultdict(list) try: with open(args.feature_file) as f: feature_file = json.load(f) for feature in feature_file['features']: featuresToSimplify['features'].append(feature) del feature_file except: print "Error parsing geojson file: {}".format(args.feature_file) raise for feature in featuresToSimplify['features']: name = feature['properties']['name'] if feature_already_exists(features, feature): print "Warning: feature {} already in features.geojson. " \ "Skipping...".format(name) continue featureShape = shapely.geometry.shape(feature['geometry']) simplifiedFeature = featureShape.simplify(args.tolerance) feature['geometry'] = shapely.geometry.mapping(simplifiedFeature) features['features'].append(feature) write_all_features(features, out_file_name, indent=4) # vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
except: pass featuresToSimplify = defaultdict(list) try: with open(args.feature_file) as f: feature_file = json.load(f) for feature in feature_file['features']: featuresToSimplify['features'].append(feature) del feature_file except: print "Error parsing geojson file: %s"%(args.feature_file) raise for feature in featuresToSimplify['features']: name = feature['properties']['name'] if feature_already_exists(features, feature): print "Warning: feature %s already in features.geojson. Skipping..."%name continue featureShape = shapely.geometry.shape(feature['geometry']) simplifiedFeature = featureShape.simplify(args.tolerance) feature['geometry'] = shapely.geometry.mapping(simplifiedFeature) features['features'].append(feature) write_all_features(features, out_file_name, indent=4) # vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
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 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(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 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: {}".format(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('{}/{}'.format(dirpath, filename)) for path in sorted(paths): try: