Lut.append (n / step ) n = n + hist im = im.point (lut ) return imageToArray (im )ĭef main ( shapefile_path, raster_path ): # step size step = reduce (operator.add, hist ) / 255 """ hist = histogram (a ) im = arrayToImage (a ) lut = for b in range (0, len (hist ), 256 ): """ Performs a histogram stretch on a gdalnumeric array image. N = archsorted (gdalnumeric.sort (fa ), bins ) n = ncatenate (]) hist = n -n return hist a = array bins = range of numbers to match """ fa = a.flat """ Histogram function for multi-dimensional array. Gdalnumeric.CopyDatasetInfo ( prototype_ds, ds, xoff =xoff, yoff =yoff ) return ds Prototype_ds = gdal.Open ( prototype_ds ) if prototype_ds is not None: """ Uses a gdal geomatrix (gdal.GetGeoTransform()) to calculate the pixel location of a geospatial coordinate """ ulX = geoMatrix ulY = geoMatrix xDist = geoMatrix yDist = geoMatrix rtnX = geoMatrix rtnY = geoMatrix pixel = int ((x - ulX ) / xDist ) line = int ((ulY - y ) / xDist ) return (pixel, line ) # EDIT: this is basically an overloaded # version of the gdal_array.OpenArray passing in xoff, yoff explicitly # so we can pass these params off to CopyDatasetInfo #ĭef OpenArray ( array, prototype_ds = None, xoff =0, yoff = 0 ):ĭs = gdal.Open ( gdalnumeric.GetArrayFilename (array ) ) if ds is not None and prototype_ds is not None: """ Converts a gdalnumeric array to a Python Imaging Library Image. """ a =omstring (i.tostring (), 'b' )Ī.shape =i.im.size, i.im.size return a """ Converts a Python Imaging Library array to a gdalnumeric image. Gdal.UseExceptions () # This function will convert the rasterized clipper shapefile # to a mask for use within GDAL. argv )įrom osgeo import gdal, gdalnumeric, ogr, osr argv ) < 3 : print """ you must supply at least two arguments: 1) the band number to retrieve and 2) input raster """ Usage () main ( int ( sys. GetColorEntryAsRGB ( i, entry ) if _name_ = '_main_' : if len ( sys. GetColorEntry ( i ) if not entry : continue print " = ", ctable. GetColorTable () if ctable is None : print 'No ColorTable found' sys. GetRasterBand ( band_num ) except RuntimeError, e : print 'No band %i found' % band_num print e sys. Open ( input_file ) if src_ds is None : print 'Unable to open %s ' % input_file sys. exit ( 1 ) def main ( band_num, input_file ): src_ds = gdal. UseExceptions () def Usage (): print ( """ $ getrasterband.py input-raster """ ) sys.