def listTileEntities(raster, outpath, feature): """ entities ID list of tile in : raster : bi-band raster (classification - clump) outpath : out directory feature : feature of tile from shapefile out : tile_id : list with ID """ # Classification and Clump opening datas_classif, xsize_classif, ysize_classif, projection_classif, transform_classif = fu.readRaster(raster, True, 1) datas_clump, xsize_clump, ysize_clump, projection_clump, transform_clump = fu.readRaster(raster, True, 2) # Generate pixel coordinates of square feature corresponding to raster transform cols_xmin_decoup, cols_xmax_decoup, cols_ymin_decoup, cols_ymax_decoup = cellCoords(feature, transform_classif) # subset raster data array based on feature coordinates tile_classif = datas_classif[cols_ymin_decoup:cols_ymax_decoup, cols_xmin_decoup:cols_xmax_decoup] tile_id_all = datas_clump[cols_ymin_decoup:cols_ymax_decoup, cols_xmin_decoup:cols_xmax_decoup] del datas_classif, datas_clump # entities ID list of tile (except nodata and sea) tile_id = np.unique(np.where(((tile_classif > 1) & (tile_classif < 250)), tile_id_all, 0)).tolist() # delete 0 value tile_id = [int(x) for x in tile_id if x != 0] return tile_id
def storeRasterInArray(rasters): """Store list of raster or multi-band raster in a ndarray Parameters ---------- rasters : list list of rasters to analyse Return ---------- ndarray """ # get raster size with first raster file data = fut.readRaster(rasters[0], False) # get rasters or bands number if len(rasters) == 1: nbbands = fut.getRasterNbands(rasters[0]) elif len(rasters) > 1: nbbands = len(rasters) # Set a empty ndarrays with same dimension as input rasters outdata = np.zeros([data[1], data[0], nbbands]) # Populate output ndarrays if len(rasters) == 1: for nbband in range(nbbands): outdata[:, :, nbband] = fut.readRaster(rasters[0], True, nbband + 1)[0] elif len(rasters) > 1: for idx, raster in enumerate(rasters): outdata[:, :, idx] = fut.readRaster(raster, True)[0] else: raise Exception("No input raster provided to store in Numpy array") return outdata
def grid_generate(outname, xysize, epsg=2154, raster=None, coordinates=None): """ Grid generation from raster. in : outname : out name of grid raster : raster name xysize : coefficient for tile size out : shapefile """ if raster is not None: xsize, ysize, projection, transform = fut.readRaster(raster, False, 1) xmin = float(transform[0]) xmax = float((transform[0] + transform[1] * xsize)) ymin = float((transform[3] + transform[5] * ysize)) ymax = float(transform[3]) else: xmin = float(coordinates[0]) xmax = float(coordinates[1]) ymin = float(coordinates[2]) ymax = float(coordinates[3]) xSize = (xmax - xmin) / xysize intervalX = np.arange(xmin, xmax + xSize, xSize) ySize = (ymax - ymin) / xysize intervalY = np.arange(ymin, ymax + ySize, ySize) # create output file createPolygonShape(outname, epsg, 'ESRI Shapefile') driver = ogr.GetDriverByName("ESRI Shapefile") shape = driver.Open(outname, 1) outLayer = shape.GetLayer() featureDefn = outLayer.GetLayerDefn() # create grid cel countcols = 0 while countcols < len(intervalX) - 1: countrows = 0 while countrows < len(intervalY) - 1: # create vertex ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(intervalX[countcols], intervalY[countrows]) ring.AddPoint(intervalX[countcols], intervalY[countrows + 1]) ring.AddPoint(intervalX[countcols + 1], intervalY[countrows + 1]) ring.AddPoint(intervalX[countcols + 1], intervalY[countrows]) ring.AddPoint(intervalX[countcols], intervalY[countrows]) poly = ogr.Geometry(ogr.wkbPolygon) poly.AddGeometry(ring) # add new geom to layer outFeature = ogr.Feature(featureDefn) outFeature.SetGeometry(poly) outLayer.CreateFeature(outFeature) outFeature.Destroy ymin = intervalY[countrows] countrows += 1 xmin = intervalX[countcols] countcols += 1 # Close DataSources shape.Destroy() print("Grid has %s tiles" % (int(xysize * xysize))) return int(xysize * xysize)