Example #1
0
def dissolve(sourcefile, sinkfile, key, unsplit=None):
    try:
        shape
    except NameError:
        raise NotImplementedError("dissolve require shapely")

    with fiona.drivers():
        with fiona.open(sourcefile) as source:
            schema = source.schema
            schema['properties'] = {key: source.schema['properties'][key]}

            with fiona.open(sinkfile,
                            'w',
                            crs=source.crs,
                            schema=schema,
                            driver=source.driver) as sink:

                gotkeys = dict()

                for _, feat in source.items():
                    fkey = feat['properties'][key]
                    fshape = shapelyshape(feat['geometry'])

                    if fkey in gotkeys:
                        gotkeys[fkey][0] = gotkeys[fkey][0].union(fshape)
                    else:
                        gotkeys[fkey] = [fshape]

                for shapelist in gotkeys.values():
                    if unsplit:
                        for s in disjointed(shapelist):
                            sink.write(s)

                    else:
                        sink.write(shapelist[0])
Example #2
0
def area(feature):
    try:
        geom = shapelyshape(feature['geometry'])
        return geom.area

    except NameError:
        raise NotImplementedError("length requires shapely")
Example #3
0
def length(feature):
    '''Returns shapely length'''
    try:
        geom = shapelyshape(feature['geometry'])
        return geom.length

    except NameError:
        raise NotImplementedError("length requires shapely")
def shape(feature):
    '''Applies shapely.geometry.shape to the geometry part of a feature
    and returns a new feature object with properties intact'''
    try:
        return {
            'properties': feature.get('properties'),
            'geometry': shapelyshape(feature['geometry'])
        }

    except NameError:
        raise NotImplementedError("shapify requires shapely")
Example #5
0
def shapes(filename, crs=None):
    '''
    Generator that yields a Shapely shape for every feature in a layer.
    '''
    try:
        shapelyshape
    except NameError:
        raise NotImplementedError("length require shapely")

    with fiona.drivers():
        with fiona.open(filename, 'r') as layer:
            if crs is not None:

                def _geom(feature):
                    return fiona.transform.transform_geom(
                        layer.crs, crs, feature['geometry'])
            else:

                def _geom(feature):
                    return feature['geometry']

            for feature in layer:
                yield shapelyshape(_geom(feature))