def loaddata(imgpath, use_bands): gdal.UseExceptions() gdal.AllRegister() # for b in range(img.shape[2]): img_ds = gdal.Open(imgpath, gdal.GA_ReadOnly) if use_bands: img = np.zeros( (img_ds.RasterYSize, img_ds.RasterXSize, len(use_bands)), gdal_array.GDALTypeCodeToNumericTypeCode( img_ds.GetRasterBand(1).DataType)) usebandnum = 0 for b in use_bands: img[:, :, usebandnum] = img_ds.GetRasterBand(b + 1).ReadAsArray() usebandnum += 1 else: img = np.zeros( (img_ds.RasterYSize, img_ds.RasterXSize, img_ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( img_ds.GetRasterBand(1).DataType)) for b in range(img.shape[2]): img[:, :, b] = img_ds.GetRasterBand(b + 1).ReadAsArray() # Take our full image, ignore the Fmask band, and reshape into long 2d array (nrow * ncol, nband) for classification new_shape = (img.shape[0] * img.shape[1], img.shape[2]) img_as_array = img.reshape(new_shape) # print('Reshaped from {o} to {n}'.format(o=img.shape, # n=img_as_array.shape)) img_shape = img[:, :, 0].shape return img_as_array, img_shape
def stack_to_obj(VHRstack): print("now: stack to object") img_ds = gdal.Open(VHRstack, gdal.GA_ReadOnly) # GDAL dataset gt = img_ds.GetGeoTransform() proj = img_ds.GetProjection() ncols = img_ds.RasterXSize nrows = img_ds.RasterYSize ndval = img_ds.GetRasterBand(1).GetNoDataValue() # should be -999 for all layers, unless using scene as input print('nd val:', ndval, ', type:', type(ndval)) # ndval = 0.00#float(ndval1) imgProperties = (gt, proj, ncols, nrows, ndval) print('imgProperties', imgProperties, '\n') """ Read data stack into array """ img = np.zeros((nrows, ncols, img_ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode(img_ds.GetRasterBand(1).DataType)) print('gdal_array.GDALTypeCodeToNumericTypeCode(img_ds.GetRasterBand(1).DataType):', gdal_array.GDALTypeCodeToNumericTypeCode(img_ds.GetRasterBand(1).DataType)) print('\n and raw data type is:', img_ds.GetRasterBand(1).DataType) for b in range(img.shape[2]): # the 3rd index of img.shape gives us the number of bands in the stack print('\nb: {}'.format(b)) img[:, :, b] = img_ds.GetRasterBand(b + 1).ReadAsArray() # .astype(np.float32) # GDAL is 1-based while Python is 0-based print('done reading stack into array') print('deleting img_ds') del img_ds return (img, imgProperties)
def populateResult(filename, slopes_p005): def process(band, yoff, cols): def getResult(): """ 0: NoData 1: CER 2: NEG 3: NONE 4: POS 5: ALT """ size = arryColSlope.size totalNanP005 = np.isnan(arryColP005).sum().item() totalNanSlope = np.isnan(arryColSlope).sum().item() if totalNanSlope == size: return 0 if totalNanP005 == size else 1 arry = arryColSlope[~np.isnan(arryColSlope)] # Remove NaN totalNeg = (arry < 0.0).sum().item() totalNone = (arry == 0.0).sum().item() totalPos = (arry > 0.0).sum().item() if (totalNeg + totalNanSlope) == size: return 2 if (totalNone + totalNanSlope) == size: return 3 if (totalPos + totalNanSlope) == size: return 4 return 5 rows = len(slopes_p005) size = (rows, cols) arrySlope = np.ndarray(size, dtype=dtype_in) arryP005 = np.ndarray(size, dtype=dtype_in) for idx in range(rows): arrySlope[idx] = slopes_p005[idx]['slope'].ReadAsArray( 0, yoff, win_ysize=1) # xoff, yoff, win_xsize, win_ysize arryP005[idx] = slopes_p005[idx]['p005'].ReadAsArray( 0, yoff, win_ysize=1) # xoff, yoff, win_xsize, win_ysize arryResult = np.ndarray((1, cols), dtype=dtype_out) for idy in range(cols): arryColSlope = arrySlope[:, idy] # Months arryColP005 = arryP005[:, idy] # Months arryResult[0, idy] = getResult() band.WriteArray(arryResult, 0, yoff) # xoff, yoff band.FlushCache() del arrySlope, arryP005, arryResult, arryColSlope, arryColP005 dsResult = createResultDS(slopes_p005[0]['ds'], filename) band = dsResult.GetRasterBand(1) dtype_in = gdal_array.GDALTypeCodeToNumericTypeCode(gdal.GDT_Float32) dtype_out = gdal_array.GDALTypeCodeToNumericTypeCode(gdal.GDT_Byte) xsize = dsResult.RasterXSize ysize = dsResult.RasterYSize for y in range(0, ysize): printProgressBar(y + 1, ysize, prefix=filename, suffix='Complete', length=50) process(band, y, xsize) dsResult = None
def loaddata(imgpath, use_bands): gdal.UseExceptions() gdal.AllRegister() img_ds = gdal.Open(imgpath, gdal.GA_ReadOnly) if use_bands: img = np.zeros( (img_ds.RasterYSize, img_ds.RasterXSize, len(use_bands)), gdal_array.GDALTypeCodeToNumericTypeCode( img_ds.GetRasterBand(1).DataType)) usebandnum = 0 for b in use_bands: img[:, :, usebandnum] = img_ds.GetRasterBand(b + 1).ReadAsArray() usebandnum += 1 else: img = np.zeros( (img_ds.RasterYSize, img_ds.RasterXSize, img_ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( img_ds.GetRasterBand(1).DataType)) for b in range(img.shape[2]): img[:, :, b] = img_ds.GetRasterBand(b + 1).ReadAsArray() new_shape = (img.shape[0] * img.shape[1], img.shape[2]) img_as_array = img.reshape(new_shape) img_shape = img[:, :, 0].shape return img_as_array, img_shape
def stretch(src, dst, stretch, bands, ndv, minmax, pct, _format, dtype, co, verbose): # Read input image try: ds = gdal.Open(src, gdal.GA_ReadOnly) except: click.echo("Could not open source dataset {0}".format(src)) raise driver = gdal.GetDriverByName(str(_format)) # If no output dtype selected, default to input image dtype if not dtype: dtype = gdal_array.GDALTypeCodeToNumericTypeCode( ds.GetRasterBand(1).DataType) dtype = np.dtype(dtype) gdal_dtype = gdal_array.NumericTypeCodeToGDALTypeCode(dtype) if not bands: nbands = ds.RasterCount bands = range(1, nbands + 1) else: nbands = len(bands) # Create output if _format.lower() in COPY_FORMATS: if nbands not in (1, 3, 4): raise click.BadParameter( 'JPEG/PNG images must have 1, 3, or 4 bands') mem_driver = gdal.GetDriverByName('MEM') out_ds = mem_driver.Create('', ds.RasterXSize, ds.RasterYSize, nbands, gdal_dtype) else: out_ds = driver.Create(dst, ds.RasterXSize, ds.RasterYSize, nbands, gdal_dtype) for idx, (b, _minmax) in enumerate(zip_longest(bands, minmax)): kwargs = dict(ndv=ndv, minmax=_minmax, percent=pct) in_band = ds.GetRasterBand(b) arr = in_band.ReadAsArray() arr, out_ndv = _STRETCH_FUNCS[stretch](arr, **kwargs) out_band = out_ds.GetRasterBand(idx + 1) out_band.WriteArray(arr) out_band.SetDescription(in_band.GetDescription()) out_band.SetNoDataValue(out_ndv) out_ds.SetMetadata(ds.GetMetadata()) out_ds.SetProjection(ds.GetProjection()) out_ds.SetGeoTransform(ds.GetGeoTransform()) if _format.lower() in COPY_FORMATS: _out_ds = driver.CreateCopy(dst, out_ds, 0, co) _out_ds = None ds = None out_ds = None if verbose: click.echo('Complete!')
def main(): int_tif = 'Z:/AKSeward/DEM/Lidar/2017_UAS/DEMs/DEM_Intensity_Rasters/9-17_top_allpts_0.2m.tif' ds = gdal.Open(int_tif) print(ds.GetMetadataItem('')) print(ds.RasterCount) for i in range(1, 1 + ds.RasterCount): print('Band Number: ', i) band = ds.GetRasterBand(i) arr = band.ReadAsArray() print(arr.size) arr = pd.DataFrame(arr) #arr.to_csv('Z:/JDann/Documents/Documents/arr.csv') arr[arr == 100.0] = np.nan print(arr.values.shape) print('MAX:', np.nanmax(arr.values)) print('Min:', np.nanmin(arr.values)) #find datatype to use in GDAL plotting image_datatype = ds.GetRasterBand(1).DataType #create empty array with pixel row,column,band values image = np.zeros( (ds.RasterYSize, ds.RasterXSize, ds.RasterCount + 1), dtype=gdal_array.GDALTypeCodeToNumericTypeCode(image_datatype)) # Read in the band's data into the third dimension of our array image[:, :, i] = arr [cols, rows] = arr.shape plt.imshow(image[:, :, i]) plt.colorbar() plt.show()
def _get_tile_data(self, tile: Tile, band: gdal.Band): ''' Loads the band data for the tile and converts it to the output data type. Returns a numpy array ''' band_data = np.array( band.ReadAsArray(tile.min_x, tile.min_y, tile.max_x - tile.min_x, tile.max_y - tile.min_y)) np_out_dt = gdal_array.GDALTypeCodeToNumericTypeCode( self.output_datatype) nodata = band.GetNoDataValue() if nodata is None: # convert the input data to the datatype we are going to use for # output band_data_converted = band_data.astype(np_out_dt) else: # conversion for data that includes nodata is done via a masked array # to prevent issues of converting a nodata value, losing some precision, # and having it not match the nodata value exactly (causing nodata to # not be nodata!) masked_band_data = np.ma.masked_where(band_data == nodata, band_data) masked_band_data_converted = masked_band_data.astype(np_out_dt) masked_band_data_converted.fill_value = self.output_nodata band_data_converted = masked_band_data_converted.filled() return band_data_converted
def envi_load(name: str, subset=False): address = './MATLAB/ENVI/' + name envi_ds = gdal.Open(address, gdal.GA_ReadOnly) envi = np.zeros( (envi_ds.RasterYSize, envi_ds.RasterXSize, envi_ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( envi_ds.GetRasterBand(1).DataType)) print(str(envi.shape[2]) + ' bands loaded from ' + name) for b in range(envi.shape[2]): envi[:, :, b] = envi_ds.GetRasterBand(b + 1).ReadAsArray() if subset: s1 = int(input('which n bands do you need? ')) s2 = int(input('which n bands do you need? ')) envi = envi[:, :, s1:s2] normal_envi = np.zeros_like(envi, np.float32) for i in range(envi.shape[2]): band = envi[:, :, i] minimum = np.nanmin(band) maximum = np.nanmax(band) normal_envi[:, :, i] = np.divide((band - minimum), (maximum - minimum)) print(normal_envi.shape) return normal_envi
def pcaRaster(raster): # Open up raster image and load into an n dimensional array where n is number of bands image = gdal.Open(raster) data = np.zeros((image.RasterYSize, image.RasterXSize, image.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( image.GetRasterBand(1).DataType)) for b in range(data.shape[2]): data[:, :, b] = image.GetRasterBand(b + 1).ReadAsArray() # Load nd-array into PCA class object pca = principal_components(data) # Print eigenvalues eigenvalues = pca.eigenvalues print eigenvalues # Reduce data array to where 99% of data variance is explained pcdata = pca.reduce(fraction=.99).transform(data) # Save data array into a GTiff raster rasterout = raster.split('.')[0] + '_PCAReduced.TIF' array2raster(pcdata, image, rasterout) return pcdata
def generate_empty_img(dataset): # Allocates the array using the first band's datatype image_datatype = dataset.GetRasterBand(1).DataType empty_img = np.zeros( (dataset.RasterYSize, dataset.RasterXSize, dataset.RasterCount), dtype=gdal_array.GDALTypeCodeToNumericTypeCode(image_datatype)) return empty_img
def getRasterProfile(self, filename): """Retrieve the meta information of an image using GDAL Args: filename: str file name to read from Returns: meta: dict returned meta data records about the image """ ds = gdal.Open(filename, gdal.GA_ReadOnly) if ds is None: sys.stderr.write("Failed to open the file {0:s}\n".format(filename)) return None ncol = ds.RasterXSize nrow = ds.RasterYSize nbands = ds.RasterCount geo_trans = ds.GetGeoTransform() proj_ref = ds.GetProjectionRef() metadata = {dm:ds.GetMetadata(dm) for dm in ds.GetMetadataDomainList()} bands = [ds.GetRasterBand(idx+1) for idx in range(nbands)] nodata = [self._img_nodata if b.GetNoDataValue() is None else b.GetNoDataValue() for b in bands] category_names = [b.GetCategoryNames() for b in bands] dtype = [gdal_array.GDALTypeCodeToNumericTypeCode(b.DataType) for b in bands] return {"RasterXSize":ncol, "RasterYSize":nrow, "RasterCount":nbands, "DataType":dtype, "NoDataValue":nodata, "CategoryNames":category_names, "GeoTransform":geo_trans, "ProjectionRef":proj_ref, "Metadata":metadata}
def tile_data(self, z, x, y, width=256, height=256): """ Fetch tile data by warping into a tile. TODO: resampling algo """ tile = mercantile.xy_bounds(x, y, z) ds = gdal.Warp('', self.ds, format='VRT', dstSRS='EPSG:3857', outputType=self.metadata['layers'][0].get("datatype"), width=width, height=height, outputBounds=(tile.left, tile.bottom, tile.right, tile.top)) dtype = gdal_array.GDALTypeCodeToNumericTypeCode(self.metadata['layers'][0].get("datatype")) bands = np.empty((256,256, ds.RasterCount), dtype=dtype) masks = np.empty((256,256, ds.RasterCount), dtype=dtype) for b in range(1, ds.RasterCount+1): band = ds.GetRasterBand(b).ReadAsArray() bands[:,:,b-1] = band mask = np.where(band == self.metadata['layers'][0].get("nodata"), 1, 0) masks[:,:,b-1] = mask data = ma.masked_array(bands, mask=masks) ds = None return (data, mask)
def readRasterSubset(imgf, lon, lat, out_imgsize, bands=None): csample, cline = geo2Pixel(imgf, lon, lat, ret_int=True) cx, cy = geo2Proj(imgf, lon, lat) ds = gdal.Open(imgf, gdal.GA_ReadOnly) geo_trans = ds.GetGeoTransform() out_nsample = int(out_imgsize / geo_trans[1]) + 1 out_nline = int(-1 * out_imgsize / geo_trans[5]) + 1 min_sample = csample - int(out_nsample * 0.5) min_line = cline - int(out_nline * 0.5) max_sample = min_sample + out_nsample max_line = min_line + out_nline min_sample = min_sample if min_sample > 0 else 0 min_line = min_line if min_line > 0 else 0 max_sample = max_sample if max_sample < ds.RasterXSize else ds.RasterXSize max_line = max_line if max_line < ds.RasterYSize else ds.RasterYSize if bands is None: bands = range(1, ds.RasterCount + 1) # band index starts from 0 as the first band. out_img = np.zeros( (max_line - min_line, max_sample - min_sample, len(bands)), dtype=gdal_array.GDALTypeCodeToNumericTypeCode( ds.GetRasterBand(1).DataType)) for i, ib in enumerate(bands): band = ds.GetRasterBand(ib) img = band.ReadAsArray() out_img[:, :, i] = img[min_line:max_line, min_sample:max_sample] return out_img # nrow, ncol, nband
def _read(self, filename, rasterBand, verbose=False): """ Private method for reading a GeoTIFF file. """ self.path, self.name = os.path.split(filename) if verbose: print('Reading GeoTIFF file: %s' % filename) sys.stdout.flush() src_ds = gdal.Open(filename) self._src_ds = src_ds band = src_ds.GetRasterBand(rasterBand) self.dtype = gdal_array.GDALTypeCodeToNumericTypeCode(band.DataType) self.nodata = band.GetNoDataValue() self.geotransform = src_ds.GetGeoTransform() self.w = self.geotransform[0] self.n = self.geotransform[3] self.dx = self.geotransform[1] self.dy = self.geotransform[5] self.nx = band.XSize self.ny = band.YSize self.e = self.w + (self.nx) * self.dx self.s = self.n + (self.ny) * self.dy self.extent = (self.w, self.e, self.s, self.n) self.proj4 = osr.SpatialReference(self._src_ds.GetProjection())\ .ExportToProj4() self.elev = band.ReadAsArray()
def ras2num(data): img = np.zeros((data.RasterYSize, data.RasterXSize, data.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( data.GetRasterBand(1).DataType)) for b in range(img.shape[2]): img[:, :, b] = data.GetRasterBand(b + 1).ReadAsArray() return img
def read_dsm_tif(file): assert os.path.exists(file) ds = gdal.Open(file) geo = ds.GetGeoTransform() proj = ds.GetProjection() meta = ds.GetMetadata() width = ds.RasterXSize height = ds.RasterYSize assert ds.RasterCount == 1 # dsm is only one band band = ds.GetRasterBand(1) # band index is one-based data_type = gdal_array.GDALTypeCodeToNumericTypeCode(band.DataType) assert isinstance(data_type(), np.float32) image = np.zeros((ds.RasterYSize, ds.RasterXSize), dtype=np.float32) image[:, :] = band.ReadAsArray() nodata = band.GetNoDataValue() # to ease later processing, replace nodata regions with nan if nodata is not None: mask = np.isclose(image, nodata) image[mask] = np.nan zone_number, hemisphere = _parse_proj_str(proj) # return a meta dict meta_dict = { "geo": geo, "proj": proj, "meta": meta, "img_width": width, "img_height": height, "nodata": nodata, "zone_number": zone_number, "hemisphere": hemisphere, "ul_easting": geo[0], "ul_northing": geo[3], "east_resolution": geo[1], "north_resolution": abs(geo[5]), } # add other info for convenience # lr_easting are left-margin of the last column # lr_northing are top-margin of the last row meta_dict["lr_easting"] = ( meta_dict["ul_easting"] + (meta_dict["img_width"] - 1) * meta_dict["east_resolution"]) meta_dict["lr_northing"] = ( meta_dict["ul_northing"] - (meta_dict["img_height"] - 1) * meta_dict["north_resolution"]) meta_dict["area_width"] = meta_dict["lr_easting"] - meta_dict["ul_easting"] meta_dict["area_height"] = (meta_dict["ul_northing"] - meta_dict["lr_northing"]) meta_dict["alt_min"] = float(np.nanmin(image)) # for json serialization meta_dict["alt_max"] = float(np.nanmax(image)) del ds return image, meta_dict
def count_vpx(self, input_files, output_file): """ Perform valid pixels coverage. 0: "noData", 1: "valid", 2: "clouds", 3: "shadows", 4: "snow", 5: "water" Raise ProcessorFailedError on failure. :param list input_files: list of input files :param str output_file: output filename """ import numpy as np from osgeo import gdal, gdalconst, gdal_array try: # open input data ids = gdal.Open(input_files[0], gdalconst.GA_ReadOnly) iproj = ids.GetProjection() itrans = ids.GetGeoTransform() vpx_band = ids.GetRasterBand(1) # open output data driver = gdal.GetDriverByName('GTiff') ods = driver.Create(output_file, vpx_band.XSize, vpx_band.YSize, eType=vpx_band.DataType) ods.SetGeoTransform(itrans) ods.SetProjection(iproj) # create countVpx array dtype = gdal_array.GDALTypeCodeToNumericTypeCode( ids.GetRasterBand(1).DataType) # dtype vs. no of images < 255 ? vpx_count = np.zeros((vpx_band.YSize, vpx_band.XSize), dtype=dtype) ids = None # count valid pixels for i in range(len(input_files)): ids = gdal.Open(input_files[i], gdalconst.GA_ReadOnly) vpx_band = ids.GetRasterBand(1) vpx_arr = vpx_band.ReadAsArray() vpx_count += vpx_arr # save count ods.GetRasterBand(1).WriteArray(vpx_count) # ods.GetRasterBand(1).SetNoDataValue(vpx_band.GetNoDataValue()) # set color table StyleReader(self.identifier).set_band_colors(ods) # close data sources & write out ids = None ods = None except RuntimeError as e: raise ProcessorFailedError( self, "Count Vpx processor failed: {}".format(e))
def GDALTypeCodeToNumericTypeCodeEx(buf_type, signed_byte, default=None): typecode = gdal_array.GDALTypeCodeToNumericTypeCode(buf_type) if typecode is None: typecode = default if buf_type == gdal.GDT_Byte and signed_byte: typecode = np.int8 return typecode
def computeNdvi(gs_baseurl, outdir, instrument, model_url): if instrument is 'LANDSAT_7': band1 = 'B1' band2 = 'B2' band3 = 'B3' band4 = 'B4' else: band1 = 'B2' band2 = 'B3' band3 = 'B4' band4 = 'B5' with LandsatReader(gs_baseurl, band1, '.') as green_ds, \ LandsatReader(gs_baseurl, band2, '.') as blue_ds, \ LandsatReader(gs_baseurl, band3, '.') as red_ds, \ LandsatReader(gs_baseurl, band4, '.') as nir_ds, \ ModelReader(model_url, '.') as model: # define output tmpfilename = os.path.join( tempfile.gettempdir(), '{0}_clf.TIF'.format(os.path.basename(gs_baseurl))) # define input green = green_ds.GetRasterBand(1).ReadAsArray() blue = blue_ds.GetRasterBand(1).ReadAsArray() red = red_ds.GetRasterBand(1).ReadAsArray() nir = nir_ds.GetRasterBand(1).ReadAsArray() img = np.zeros((nir_ds.RasterYSize, nir_ds.RasterXSize, 4), gdal_array.GDALTypeCodeToNumericTypeCode( red_ds.GetRasterBand(1).DataType)) img[:, :, 0] = green img[:, :, 1] = blue img[:, :, 2] = red img[:, :, 3] = nir # pairing X and y rows = img.shape[0] cols = img.shape[1] band_num = img.shape[2] # create mask image mask_landsat = np.zeros((rows, cols)) mask_landsat[~np.isnan(img[:, :, 1])] = 1 mask = mask_landsat # handle missing value img[np.isnan(img)] = 0 predict = model.predict(img.reshape(cols * rows, band_num)) output = predict.reshape(rows, cols) * mask array_to_raster(output, tmpfilename, nir_ds) outfilename = os.path.join( outdir, '{0}_clf.TIF'.format(os.path.basename(gs_baseurl))) ret = subprocess.check_call(['gsutil', 'mv', tmpfilename, outfilename]) print 'Wrote {0} ...'.format(outfilename)
def read(path): img_ds = gdal.Open(path, gdal.GA_ReadOnly) img = np.zeros( (img_ds.RasterYSize, img_ds.RasterXSize, img_ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode( img_ds.GetRasterBand(1).DataType)) for b in range(img.shape[2]): img[:, :, b] = img_ds.GetRasterBand(b + 1).ReadAsArray() return img
def __init__(self, name, offsets=None, get_array=False, nan_replacement=0.0): """ Raster class instantiate :param name: Raster file path :param offsets: Offsets for reading Raster array in array coordinate system (pixel x coordinate, pixel y coordinate, x offset, y offset) :param get_array: If raster array should be read in memory :param nan_replacement: NAN or INF values, if found, will be replaced by this number """ self.name = name self.bnames = [] self.tile_grid = None # open raster file if os.path.isfile(self.name): fileptr = gdal.Open(self.name) # open file self.datasource = fileptr else: raise ImageProcessingError('No datasource found') band_order = list(range(fileptr.RasterCount)) # raster properties self.shape = [fileptr.RasterCount, fileptr.RasterYSize, fileptr.RasterXSize] self.dtype = fileptr.GetRasterBand(1).DataType self.nodatavalue = fileptr.GetRasterBand(1).GetNoDataValue() self.transform = fileptr.GetGeoTransform() self.crs_string = fileptr.GetProjection() self.bnames = list(fileptr.GetRasterBand(band_indx + 1).GetDescription() for band_indx in range(self.shape[0])) if offsets is not None and len(offsets) == 4: self.offsets = offsets else: self.offsets = (0, 0, self.shape[2], self.shape[1]) if get_array: sys.stdout.write('Reading bands: {}\n'.format(" ".join([self.bnames[band_indx] for band_indx in band_order]))) self.array = np.zeros((self.shape[0], self.offsets[3], self.offsets[2]), gdal_array.GDALTypeCodeToNumericTypeCode(self.dtype)) # read array and store the band values and name in array for band_indx, order_indx in enumerate(band_order): self.array[order_indx, :, :] = fileptr.GetRasterBand(band_indx + 1).ReadAsArray(*self.offsets) # if flag for finite values is present if np.isnan(self.array).any() or np.isinf(self.array).any(): self.array[np.isnan(self.array)] = nan_replacement self.array[np.isinf(self.array)] = nan_replacement tie_pt = [self.transform[0], self.transform[3]] # raster bounds tuple of format: (xmin, xmax, ymin, ymax) self.bounds = [tie_pt[0], tie_pt[0] + self.transform[1] * self.shape[2], # xmin, xmax tie_pt[1] + self.transform[5] * self.shape[1], tie_pt[1]] # ymin, ymax
def _init_attrs(self, filenames): self.filenames = filenames self.files = [open(f, 'rb') for f in self.filenames] self.n_image = len(self.filenames) ds = gdal.Open(self.filenames[0], gdal.GA_ReadOnly) self.size = (ds.RasterXSize, ds.RasterCount) self.datatype = gdal_array.GDALTypeCodeToNumericTypeCode( ds.GetRasterBand(1).DataType)
def readArray(filename, n_band): ds = gdal.Open( filename, GA_ReadOnly ) band = ds.GetRasterBand( n_band ) dtype = gdal_array.GDALTypeCodeToNumericTypeCode(gdal.GDT_Float32) arry = band.ReadAsArray().astype( dtype ) # nodata = band.GetNoDataValue() ds = None return arry, nodata
def read_dsm_tif(file): assert (os.path.exists(file)) ds = gdal.Open(file) geo = ds.GetGeoTransform() proj = ds.GetProjection() meta = ds.GetMetadata() width = ds.RasterXSize height = ds.RasterYSize assert (ds.RasterCount == 1) # dsm is only one band band = ds.GetRasterBand(1) # band index is one-based data_type = gdal_array.GDALTypeCodeToNumericTypeCode(band.DataType) assert (isinstance(data_type(), np.float32)) image = np.zeros((ds.RasterYSize, ds.RasterXSize), dtype=np.float32) image[:, :] = band.ReadAsArray() nodata = band.GetNoDataValue() # to ease later processing, replace nodata regions with nan if nodata is not None: mask = np.isclose(image, nodata) image[mask] = np.nan zone_number, hemisphere = parse_proj_str(proj) # return a meta dict meta_dict = { 'geo': geo, 'proj': proj, 'meta': meta, 'img_width': width, 'img_height': height, 'nodata': nodata, 'zone_number': zone_number, 'hemisphere': hemisphere, 'ul_easting': geo[0], 'ul_northing': geo[3], 'east_resolution': geo[1], 'north_resolution': abs(geo[5]) } # add other info for convenience # lr_easting are left-margin of the last column # lr_northing are top-margin of the last row meta_dict['lr_easting'] = meta_dict['ul_easting'] + ( meta_dict['img_width'] - 1) * meta_dict['east_resolution'] meta_dict['lr_northing'] = meta_dict['ul_northing'] - ( meta_dict['img_height'] - 1) * meta_dict['north_resolution'] meta_dict['area_width'] = meta_dict['lr_easting'] - meta_dict['ul_easting'] meta_dict[ 'area_height'] = meta_dict['ul_northing'] - meta_dict['lr_northing'] meta_dict['alt_min'] = float(np.nanmin(image)) # for json serialization meta_dict['alt_max'] = float(np.nanmax(image)) del ds return image, meta_dict
def _read_image(f): ds = gdal.Open(f, gdal.GA_ReadOnly) dtype = gdal_array.GDALTypeCodeToNumericTypeCode( ds.GetRasterBand(1).DataType) nrow, ncol, nband = ds.RasterYSize, ds.RasterYSize, ds.RasterCount img = np.empty((nband, nrow, ncol), dtype=dtype) for i_b in range(nband): b = ds.GetRasterBand(i_b + 1) img[i_b, ...] = b.ReadAsArray() return img
def toArray(self): """ Read data stack into numpy array """ typeCode = gdal_array.GDALTypeCodeToNumericTypeCode(self.ogrDataType) arr = np.zeros((self.nRows, self.nColumns, self.nLayers), typeCode) for b in range(self.nLayers): # For each band arr[:, :, b] = self.dataset.GetRasterBand(b + 1).ReadAsArray() return arr
def gdalint2numpystr(self): code = 1 map = [] while True: out = gdal_array.GDALTypeCodeToNumericTypeCode(code) if out is None: break else: map.append((code, out().dtype.name)) code += 1 return dict(map)
def create_multi_image(ds): """ From the multispectral image will create a multi-level array of the image """ img = np.zeros( (ds.RasterYSize, ds.RasterXSize, ds.RasterCount), gdal_array.GDALTypeCodeToNumericTypeCode(ds.GetRasterBand(1).DataType)) for b in range(1, img.shape[2]): img[:, :, b] = ds.GetRasterBand(b + 1).ReadAsArray() return img
def rst_dtype(rst): """ Return Type of Raster Dataset return numpy class """ from osgeo import gdal, gdal_array img = gdal.Open(rst, gdal.GA_ReadOnly) bnd = img.GetRasterBand(1).DataType return gdal_array.GDALTypeCodeToNumericTypeCode(bnd)
def _init_attrs(self, filenames): self.filenames = filenames self.datasets = [ gdal.Open(f, gdal.GA_ReadOnly) for f in self.filenames ] self.n_image = len(filenames) self.n_band = self.datasets[0].RasterCount self.n_col = self.datasets[0].RasterXSize self.datatype = gdal_array.GDALTypeCodeToNumericTypeCode( self.datasets[0].GetRasterBand(1).DataType) self.dataset_bands = [[ ds.GetRasterBand(i + 1) for i in range(self.n_band) ] for ds in self.datasets]