def rst_to_geodf(in_rst): """ Raster To GeoDataframe """ from osgeo import gdal import numpy as np import pandas as pd from gasp.pyt.df.mng import dfcolstorows from gasp.g.to import pnt_dfwxy_to_geodf from gasp.gt.prop.prj import get_rst_epsg src = gdal.Open(in_rst) num = src.ReadAsArray() ndval = src.GetRasterBand(1).GetNoDataValue() left, cellx, z, top, c, celly = src.GetGeoTransform() numdf = pd.DataFrame(num) numdf['idx'] = numdf.index res = dfcolstorows(numdf, 'col', 'val', colFid='idx') res = res[res.val != ndval] res['x'] = (left + (cellx / 2)) + (cellx * res.col) res['y'] = (top + (celly / 2)) + (celly * res.idx) res.drop(['col', 'idx'], axis=1, inplace=True) res.rename(columns={'val' : 'Value'}, inplace=True) geodf = pnt_dfwxy_to_geodf(res, 'x', 'y', get_rst_epsg(in_rst)) return geodf
def rstext_to_shp(inRst, outShp, epsg=None): """ Raster Extent to Feature Class """ from gasp.gt.prop.rst import rst_ext # Get Raster Extent left, right, bottom, top = rst_ext(inRst) # Get EPSG if not epsg: from gasp.gt.prop.prj import get_rst_epsg epsg = get_rst_epsg(inRst) # Create Boundary return coords_to_boundshp((left, top), (right, bottom), epsg, outShp)
def comp_bnds(rsts, outRst): """ Composite Bands """ from osgeo import gdal, gdal_array from gasp.gt.fmrst import rst_to_array from gasp.gt.prop.ff import drv_name from gasp.gt.prop.rst import get_nodata from gasp.gt.prop.prj import get_rst_epsg, epsg_to_wkt # Get Arrays _as = [rst_to_array(r) for r in rsts] # Get nodata values nds = [get_nodata(r) for r in rsts] # Assume that first raster is the template img_temp = gdal.Open(rsts[0]) geo_tran = img_temp.GetGeoTransform() band = img_temp.GetRasterBand(1) dataType = gdal_array.NumericTypeCodeToGDALTypeCode(_as[0].dtype) rows, cols = _as[0].shape epsg = get_rst_epsg(rsts[0]) # Create Output drv = gdal.GetDriverByName(drv_name(outRst)) out = drv.Create(outRst, cols, rows, len(_as), dataType) out.SetGeoTransform(geo_tran) out.SetProjection(epsg_to_wkt(epsg)) # Write all bands for i in range(len(_as)): outBand = out.GetRasterBand(i + 1) outBand.SetNoDataValue(nds[i]) outBand.WriteArray(_as[i]) outBand.FlushCache() return outRst
def match_cellsize_and_clip(rstBands, refRaster, outFolder, clipShp=None): """ Resample images to make them with the same resolution and clip Good to resample Sentinel bands with more than 10 meters. Dependencies: * GRASS GIS; * GDAL/OGR. """ import os from gasp.gt.prop.prj import get_rst_epsg from gasp.gt.wenv.grs import run_grass from gasp.pyt.oss import fprop, mkdir # Check if outfolder exists if not os.path.exists(outFolder): mkdir(outFolder, overwrite=None) # Get EPSG from refRaster epsg = get_rst_epsg(refRaster, returnIsProj=None) """ Start GRASS GIS Session """ GRS_WORKSPACE = mkdir(os.path.join(outFolder, 'grswork')) grsb = run_grass( GRS_WORKSPACE, grassBIN='grass78', location='resample', srs=epsg ) import grass.script as grass import grass.script.setup as gsetup gsetup.init(grsb, GRS_WORKSPACE, 'resample', 'PERMANENT') """ Import packages related with GRASS GIS """ from gasp.gt.torst import rst_to_grs, grs_to_rst from gasp.gt.wenv.grs import rst_to_region from gasp.gt.toshp.cff import shp_to_grs from gasp.gt.torst import shp_to_rst, grs_to_mask # Send Ref Raster to GRASS GIS and set region extRst = rst_to_grs(refRaster, 'ext_rst') rst_to_region(extRst) # Import all bands in rstBands grs_bands = [rst_to_grs(i, fprop(i, 'fn')) for i in rstBands] if clipShp: # Add clipShp to GRASS grs_clip = shp_to_grs(clipShp, fprop(clipShp, 'fn'), asCMD=True) # SHP to Raster rstClip = shp_to_rst( grs_clip, 1, None, 0, 'rst_' + grs_clip, api='grass' ) # Set region using rst_to_region(rstClip) # Set mask grs_to_mask(rstClip) # Export bands return [grs_to_rst( i, os.path.join(outFolder, i + '.tif') ) for i in grs_bands]
def bnds_to_mosaic(bands, outdata, ref_raster, loc=None): """ Satellite image To mosaic bands = { 'bnd_2' : [path_to_file, path_to_file], 'bnd_3' : [path_to_file, path_to_file], 'bnd_4' : [path_to_file, path_to_file], } """ """ Start GRASS GIS Session """ import os from gasp.pyt.oss import fprop from gasp.gt.prop.prj import get_rst_epsg from gasp.gt.wenv.grs import run_grass # Get EPSG from refRaster epsg = get_rst_epsg(ref_raster, returnIsProj=None) LOC = loc if loc else 'gr_loc' grass_base = run_grass( outdata, grassBIN='grass78', location=LOC, srs=epsg ) import grass.script as grass import grass.script.setup as gsetup gsetup.init(grass_base, outdata, LOC, 'PERMANENT') # ************************************************************************ # # GRASS MODULES # # ************************************************************************ # from gasp.gt.torst import rst_to_grs, grs_to_rst from gasp.gt.wenv.grs import rst_to_region # ************************************************************************ # # SET GRASS GIS LOCATION EXTENT # # ************************************************************************ # extRst = rst_to_grs(ref_raster, 'extent_raster') rst_to_region(extRst) # ************************************************************************ # # SEND DATA TO GRASS GIS # # ************************************************************************ # grs_bnds = {} for bnd in bands: l= [] for b in bands[bnd]: bb = rst_to_grs(b, fprop(b, 'fn')) l.append(bb) grs_bnds[bnd] = l # ************************************************************************ # # PATCH bands and export # # ************************************************************************ # for bnd in grs_bnds: mosaic_band = rseries(grs_bnds[bnd], bnd, 'maximum') grs_bnds[bnd] = grs_to_rst(mosaic_band, os.path.join( outdata, mosaic_band + '.tif' ), as_cmd=True) return grs_bnds
def get_ref_raster(refBoundBox, folder, cellsize=None): """ Get Reference Raster """ import os from gasp.gt.prop.ff import check_isRaster # Check if refRaster is really a Raster isRst = check_isRaster(refBoundBox) if not isRst: from gasp.gt.prop.ff import check_isShp if not check_isShp(refBoundBox): raise ValueError(( 'refRaster File has an invalid file format. Please give a file ' 'with one of the following extensions: ' 'shp, gml, json, kml, tif or img' )) else: # We have a shapefile # Check SRS and see if it is a projected SRS from gasp.gt.prop.prj import get_epsg_shp epsg, isProj = get_epsg_shp(refBoundBox, returnIsProj=True) if not epsg: raise ValueError('Cannot get epsg code from {}'.format(refBoundBox)) if not isProj: # A conversion between SRS is needed from gasp.gt.prj import proj ref_shp = proj( refBoundBox, os.path.join(folder, 'tmp_ref_shp.shp'), outEPSG=3857, inEPSG=epsg, gisApi='ogr2ogr' ) epsg = 3857 else: ref_shp = refBoundBox # Convert to Raster from gasp.gt.torst import shp_to_rst refRaster = shp_to_rst( ref_shp, None, 2 if not cellsize else cellsize, -1, os.path.join(folder, 'ref_raster.tif'), api='gdal' ) else: # We have a raster from gasp.gt.prop.prj import get_rst_epsg epsg, isProj = get_rst_epsg(refBoundBox, returnIsProj=True) if not epsg: raise ValueError('Cannot get epsg code from {}'.format(refBoundBox)) # Check if Raster has a SRS with projected coordinates if not isProj: # We need to reproject raster from gasp.gt.prj import reprj_rst refRaster = reprj_rst( refBoundBox, os.path.join(folder, 'refrst_3857.tif'), epsg, 3857 ) epsg = 3857 else: refRaster = refBoundBox return refRaster, epsg