def MakeBrowseImage(src_ds, browse_filename, subset_filename, osm_bg_image, sw_osm_image, levels, hexColors, _force, _verbose, zoom=4, scale=1): verbose = _verbose force = _force if verbose: print "levels", levels print "hexColors", hexColors assert( len(levels) == len(hexColors)) decColors = [] for h in hexColors: rgb = hex_to_rgb(h) decColors.append(rgb) projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() band = src_ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize ) if scale != 1: print "rescale for browse", scale data *= scale xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1]* src_ds.RasterXSize ymax = yorg - geotransform[1]* src_ds.RasterYSize if verbose: print "original coords", xorg, xmax, yorg, ymax deltaX = xmax - xorg deltaY = ymax - yorg driver = gdal.GetDriverByName( "GTiff" ) if force or not os.path.isfile(browse_filename): dst_ds_dataset = driver.Create( browse_filename, src_ds.RasterXSize, src_ds.RasterYSize, 2, gdal.GDT_Byte, [ 'COMPRESS=DEFLATE', 'ALPHA=YES' ] ) dst_ds_dataset.SetGeoTransform( geotransform ) dst_ds_dataset.SetProjection( projection ) firstItem = levels.pop() lastItem = levels.pop(0) rlist = reversed(levels) data[data < firstItem] = 0 idx = -1 l = firstItem for idx, l in enumerate(rlist): if verbose: print "data>=", firstItem , " data<", l, " index:", idx+1 data[numpy.logical_and(data>=firstItem, data<l)]= idx+1 firstItem = l idx += 2 if verbose: print "data>=",l, "data<", lastItem, "index:", idx print "data>=",lastItem, " index:", idx+1 data[numpy.logical_and(data>=l, data<lastItem)] = idx data[data>=lastItem] = idx+1 dst_ds_dataset.SetGeoTransform( geotransform ) dst_ds_dataset.SetProjection( projection ) o_band = dst_ds_dataset.GetRasterBand(1) o_band.WriteArray(data.astype('i1'), 0, 0) a_band = dst_ds_dataset.GetRasterBand(2) data[data > 0] = 255 data[data < 0] = 0 a_band.WriteArray(data.astype('i1'), 0, 0) ct = gdal.ColorTable() ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry( i, (0, 0, 0, 0) ) for idx,d in enumerate(decColors): if verbose: print "SetColorEntry", idx+1, decColors[idx] ct.SetColorEntry( idx+1, decColors[idx] ) o_band.SetRasterColorTable(ct) band.SetNoDataValue(0) dst_ds_dataset = None print "Created Browse Image:", browse_filename # centerlon = (xorg + xmax)/2 centerlat = (yorg + ymax)/2 #zoom = 4 if verbose: print "center target", centerlon, centerlat, zoom # Check raster size - thumbnail should be about 256x256 minDim = min(src_ds.RasterXSize, src_ds.RasterYSize) ratio = 256.0 / minDim if ratio >= 1: ratio = round(ratio)+1 # round up rasterXSize = int(src_ds.RasterXSize*ratio) rasterYSize = int(src_ds.RasterYSize*ratio) if verbose: print "** Adjust", src_ds.RasterXSize, src_ds.RasterYSize, minDim, ratio, rasterXSize, rasterYSize #if 1 or force or not os.path.isfile(osm_bg_image): mapbox_image(centerlat, centerlon, zoom, rasterXSize, rasterYSize, osm_bg_image) ullon, ullat, lrlon, lrlat = bbox(centerlat, centerlon, zoom, rasterXSize, rasterYSize) if verbose: print "bbox coords", ullon, ullat, lrlon, lrlat if force or not os.path.isfile(subset_filename): ofStr = ' -of GTiff ' bbStr = ' -te %s %s %s %s '%(ullon, lrlat, lrlon, ullat) #resStr = ' -tr %s %s '%(pres, pres) resStr = ' ' projectionStr = ' -t_srs EPSG:4326 ' overwriteStr = ' -overwrite ' # Overwrite output if it exists additionalOptions = ' -co COMPRESS=DEFLATE -setci ' # Additional options wh = ' -ts %d %d ' % ( rasterXSize, rasterYSize ) warpOptions = ofStr + bbStr + projectionStr + resStr + overwriteStr + additionalOptions + wh warpCMD = 'gdalwarp ' + warpOptions + browse_filename + ' ' + subset_filename execute(warpCMD) # superimpose the suface water over map background #if force or not os.path.isfile(sw_osm_image): if force or not os.path.isfile(sw_osm_image): cmd = str.format("composite -gravity center -blend 50 {0} {1} {2}", subset_filename, osm_bg_image, sw_osm_image) execute(cmd)
def colorizeMyOutputRaster(out_ds): ct = gdal.ColorTable() ct.SetColorEntry(0, (0, 0, 0, 255)) ct.SetColorEntry(1, (110, 220, 110, 255)) out_ds.SetColorTable(ct) return out_ds
def createColorTable(): ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry(i, (255, 255 - i, i, 255)) return ct
def MakeBrowseImage(src_ds, browse_filename, subset_filename, osm_bg_image, sw_osm_image): projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() band = src_ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize if verbose: print "original coords", xorg, xmax, yorg, ymax deltaX = xmax - xorg deltaY = ymax - yorg driver = gdal.GetDriverByName("GTiff") levels = [340, 210, 130, 80, 50, 30, 20, 10] if force or not os.path.isfile(browse_filename): dst_ds_dataset = driver.Create(browse_filename, src_ds.RasterXSize, src_ds.RasterYSize, 2, gdal.GDT_Byte, ['COMPRESS=DEFLATE', 'ALPHA=YES']) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) data[data <= 0] = 0 data[numpy.logical_and(data > 0, data <= 10)] = 1 data[numpy.logical_and(data > 10, data <= 20)] = 2 data[numpy.logical_and(data > 20, data <= 30)] = 3 data[numpy.logical_and(data > 30, data <= 50)] = 5 data[numpy.logical_and(data > 50, data <= 80)] = 8 data[numpy.logical_and(data > 80, data <= 130)] = 13 data[numpy.logical_and(data > 130, data <= 210)] = 21 data[numpy.logical_and(data > 210, data <= 340)] = 34 data[data > 340] = 55 dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band = dst_ds_dataset.GetRasterBand(1) o_band.WriteArray(data.astype('i1'), 0, 0) a_band = dst_ds_dataset.GetRasterBand(2) data[data > 0] = 255 data[data < 0] = 0 a_band.WriteArray(data.astype('i1'), 0, 0) ct = gdal.ColorTable() ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry(i, (0, 0, 0, 0)) # From http://colorbrewer2.org/ ct.SetColorEntry(0, (0, 0, 0, 0)) ct.SetColorEntry(1, (247, 252, 240, 255)) #f7fcf0 ct.SetColorEntry(2, (224, 243, 219, 255)) #e0f3db ct.SetColorEntry(3, (204, 235, 197, 255)) #ccebc5 ct.SetColorEntry(5, (168, 221, 181, 255)) #a8ddb5 ct.SetColorEntry(8, (123, 204, 196, 255)) #7bccc4 ct.SetColorEntry(13, (78, 179, 211, 255)) #4eb3d3 ct.SetColorEntry(21, (43, 140, 190, 255)) #2b8cbe ct.SetColorEntry(34, (8, 104, 172, 255)) #0868ac ct.SetColorEntry(55, (8, 64, 129, 255)) #084081 o_band.SetRasterColorTable(ct) band.SetNoDataValue(0) dst_ds_dataset = None print "Created Browse Image:", browse_filename # centerlon = (xorg + xmax) / 2 centerlat = (yorg + ymax) / 2 zoom = 4 if verbose: print "center target", centerlon, centerlat, zoom # raster is too small so double the size rasterXSize = src_ds.RasterXSize * 2 rasterYSize = src_ds.RasterYSize * 2 if force or not os.path.isfile(osm_bg_image): mapbox_image(centerlat, centerlon, zoom, rasterXSize, rasterYSize, osm_bg_image) ullon, ullat, lrlon, lrlat = bbox(centerlat, centerlon, zoom, rasterXSize, rasterYSize) if verbose: print "bbox coords", ullon, ullat, lrlon, lrlat if force or not os.path.isfile(subset_filename): ofStr = ' -of GTiff ' bbStr = ' -te %s %s %s %s ' % (ullon, lrlat, lrlon, ullat) #resStr = ' -tr %s %s '%(pres, pres) resStr = ' ' projectionStr = ' -t_srs EPSG:4326 ' overwriteStr = ' -overwrite ' # Overwrite output if it exists additionalOptions = ' -co COMPRESS=DEFLATE -setci ' # Additional options wh = ' -ts %d %d ' % (rasterXSize, rasterYSize) warpOptions = ofStr + bbStr + projectionStr + resStr + overwriteStr + additionalOptions + wh warpCMD = 'gdalwarp ' + warpOptions + browse_filename + ' ' + subset_filename execute(warpCMD) # superimpose the suface water over map background #if force or not os.path.isfile(sw_osm_image): if force or not os.path.isfile(sw_osm_image): cmd = str.format("composite -gravity center {0} {1} {2}", subset_filename, osm_bg_image, sw_osm_image) execute(cmd)
def CreateLevel(l, geojsonDir, fileName, src_ds, data, attr, regionName): global force, verbose minl = l projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() #band = src_ds.GetRasterBand(1) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize if not force and os.path.exists(fileName): return driver = gdal.GetDriverByName("GTiff") dst_ds_dataset = driver.Create(fileName, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte, ['COMPRESS=DEFLATE']) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band = dst_ds_dataset.GetRasterBand(1) o_data = o_band.ReadAsArray(0, 0, dst_ds_dataset.RasterXSize, dst_ds_dataset.RasterYSize) o_data[data >= l] = 255 o_data[data < l] = 0 count = (o_data > 0).sum() if verbose: print "Level", minl, " count:", count if count > 0: dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band.WriteArray(o_data, 0, 0) ct = gdal.ColorTable() ct.SetColorEntry(0, (255, 255, 255, 255)) ct.SetColorEntry(255, (255, 0, 0, 255)) o_band.SetRasterColorTable(ct) dst_ds_dataset = None if verbose: print "Created", fileName cmd = "gdal_translate -q -of PNM " + fileName + " " + fileName + ".pgm" execute(cmd) # -i invert before processing # -t 2 suppress speckles of up to this many pixels. # -a 1.5 set the corner threshold parameter # -z black specify how to resolve ambiguities in path decomposition. Must be one of black, white, right, left, minority, majority, or random. Default is minority # -x scaling factor # -L left margin # -B bottom margin cmd = str.format( "potrace -i -z black -a 1.5 -t 3 -b geojson -o {0} {1} -x {2} -L {3} -B {4} ", fileName + ".geojson", fileName + ".pgm", pres, xorg, ymax) execute(cmd) #cmd = str.format("node set_geojson_property.js --file {0} --prop frost={1}", fileName+".geojson", frost) #execute(cmd) #cmd = str.format("topojson -o {0} --simplify-proportion 0.5 -p {3}={1} -- {3}={2}", fileName+".topojson", l, fileName+".geojson", attr ); quiet = " > /dev/null 2>&1" if verbose: quiet = " " if regionName == 'global': sp = 0.4 #proportion of points to retain for Visvalingam simplification else: sp = 0.5 cmd = str.format( "topojson --bbox --simplify-proportion {0} -o {1} --no-stitch-poles -p {4}={2} -- {4}={3} {5}", sp, fileName + ".topojson", minl, fileName + ".geojson", attr, quiet) execute(cmd) # convert it back to json cmd = "topojson-geojson --precision 4 -o %s %s" % ( geojsonDir, fileName + ".topojson") execute(cmd) # rename file output_file = "%s_level_%d.geojson" % (attr, minl) json_file = "%s.json" % attr cmd = "mv %s %s" % (os.path.join( geojsonDir, json_file), os.path.join(geojsonDir, output_file)) execute(cmd)
def setColorTable(imgfile, colorTblArray, layernum=1): """ Set the color table for the specified band. You can specify either the imgfile as either a filename string or a gdal.Dataset object. The layer number defaults to 1, i.e. the first layer in the file. The color table is given as a numpy array of 5 columns. There is one row (i.e. first array index) for every value to be set, and the columns are: * pixelValue * Red * Green * Blue * Opacity The Red/Green/Blue values are on the range 0-255, with 255 meaning full color, and the opacity is in the range 0-255, with 255 meaning fully opaque. The pixels values in the first column must be in ascending order, but do not need to be a complete set (i.e. you don't need to supply a color for every possible pixel value - any not given will default to transparent black). It does not even need to be contiguous. For reasons of backwards compatability, a 4-column array will also be accepted, and will be treated as though the row index corresponds to the pixelValue (i.e. starting at zero). """ arrayShape = colorTblArray.shape if len(arrayShape) != 2: raise rioserrors.ArrayShapeError( "ColorTableArray must be 2D. Found shape %s instead" % arrayShape) (numRows, numCols) = arrayShape if numRows > MAX_RECOMMENDED_COLOR_TABLE: msg = "We recommend using the rios.colortable for large color tables. " msg += "Large tables may be very slow with this function" warnings.warn(msg, DeprecationWarning) # Handle the backwards-compatible case of a 4-column array if numCols == 4: pixVals = numpy.arange(numRows) colorTbl4cols = colorTblArray elif numCols == 5: pixVals = colorTblArray[:, 0] colorTbl4cols = colorTblArray[:, 1:] else: raise rioserrors.ArrayShapeError( "Color table array has %d columns, expecting 4 or 5" % numCols) # Open the image file and get the band object if isinstance(imgfile, gdal.Dataset): ds = imgfile elif isinstance(imgfile, basestring): ds = gdal.Open(imgfile, gdal.GA_Update) bandobj = ds.GetRasterBand(layernum) clrTbl = gdal.ColorTable() maxPixVal = pixVals.max() i = 0 # This loop sets an entry for every pixel value up to the largest given. Imagine # bitches if we don't do this. tblMaxVal = maxPixVal if bandobj.DataType == gdal.GDT_Byte: # For Byte files, we always add rows for entries up to 255. Imagine gets # confused if we don't tblMaxVal = 255 for pixVal in range(tblMaxVal + 1): while i < numRows and pixVals[i] < pixVal: i += 1 if i < numRows: tblPixVal = pixVals[i] if tblPixVal == pixVal: colEntry = tuple(colorTbl4cols[i]) else: colEntry = (0, 0, 0, 0) else: colEntry = (0, 0, 0, 0) clrTbl.SetColorEntry(pixVal, colEntry) bandobj.SetRasterColorTable(clrTbl)
def CreateLevel(l, geojsonDir, fileName, src_ds, data, attr, _force, _verbose): global force, verbose force = _force verbose = _verbose if verbose: print "CreateLevel", l, _force, _verbose projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() #band = src_ds.GetRasterBand(1) xorg = geotransform[0] yorg = geotransform[3] xres = geotransform[1] yres = -geotransform[5] stretch = 1 # xres/yres if verbose: print xres, yres, stretch xmax = xorg + geotransform[1]* src_ds.RasterXSize ymax = yorg + geotransform[5]* src_ds.RasterYSize if not force and os.path.exists(fileName): return driver = gdal.GetDriverByName( "GTiff" ) dst_ds_dataset = driver.Create( fileName, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte, [ 'COMPRESS=DEFLATE' ] ) dst_ds_dataset.SetGeoTransform( geotransform ) dst_ds_dataset.SetProjection( projection ) o_band = dst_ds_dataset.GetRasterBand(1) o_data = o_band.ReadAsArray(0, 0, dst_ds_dataset.RasterXSize, dst_ds_dataset.RasterYSize ) count = (data >= l).sum() o_data[data>=l] = 255 o_data[data<l] = 0 if verbose: print "*** Level", l, " count:", count if count > 0 : dst_ds_dataset.SetGeoTransform( geotransform ) dst_ds_dataset.SetProjection( projection ) o_band.WriteArray(o_data, 0, 0) ct = gdal.ColorTable() ct.SetColorEntry( 0, (255, 255, 255, 255) ) ct.SetColorEntry( 255, (255, 0, 0, 255) ) o_band.SetRasterColorTable(ct) dst_ds_dataset = None if verbose: print "Created", fileName cmd = "gdal_translate -q -of PNM " + fileName + " "+fileName+".pgm" execute(cmd) # -i invert before processing # -t 2 suppress speckles of up to this many pixels. # -a 1.5 set the corner threshold parameter # -z black specify how to resolve ambiguities in path decomposition. Must be one of black, white, right, left, minority, majority, or random. Default is minority # -x scaling factor # -L left margin # -B bottom margin if stretch != 1: cmd = str.format("potrace -i -z black -a 1.5 -t 3 -b geojson -o {0} {1} -x {2} -S {3} -L {4} -B {5} ", fileName+".geojson", fileName+".pgm", xres, stretch, xorg, ymax ); else: cmd = str.format("potrace -i -z black -a 3 -t 4 -b geojson -o {0} {1} -x {2} -L {3} -B {4} ", fileName+".geojson", fileName+".pgm", xres, xorg, ymax ); execute(cmd) #cmd = str.format("topojson -o {0} --simplify-proportion 0.5 -p {3}={1} -- {3}={2} > /dev/null 2>&1", fileName+".topojson", l, fileName+".geojson", attr ); out = "" if not verbose: out = "> /dev/null 2>&1" cmd = str.format("topojson -q 1e6 -s 0.0000001 -o {0} -p {3}={1} -- {3}={2} {4}", fileName+".topojson", l, fileName+".geojson", attr, out ); execute(cmd) # convert it back to json cmd = "topojson-geojson --precision 4 -o %s %s" % ( geojsonDir, fileName+".topojson" ) execute(cmd) # rename file output_file = "%s_level_%d.geojson" % (attr, l) json_file = "%s.json" % attr cmd = "mv %s %s" % (os.path.join(geojsonDir,json_file), os.path.join(geojsonDir, output_file)) execute(cmd)
def ComposeSubsets(subsetDir, srcDir, ymd): try: dirList = os.listdir(subsetDir) composite = None for fname in dirList: if fnmatch.fnmatch(fname, '*.tif'): fullName = os.path.join(subsetDir, fname) print "reading:", fullName ds = gdal.Open(fullName) band = ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, ds.RasterXSize, ds.RasterYSize) invalid = (data < 1) data[invalid] = 65535 if composite == None: composite = data else: composite = numpy.minimum(data, composite) projection = ds.GetProjection() geotransform = ds.GetGeoTransform() mask = (composite == 65535) composite = composite.astype(float) composite *= 0.02 # nodata 0 # very severe frost 0-250 # severe frost 250-260 # moderate frost 260-270 # minor frost 270- 280 # no frost > 280 composite[mask] = 0 # no data print numpy.min(composite), numpy.max(composite), numpy.mean(composite) composite[composite > 288] = 1 # no frost composite[composite > 270] = 2 # minor frost composite[composite > 260] = 3 # moderate frost composite[composite > 250] = 4 # severe frost composite[composite > 5] = 5 # very severe frost compositeFileName = os.path.join(srcPath, "Frost." + ymd + ".tif") print "Creating:", compositeFileName driver = gdal.GetDriverByName("GTiff") o_ds = driver.Create(compositeFileName, ds.RasterXSize, ds.RasterYSize, 1, gdal.GDT_Byte, ['COMPRESS=DEFLATE']) o_band = o_ds.GetRasterBand(1) ct = gdal.ColorTable() ct.SetColorEntry(0, (255, 255, 255, 255)) ct.SetColorEntry(1, (0, 255, 0, 255)) ct.SetColorEntry(2, (255, 154, 0, 255)) ct.SetColorEntry(3, (255, 0, 0, 255)) ct.SetColorEntry(4, (255, 153, 204, 255)) ct.SetColorEntry(5, (204, 0, 204, 255)) o_band.SetRasterColorTable(ct) o_ds.SetGeoTransform(geotransform) o_ds.SetProjection(projection) o_band.WriteArray(composite.astype('i1'), 0, 0) o_ds = None ds = None except IOError as e: print "I/O error({0}): {1}".format(e.errno, e.strerror)
def writeClassData(self, out_cls_file, fmt="ENVI"): """Write classified image to a raster file given file format. Parameters: **out_cls_file**, *str* File name of the output classification image. **fmt**, *str* Format of the output raster. Default: "ENVI". Supported format: all GDAL-supported format with writing ability. """ cls_map = np.zeros((self.imgdata['dims'][0], self.imgdata['dims'][1]), dtype=np.uint8) cls_map.flat[:] = self.imgdata['y'].astype(np.uint8) # Write the classification to an ENVI file raster_profile = self.getRasterProfile(self.imgdata['Xsrc']['files'][0]) cm_used = plt.get_cmap("rainbow", len(np.unique(cls_map))) color_table = cm_used(range(len(np.unique(cls_map)))) driver = gdal.GetDriverByName(fmt) if fmt == "ENVI": out_ds = driver.Create(out_cls_file, \ cls_map.shape[1], cls_map.shape[0], 1, \ gdal_array.NumericTypeCodeToGDALTypeCode(cls_map.dtype.type)) else: cls_map = cls_map.astype(np.float32) ncls = self.imgdata['prob'].shape[1] out_ds = driver.Create(out_cls_file, \ cls_map.shape[1], cls_map.shape[0], 1+ncls, \ gdal_array.NumericTypeCodeToGDALTypeCode(cls_map.dtype.type)) out_band = out_ds.GetRasterBand(1) out_band.WriteArray(cls_map) out_band.SetCategoryNames(["no_data"]+[cn for cn in self.traindata['label_meta']['cls_name']]) # set the class names out_band.SetDescription("Classification by {0:s}".format(self.estimator)) # set band name out_band.SetNoDataValue(self._cls_nodata) if fmt == "ENVI": out_ct = gdal.ColorTable() for i in range(len(color_table)): out_ct.SetColorEntry(i, tuple((color_table[i, :]*255).astype(np.int))) out_band.SetColorTable(out_ct) out_band.FlushCache() if fmt == "ENVI": bands_str = [str(b).replace('[', '"').replace(']', '"') for b in self.imgdata['Xsrc']['bands']] envi_meta_dict = dict(data_ignore_value=str(self._cls_nodata), \ images_for_classification="{{{0:s}}}".format(", ".join(self.imgdata['Xsrc']['files'])), \ bands_for_classification="{{{0:s}}}".format(", ".join(bands_str))) print self.imgdata['Xsrc']['files'] print self.imgdata['Xsrc']['bands'] print envi_meta_dict for kw in envi_meta_dict.keys(): out_ds.SetMetadataItem(kw, envi_meta_dict[kw], "ENVI") else: for icls in range(ncls): cls_map_prob = np.zeros((self.imgdata['dims'][0], self.imgdata['dims'][1]), dtype=np.float32) cls_map_prob.flat[:] = self.imgdata['prob'][:, icls].astype(np.float32) out_band = out_ds.GetRasterBand(icls+1+1) out_band.WriteArray(cls_map_prob) out_band.SetDescription("Probability of {0:s} by {1:s}".format(self.traindata['label_meta']['cls_name'][icls], self.estimator)) out_band.SetNoDataValue(self._clsprob_nodata) out_band.FlushCache() out_ds.SetGeoTransform(raster_profile['GeoTransform']) out_ds.SetProjection(raster_profile['ProjectionRef']) out_ds = None
def generate_color_table(self): self.ct = gdal.ColorTable() for i in range(256): self.ct.SetColorEntry( i, (0, 0, 0, 0) ) self.ct.SetColorEntry( 1, (255, 0, 0, 255) )
def CreateLevels(srcPath, ymd): levelsDir = os.path.join(srcPath, "levels") if not os.path.exists(levelsDir): os.makedirs(levelsDir) geojsonDir = os.path.join(srcPath, "geojson") if not os.path.exists(geojsonDir): os.makedirs(geojsonDir) compositeFileName = os.path.join(srcPath, "Frost." + ymd + ".tif") smoothedFileName = os.path.join(srcPath, "Smoothed_Frost." + ymd + ".tif") level1FileName = os.path.join(levelsDir, "Level_1_Frost." + ymd + ".tif") level2FileName = os.path.join(levelsDir, "Level_2_Frost." + ymd + ".tif") level3FileName = os.path.join(levelsDir, "Level_3_Frost." + ymd + ".tif") level4FileName = os.path.join(levelsDir, "Level_4_Frost." + ymd + ".tif") level5FileName = os.path.join(levelsDir, "Level_5_Frost." + ymd + ".tif") topojsonFileName = os.path.join(srcPath, "Smoothed_Frost." + ymd + ".topojson") driver = gdal.GetDriverByName("GTiff") #src_ds = gdal.Open( smoothedFileName ) src_ds = gdal.Open(compositeFileName) projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() band = src_ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize ct = gdal.ColorTable() ct.SetColorEntry(0, (255, 255, 255, 255)) ct.SetColorEntry(1, (0, 0, 0, 255)) ct.SetColorEntry(2, (0, 0, 0, 255)) ct.SetColorEntry(3, (0, 0, 0, 255)) ct.SetColorEntry(4, (0, 0, 0, 255)) ct.SetColorEntry(5, (0, 0, 0, 255)) CreateTopojsonFile(srcPath, level1FileName, src_ds, projection, geotransform, ct, data, pres, xorg, ymax, 1) ct.SetColorEntry(1, (255, 255, 255, 255)) CreateTopojsonFile(srcPath, level2FileName, src_ds, projection, geotransform, ct, data, pres, xorg, ymax, 2) ct.SetColorEntry(2, (255, 255, 255, 255)) CreateTopojsonFile(srcPath, level3FileName, src_ds, projection, geotransform, ct, data, pres, xorg, ymax, 3) ct.SetColorEntry(3, (255, 255, 255, 255)) CreateTopojsonFile(srcPath, level4FileName, src_ds, projection, geotransform, ct, data, pres, xorg, ymax, 4) ct.SetColorEntry(4, (255, 255, 255, 255)) CreateTopojsonFile(srcPath, level5FileName, src_ds, projection, geotransform, ct, data, pres, xorg, ymax, 5)
def addStatistics(ds, progress, ignore=None): """ Calculates statistics and adds them to the image Uses gdal.Band.ComputeStatistics() for mean, stddev, min and max, and gdal.Band.GetHistogram() to do histogram calculation. The median and mode are estimated using the histogram, and so for larger datatypes, they will be approximate only. For thematic layers, the histogram is calculated with as many bins as required, for athematic integer and float types, a maximum of 256 bins is used. """ progress.setLabelText("Computing Statistics...") progress.setProgress(0) percent = 0 percentstep = 100.0 / (ds.RasterCount * 2) # 2 steps for each layer for bandnum in range(ds.RasterCount): band = ds.GetRasterBand(bandnum + 1) # fill in the metadata tmpmeta = band.GetMetadata() if ignore is not None: # tell QGIS that the ignore value was ignored band.SetNoDataValue(ignore) tmpmeta["STATISTICS_EXCLUDEDVALUES"] = repr( ignore) # doesn't seem to do anything # get GDAL to calculate statistics - force recalculation. Trap errors useExceptions = gdal.GetUseExceptions() gdal.UseExceptions() try: (minval, maxval, meanval, stddevval) = band.ComputeStatistics(False) except RuntimeError as e: if str(e).endswith( 'Failed to compute statistics, no valid pixels found in sampling.' ): minval = ignore maxval = ignore meanval = ignore stddevval = 0 if not useExceptions: gdal.DontUseExceptions() percent = percent + percentstep progress.setProgress(percent) tmpmeta["STATISTICS_MINIMUM"] = repr(minval) tmpmeta["STATISTICS_MAXIMUM"] = repr(maxval) tmpmeta["STATISTICS_MEAN"] = repr(meanval) tmpmeta["STATISTICS_STDDEV"] = repr(stddevval) # because we did at full res - these are the default anyway tmpmeta["STATISTICS_SKIPFACTORX"] = "1" tmpmeta["STATISTICS_SKIPFACTORY"] = "1" # create a histogram so we can do the mode and median if band.DataType == gdal.GDT_Byte: # if byte data use 256 bins and the whole range histmin = 0 histmax = 255 histstep = 1.0 histCalcMin = -0.5 histCalcMax = 255.5 histnbins = 256 tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct' elif "LAYER_TYPE" in tmpmeta and tmpmeta["LAYER_TYPE"] == 'thematic': # all other thematic types a bin per value histmin = 0 histmax = int(numpy.ceil(maxval)) histstep = 1.0 histCalcMin = -0.5 histCalcMax = maxval + 0.5 histnbins = histmax + 1 tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct' elif band.DataType in gdalLargeIntTypes: histrange = int(numpy.ceil(maxval) - numpy.floor(minval)) + 1 (histmin, histmax) = (minval, maxval) if histrange <= 256: histnbins = histrange histstep = 1.0 tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct' histCalcMin = histmin - 0.5 histCalcMax = histmax + 0.5 else: histnbins = 256 tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'linear' histCalcMin = histmin histCalcMax = histmax histstep = float(histCalcMax - histCalcMin) / histnbins elif band.DataType in gdalFloatTypes: histnbins = 256 (histmin, histmax) = (minval, maxval) tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'linear' histCalcMin = minval histCalcMax = maxval histstep = float(histCalcMax - histCalcMin) / histnbins # Note that the complex number data types are not handled, as I am not sure # what a histogram or a median would mean for such types. userdata = ProgressUserData() userdata.progress = progress userdata.nbands = ds.RasterCount * 2 userdata.curroffset = percent # get histogram and force GDAL to recalculate it hist = band.GetHistogram(histCalcMin, histCalcMax, histnbins, False, False, progressFunc, userdata) # Check if GDAL's histogram code overflowed. This is not a fool-proof test, # as some overflows will not result in negative counts. histogramOverflow = (min(hist) < 0) # we may use this rat reference for the colours below also # may be None if format does not support RATs rat = band.GetDefaultRAT() if not histogramOverflow: # comes back as a list for some reason hist = numpy.array(hist) # Note that we have explicitly set histstep in each datatype case # above. In principle, this can be calculated, as it is done in the # float case, but for some of the others we need it to be exactly # equal to 1, so we set it explicitly there, to avoid rounding # error problems. # do the mode - bin with the highest count modebin = numpy.argmax(hist) modeval = modebin * histstep + histmin if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64: tmpmeta["STATISTICS_MODE"] = repr(modeval) else: tmpmeta["STATISTICS_MODE"] = repr(int(round(modeval))) tmpmeta["STATISTICS_HISTOMIN"] = repr(histmin) tmpmeta["STATISTICS_HISTOMAX"] = repr(histmax) tmpmeta["STATISTICS_HISTONUMBINS"] = repr(histnbins) if haveRFC40 and rat is not None: histIndx, histNew = findOrCreateColumn(rat, gdal.GFU_PixelCount, "Histogram", gdal.GFT_Real) # write the hist in a single go rat.SetRowCount(histnbins) rat.WriteArray(hist, histIndx) # The HFA driver still honours the STATISTICS_HISTOBINVALUES # metadata item. If we are recalculating the histogram the old # values will be copied across with the metadata so clobber it if "STATISTICS_HISTOBINVALUES" in tmpmeta: del tmpmeta["STATISTICS_HISTOBINVALUES"] else: # old method tmpmeta["STATISTICS_HISTOBINVALUES"] = '|'.join(map( repr, hist)) + '|' # estimate the median - bin with the middle number middlenum = hist.sum() / 2 gtmiddle = hist.cumsum() >= middlenum medianbin = gtmiddle.nonzero()[0][0] medianval = medianbin * histstep + histmin if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64: tmpmeta["STATISTICS_MEDIAN"] = repr(medianval) else: tmpmeta["STATISTICS_MEDIAN"] = repr(int(round(medianval))) # set the data band.SetMetadata(tmpmeta) # if it is thematic and there is no colour table # add one because Imagine fails in weird ways otherwise # we make a random colour table to make it obvious if "LAYER_TYPE" in tmpmeta and tmpmeta["LAYER_TYPE"] == 'thematic': # old way if (not haveRFC40 or rat is None) and band.GetColorTable() is None: import random # this also seeds on the time colorTable = gdal.ColorTable() alpha = 255 for i in range(histnbins): c1 = int(random.random() * 255) c2 = int(random.random() * 255) c3 = int(random.random() * 255) entry = (c1, c2, c3, alpha) colorTable.SetColorEntry(i, entry) #band.SetColorTable(colorTable) elif haveRFC40 and rat is not None: # check all the columns redIdx, redNew = findOrCreateColumn(rat, gdal.GFU_Red, "Red", gdal.GFT_Integer) greenIdx, greenNew = findOrCreateColumn( rat, gdal.GFU_Green, "Green", gdal.GFT_Integer) blueIdx, blueNew = findOrCreateColumn(rat, gdal.GFU_Blue, "Blue", gdal.GFT_Integer) alphaIdx, alphaNew = findOrCreateColumn( rat, gdal.GFU_Alpha, "Alpha", gdal.GFT_Integer) # were any of these not already existing? if redNew or greenNew or blueNew or alphaNew: data = numpy.random.randint(0, 256, histnbins) rat.WriteArray(data, redIdx) data = numpy.random.randint(0, 256, histnbins) rat.WriteArray(data, greenIdx) data = numpy.random.randint(0, 256, histnbins) rat.WriteArray(data, blueIdx) data = numpy.empty(histnbins, dtype=numpy.int) data.fill(255) #rat.WriteArray(data, alphaIdx) if haveRFC40 and rat is not None and not rat.ChangesAreWrittenToFile(): # For drivers that require the in memory thing band.SetDefaultRAT(rat) percent = percent + percentstep progress.setProgress(percent) if progress.wasCancelled(): raise ProcessCancelledError() progress.setProgress(100)
def save_data(self): print str(datetime.now()), "Saving Hand data..." print " water pix:", self.wp print " processed:", self.count print " total pixs:", self.RasterXSize * self.RasterYSize print " hand pixs:", numpy.count_nonzero(self.hand) hand_img = os.path.join(self.inpath, self.zone, self.tile + "_hand.tif") driver = gdal.GetDriverByName("GTiff") #dst_ds = driver.CreateCopy( hand_img, self.hds, 0, [ 'TILED=YES', 'COMPRESS=PACKBITS' ] ) dst_ds = driver.Create(hand_img, self.RasterXSize, self.RasterYSize, 1, gdal.GDT_Byte, ['INTERLEAVE=PIXEL', 'COMPRESS=DEFLATE']) ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry(i, (255, 255, 255, 255)) # Colorbrewer, sequential, 7 ct.SetColorEntry(0, (0, 0, 0, 255)) ct.SetColorEntry(1, (8, 48, 107, 255)) ct.SetColorEntry(2, (8, 48, 107, 255)) ct.SetColorEntry(3, (8, 81, 156, 255)) ct.SetColorEntry(4, (8, 81, 156, 255)) ct.SetColorEntry(5, (33, 113, 181, 255)) ct.SetColorEntry(6, (33, 113, 181, 255)) ct.SetColorEntry(7, (66, 146, 198, 255)) ct.SetColorEntry(8, (66, 146, 198, 255)) ct.SetColorEntry(9, (107, 174, 214, 255)) ct.SetColorEntry(10, (107, 174, 214, 255)) ct.SetColorEntry(11, (158, 202, 225, 255)) ct.SetColorEntry(12, (158, 202, 225, 255)) ct.SetColorEntry(13, (198, 219, 239, 255)) ct.SetColorEntry(14, (198, 219, 239, 255)) ct.SetColorEntry(15, (222, 235, 2247, 255)) ct.SetColorEntry(16, (222, 235, 2247, 255)) ct.SetColorEntry(17, (247, 251, 255, 255)) ct.SetColorEntry(18, (247, 251, 255, 255)) # ocean ct.SetColorEntry(255, (0, 0, 0, 0)) band = dst_ds.GetRasterBand(1) band.SetRasterColorTable(ct) band.WriteArray(self.hand, 0, 0) band.SetNoDataValue(0) # copy projection projection = self.hds.GetProjection() geotransform = self.hds.GetGeoTransform() dst_ds.SetGeoTransform(geotransform) dst_ds.SetProjection(projection) dst_ds = None self.hds = None
def CreateLevel(maxl, minl, geojsonDir, fileName, src_ds, data, attr, _force, _verbose): force = _force verbose = _verbose projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() #band = src_ds.GetRasterBand(1) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize driver = gdal.GetDriverByName("GTiff") dst_ds_dataset = driver.Create(fileName, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte, ['COMPRESS=DEFLATE']) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band = dst_ds_dataset.GetRasterBand(1) o_data = o_band.ReadAsArray(0, 0, dst_ds_dataset.RasterXSize, dst_ds_dataset.RasterYSize) o_data.fill(255) o_data[data >= maxl] = 0 o_data[data < minl] = 0 #o_data[data>0] = 255 count = (o_data > 0).sum() if verbose: print "Level", minl, maxl, " count:", count if count > 0: dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band.WriteArray(o_data, 0, 0) ct = gdal.ColorTable() ct.SetColorEntry(0, (255, 255, 255, 255)) ct.SetColorEntry(255, (255, 0, 0, 255)) o_band.SetRasterColorTable(ct) dst_ds_dataset = None if verbose: print "Created", fileName cmd = "gdal_translate -q -of PNM " + fileName + " " + fileName + ".pgm" execute(cmd) # -i invert before processing # -t 2 suppress speckles of up to this many pixels. # -a 1.5 set the corner threshold parameter # -z black specify how to resolve ambiguities in path decomposition. Must be one of black, white, right, left, minority, majority, or random. Default is minority # -x scaling factor # -L left margin # -B bottom margin cmd = str.format( "potrace -i -z black -a 1.5 -t 3 -b geojson -o {0} {1} -x {2} -L {3} -B {4} ", fileName + ".geojson", fileName + ".pgm", pres, xorg, ymax) execute(cmd) cmd = str.format( "topojson -o {0} --simplify-proportion 0.5 -p {3}={1} -- {3}={2}", fileName + ".topojson", maxl, fileName + ".geojson", attr) execute(cmd) # convert it back to json cmd = "topojson-geojson --precision 4 -o %s %s" % ( geojsonDir, fileName + ".topojson") execute(cmd) # rename file output_file = "%s_level_%d.geojson" % (attr, minl) json_file = "%s.json" % attr cmd = "mv %s %s" % (os.path.join( geojsonDir, json_file), os.path.join(geojsonDir, output_file)) execute(cmd)
def main(argv): color_count = 256 frmt = None src_filename = None dst_filename = None pct_filename = None gdal.AllRegister() argv = gdal.GeneralCmdLineProcessor(argv) if argv is None: sys.exit(0) # Parse command line arguments. i = 1 while i < len(argv): arg = argv[i] if arg == '-of' or arg == '-f': i = i + 1 frmt = argv[i] elif arg == '-n': i = i + 1 color_count = int(argv[i]) elif arg == '-pct': i = i + 1 pct_filename = argv[i] elif src_filename is None: src_filename = argv[i] elif dst_filename is None: dst_filename = argv[i] else: Usage() i = i + 1 if dst_filename is None: Usage() # Open source file src_ds = gdal.Open(src_filename) if src_ds is None: print('Unable to open %s' % src_filename) sys.exit(1) if src_ds.RasterCount < 3: print('%s has %d band(s), need 3 for inputs red, green and blue.' % (src_filename, src_ds.RasterCount)) sys.exit(1) # Ensure we recognise the driver. if frmt is None: frmt = GetOutputDriverFor(dst_filename) dst_driver = gdal.GetDriverByName(frmt) if dst_driver is None: print('"%s" driver not registered.' % frmt) sys.exit(1) # Generate palette ct = gdal.ColorTable() if pct_filename is None: err = gdal.ComputeMedianCutPCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), color_count, ct, callback=gdal.TermProgress_nocb) else: pct_ds = gdal.Open(pct_filename) ct = pct_ds.GetRasterBand(1).GetRasterColorTable().Clone() # Create the working file. We have to use TIFF since there are few formats # that allow setting the color table after creation. if format == 'GTiff': tif_filename = dst_filename else: import tempfile tif_filedesc, tif_filename = tempfile.mkstemp(suffix='.tif') gtiff_driver = gdal.GetDriverByName('GTiff') tif_ds = gtiff_driver.Create(tif_filename, src_ds.RasterXSize, src_ds.RasterYSize, 1) tif_ds.GetRasterBand(1).SetRasterColorTable(ct) # ---------------------------------------------------------------------------- # We should copy projection information and so forth at this point. tif_ds.SetProjection(src_ds.GetProjection()) tif_ds.SetGeoTransform(src_ds.GetGeoTransform()) if src_ds.GetGCPCount() > 0: tif_ds.SetGCPs(src_ds.GetGCPs(), src_ds.GetGCPProjection()) # ---------------------------------------------------------------------------- # Actually transfer and dither the data. err = gdal.DitherRGB2PCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), tif_ds.GetRasterBand(1), ct, callback=gdal.TermProgress_nocb) tif_ds = None if tif_filename != dst_filename: tif_ds = gdal.Open(tif_filename) dst_driver.CreateCopy(dst_filename, tif_ds) tif_ds = None os.close(tif_filedesc) gtiff_driver.Delete(tif_filename) return err
def export_GTiff(data_list, output_dir, array, output_name="test_raster.tif", classify=False): '''Exports rasters as geotiffs input: data_list (list) output_dir (string) array (array) output_name (string) classify (boolean) output: None ''' total_ncols = array.shape[1] total_nrows = array.shape[0] if array.dtype == "int32": etype = gdal.GDT_Int32 elif array.dtype == "uint16": print("uint16 set") etype = gdal.GDT_UInt16 else: etype = gdal.GDT_Float32 geotransform = data_list[0].geotransform projection = data_list[0].projection save_location = output_dir + "/geotifs" if not os.path.exists(save_location): os.makedirs(save_location) output_key = output_dir.replace("/", "-").split("-")[1] dst_ds = gdal.GetDriverByName('GTiff').Create( save_location + "/" + output_name + "_" + output_key + ".tif", xsize=total_ncols, ysize=total_nrows, bands=1, eType=etype) dst_ds.SetGeoTransform(geotransform) dst_ds.SetProjection(projection) band = dst_ds.GetRasterBand(1) # maybe you need to run the band.set xxx after writing the array band.WriteArray(array) if classify == True: colors = gdal.ColorTable() #colors = band.GetRasterColorTable() colors.SetColorEntry(1, (245, 245, 220)) colors.SetColorEntry(2, (255, 255, 0)) colors.SetColorEntry(3, (255, 165, 0)) colors.SetColorEntry(4, (255, 0, 0)) colors.SetColorEntry(5, (139, 0, 0)) colors.SetColorEntry(6, (152, 251, 152)) colors.SetColorEntry(7, (0, 238, 0)) colors.SetColorEntry(8, (34, 139, 34)) colors.SetColorEntry(9, (0, 100, 0)) colors.SetColorEntry(0, (0, 0, 0)) band.SetRasterColorTable(colors) band.SetRasterColorInterpretation(gdal.GCI_PaletteIndex) dst_ds.FlushCache() del dst_ds, band print("Geotiff saved in " + save_location + "/" + output_name)
def _gdal_api_proxy_sub(): src_ds = gdal.Open('data/byte.tif') src_cs = src_ds.GetRasterBand(1).Checksum() src_gt = src_ds.GetGeoTransform() src_prj = src_ds.GetProjectionRef() src_data = src_ds.ReadRaster(0, 0, 20, 20) src_md = src_ds.GetMetadata() src_ds = None drv = gdal.IdentifyDriver('data/byte.tif') assert drv.GetDescription() == 'API_PROXY' ds = gdal.GetDriverByName('GTiff').Create('tmp/byte.tif', 1, 1, 3) ds = None src_ds = gdal.Open('data/byte.tif') ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/byte.tif', src_ds, options=['TILED=YES']) got_cs = ds.GetRasterBand(1).Checksum() assert src_cs == got_cs ds = None ds = gdal.Open('tmp/byte.tif', gdal.GA_Update) ds.SetGeoTransform([1, 2, 3, 4, 5, 6]) got_gt = ds.GetGeoTransform() assert src_gt != got_gt ds.SetGeoTransform(src_gt) got_gt = ds.GetGeoTransform() assert src_gt == got_gt assert ds.GetGCPCount() == 0 assert ds.GetGCPProjection() == '', ds.GetGCPProjection() assert not ds.GetGCPs() gcps = [gdal.GCP(0, 1, 2, 3, 4)] sr = osr.SpatialReference() sr.ImportFromEPSG(4326) wkt = sr.ExportToWkt() assert ds.SetGCPs(gcps, wkt) == 0 got_gcps = ds.GetGCPs() assert len(got_gcps) == 1 assert (got_gcps[0].GCPLine == gcps[0].GCPLine and \ got_gcps[0].GCPPixel == gcps[0].GCPPixel and \ got_gcps[0].GCPX == gcps[0].GCPX and \ got_gcps[0].GCPY == gcps[0].GCPY) assert ds.GetGCPProjection() == wkt ds.SetGCPs([], "") assert not ds.GetGCPs() ds.SetProjection('') got_prj = ds.GetProjectionRef() assert src_prj != got_prj ds.SetProjection(src_prj) got_prj = ds.GetProjectionRef() assert src_prj == got_prj ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() assert got_cs == 0 ds.GetRasterBand(1).WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() assert src_cs == got_cs ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() assert got_cs == 0 ds.WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() assert src_cs == got_cs # Not bound to SWIG # ds.AdviseRead(0,0,20,20,20,20) got_data = ds.ReadRaster(0, 0, 20, 20) assert src_data == got_data got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) assert src_data == got_data got_data_weird_spacing = ds.ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) assert len(got_data_weird_spacing) == 32 * (20 - 1) + 20 assert got_data[20:20 + 20] == got_data_weird_spacing[32:32 + 20] got_data_weird_spacing = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) assert len(got_data_weird_spacing) == 32 * (20 - 1) + 20 assert got_data[20:20 + 20] == got_data_weird_spacing[32:32 + 20] got_block = ds.GetRasterBand(1).ReadBlock(0, 0) assert len(got_block) == 256 * 256 assert got_data[20:20 + 20] == got_block[256:256 + 20] ds.FlushCache() ds.GetRasterBand(1).FlushCache() got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) assert src_data == got_data assert len(ds.GetFileList()) == 1 assert ds.AddBand(gdal.GDT_Byte) != 0 got_md = ds.GetMetadata() assert src_md == got_md assert ds.GetMetadataItem('AREA_OR_POINT') == 'Area' assert ds.GetMetadataItem('foo') is None ds.SetMetadataItem('foo', 'bar') assert ds.GetMetadataItem('foo') == 'bar' ds.SetMetadata({'foo': 'baz'}, 'OTHER') assert ds.GetMetadataItem('foo', 'OTHER') == 'baz' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}, 'OTHER') assert ds.GetRasterBand(1).GetMetadataItem('foo', 'OTHER') == 'baw' assert ds.GetMetadataItem('INTERLEAVE', 'IMAGE_STRUCTURE') == 'BAND' assert not ds.GetRasterBand(1).GetMetadata() assert ds.GetRasterBand(1).GetMetadataItem('foo') is None ds.GetRasterBand(1).SetMetadataItem('foo', 'baz') assert ds.GetRasterBand(1).GetMetadataItem('foo') == 'baz' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}) assert ds.GetRasterBand(1).GetMetadataItem('foo') == 'baw' assert ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_GrayIndex ds.GetRasterBand(1).SetColorInterpretation(gdal.GCI_Undefined) ct = ds.GetRasterBand(1).GetColorTable() assert ct is None ct = gdal.ColorTable() ct.SetColorEntry(0, (1, 2, 3)) assert ds.GetRasterBand(1).SetColorTable(ct) == 0 ct = ds.GetRasterBand(1).GetColorTable() assert ct is not None assert ct.GetColorEntry(0) == (1, 2, 3, 255) ct = ds.GetRasterBand(1).GetColorTable() assert ct is not None assert ds.GetRasterBand(1).SetColorTable(None) == 0 ct = ds.GetRasterBand(1).GetColorTable() assert ct is None rat = ds.GetRasterBand(1).GetDefaultRAT() assert rat is None assert ds.GetRasterBand(1).SetDefaultRAT(None) == 0 ref_rat = gdal.RasterAttributeTable() assert ds.GetRasterBand(1).SetDefaultRAT(ref_rat) == 0 rat = ds.GetRasterBand(1).GetDefaultRAT() assert rat is None assert ds.GetRasterBand(1).SetDefaultRAT(None) == 0 rat = ds.GetRasterBand(1).GetDefaultRAT() assert rat is None assert ds.GetRasterBand(1).GetMinimum() is None got_stats = ds.GetRasterBand(1).GetStatistics(0, 0) assert got_stats[3] < 0.0 got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) assert got_stats[0] == 74.0 assert ds.GetRasterBand(1).GetMinimum() == 74.0 assert ds.GetRasterBand(1).GetMaximum() == 255.0 ds.GetRasterBand(1).SetStatistics(1, 2, 3, 4) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) assert got_stats == [1, 2, 3, 4] ds.GetRasterBand(1).ComputeStatistics(0) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) assert got_stats[0] == 74.0 minmax = ds.GetRasterBand(1).ComputeRasterMinMax() assert minmax == (74.0, 255.0) assert ds.GetRasterBand(1).GetOffset() is None assert ds.GetRasterBand(1).GetScale() is None ds.GetRasterBand(1).SetOffset(10.0) assert ds.GetRasterBand(1).GetOffset() == 10.0 ds.GetRasterBand(1).SetScale(2.0) assert ds.GetRasterBand(1).GetScale() == 2.0 ds.BuildOverviews('NEAR', [2]) assert ds.GetRasterBand(1).GetOverviewCount() == 1 assert ds.GetRasterBand(1).GetOverview(-1) is None assert ds.GetRasterBand(1).GetOverview(0) is not None assert ds.GetRasterBand(1).GetOverview(0) is not None got_hist = ds.GetRasterBand(1).GetHistogram() assert len(got_hist) == 256 (minval, maxval, nitems, got_hist2) = ds.GetRasterBand(1).GetDefaultHistogram() assert minval == -0.5 assert maxval == 255.5 assert nitems == 256 assert got_hist == got_hist2 ds.GetRasterBand(1).SetDefaultHistogram(1, 2, [3]) (minval, maxval, nitems, got_hist3) = ds.GetRasterBand(1).GetDefaultHistogram() assert minval == 1 assert maxval == 2 assert nitems == 1 assert got_hist3[0] == 3 got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() assert got_nodatavalue is None ds.GetRasterBand(1).SetNoDataValue(123) got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() assert got_nodatavalue == 123 assert ds.GetRasterBand(1).GetMaskFlags() == 8 assert ds.GetRasterBand(1).GetMaskBand() is not None ret = ds.GetRasterBand(1).DeleteNoDataValue() assert ret == 0 got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() assert got_nodatavalue is None ds.CreateMaskBand(0) assert ds.GetRasterBand(1).GetMaskFlags() == 2 assert ds.GetRasterBand(1).GetMaskBand() is not None ds.GetRasterBand(1).CreateMaskBand(0) assert ds.GetRasterBand(1).HasArbitraryOverviews() == 0 ds.GetRasterBand(1).SetUnitType('foo') assert ds.GetRasterBand(1).GetUnitType() == 'foo' assert ds.GetRasterBand(1).GetCategoryNames() is None ds.GetRasterBand(1).SetCategoryNames(['foo']) assert ds.GetRasterBand(1).GetCategoryNames() == ['foo'] ds.GetRasterBand(1).SetDescription('bar') ds = None gdal.GetDriverByName('GTiff').Delete('tmp/byte.tif')
def process(self): coastlines_ds = gdal.Open(self.coastlines) coastal_band = coastlines_ds.GetRasterBand(1) # coastal_data = coastal_band.ReadAsArray(0, 0, coastlines_ds.RasterXSize, coastlines_ds.RasterYSize ) coastal_data = coastal_band.ReadAsArray(0, 0, self.RasterXSize, self.RasterYSize) # Surface Water band = self.ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, self.ds.RasterXSize, self.ds.RasterYSize) data = (data >= 2) hand_ds = gdal.Open(self.hand_file) hand_band = hand_ds.GetRasterBand(1) hand_data = hand_band.ReadAsArray(0, 0, hand_ds.RasterXSize, hand_ds.RasterYSize) # HAND Masking mask = hand_data == 0 data[mask] = 0 # Oceans mask = hand_data == 255 data[mask] = 0 # Coastal Masking mask = coastal_data > 0 data[mask] = 0 driver = gdal.GetDriverByName("GTIFF") dst_ds = driver.Create(self.swp, self.RasterXSize, self.RasterYSize, 1, gdal.GDT_Byte, ['INTERLEAVE=PIXEL', 'COMPRESS=DEFLATE']) ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry(i, (255, 255, 255, 255)) ct.SetColorEntry(0, (0, 0, 0, 255)) ct.SetColorEntry(1, (255, 255, 255, 255)) ct.SetColorEntry(2, (255, 255, 255, 255)) ct.SetColorEntry(3, (255, 255, 255, 255)) band = dst_ds.GetRasterBand(1) band.SetRasterColorTable(ct) band.WriteArray(data, 0, 0) band.SetNoDataValue(0) #dst_ds.SetGeoTransform( geotransform ) #dst_ds.SetProjection( projection ) dst_ds = None self.ds = None coastlines_ds = None self.convert_to_pgm() #self.convert_to_svg() self.convert_to_geojson(self.res, self.xorg, self.yorg) self.convert_to_topojson() self.execute("rm -f " + self.pnm + ".aux.xml") self.execute("rm -f " + self.pgm) self.execute("rm -f " + self.geojson)
def rgb2pct(src_filename: PathLikeOrStr, pct_filename: Optional[PathLikeOrStr] = None, dst_filename: Optional[PathLikeOrStr] = None, color_count: int = 256, driver_name: Optional[str] = None): # Open source file src_ds = open_ds(src_filename) if src_ds is None: raise Exception(f'Unable to open {src_filename}') if src_ds.RasterCount < 3: raise Exception( f'{src_filename} has {src_ds.RasterCount} band(s), need 3 for inputs red, green and blue.' ) # Ensure we recognise the driver. if not driver_name: driver_name = GetOutputDriverFor(dst_filename) dst_driver = gdal.GetDriverByName(driver_name) if dst_driver is None: raise Exception(f'"{driver_name}" driver not registered.') # Generate palette if pct_filename is None: ct = gdal.ColorTable() err = gdal.ComputeMedianCutPCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), color_count, ct, callback=gdal.TermProgress_nocb) else: ct = get_color_table(pct_filename) # Create the working file. We have to use TIFF since there are few formats # that allow setting the color table after creation. if driver_name.lower() == 'gtiff': tif_filename = dst_filename else: import tempfile tif_filedesc, tif_filename = tempfile.mkstemp(suffix='.tif') gtiff_driver = gdal.GetDriverByName('GTiff') tif_ds = gtiff_driver.Create(tif_filename, src_ds.RasterXSize, src_ds.RasterYSize, 1) tif_ds.GetRasterBand(1).SetRasterColorTable(ct) # ---------------------------------------------------------------------------- # We should copy projection information and so forth at this point. tif_ds.SetProjection(src_ds.GetProjection()) tif_ds.SetGeoTransform(src_ds.GetGeoTransform()) if src_ds.GetGCPCount() > 0: tif_ds.SetGCPs(src_ds.GetGCPs(), src_ds.GetGCPProjection()) # ---------------------------------------------------------------------------- # Actually transfer and dither the data. err = gdal.DitherRGB2PCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), tif_ds.GetRasterBand(1), ct, callback=gdal.TermProgress_nocb) if err != gdal.CE_None: raise Exception('DitherRGB2PCT failed') if tif_filename == dst_filename: dst_ds = tif_ds else: dst_ds = dst_driver.CreateCopy(dst_filename or '', tif_ds) tif_ds = None os.close(tif_filedesc) gtiff_driver.Delete(tif_filename) return dst_ds
def doit(pct_filename=None, src_filename=None, dst_filename=None, color_count=256, frmt=None): # Open source file src_ds = gdal.Open(src_filename) if src_ds is None: print('Unable to open %s' % src_filename) return None, 1 if src_ds.RasterCount < 3: print('%s has %d band(s), need 3 for inputs red, green and blue.' % (src_filename, src_ds.RasterCount)) return None, 1 # Ensure we recognise the driver. if frmt is None: frmt = GetOutputDriverFor(dst_filename) dst_driver = gdal.GetDriverByName(frmt) if dst_driver is None: print('"%s" driver not registered.' % frmt) return None, 1 # Generate palette ct = gdal.ColorTable() if pct_filename is None: err = gdal.ComputeMedianCutPCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), color_count, ct, callback=gdal.TermProgress_nocb) else: pct_ds = gdal.Open(pct_filename) ct = pct_ds.GetRasterBand(1).GetRasterColorTable().Clone() # Create the working file. We have to use TIFF since there are few formats # that allow setting the color table after creation. if format == 'GTiff': tif_filename = dst_filename else: import tempfile tif_filedesc, tif_filename = tempfile.mkstemp(suffix='.tif') gtiff_driver = gdal.GetDriverByName('GTiff') tif_ds = gtiff_driver.Create(tif_filename, src_ds.RasterXSize, src_ds.RasterYSize, 1) tif_ds.GetRasterBand(1).SetRasterColorTable(ct) # ---------------------------------------------------------------------------- # We should copy projection information and so forth at this point. tif_ds.SetProjection(src_ds.GetProjection()) tif_ds.SetGeoTransform(src_ds.GetGeoTransform()) if src_ds.GetGCPCount() > 0: tif_ds.SetGCPs(src_ds.GetGCPs(), src_ds.GetGCPProjection()) # ---------------------------------------------------------------------------- # Actually transfer and dither the data. err = gdal.DitherRGB2PCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), tif_ds.GetRasterBand(1), ct, callback=gdal.TermProgress_nocb) if tif_filename == dst_filename: dst_ds = tif_ds else: dst_ds = dst_driver.CreateCopy(dst_filename, tif_ds) tif_ds = None gtiff_driver.Delete(tif_filename) os.close(tif_filedesc) return dst_ds, err
def get_fixed_color_table(c=(0, 0, 0, 0), count=1): color_table = gdal.ColorTable() for i in range(count): color_table.SetColorEntry(i, c) return color_table
def gdal_api_proxy_sub(): src_ds = gdal.Open('data/byte.tif') src_cs = src_ds.GetRasterBand(1).Checksum() src_gt = src_ds.GetGeoTransform() src_prj = src_ds.GetProjectionRef() src_data = src_ds.ReadRaster(0, 0, 20, 20) src_md = src_ds.GetMetadata() src_ds = None drv = gdal.IdentifyDriver('data/byte.tif') if drv.GetDescription() != 'API_PROXY': gdaltest.post_reason('fail') return 'fail' ds = gdal.GetDriverByName('GTiff').Create('tmp/byte.tif', 1, 1, 3) ds = None src_ds = gdal.Open('data/byte.tif') ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/byte.tif', src_ds, options=['TILED=YES']) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' ds = None ds = gdal.Open('tmp/byte.tif', gdal.GA_Update) ds.SetGeoTransform([1, 2, 3, 4, 5, 6]) got_gt = ds.GetGeoTransform() if src_gt == got_gt: gdaltest.post_reason('fail') return 'fail' ds.SetGeoTransform(src_gt) got_gt = ds.GetGeoTransform() if src_gt != got_gt: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPCount() != 0: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPProjection() != '': print(ds.GetGCPProjection()) gdaltest.post_reason('fail') return 'fail' if len(ds.GetGCPs()) != 0: gdaltest.post_reason('fail') return 'fail' gcps = [gdal.GCP(0, 1, 2, 3, 4)] ds.SetGCPs(gcps, "foo") got_gcps = ds.GetGCPs() if len(got_gcps) != 1: gdaltest.post_reason('fail') return 'fail' if got_gcps[0].GCPLine != gcps[0].GCPLine or \ got_gcps[0].GCPPixel != gcps[0].GCPPixel or \ got_gcps[0].GCPX != gcps[0].GCPX or \ got_gcps[0].GCPY != gcps[0].GCPY: gdaltest.post_reason('fail') return 'fail' if ds.GetGCPProjection() != 'foo': gdaltest.post_reason('fail') print(ds.GetGCPProjection()) return 'fail' ds.SetGCPs([], "") if len(ds.GetGCPs()) != 0: gdaltest.post_reason('fail') return 'fail' ds.SetProjection('') got_prj = ds.GetProjectionRef() if src_prj == got_prj: gdaltest.post_reason('fail') return 'fail' ds.SetProjection(src_prj) got_prj = ds.GetProjectionRef() if src_prj != got_prj: gdaltest.post_reason('fail') print(src_prj) print(got_prj) return 'fail' ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() if 0 != got_cs: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).Fill(0) got_cs = ds.GetRasterBand(1).Checksum() if 0 != got_cs: gdaltest.post_reason('fail') return 'fail' ds.WriteRaster(0, 0, 20, 20, src_data) got_cs = ds.GetRasterBand(1).Checksum() if src_cs != got_cs: gdaltest.post_reason('fail') return 'fail' # Not bound to SWIG # ds.AdviseRead(0,0,20,20,20,20) got_data = ds.ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' got_data_weird_spacing = ds.ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) if len(got_data_weird_spacing) != 32 * (20 - 1) + 20: gdaltest.post_reason('fail') print(len(got_data_weird_spacing)) return 'fail' if got_data[20:20 + 20] != got_data_weird_spacing[32:32 + 20]: gdaltest.post_reason('fail') return 'fail' got_data_weird_spacing = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20, buf_pixel_space=1, buf_line_space=32) if len(got_data_weird_spacing) != 32 * (20 - 1) + 20: gdaltest.post_reason('fail') print(len(got_data_weird_spacing)) return 'fail' if got_data[20:20 + 20] != got_data_weird_spacing[32:32 + 20]: gdaltest.post_reason('fail') return 'fail' got_block = ds.GetRasterBand(1).ReadBlock(0, 0) if len(got_block) != 256 * 256: gdaltest.post_reason('fail') return 'fail' if got_data[20:20 + 20] != got_block[256:256 + 20]: gdaltest.post_reason('fail') return 'fail' ds.FlushCache() ds.GetRasterBand(1).FlushCache() got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20) if src_data != got_data: gdaltest.post_reason('fail') return 'fail' if len(ds.GetFileList()) != 1: gdaltest.post_reason('fail') return 'fail' if ds.AddBand(gdal.GDT_Byte) == 0: gdaltest.post_reason('fail') return 'fail' got_md = ds.GetMetadata() if src_md != got_md: gdaltest.post_reason('fail') print(src_md) print(got_md) return 'fail' if ds.GetMetadataItem('AREA_OR_POINT') != 'Area': gdaltest.post_reason('fail') return 'fail' if ds.GetMetadataItem('foo') is not None: gdaltest.post_reason('fail') return 'fail' ds.SetMetadataItem('foo', 'bar') if ds.GetMetadataItem('foo') != 'bar': gdaltest.post_reason('fail') return 'fail' ds.SetMetadata({'foo': 'baz'}, 'OTHER') if ds.GetMetadataItem('foo', 'OTHER') != 'baz': gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}, 'OTHER') if ds.GetRasterBand(1).GetMetadataItem('foo', 'OTHER') != 'baw': gdaltest.post_reason('fail') return 'fail' if ds.GetMetadataItem('INTERLEAVE', 'IMAGE_STRUCTURE') != 'BAND': gdaltest.post_reason('fail') return 'fail' if len(ds.GetRasterBand(1).GetMetadata()) != 0: gdaltest.post_reason('fail') print(ds.GetRasterBand(1).GetMetadata()) return 'fail' if ds.GetRasterBand(1).GetMetadataItem('foo') is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadataItem('foo', 'baz') if ds.GetRasterBand(1).GetMetadataItem('foo') != 'baz': gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetMetadata({'foo': 'baw'}) if ds.GetRasterBand(1).GetMetadataItem('foo') != 'baw': gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetColorInterpretation() != gdal.GCI_GrayIndex: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetColorInterpretation(gdal.GCI_Undefined) ct = ds.GetRasterBand(1).GetColorTable() if ct is not None: gdaltest.post_reason('fail') return 'fail' ct = gdal.ColorTable() ct.SetColorEntry(0, (1, 2, 3)) if ds.GetRasterBand(1).SetColorTable(ct) != 0: gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is None: gdaltest.post_reason('fail') return 'fail' if ct.GetColorEntry(0) != (1, 2, 3, 255): gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetColorTable(None) != 0: gdaltest.post_reason('fail') return 'fail' ct = ds.GetRasterBand(1).GetColorTable() if ct is not None: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetDefaultRAT(None) != 0: gdaltest.post_reason('fail') return 'fail' ref_rat = gdal.RasterAttributeTable() if ds.GetRasterBand(1).SetDefaultRAT(ref_rat) != 0: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).SetDefaultRAT(None) != 0: gdaltest.post_reason('fail') return 'fail' rat = ds.GetRasterBand(1).GetDefaultRAT() if rat is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMinimum() is not None: gdaltest.post_reason('fail') return 'fail' got_stats = ds.GetRasterBand(1).GetStatistics(0, 0) if got_stats[3] >= 0.0: gdaltest.post_reason('fail') return 'fail' got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats[0] != 74.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMinimum() != 74.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaximum() != 255.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetStatistics(1, 2, 3, 4) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats != [1, 2, 3, 4]: print(got_stats) gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).ComputeStatistics(0) got_stats = ds.GetRasterBand(1).GetStatistics(1, 1) if got_stats[0] != 74.0: gdaltest.post_reason('fail') return 'fail' minmax = ds.GetRasterBand(1).ComputeRasterMinMax() if minmax != (74.0, 255.0): gdaltest.post_reason('fail') print(minmax) return 'fail' if ds.GetRasterBand(1).GetOffset() != 0.0: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetScale() != 1.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetOffset(10.0) if ds.GetRasterBand(1).GetOffset() != 10.0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetScale(2.0) if ds.GetRasterBand(1).GetScale() != 2.0: gdaltest.post_reason('fail') return 'fail' ds.BuildOverviews('NEAR', [2]) if ds.GetRasterBand(1).GetOverviewCount() != 1: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(-1) is not None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(0) is None: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetOverview(0) is None: gdaltest.post_reason('fail') return 'fail' got_hist = ds.GetRasterBand(1).GetHistogram() if len(got_hist) != 256: gdaltest.post_reason('fail') return 'fail' (minval, maxval, nitems, got_hist2) = ds.GetRasterBand(1).GetDefaultHistogram() if minval != -0.5: gdaltest.post_reason('fail') return 'fail' if maxval != 255.5: gdaltest.post_reason('fail') return 'fail' if nitems != 256: gdaltest.post_reason('fail') return 'fail' if got_hist != got_hist2: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetDefaultHistogram(1, 2, [3]) (minval, maxval, nitems, got_hist3) = ds.GetRasterBand(1).GetDefaultHistogram() if minval != 1: gdaltest.post_reason('fail') return 'fail' if maxval != 2: gdaltest.post_reason('fail') return 'fail' if nitems != 1: gdaltest.post_reason('fail') return 'fail' if got_hist3[0] != 3: gdaltest.post_reason('fail') return 'fail' got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() if got_nodatavalue is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetNoDataValue(123) got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() if got_nodatavalue != 123: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskFlags() != 8: gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskBand() is None: gdaltest.post_reason('fail') return 'fail' ret = ds.GetRasterBand(1).DeleteNoDataValue() if ret != 0: gdaltest.post_reason('fail') return 'fail' got_nodatavalue = ds.GetRasterBand(1).GetNoDataValue() if got_nodatavalue is not None: gdaltest.post_reason('fail') return 'fail' ds.CreateMaskBand(0) if ds.GetRasterBand(1).GetMaskFlags() != 2: print(ds.GetRasterBand(1).GetMaskFlags()) gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetMaskBand() is None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).CreateMaskBand(0) if ds.GetRasterBand(1).HasArbitraryOverviews() != 0: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetUnitType('foo') if ds.GetRasterBand(1).GetUnitType() != 'foo': gdaltest.post_reason('fail') return 'fail' if ds.GetRasterBand(1).GetCategoryNames() is not None: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetCategoryNames(['foo']) if ds.GetRasterBand(1).GetCategoryNames() != ['foo']: gdaltest.post_reason('fail') return 'fail' ds.GetRasterBand(1).SetDescription('bar') ds = None gdal.GetDriverByName('GTiff').Delete('tmp/byte.tif') return 'success'
def CreateLevel(l, geojsonDir, fileName, src_ds): projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() band = src_ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize if os.path.exists(fileName): return driver = gdal.GetDriverByName("GTiff") dst_ds_dataset = driver.Create(fileName, src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte, ['COMPRESS=DEFLATE']) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) data[data >= l] = 255 data[data < l] = 0 count = (data >= l).sum() print "level", l, " count:", count if count > 0: o_band = dst_ds_dataset.GetRasterBand(1) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band.WriteArray(data.astype('i1'), 0, 0) ct = gdal.ColorTable() ct.SetColorEntry(0, (255, 255, 255, 255)) ct.SetColorEntry(255, (255, 0, 0, 255)) o_band.SetRasterColorTable(ct) dst_ds_dataset = None print "Created", fileName cmd = "gdal_translate -q -of PNM " + fileName + " " + fileName + ".pgm" execute(cmd) # -i invert before processing # -t 2 suppress speckles of up to this many pixels. # -a 1.5 set the corner threshold parameter # -z black specify how to resolve ambiguities in path decomposition. Must be one of black, white, right, left, minority, majority, or random. Default is minority # -x scaling factor # -L left margin # -B bottom margin cmd = str.format( "potrace -i -z black -a 1.5 -t 3 -b geojson -o {0} {1} -x {2} -L {3} -B {4} ", fileName + ".geojson", fileName + ".pgm", pres, xorg, ymax) execute(cmd) #cmd = str.format("node set_geojson_property.js --file {0} --prop frost={1}", fileName+".geojson", frost) #execute(cmd) cmd = str.format( "topojson -o {0} --simplify-proportion 0.5 -p swe={1} -- swe={2}", fileName + ".topojson", l, fileName + ".geojson") execute(cmd) # convert it back to json cmd = "topojson-geojson --precision 4 -o %s %s" % ( geojsonDir, fileName + ".topojson") execute(cmd) # rename file output_file = "swe_level_%d.geojson" % l cmd = "mv %s %s" % (os.path.join( geojsonDir, "swe.json"), os.path.join(geojsonDir, output_file)) execute(cmd)
def save_hand_as_png(self): hand_ds = gdal.Open( self.hand_file ) if hand_ds is None: print('ERROR: hand file no data:') sys.exit(-1) RasterXSize = hand_ds.RasterXSize RasterYSize = hand_ds.RasterYSize RasterCount = hand_ds.RasterCount if verbose: print "Hand file", RasterXSize, RasterYSize, RasterCount driver = gdal.GetDriverByName( "GTiff" ) dst_ds = driver.Create( self.hand_rgb, RasterXSize, RasterYSize, 1, gdal.GDT_Byte, [ 'INTERLEAVE=PIXEL', 'COMPRESS=DEFLATE' ] ) ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry( i, (255, 255, 255, 255) ) ct.SetColorEntry( 0, (0, 0, 0, 255) ) ct.SetColorEntry( 1, (8, 48, 107, 255) ) ct.SetColorEntry( 2, (8, 48, 107, 255) ) ct.SetColorEntry( 3, (8, 81, 156, 255) ) ct.SetColorEntry( 4, (8, 81, 156, 255) ) ct.SetColorEntry( 5, (33, 113, 181, 255) ) ct.SetColorEntry( 6, (33, 113, 181, 255) ) ct.SetColorEntry( 7, (66, 146, 198, 255) ) ct.SetColorEntry( 8, (66, 146, 198, 255) ) ct.SetColorEntry( 9, (107, 174, 214, 255) ) ct.SetColorEntry( 10, (107, 174, 214, 255) ) ct.SetColorEntry( 11, (158, 202, 225, 255) ) ct.SetColorEntry( 12, (158, 202, 225, 255) ) ct.SetColorEntry( 13, (198, 219, 239, 255) ) ct.SetColorEntry( 14, (198, 219, 239, 255) ) ct.SetColorEntry( 15, (222, 235, 2247, 255) ) ct.SetColorEntry( 16, (222, 235, 2247, 255) ) ct.SetColorEntry( 17, (247, 251, 255, 255) ) ct.SetColorEntry( 18, (247, 251, 255, 255) ) # ocean ct.SetColorEntry( 255, (0, 0, 0, 0) ) hand = hand_ds.GetRasterBand(1) data = hand.ReadAsArray(0, 0, RasterXSize, RasterYSize ) band = dst_ds.GetRasterBand(1) band.SetRasterColorTable(ct) band.WriteArray(data, 0, 0) band.SetNoDataValue(0) # copy projection projection = hand_ds.GetProjection() geotransform = hand_ds.GetGeoTransform() dst_ds.SetGeoTransform( geotransform ) dst_ds.SetProjection( projection ) dst_ds = None hand_ds = None
def calculation(output_directory, inputs_raster_selection, inputs_parameter_selection): #TODO the folowing code must be changed by the code of the calculation module # generate the output raster file output_raster1 = generate_output_file_tif(output_directory) #retrieve the inputs layes input_raster_selection = inputs_raster_selection["heat_tot_curr_density"] #retrieve the inputs all input defined in the signature factor = int(inputs_parameter_selection["reduction_factor"]) # TODO this part bellow must be change by the CM provider ds = gdal.Open(input_raster_selection) ds_band = ds.GetRasterBand(1) #---------------------------------------------------- pixel_values = ds.ReadAsArray() #----------Reduction factor---------------- pixel_values_modified = pixel_values / float(factor) hdm_sum = pixel_values_modified.sum() gtiff_driver = gdal.GetDriverByName('GTiff') #print () out_ds = gtiff_driver.Create(output_raster1, ds_band.XSize, ds_band.YSize, 1, gdal.GDT_UInt16, [ 'compress=DEFLATE', 'TILED=YES', 'TFW=YES', 'ZLEVEL=9', 'PREDICTOR=1' ]) out_ds.SetProjection(ds.GetProjection()) out_ds.SetGeoTransform(ds.GetGeoTransform()) ct = gdal.ColorTable() ct.SetColorEntry(0, (0, 0, 0, 255)) ct.SetColorEntry(1, (110, 220, 110, 255)) out_ds.GetRasterBand(1).SetColorTable(ct) out_ds_band = out_ds.GetRasterBand(1) out_ds_band.SetNoDataValue(0) out_ds_band.WriteArray(pixel_values_modified) del out_ds # output geneneration of the output graphics = [] vector_layers = [] result = dict() result['name'] = 'CM Heat density divider' result['indicator'] = [{ "unit": "KWh", "name": "Heat density total divided by {}".format(factor), "value": str(hdm_sum) }] result['graphics'] = graphics result['vector_layers'] = vector_layers result['raster_layers'] = [{ "name": "layers of heat_densiy {}".format(factor), "path": output_raster1 }] return result
def dwel_points2atproj(infile, outfile, \ xyzcols, \ indexcol, indexfunc, \ pixelcol, pixelfunc, \ outres=2.0, \ minzen=0.0, maxzen=91.0, \ camheight=0.0, \ pulsemax=True, \ classflag=True, \ pulsenocol=5, \ intensitycol=3, \ north_az=0.0): """ Project 3D points to a AT projection image. Args: infile: outfile: xyzcols (list or numpy array of three elements): column indices of X, Y and Z coordinates, with first column being 0. indexcol: indexfunc (function handle): ALWAYS check if input variable to indexfunc has more than one element. ONLY pass sequence-like numpy array variable to indexfunc, NOT scalar. pixelcol: pixelfunc: outres (float): resolution of AT projection, unit: mrad. minzen (float): minimum zenith angle of AT projection, unit: deg. minzen (float): maximum zenith angle of AT projection, unit: deg. Returns: """ # set debug switch debug = False # set some parameters for now. may need to move to function argument in # future. # read points print "Loading points" points = np.loadtxt(infile, dtype=np.float32, delimiter=',', comments="//") # preprocess points if pulsemax: # Now the two indices are used only when pulsemax is switched on. cind = {'shotnum':pulsenocol, 'd_I_nir':intensitycol} print "Selecting out maximum of each pulse" index_max = hunt_max_return(points[:, \ [cind['d_I_nir'], cind['shotnum']]]) points = points[index_max, :] npts = points.shape[0] # convert zenith angle range to radians maxzen = maxzen / 180.0 * np.pi minzen = minzen / 180.0 * np.pi outres = outres * 0.001 # calculate the dimension of projection image nrows = np.fix(2*np.pi / outres).astype(int) + 1 ncols = np.fix((maxzen - minzen) / outres).astype(int) + 1 # set up a 2D numpy array as the projection-image outimage = np.zeros((nrows, ncols)) outimage.fill(np.nan) # calculate azimuth angles of all points ptsaz = np.arctan2(points[:, xyzcols[0]], points[:, xyzcols[1]]) tmpind = np.where(ptsaz < 0.0)[0] ptsaz[tmpind] = ptsaz[tmpind] + 2*np.pi ptsaz = ptsaz - north_az tmpind = np.where(ptsaz < 0.0)[0] ptsaz[tmpind] = ptsaz[tmpind] + 2*np.pi tmpind = np.where(ptsaz > 2*np.pi)[0] ptsaz[tmpind] = ptsaz[tmpind] - 2*np.pi # calculate zenith angles of all points xoyrg = np.sqrt(points[:, xyzcols[0]]**2 + points[:, xyzcols[1]]**2) ptszen = np.arctan2(xoyrg, points[:, xyzcols[2]]-camheight) # calculate the pixellocation on projection-image of all points ptsoutx, ptsouty = at_tp2xy(ptszen, ptsaz) ptsoutcol = np.fix(ptsoutx/outres+0.5*np.sign(ptsoutx)).astype(int) ptsoutrow = np.fix(ptsouty/outres+0.5*np.sign(ptsouty)).astype(int) # set up a 1D numpy array to store flattened indices in projection-image of all # points. if a point is outside the projection extent, its index is assigned # -1 ptsoutind = np.zeros_like(ptsoutrow)-1 # select points inside projection extent rowinflag = np.logical_and(ptsoutrow > 0, ptsoutrow < nrows) colinflag = np.logical_and(ptsoutcol > 0, ptsoutcol < ncols) bothinflag = np.logical_and(rowinflag, colinflag) # calcualte the flattened indices in projection-image of these points ptsoutind[bothinflag] = \ np.ravel_multi_index((ptsoutrow[bothinflag], ptsoutcol[bothinflag]), \ (nrows, ncols)) # Now sorted the flattened indices in projection-image of all points and # record the indices of each point in original input point cloud. ptsinind = np.argsort(ptsoutind) ptsoutind = ptsoutind[ptsinind] # find out each projection-image pixel has how many points are located inside. _, ui_inverse, ucounts = \ np.unique(ptsoutind, return_inverse=True, return_counts=True) ptsoutcounts = ucounts[ui_inverse] # number of points outside projection extent, i.e. ptsoutind == -1 ninvalid = np.sum(ptsoutind==-1) # from valid points inside the projection extent, start projecting! print "Projecting!" p = ninvalid while p < npts: # flattened indice in projection-image of current pixel to be assigned # value currentoutind = ptsoutind[p] if debug: # check if count of points in a pixel is correct if np.sum(np.fabs(ptsoutcounts[p:p+ptsoutcounts[p]]-ptsoutcounts[p])) > 1e-10: print "Error, counts of points in a projected is wrong at " \ +"{0:d}".format(p) # get points located in this pixel tmppoints = points[ptsinind[p:p+ptsoutcounts[p]], :] # Call indexfunc to choose points to assign this pixel. tmpind = indexfunc(tmppoints[:, indexcol]) if len(tmpind) > 0: tmppoints = tmppoints[tmpind, :] if debug and pulsemax: # test tmpu, tmpucounts = np.unique(tmppoints[:, cind['shotnum']], return_counts=True) if np.count_nonzero(tmpucounts>1) > 0: print "Error, maximum of each pulse are not select out properly" outimage[np.unravel_index(currentoutind, (nrows, ncols))] \ = np.asscalar(pixelfunc(np.array([tmppoints[:, pixelcol]]))) # go to next projection-image pixel p += ptsoutcounts[p] # before export the image, transpose the array so that it conforms with the # view from bottom to up outimage = np.transpose(outimage) # export image resolution dpi = 72 # get image size outwidth = outimage.shape[1]/float(dpi) outheight = outimage.shape[0]/float(dpi) outimage_masked = np.ma.masked_where(np.isnan(outimage), outimage) outpngfile = outfile.rsplit(".") outpngfile = ".".join(outpngfile[0:-1]) # plt.figure() # plt.axis("off") # plt.imshow(outimage_masked, vmin=np.percentile(outimage[~np.isnan(outimage)], 2), \ # vmax=np.percentile(outimage[~np.isnan(outimage)], 98)) # plt.savefig(outpngfile+".png", dpi=dpi, bbox_inches="tight") if classflag: # now write projection image to ENVI classification file tmpflag = np.logical_not(np.isnan(outimage)) vmin=np.percentile(outimage[tmpflag], 2) vmax=np.percentile(outimage[tmpflag], 98) outimage[np.isnan(outimage)] = 0 outimage = outimage.astype(np.uint8) cls_list = np.unique(outimage[tmpflag]) cm_used = plt.get_cmap("jet", len(cls_list)) mpl.image.imsave(outpngfile+".png", outimage_masked, dpi=dpi, \ vmin=vmin, vmax=vmax, \ cmap=cm_used) outformat = "ENVI" driver = gdal.GetDriverByName(outformat) outds = driver.Create(outfile, outimage.shape[1], outimage.shape[0], 1, gdal.GDT_Byte) outband = outds.GetRasterBand(1) outband.WriteArray(outimage) outband.SetCategoryNames(["0"] + ["{0:d}".format(int(cn)) for cn in cls_list]) outband.SetDescription("DWEL Classification") outct = gdal.ColorTable() color_table = np.vstack(( [0, 0, 0, 1], cm_used(range(len(cls_list))) )) for i in range(len(color_table)): outct.SetColorEntry(i, tuple((color_table[i, :]*255).astype(np.int))) outband.SetColorTable(outct) outband.FlushCache() # ENVI meta data envi_meta_dict = dict(projection_type="AT projection", \ inputs_for_projection=infile, \ create_time=time.strftime("%c"), \ camera_height="{0:f}".format(camheight)) for kw in envi_meta_dict.keys(): outds.SetMetadataItem(kw, envi_meta_dict[kw], "ENVI") outds.FlushCache() # close the dataset outds = None else: mpl.image.imsave(outpngfile+".png", outimage_masked, dpi=dpi, \ vmin=np.percentile(outimage[~np.isnan(outimage)], 2), \ vmax=np.percentile(outimage[~np.isnan(outimage)], 98), \ cmap=plt.get_cmap("gray")) outimagemask = np.isnan(outimage) outimage[outimagemask] = 0 # now write projection image to ENVI standard file outimage.astype(np.float32) outformat = "ENVI" driver = gdal.GetDriverByName(outformat) outds = driver.Create(outfile, outimage.shape[1], outimage.shape[0], 2, gdal.GDT_Float32) outds.GetRasterBand(1).WriteArray(outimage) outds.FlushCache() # write a mask band outimagemask = np.logical_not(outimagemask) outimagemask.astype(np.float32) outds.GetRasterBand(2).WriteArray(outimagemask) outds.FlushCache() # close the dataset outds = None # Now write envi header file manually. NOT by gdal...which can't do this # job... set header file # get header file name strlist = outfile.rsplit('.') hdrfile = ".".join(strlist[0:-1]) + ".hdr" if os.path.isfile(hdrfile): os.remove(hdrfile) print "Old header file removed: " + hdrfile hdrfile = outfile + ".hdr" hdrstr = \ "ENVI\n" + \ "description = {\n" + \ "AT projection image of DWEL point cloud, \n" + \ infile + ", \n" + \ "Create, [" + time.strftime("%c") + "], \n" + \ "Camera height for projection, {0:f}}}\n".format(camheight) + \ "samples = " + "{0:d}".format(outimage.shape[1]) + "\n" \ "lines = " + "{0:d}".format(outimage.shape[0]) + "\n" \ "bands = 2\n" + \ "header offset = 0\n" + \ "file type = ENVI standard\n" + \ "data type = 4\n" + \ "interleave = bsq\n" + \ "sensor type = Unknown\n" + \ "byte order = 0\n" + \ "wavelength units = unknown\n" + \ "band names = {pts_proj, mask}" with open(hdrfile, 'w') as hdrf: hdrf.write(hdrstr) return 0
if src_ds.RasterCount < 3: print('%s has %d band(s), need 3 for inputs red, green and blue.' \ % (src_filename, src_ds.RasterCount)) sys.exit(1) # Ensure we recognise the driver. dst_driver = gdal.GetDriverByName(format) if dst_driver is None: print('"%s" driver not registered.' % format) sys.exit(1) # Generate palette ct = gdal.ColorTable() if pct_filename is None: err = gdal.ComputeMedianCutPCT(src_ds.GetRasterBand(1), src_ds.GetRasterBand(2), src_ds.GetRasterBand(3), color_count, ct, callback=gdal.TermProgress, callback_data='Generate PCT') else: pct_ds = gdal.Open(pct_filename) ct = pct_ds.GetRasterBand(1).GetRasterColorTable().Clone() # Create the working file. We have to use TIFF since there are few formats # that allow setting the color table after creation.
def MakeBrowseImage(src_ds, browse_filename, subset_filename, osm_bg_image, sw_osm_image): projection = src_ds.GetProjection() geotransform = src_ds.GetGeoTransform() band = src_ds.GetRasterBand(1) data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize) xorg = geotransform[0] yorg = geotransform[3] pres = geotransform[1] xmax = xorg + geotransform[1] * src_ds.RasterXSize ymax = yorg - geotransform[1] * src_ds.RasterYSize deltaX = xmax - xorg deltaY = ymax - yorg driver = gdal.GetDriverByName("GTiff") if force or not os.path.isfile(browse_filename): dst_ds_dataset = driver.Create(browse_filename, src_ds.RasterXSize, src_ds.RasterYSize, 2, gdal.GDT_Byte, ['COMPRESS=DEFLATE', 'ALPHA=YES']) dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) data[data <= 0] = 0 data[numpy.logical_and(data > 0, data <= 1)] = 1 data[numpy.logical_and(data > 1, data <= 2)] = 2 data[numpy.logical_and(data > 2, data <= 3)] = 3 data[numpy.logical_and(data > 3, data <= 5)] = 5 data[numpy.logical_and(data > 5, data <= 8)] = 8 data[numpy.logical_and(data > 8, data <= 13)] = 13 data[data > 13] = 21 dst_ds_dataset.SetGeoTransform(geotransform) dst_ds_dataset.SetProjection(projection) o_band = dst_ds_dataset.GetRasterBand(1) o_band.WriteArray(data.astype('i1'), 0, 0) a_band = dst_ds_dataset.GetRasterBand(2) data[data > 0] = 255 data[data < 0] = 0 a_band.WriteArray(data.astype('i1'), 0, 0) ct = gdal.ColorTable() ct = gdal.ColorTable() for i in range(256): ct.SetColorEntry(i, (0, 0, 0, 0)) ct.SetColorEntry(0, (0, 0, 0, 0)) ct.SetColorEntry(1, (254, 229, 217, 255)) ct.SetColorEntry(2, (252, 187, 161, 255)) ct.SetColorEntry(3, (252, 146, 114, 255)) ct.SetColorEntry(5, (251, 106, 74, 255)) ct.SetColorEntry(8, (239, 59, 44, 255)) ct.SetColorEntry(13, (203, 24, 29, 255)) ct.SetColorEntry(21, (153, 0, 13, 255)) o_band.SetRasterColorTable(ct) band.SetNoDataValue(0) dst_ds_dataset = None print "Created Browse Image:", browse_filename # subset it minX = xorg + deltaX / 4 maxX = xmax - deltaX / 4 minY = ymax - deltaY / 2 maxY = yorg # centerlon = (minX + maxX) / 2 centerlat = (minY + maxY) / 2 zoom = 8 if force or not os.path.isfile(osm_bg_image): mapbox_image(centerlat, centerlon, zoom, src_ds.RasterXSize / 8, src_ds.RasterYSize / 8, osm_bg_image) ullon, ullat, lrlon, lrlat = bbox(centerlat, centerlon, zoom, src_ds.RasterXSize / 8, src_ds.RasterYSize / 8) if force or not os.path.isfile(subset_filename): ofStr = ' -of GTiff ' bbStr = ' -te %s %s %s %s ' % (ullon, lrlat, lrlon, ullat) #resStr = ' -tr %s %s '%(pres, pres) resStr = ' ' projectionStr = ' -t_srs EPSG:4326 ' overwriteStr = ' -overwrite ' # Overwrite output if it exists additionalOptions = ' -co COMPRESS=DEFLATE -setci ' # Additional options wh = ' -ts %d %d ' % (src_ds.RasterXSize / 8, src_ds.RasterYSize / 8) warpOptions = ofStr + bbStr + projectionStr + resStr + overwriteStr + additionalOptions + wh warpCMD = 'gdalwarp ' + warpOptions + browse_filename + ' ' + subset_filename execute(warpCMD) # superimpose the suface water over map background #if force or not os.path.isfile(sw_osm_image): if force or not os.path.isfile(sw_osm_image): cmd = str.format("composite -gravity center {0} {1} {2}", subset_filename, osm_bg_image, sw_osm_image) execute(cmd)
def process(self): driver = gdal.GetDriverByName("GTiff") if force or not os.path.exists(self.hand_output_file): if verbose: print "processing ", self.input_file src_ds = gdal.Open(self.input_file) band = src_ds.GetRasterBand(1) output_data = band.ReadAsArray(0, 0, src_ds.RasterXSize, src_ds.RasterYSize) self.metadata = src_ds.GetDriver().GetMetadata() self.geotransform = src_ds.GetGeoTransform() self.projection = src_ds.GetProjection() self.north = self.geotransform[3] self.west = self.geotransform[0] self.south = self.north - self.geotransform[1] * src_ds.RasterYSize self.east = self.west + self.geotransform[1] * src_ds.RasterXSize if verbose: print("north: %9.5f south:%9.5f west:%9.5f east:%9.5f" % (self.north, self.south, self.west, self.east)) print("bbox: %9.5f,%9.5f,%9.5f,%9.5f" % (self.west, self.south, self.east, self.north)) xorg = self.geotransform[0] yorg = self.geotransform[3] pres = self.geotransform[1] xmax = xorg + self.geotransform[1] * src_ds.RasterXSize ymax = yorg - self.geotransform[1] * src_ds.RasterYSize # Create reference water/watershed vectors as well # Needed for refining hand & remove coastal zones cmd = str.format( os.path.join("geojson_osm.py") + " --dir {0} --bbox {1} {2} {3} {4} --img {5} {6} --res {7}", self.outpath, xorg, ymax, xmax, yorg, src_ds.RasterXSize, src_ds.RasterYSize, pres) self.execute(cmd) if verbose: print("get HAND data:" + self.hand_file) hand_ds = gdal.Open(self.hand_file) hand_band = hand_ds.GetRasterBand(1) hand_data = hand_band.ReadAsArray(0, 0, hand_ds.RasterXSize, hand_ds.RasterYSize) if verbose: print("get coastlines data:" + self.coastlines) coastlines_ds = gdal.Open(self.coastlines) coastal_band = coastlines_ds.GetRasterBand(1) coastal_data = coastal_band.ReadAsArray(0, 0, coastlines_ds.RasterXSize, coastlines_ds.RasterYSize) print("create hand corrected output file:" + self.hand_output_file) hand_output_dataset = driver.Create(self.hand_output_file, src_ds.RasterXSize, src_ds.RasterYSize, 2, gdal.GDT_Byte, ['COMPRESS=DEFLATE']) hand_output_band = hand_output_dataset.GetRasterBand(1) # Add Alpha band #hand_output_dataset.AddBand(gdal.GDT_Byte); alpha_band = hand_output_dataset.GetRasterBand(2) alpha_data = alpha_band.ReadAsArray( 0, 0, hand_output_dataset.RasterXSize, hand_output_dataset.RasterYSize) # Detected Surface Water #mask = output_data<1 #output_data[mask] = 0 #mask = output_data>=1 #output_data[mask] = 255 if verbose: print "Non Zero Pixels before any masking:", numpy.count_nonzero( output_data) # Removed for non-coastal areas... decide if you want to mask watersheds...!!! # # mask = (coastal_data==1) # output_data[mask] = 0 # if verbose: # print "Non Zero Pixels After Coastal/Watershed Masking:", numpy.count_nonzero(output_data) # Now apply HAND Filter # Mask HAND mask = hand_data == 0 output_data[mask] = 0 mask = hand_data == 255 output_data[mask] = 0 if verbose: print "Non Zero Pixels after HAND:", numpy.count_nonzero( output_data) # # Morphing to smooth and filter the data # octagon_2 = [[0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [0, 1, 1, 1, 0]] octagon_2_size = (5, 5) octagon_1 = [[0, 1, 0], [1, 1, 1], [0, 1, 0]] octagon_1_size = (3, 3) #morphed = ndimage.grey_opening(output_data, size=octagon_1_size, structure=octagon_1) ct = gdal.ColorTable() ct.SetColorEntry(0, (0, 0, 0, 0)) ct.SetColorEntry(1, (255, 0, 0, 255)) hand_output_band.SetRasterColorTable(ct) hand_output_band.WriteArray(output_data, 0, 0) hand_output_band.SetNoDataValue(0) # set transparency alpha_data[output_data < 1] = 0 alpha_data[output_data >= 1] = 255 alpha_band.WriteArray(alpha_data, 0, 0) # Copy projection hand_output_dataset.SetGeoTransform(self.geotransform) hand_output_dataset.SetProjection(self.projection) src_ds = None hand_band = None hand_data = None hand_output_dataset = None coastal_data = None coastlines_ds = None if verbose: print("done")
def saveLabel_fromSoftmax(labPred, tiffPath): # this function save the predicted label try: fid = gdal.Open(tiffPath) except RuntimeError as e: print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) print( "ERROR: the given data geotiff can not be open by GDAL") print("DIRECTORY: " + tiffPath) print("GDAL EXCEPCTION: " + e) print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) sys.exit(1) row = fid.RasterYSize col = fid.RasterXSize bnd = fid.RasterCount proj = fid.GetProjection() geoInfo = fid.GetGeoTransform() del (fid) if labPred.shape[0] != row * col: print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) print( "ERROR: number of patches does not suit the output size") print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) sys.exit(1) print(labPred.shape) print(np.median(labPred[:])) prob = np.reshape(labPred, (row, col, 17)) print(prob.shape) print(np.median(prob[:])) lab = prob.argmax(axis=2).astype(np.uint8) + 1 print(lab.shape) print(np.median(lab[:])) LCZDriver = gdal.GetDriverByName('GTiff') LCZFile = LCZDriver.Create( tiffPath, col, row, bnd, gdal.GDT_Byte ) #gdal.GDT_UInt16 he gdal documentation describes the GDT_Byte as an 8 bit unsigned integer (see here). LCZFile.SetProjection(proj) LCZFile.SetGeoTransform(geoInfo) # save file with predicted label outBand = LCZFile.GetRasterBand(1) # create color table colors = gdal.ColorTable() # set color for each value colors.SetColorEntry(1, (165, 0, 33)) colors.SetColorEntry(2, (204, 0, 0)) colors.SetColorEntry(3, (255, 0, 0)) colors.SetColorEntry(4, (153, 51, 0)) colors.SetColorEntry(5, (204, 102, 0)) colors.SetColorEntry(6, (255, 153, 0)) colors.SetColorEntry(7, (255, 255, 0)) colors.SetColorEntry(8, (192, 192, 192)) colors.SetColorEntry(9, (255, 204, 153)) colors.SetColorEntry(10, (77, 77, 77)) colors.SetColorEntry(11, (0, 102, 0)) colors.SetColorEntry(12, (21, 255, 21)) colors.SetColorEntry(13, (102, 153, 0)) colors.SetColorEntry(14, (204, 255, 102)) colors.SetColorEntry(15, (0, 0, 102)) colors.SetColorEntry(16, (255, 255, 204)) colors.SetColorEntry(17, (51, 102, 255)) # set color table and color interpretation outBand.SetRasterColorTable(colors) outBand.SetRasterColorInterpretation(gdal.GCI_PaletteIndex) outBand.WriteArray(lab) outBand.FlushCache() del (outBand) return lab