Exemple #1
0
    def polygonize(self, band_number=1):
        """
        Extract shapes from raster features. This is the inverse operation of rasterizing shapes.
        Uses the `Rasterio <https://mapbox.github.io/rasterio/_modules/rasterio/features.html>'_ library
        for this purpose. The data is loaded into a `geopandas <http://geopandas.org/user.html>`_ GeoDataFrame.
        GeoDataFrame data structures are pandas DataFrames with added functionality, containing a ``geometry``
        column for the `Shapely <http://toblerity.org/shapely/shapely.geometry.html>`_ geometries.
        The raster data should be loaded in the layer before calling this method.

        :param int band_number: The index of the raster band which is to be used as input for extracting \
        gemetrical shapes.

        :returns: geopandas.GeoDataFrame

        """
        raster_data = self.read(band_number)
        mask = raster_data != self.raster_reader.nodata
        T0 = self.raster_reader.affine
        shapes = features.shapes(raster_data, mask=mask, transform=T0)
        df = GeoDataFrame.from_records(shapes, columns=['geometry', 'value'])
        # convert the geometry dictionary from a dictionary format like {'coordinates': [[(-73.5, 83.5),
        #   (-73.5, 83.0),
        #   (-68.0, 83.0),
        #   (-68.0, 83.5),
        #   (-73.5, 83.5)]],
        # 'type': 'Polygon'}
        # to a proper shapely polygon format
        df.geometry = df.geometry.apply(lambda row: Polygon(row['coordinates'][0]))
        df.crs = self.raster_reader.crs
        return df
Exemple #2
0
def intersections(sources, targets, area_cutoff=None):
    """Computes all of the nonempty intersections between two sets of geometries.
    The returned `~geopandas.GeoSeries` will have a MultiIndex, where the geometry
    at index *(i, j)* is the intersection of ``sources[i]`` and ``targets[j]``
    (if it is not empty).

    :param sources: geometries
    :type sources: :class:`~geopandas.GeoSeries` or :class:`~geopandas.GeoDataFrame`
    :param targets: geometries
    :type targets: :class:`~geopandas.GeoSeries` or :class:`~geopandas.GeoDataFrame`
    :rtype: :class:`~geopandas.GeoSeries`
    :param area_cutoff: (optional) if provided, only return intersections with
        area greater than ``area_cutoff``
    :type area_cutoff: Number or None
    """
    reindexed_sources = get_geometries_with_range_index(sources)
    reindexed_targets = get_geometries_with_range_index(targets)
    spatially_indexed_sources = IndexedGeometries(reindexed_sources)

    records = [
        # Flip i, j to j, i so that the index is ["source", "target"]
        (sources.index[j], targets.index[i], geometry) for i, j, geometry in
        spatially_indexed_sources.enumerate_intersections(reindexed_targets)
    ]
    df = GeoDataFrame.from_records(records,
                                   columns=["source", "target", "geometry"])
    geometries = df.set_index(["source", "target"]).geometry
    geometries.sort_index(inplace=True)
    geometries.crs = sources.crs

    if area_cutoff is not None:
        geometries = geometries[geometries.area > area_cutoff]

    return geometries