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)  # }}}
示例#4
0
    feature_file = json.load(f)

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

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


for featureIndex in range(len(features['features'])):
  feature = features['features'][featureIndex]
  featureShape = shapely.geometry.shape(feature['geometry'])
  for maskFeature in mask['features']:
    maskShape = shapely.geometry.shape(maskFeature['geometry'])
    featureShape = featureShape.difference(maskShape)
  features['features'][featureIndex]['geometry'] = shapely.geometry.mapping(featureShape)

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(features, out_file, '\t\t')
out_file.write('\n')
out_file.write('\t]\n')
out_file.write('}\n')

示例#5
0
bed = inFile.variables['bed'][:, :]

bedMask = numpy.logical_and(bed > 0., bed.mask == False)

iceMask = numpy.logical_or(iceMask, bedMask)
groundedMask = numpy.logical_or(groundedMask, bedMask)

# flood fill to remove lakes

masks = {}
masks['AntarcticIceCoverage'] = iceMask
masks['AntarcticGroundedIceCoverage'] = groundedMask
features_file = {}
features_file['features'] = []
for name in masks:

    properties = {}
    properties['name'] = name
    properties['component'] = 'bedmap2'
    properties['author'] = 'https://www.bas.ac.uk/project/bedmap-2/'
    properties['object'] = 'region'
    properties['tags'] = ''
    feature = {}
    feature['properties'] = properties
    feature['geometry'] = extract_geometry(masks[name])
    features_file['features'].append(feature)

out_file_name = "AntarcticIceCoverage.geojson"

write_all_features(features_file, out_file_name, indend=4)
try:
    filePointer = open(args.feature_file, 'r')
except IOError:
    print "Error: file %s does not exist"%(args.feature_file)
    raise

appended_file = json.load(filePointer)
for feature in appended_file['features']:
    try:
        feature_tags = feature['properties']['tags']
    except KeyError:
        feature_tags = ''
    feature_tag_list = feature_tags.split(';')
    if args.remove:
        if args.tag in feature_tag_list:
            feature_tag_list.remove(args.tag)
        feature['properties']['tags'] = ';'.join(feature_tag_list)
    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)

write_all_features(all_features, out_file_name, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
        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
    return shapely.geometry.mapping(outShape)


parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--features_file", dest="features_file", help="File containing features to split at the antimeridian", metavar="FILE", required=True)
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 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']:
    print feature['properties']['name']

    geometry = feature['geometry']

    result = splitGeometryCrossingAntimeridian(geometry)
    if(result is not None):
        feature['geometry'] = result

write_all_features(features_file, args.output_file_name, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
    for index in range(len(paths)):
        paths[index].vertices = fromPolar(paths[index].vertices)

    # extract the contour as a polygon or multipolygon
    if (len(paths) == 1):
        # create a Polygon
        feature['geometry']['type'] = 'Polygon'
        feature['geometry']['coordinates'] = [paths[0].vertices.tolist()]
    else:
        # create a MultiPolygon
        feature['geometry']['type'] = 'MultiPolygon'
        polys = []
        for index in range(len(paths)):
            polys.append([paths[index].vertices.tolist()])
        feature['geometry']['coordinates'] = polys

    feature_name = feature['properties']['name']
    component = feature['properties']['component']
    object_type = feature['properties']['object']

    features['features'].append(feature)

    del cs, paths
    fig.clf()
    plt.close()
    gc.collect()

if os.path.exists(outFileName):
    os.remove(outFileName)
write_all_features(features, outFileName, indent=4)
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']

    base_dir = component
    if args.output_dir_name is not None:
        base_dir = args.output_dir_name

    dir_name = feature_name.strip().replace(' ','_').strip('\'').strip('.',)

    if not os.path.exists('%s/%s/%s'%(base_dir, object_type, dir_name)):
        os.makedirs('%s/%s/%s'%(base_dir, object_type, dir_name))

    out_file_name = '%s/%s/%s/%s.geojson'%(base_dir, object_type, dir_name,
                                           object_type)

    write_all_features({'features':[feature]}, out_file_name, indent=4,
                       defaultGroupName=None)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
                    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

write_all_features(all_features, file_to_append, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
                    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:
            with open('{}'.format(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: {}".format(path)
    del paths

write_all_features(all_features, file_to_append, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
from utils.feature_test_utils import feature_already_exists

parser = argparse.ArgumentParser(description=__doc__,
                                 formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-f", "--feature_file", dest="feature_file", required=True,
                    help="Input and output feature file where group name is to"
                    "be set", metavar="FILE")
parser.add_argument("-g", "--group", dest="groupName",
                    help="Feature group name", metavar="GROUPNAME",
                    required=True)

args = parser.parse_args()

if not os.path.exists(args.feature_file):
    parser.error('The file %s does not exist.'%(args.feature_file))

all_features = defaultdict(list)

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

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

all_features['groupName'] = args.groupName

write_all_features(all_features, args.feature_file, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
示例#14
0
                    metavar="FILE",
                    required=True)
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 args.features_file:
    if not os.path.exists(args.features_file):
        parser.error('The file {} does not exist.'.format(args.features_file))

with open(args.features_file) as f:
    features_file = json.load(f)

for feature in features_file['features']:
    print feature['properties']['name']

    geometry = feature['geometry']

    result = splitGeometryCrossingAntimeridian(geometry)
    if (result is not None):
        feature['geometry'] = result

write_all_features(features_file, args.output_file_name, indent=4)

# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python