def OpenArray(array, prototype_ds=None, xoff=0, yoff=0): ds = gdal.Open(gdalnumeric.GetArrayFilename(array)) if ds is not None and prototype_ds is not None: if type(prototype_ds).__name__ == 'str': prototype_ds = gdal.Open(prototype_ds) if prototype_ds is not None: gdalnumeric.CopyDatasetInfo(prototype_ds, ds, xoff=xoff, yoff=yoff) return ds
def open_array(array, prototype_ds=None, xoff=0, yoff=0): from osgeo import gdal_array ds = gdal_array.OpenArray(array) if ds is not None and prototype_ds is not None: if type(prototype_ds).__name__ == 'str': prototype_ds = gdalnumeric.gdal.Open(prototype_ds) if prototype_ds is not None: gdalnumeric.CopyDatasetInfo(prototype_ds, ds, xoff=xoff, yoff=yoff) return ds
def make_shp(imgname): STATIC_FOLDER = '/app/app/static/' app.config['STATIC_FOLDER'] = STATIC_FOLDER origname = STATIC_FOLDER + imgname + '.tif' alignedname = STATIC_FOLDER + imgname + '_blackalign.png' alignedname_tif = STATIC_FOLDER + imgname + '_blackalign.tif' zipname = STATIC_FOLDER + imgname + '.zip' ziplist = [ STATIC_FOLDER + imgname + '_shape.dbf', STATIC_FOLDER + imgname + '_shape.prj', STATIC_FOLDER + imgname + '_shape.shp', STATIC_FOLDER + imgname + '_shape.shx' ] source = gdal.Open(origname) srcbnd = np.array(source.GetRasterBand(1).ReadAsArray()) alignments = gdal.Open(alignedname) alignband = np.array(alignments.GetRasterBand(1).ReadAsArray()) dst_ds = gdal.GetDriverByName('GTiff').Create(alignedname_tif, srcbnd.shape[1], srcbnd.shape[0], 3, gdal.GDT_Byte, options=[ 'PHOTOMETRIC=RGB', 'PROFILE=GeoTIFF', ]) gdalnumeric.CopyDatasetInfo(source, dst_ds) dst_ds.GetRasterBand(1).WriteArray(alignband) dst_ds.FlushCache() src_ds = dst_ds proj = osr.SpatialReference(wkt=src_ds.GetProjection()) dst_layername = imgname + "_shape" srcband = src_ds.GetRasterBand(1) drv = ogr.GetDriverByName("ESRI Shapefile") dst_ds = drv.CreateDataSource(STATIC_FOLDER + dst_layername + ".shp") dst_layer = dst_ds.CreateLayer(dst_layername, proj, geom_type=ogr.wkbMultiLineString) gdal.Polygonize(srcband, None, dst_layer, -1, [], callback=None) dst_ds.FlushCache() with ZipFile(zipname, 'w') as zip: for file in ziplist: zip.write(file)
def array_to_raster_clone(a, proto, xoff=None, yoff=None): ''' Creates a raster from a given array and a prototype raster dataset, with optional x- and y-offsets if the array was clipped. Arguments: a A NumPy array proto A prototype dataset xoff The offset in the x-direction; should be provided when clipped yoff The offset in the y-direction; should be provided when clipped ''' rast = gdal_array.OpenNumPyArray(a) kwargs = dict() if xoff is not None and yoff is not None: kwargs = dict(xoff=xoff, yoff=yoff) # Copy the projection info and metadata from a prototype dataset if type(proto) == str: proto = gdal.Open(proto) gdalnumeric.CopyDatasetInfo(proto, rast, **kwargs) return rast
def writeRasterFileFromNumpy(self, pOutNumpy_, outFileName_, pInitRasterFile_): """ Program description: INPUT_PARAMETERS: inputValue_ - COMMENTS: """ #Write Gdal file from numpy array outRasterFileName = tkFileDialog.asksaveasfilename( defaultextension='TIFF', filetypes=[('TIFF', '*.tif')], initialdir=self.workspace, initialfile=outFileName_, parent=tkRoot, title='Save output raster file (GDAL)') #!!! Only for TIFF, one Numpy band and Float data implemented here pDriver = gdal.GetDriverByName("GTiff") pOutRasterDataset = pDriver.Create( outRasterFileName, pInitRasterFile_.RasterXSize, pInitRasterFile_.RasterYSize, 1, GDT_Float32) #New file, settings from original file gdalnumeric.CopyDatasetInfo( pInitRasterFile_, pOutRasterDataset ) #Copy metadata information from original file to new file gdalnumeric.BandWriteArray(pOutRasterDataset.GetRasterBand(1), pOutNumpy_) # Copy numpy-data to new file #Close datasets pOutRasterDataset = None return
def main(raster_input, raster_output, extent, nodata=0): """ This function will subset a raster by a vector polygon. Adapted from the GDAL/OGR Python Cookbook at https://pcjericks.github.io/py-gdalogr-cookbook :param raster_input: raster input filepath :param raster_output: raster output filepath :param polygon_json: polygon as geojson string :param nodata: nodata value for output raster file :return: GDAL Dataset """ def world_to_pixel(geoMatrix, x, y): """ Uses a gdal geomatrix (gdal.GetGeoTransform()) to calculate the pixel location of a geospatial coordinate """ ulX = geoMatrix[0] ulY = geoMatrix[3] xDist = geoMatrix[1] pixel = int((x - ulX) / xDist) line = int((ulY - y) / xDist) return (pixel, line) src_image = get_dataset(raster_input) # Load the source data as a gdalnumeric array src_array = src_image.ReadAsArray() src_dtype = src_array.dtype # Also load as a gdal image to get geotransform # (world file) info geo_trans = src_image.GetGeoTransform() nodata_values = [] for i in range(src_image.RasterCount): nodata_value = src_image.GetRasterBand(i + 1).GetNoDataValue() if not nodata_value: nodata_value = nodata nodata_values.append(nodata_value) # Convert the layer extent to image pixel coordinates min_x, max_x, min_y, max_y = extent ul_x, ul_y = world_to_pixel(geo_trans, min_x, max_y) lr_x, lr_y = world_to_pixel(geo_trans, max_x, min_y) bands = src_image.RasterCount if bands > 1: clip = src_array[:, ul_y:lr_y, ul_x:lr_x] else: clip = src_array[ul_y:lr_y, ul_x:lr_x] # create pixel offset to pass to new image Projection info xoffset = ul_x yoffset = ul_y # Create a new geomatrix for the image geo_trans = list(geo_trans) geo_trans[0] = min_x geo_trans[3] = max_y # create output raster raster_band = src_image.GetRasterBand(1) output_driver = gdal.GetDriverByName('MEM') if bands > 1: output_dataset = output_driver.Create('', clip.shape[2], clip.shape[1], src_image.RasterCount, raster_band.DataType) else: output_dataset = output_driver.Create('', clip.shape[1], clip.shape[0], src_image.RasterCount, raster_band.DataType) output_dataset.SetGeoTransform(geo_trans) output_dataset.SetProjection(src_image.GetProjection()) gdalnumeric.CopyDatasetInfo(src_image, output_dataset, xoff=xoffset, yoff=yoffset) if bands > 1: for i in range(bands): outBand = output_dataset.GetRasterBand(i + 1) outBand.SetNoDataValue(nodata_values[i]) outBand.WriteArray(clip[i]) else: outBand = output_dataset.GetRasterBand(1) outBand.SetNoDataValue(nodata_values[0]) outBand.WriteArray(clip) if raster_output: output_driver = gdal.GetDriverByName('GTiff') outfile = output_driver.CreateCopy(raster_output, output_dataset, False) outfile = None return output_dataset
def write_tif(self, outname, data2write= False, dtype = 1, nodata = False, option = 'COMPRESS=DEFLATE'): '''dtype: .... if nodata set to True the orig nodata value will be assigned to the raster, if it is not a double ( in this case no nodata will be assigned) write_tif(outname, data2write=False, dtype=1, nodata=False, option='Compress=Deflate') dtypes: 0 --> Int16 1 --> Int32 2 --> UIit16 3 --> UInt32 4 --> Float32 5 --> Float64 6 --> UInt8 ''' dtypeL = [zz_gdalcon.GDT_Int16, zz_gdalcon.GDT_Int32, zz_gdalcon.GDT_UInt16, zz_gdalcon.GDT_UInt32, zz_gdalcon.GDT_Float32, zz_gdalcon.GDT_Float64, zz_gdalcon.GDT_Byte] try: if type(data2write) != type(self.data): data2write = self.data if len(data2write.shape)==3: nr_of_bands = data2write.shape[0] elif len(data2write.shape)==2: nr_of_bands = 1 else: raise NameError('ERROR: in Number of Bands') if not 'int' in str(np.result_type(data2write)): dataOut = self.driver.Create(outname, self.columns, self.rows, nr_of_bands, dtypeL[dtype]) else: dataOut = self.driver.Create(outname, self.columns, self.rows, nr_of_bands, dtypeL[dtype], options=[option]) #dataOut.SetGeoTransform(self.intif.GetGeoTransform()) zz_gdalnum.CopyDatasetInfo(self.intif, dataOut) for band in range(nr_of_bands): bandOut = dataOut.GetRasterBand(band+1) if nodata: #test if nodate can be set if isinstance(nodata, bool) and 'e' not in str(self.nodata): nodata = self.nodata if 'e' in str(nodata): nodata = False if nodata: bandOut.SetNoDataValue(nodata) if nr_of_bands==1: zz_gdalnum.BandWriteArray(bandOut,data2write) else: zz_gdalnum.BandWriteArray(bandOut,data2write[band,:,:]) bandOut = None dataOut = None #print 0 except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except ValueError: print ("Could not write the nodata value") except: print "Unexpected error:", sys.exc_info()
def write_tif(file_with_srid,full_output_name, data, dtype= 1, nodata=None, option=False ): ''' write data to tif >>> write_tif(file_with_srid, full_output_name, data, 1, nodata=None, option=False) file_wite_srid --> the original file with spatial infromations full_output_name --> path + filename + tile type e.g.: r'c:\\temp\\file1.tif' data --> data you want to write to tif dtype --> Output data type (int, float ...) input number between 0 and 5: - 0 --> Int16 - 1 --> Int32 - 2 --> UInt16 - 3 --> UInt32 - 4 --> Float32 - 5 --> Float64 - 6 --> UInt8 --> default is Int32 nodata --> by default there will be no NoData Value asigned if True: it will put the max Value for Unsigned Integers it will put the min Value for signed Integers and floats if you put a Value --> this Value will be the NoData Value option --> "COMPRESS=DEFLATE" if data is a 3d array it will write all bands to the tif (in single bands) ''' dtypeL = [zz_gdalcon.GDT_Int16, zz_gdalcon.GDT_Int32, zz_gdalcon.GDT_UInt16, zz_gdalcon.GDT_UInt32, zz_gdalcon.GDT_Float32, zz_gdalcon.GDT_Float64, zz_gdalcon.GDT_Byte] try: inTiff, driver, inCols, inRows = read_tif_info(file_with_srid) if len(data.shape)==3: nr_of_bands = data.shape[0] elif len(data.shape)==2: nr_of_bands = 1 else: print('error in Bands') sys.exit(1) #print(nr_of_bands) if option: dataOut = driver.Create(full_output_name,inCols,inRows,nr_of_bands, dtypeL[dtype],options=[option]) else: dataOut = driver.Create(full_output_name,inCols,inRows,nr_of_bands, dtypeL[dtype]) zz_gdalnum.CopyDatasetInfo(inTiff,dataOut) for band in range(nr_of_bands): bandOut = dataOut.GetRasterBand(band+1) if nodata is not None: bandOut.SetNoDataValue(nodata) if nr_of_bands==1: zz_gdalnum.BandWriteArray(bandOut,data) else: zz_gdalnum.BandWriteArray(bandOut,data[band,:,:]) bandOut = None dataOut = None except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror) except ValueError: print ("Could not write the nodata value") except: print "Unexpected error:", sys.exc_info()
def compare ( TET_tem_path, SST_tem_path, outputFile ): if os.path.exists(outputFile) == False: os.mkdir(outputFile) elif os.path.exists(outputFile) == True: return os.chdir(outputFile) TET_tem = gdal.Open(TET_tem_path) SST_tem = gdal.Open(SST_tem_path) MIR_band = TET_tem.GetRasterBand(1) TIR_band = TET_tem.GetRasterBand(2) SST_band = SST_tem.GetRasterBand(1) MIR_data = MIR_band.ReadAsArray() TIR_data = TIR_band.ReadAsArray() SST_data = SST_band.ReadAsArray() default_tem = 274.15 * np.ones(SST_data.shape) # MIR_deg = MIR_data + default_tem # TIR_deg = TIR_data + default_tem SST_K = SST_data + default_tem diff_MIR = MIR_data - SST_K diff_TIR = TIR_data - SST_K logic1 = np.where(np.absolute(diff_MIR)>50) diff_MIR[logic1] = 0 logic2 = np.where(np.absolute(diff_TIR)>50) diff_TIR[logic2] = 0 diff_MIR_max = diff_MIR.max() diff_TIR_max = diff_TIR.max() diff_MIR_min = diff_MIR.min() diff_TIR_min = diff_TIR.min() diff_MIR_mean = diff_MIR.mean() diff_TIR_mean = diff_TIR.mean() diff_MIR_std = diff_MIR.std() diff_TIR_std = diff_TIR.std() diff_MIR_median = np.median(diff_MIR[diff_MIR.nonzero()]) diff_TIR_median = np.median(diff_TIR[diff_TIR.nonzero()]) abs_diff_MIR = np.absolute(diff_MIR) abs_diff_TIR = np.absolute(diff_TIR) abs_diff_MIR_mean = abs_diff_MIR.mean() abs_diff_TIR_mean = abs_diff_TIR.mean() abs_diff_MIR_std = abs_diff_MIR.std() abs_diff_TIR_std = abs_diff_TIR.std() f = open( os.path.join(outputFile, 'statistics_info.txt'), 'w' ) f.write('Difference between SST and TET temperature.\n') f.write('MIR band.\tUnit: degrees Celsius.\n') f.write('Maximum: %s.\nMinimum: %s.\n' %(str(diff_MIR_max), str(diff_MIR_min))) f.write('Mean: %s.\nStandard Deviation: %s.\nMedian Value: %s.\n' %(str(diff_MIR_mean), str(diff_MIR_std), str(diff_MIR_median))) f.write('Absolute Mean: %s.\nAbsolute Standard Deviation: %s.\n\n' %(str(abs_diff_MIR_mean), str(abs_diff_MIR_std))) f.write('Difference between SST and TET temperature.\n') f.write('TIR band.\tUnit: degrees Celsius.\n') f.write('Maximum: %s.\nMinimum: %s.\n' %(str(diff_TIR_max), str(diff_TIR_min))) f.write('Mean: %s.\nStandard Deviation: %s.\nMedian Value: %s.\n' %(str(diff_TIR_mean), str(diff_TIR_std), str(diff_TIR_median))) f.write('Absolute Mean: %s.\nAbsolute Standard Deviation: %s.\n' %(str(abs_diff_TIR_mean), str(abs_diff_TIR_std))) f.close() driver = gdal.GetDriverByName('GTiff') MIR_out = driver.Create('diff_MIR.tif', TET_tem.RasterXSize, TET_tem.RasterYSize, 1, MIR_band.DataType) gdalnumeric.CopyDatasetInfo(TET_tem, MIR_out) MIR_bandOut = MIR_out.GetRasterBand(1) MIR_bandOut.SetNoDataValue(0.0) gdalnumeric.BandWriteArray(MIR_bandOut, diff_MIR) TIR_out = driver.Create('diff_TIR.tif', TET_tem.RasterXSize, TET_tem.RasterYSize, 1, TIR_band.DataType) gdalnumeric.CopyDatasetInfo(TET_tem, TIR_out) TIR_bandOut = TIR_out.GetRasterBand(1) TIR_bandOut.SetNoDataValue(0.0) gdalnumeric.BandWriteArray(TIR_bandOut, diff_TIR) print 'end'