Esempio n. 1
0
    def to_gdal_ds(self):
        """Export raster object to `gdal.DataSource`.

        Returns
        -------
        ds: gdal.DataSource
            DataSource converted from the raster object.
        """
        ds = tgp.write_gdal_ds(self.data,
                               geo_transform=self.geo_transform,
                               projection=self.projection,
                               gdaldtype=self.gdaldtype,
                               no_data_value=self.no_data_value)
        return ds
Esempio n. 2
0
def rasterize_layer(src_vector,
                    rows,
                    cols,
                    geo_transform,
                    use_attribute,
                    all_touched=False,
                    no_data_value=0):
    """Rasterize vector data. Get the cell value in defined grid (rows, cols, geo_transform)
    from its overlapped polygon.

    Parameters
    ----------
    src_vector: Geopandas.GeoDataFrame
        Which vector data to be rasterize.
    rows: int
        Target rasterized image's rows.
    cols: int
        Target rasterized image's cols.
    geo_transform: tuple
        Target rasterized image's geo_transform which is the affine parameter.
    use_attribute: str
        The column to use as rasterized image value.
    all_touched: bool, optioonal, default: False
        Pixels that touch (not overlap over 50%) the polygon will be assign the use_attribute value of the polygon.
    no_data_value: int or float
        The pixels not covered by any polygon will be filled no_data_value.

    Returns
    -------
    raster: Raster. 
        Rasterized result.

    Examples
    -------- 
    >>> import geopandas as gpd
    >>> import TronGisPy as tgp 
    >>> from TronGisPy import ShapeGrid
    >>> from matplotlib import pyplot as plt
    >>> ref_raster_fp = tgp.get_testing_fp('satellite_tif') # get the geoinfo from the raster
    >>> src_vector_fp = tgp.get_testing_fp('satellite_tif_clipper') # read source shapefile as GeoDataFrame
    >>> src_vector = gpd.read_file(src_vector_fp)
    >>> src_vector['FEATURE'] = 1 # make the value to fill in the raster cell
    >>> rows, cols, geo_transform = tgp.get_raster_info(ref_raster_fp, ['rows', 'cols', 'geo_transform'])
    >>> raster = ShapeGrid.rasterize_layer(src_vector, rows, cols, geo_transform, use_attribute='FEATURE', no_data_value=99)
    >>> fig, (ax1, ax2) = plt.subplots(1,2) # plot the result
    >>> tgp.read_raster(ref_raster_fp).plot(ax=ax1)
    >>> src_vector.plot(ax=ax1)
    >>> ax1.set_title('polygon with ref_raster')
    >>> raster.plot(ax=ax2)
    >>> ax2.set_title('rasterized image')
    >>> plt.show()
    """
    # Open your shapefile
    assert type(
        src_vector
    ) is gpd.GeoDataFrame, "src_vector should be GeoDataFrame type."
    assert use_attribute in src_vector.columns, "attribute not exists in src_vector."
    gdaldtype = tgp.npdtype_to_gdaldtype(src_vector[use_attribute].dtype)
    # projection = src_vector.crs.to_wkt() if src_vector.crs is not None else None
    projection = pyproj.CRS(
        src_vector.crs).to_wkt() if src_vector.crs is not None else None
    src_shp_ds = ogr.Open(src_vector.to_json())
    src_shp_layer = src_shp_ds.GetLayer()

    # Create the destination raster data source
    ds = tgp.write_gdal_ds(bands=1,
                           cols=cols,
                           rows=rows,
                           geo_transform=geo_transform,
                           gdaldtype=gdaldtype,
                           no_data_value=no_data_value)

    # set it to the attribute that contains the relevant unique
    options = ["ATTRIBUTE=" + use_attribute]
    if all_touched:
        options.append('ALL_TOUCHED=TRUE')
    gdal.RasterizeLayer(
        ds, [1], src_shp_layer, options=options
    )  # target_ds, band_list, source_layer, options = options

    data = ds.GetRasterBand(1).ReadAsArray()
    raster = tgp.Raster(data, geo_transform, projection, gdaldtype,
                        no_data_value)
    return raster