コード例 #1
0
def get_flowgrid(catchment_geom, transformToRaster, transformToWGS84):
    """Use a 90 meter buffer of the local catchment to clip NHD Plus v2 flow direction raster"""

    print('start clip raster')
    with rasterio.open(IN_FDR_COG, 'r') as ds:

        # get raster crs
        dest_crs = ds.crs

        # create wgs84 crs
        wgs84 = pyproj.CRS('EPSG:4326')

        # check to see if raster is already wgs84
        latlon = dest_crs == wgs84

        # transform catchment geometry to use for clip
        projected_catchment_geom = transform_geom(transformToRaster,
                                                  catchment_geom)

        # buffer catchment geometry by 90m before clipping flow direction raster
        buffer_projected_catchment_geom = GeometryCollection(
            [projected_catchment_geom.buffer(90)])

        # clip input fd
        flwdir, flwdir_transform = rasterio.mask.mask(
            ds, buffer_projected_catchment_geom, crop=True)
        print('finish clip raster')

    # import clipped fdr into pyflwdir
    flw = pyflwdir.from_array(flwdir[0],
                              ftype='d8',
                              transform=flwdir_transform,
                              latlon=latlon)

    return flw, flwdir_transform
コード例 #2
0
def get_pfafstetter_code(dir_d8_path, acc_path, pfaf_path, stream_th, level=1):
    with rasterio.open(dir_d8_path, 'r') as src:
        flwdir = src.read(1)
        transform = src.transform
        latlon = True

        flw = pyflwdir.from_array(flwdir,
                                  ftype='d8',
                                  transform=transform,
                                  latlon=latlon)

        # upstream_cells = flw.upstream_area()
        with rasterio.open(acc_path, 'r') as acc_f:
            upstream_cells = acc_f.read(1)
            crs_r = crs.CRS.from_wkt(
                'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,'
                'AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,'
                'AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG",'
                '"9122"]],AUTHORITY["EPSG","4326"]]')
            pfaf = flw.pfafstetter(level, upstream_cells, stream_th)

            profile = src.profile
            profile.update(dtype=rasterio.uint32,
                           count=1,
                           compress='deflate',
                           crs=crs_r)
            with rasterio.open(pfaf_path, 'w', **profile) as dst:
                dst.write(pfaf, 1)
コード例 #3
0
def split_catchment(catchment_geom, projected_xy, transformToRaster,
                    transformToWGS84):
    """Produce split catchment delienation from X,Y"""

    print('start split catchment...')

    with rasterio.open(IN_FDR_COG, 'r') as ds:

        # get raster crs
        dest_crs = ds.crs

        # create wgs84 crs
        wgs84 = pyproj.CRS('EPSG:4326')

        # check to see if raster is already wgs84
        latlon = dest_crs == wgs84

        # transform catchment geometry to use for clip
        projected_catchment_geom = transform_geom(transformToRaster,
                                                  catchment_geom)

        # buffer catchment geometry by 0m before clipping flow direction raster
        buffer_projected_catchment_geom = GeometryCollection(
            [projected_catchment_geom.buffer(0)])

        # clip input fd
        flwdir, flwdir_transform = rasterio.mask.mask(
            ds, buffer_projected_catchment_geom, crop=True)
        print('finish clip raster')

    # import clipped fdr into pyflwdir
    flw = pyflwdir.from_array(flwdir[0],
                              ftype='d8',
                              transform=flwdir_transform,
                              latlon=latlon)

    # used for snapping click point
    # stream_order = flw.stream_order()

    # delineate subbasins
    subbasins = flw.basins(xy=projected_xy)  # streams=stream_order>4

    # convert subbasins from uint32
    subbasins = subbasins.astype(np.int32)

    # convert raster to features
    mask = subbasins != 0
    polys = rasterio.features.shapes(subbasins,
                                     transform=flwdir_transform,
                                     mask=mask)

    # just get one we want [not sure why we need to grab this]
    poly = next(polys)

    # project back to wgs84
    split_geom = transform(transformToWGS84, shape(poly[0]))

    print('finish split catchment...')
    return split_geom
コード例 #4
0
    def split_catchment(self, catchment_geom, x, y):
        """Use catchment bounding box to clip NHD Plus v2 flow direction raster, and product split catchment delienation from X,Y"""

        print('start clip raster')
        with rasterio.open(IN_FDR_COG, 'r') as ds:
            #get raster crs
            dest_crs = ds.crs

            #create wgs84 crs
            wgs84 = pyproj.CRS('EPSG:4326')

            #check to see if raster is already wgs84
            latlon = dest_crs == wgs84

            self.transformToRaster = pyproj.Transformer.from_crs(
                wgs84, dest_crs, always_xy=True).transform
            self.transformToWGS84 = pyproj.Transformer.from_crs(
                dest_crs, wgs84, always_xy=True).transform

            #transform catchment geometry to use for clip
            projected_catchment_geom = self.transform_geom(
                self.transformToRaster, catchment_geom)

            #clip input fd
            flwdir, flwdir_transform = rasterio.mask.mask(
                ds, projected_catchment_geom, crop=True)
            print('finish clip raster')

        #import clipped fdr into pyflwdir
        flw = pyflwdir.from_array(flwdir[0],
                                  ftype='d8',
                                  transform=flwdir_transform,
                                  latlon=latlon)

        point_geom = Point(self.x, self.y)
        print('original point:', point_geom)

        projected_point = transform(self.transformToRaster, point_geom)
        print('projected point:', projected_point)

        xy = projected_point.coords[:][0]

        #used for snapping click point
        stream_order = flw.stream_order()

        print('start split catchment...')

        # delineate subbasins
        subbasins = flw.basins(xy=xy, streams=stream_order > 4)

        #convert subbasins from uint32
        subbasins = subbasins.astype(np.int32)

        #convert raster to features
        mask = subbasins != 0
        polys = rasterio.features.shapes(subbasins,
                                         transform=flwdir_transform,
                                         mask=mask)

        #just get one we want [not sure why we need to grab this]
        poly = next(polys)

        #project back to wgs84
        split_geom = transform(self.transformToWGS84, shape(poly[0]))

        print('finish split catchment...')
        return split_geom