def doFloodmap(demFile, inZOS, outFile): # read demFile demFid = gdal.open( demFile, GA_ReadOnly ) dem = demFid.GetRasterBand(1).ReadAsArray( 0, 0, demFid.RasterXSize, demFid.RasterYSize ) demGT = demFid.GetGeoTransform() pixelToMap(demFid.XRaster) # read inZOS zosFid = gdal.open( inZOS, GA_ReadOnly ) # cut out the area needed to cover the DEM xstart, ystart = myTools.mapToPixel(demGT[0], demGT[3], zosFid.GetGeoTransform()) xend, yend = myTools.mapToPixel xend, yend = bidule zos = zosFid.GetRasterBand(1).ReadAsArray( xstart, ystart, xend-xstart+1, yend-ystart+1)
def do_slr(infile, outfile, minMax, nNegPos, nodata, bckColor): inFid = gdal.open(infile, GA_ReadOnly) data=numpy.ravel(inFid.GetRasterBand(1).ReadAsArray(0, 0, inFid.RasterXSize, inFid.RasterYSize)) red=numpy.zeros(inFid.RasterXSize*inFid.RasterYSize)+bckColor[0] green=numpy.zeros(inFid.RasterXSize*inFid.RasterYSize)+bckColor[1] blue=numpy.zeros(inFid.RasterXSize*inFid.RasterYSize)+bckColor[2] # neg values for negVal in range(0, minMax[0], minMax[0]/float(nNegPos[0])): wtc = data<negVal red[wtc]=int(255 - (abs(minMax[0])/float(nNegPos[0]))) green[wtc]=int(255 - (abs(minMax[0])/float(nNegPos[0])) ) blue[wtc]=255 for posVal in range(0, minMax[1], minMax[1]/float(nNegPos[1])): wtc = data >= posVal red[wtc]=255 green[wtc]=int(255 - (abs(minMax[0])/float(nNegPos[0])) ) blue[wtc]=int(255 - (abs(minMax[0])/float(nNegPos[0]))) outDrv=gdal.GetDriverByName('png') outDS=outDrv.Create(outfile, inFid.RasterXSize, inFid.RasterYSize, inFid.RasterCount, inFid.GetRasterBand(1).DataType, []) outDS.GetRasterBand(1).Write(red.reshape(inFid.RasterXSize, inFid.RasterYSize),0,0) outDS.GetRasterBand(2).Write(green.reshape(inFid.RasterXSize, inFid.RasterYSize),0,0) outDS.GetRasterBand(3).Write(blue.reshape(inFid.RasterXSize, inFid.RasterYSize),0,0)
def extract_values(shp, raster, band_array): #open raster src_ds = gdal.open(raster) gt = src_ds.GetGeoTransform() rb = src_ds.GetRasterBand(1) #open shapefile ds = ogr.Open(shp) lyr=ds.GetLayer() li_values = list() for feat in lyr: geom = feat.GetGeometryRef() mx,my=geom.GetX(), geom.GetY() #coord in map units #Convert from map to pixel coordinates #Only works for geotransforms with no rotation px = int((mx - gt[0]) / gt[1]) #x pixel py = int((my - gt[3]) / gt[5]) #y pixel intval=rb.ReadAsArray(px,py,1,1) li_values.append([intval[0]]) a = np.array(li_values) b = a.ravel() band_array = b.astype(np.uint8)
def applyRefs(input): print "processing %s" %input in_file = gdal.open(input) gt = in_file.GetGeoTransform() if geotransform is not None: x, x_size, x_rot, y, y_rot, y_size = geotransform world_file.write('%s\n' % x_size) world_file.write('%s\n' % x_rot) world_file.write('%s\n' % y_rot) world_file.write('%s\n' % y_size) world_file.write('%s\n' % x) world_file.write('%s\n' % y) world_file.close() ''' geotransform tuple key: [0] /* top left x */ [1] /* w-e pixel resolution */ [2] /* rotation, 0 if image is "north up" */ [3] /* top left y */ [4] /* rotation, 0 if image is "north up" */ [5] /* n-s pixel resolution */ ''' else: print "sucks to be you."
def do_colorize(infileList, band, lut, outfileList): outDrv = gdal.GetDriverByName('png') for thisFile, thisOutFile in zip(infileList, outfileList): # process input file thisFile print thisFile, thisOutFile inFid = gdal.open( thisFile, GA_ReadOnly) outDS = outDrv.Create( thisOutFile, inFid.RasterXSize, inFid.RasterYSize, 3, GDT_Byte, []) return
def resize_img(file_path, max_size=(640, 640), quality=90): try: #img = Image.open(file_path) #img = img.convert('RGB') img = gdal.open(file_path) img = img.convert('RGB') except IOError: print('Can\'t open image:', file_path) return if img.size[0] > max_size[0] or img.size[1] > max_size[1]: img.thumbnail(max_size, Image.ANTIALIAS) img.save(file_path, quality=quality)
def read_hyperspectral_gdal(path): """this function allows you read in hyperspectral images in raw format Inputs: path = path to hyperspectral bil file Returns: hyperimge = image mask bands = band centers path = path to hyperspectral image filename = name of hyperspectral image :param hyperimg: spectral object :param bands: list of band centers :param path: string :return filname: string """ params.device += 1 if os.path.isfile(path) == False: fatal_error(str(path) + " does not exist") path1, filename = os.path.split(path) gdalhyper = gdal.open(path) bands = gdalhyper.GetRasterBand(1) bandNo = gdalhyper.RasterCount print("Band Type={}".format(gdal.GetDataTypeName(bands.DataType))) if params.debug == "print": message = str( filename ) + "_input_image.png" + " succesfully opened. With a total of " + str( bandNo) + " bands." print(message) elif params.debug == "plot": message = str( filename ) + "_input_image.png" + " succesfully opened. With a total of " + str( bandNo) + " bands." print(message) return hyperimg, bands, path, filename
# Fun fact: the Fort Washington map is 1.27 miles high and 818 pixels # high, so our scale is 2.49 meters per pixel. Range on the map is # from 0 feet (sea level) to 180 feet, or 60 blocks. voxel_min = 0 voxel_max = 60.0 y_min = 12 print "Loading bitmaps for %s" % filename_prefix data = dict(elevation=[], features=[]) for t in ('elevation'): filename = filename_prefix + "-" + t + ".tif" if not os.path.exists(filename): print "Could not load image file %s!" % filename sys.exit() img = gdal.open(filename) width = img.RasterXSize height = img.RasterYSize imgarray = img.ReadAsArray for i in range(max(width, truncate_size)): row = [] for j in range(max(height, truncate_size)): pixel = imgarray[i, j] value = pixel if t == 'features': # don't go here pls value = pixel[0] value = (value, pixel[1]) # block ID, block data if t == 'elevation': # this one works though :D elevation_min = min(value, elevation_min)
temp_mn_list = [] kcb_mn_list = [] for feat in lyr: name = feat.GetField("Name") try: for value in names: if value[0:3] == name[0:3]: name = value point_id_obj = codes[names.index(name)] name_dem_list.append(point_id_obj) geom = feat.GetGeometryRef() mx, my = geom.GetX(), geom.GetY() path = os.path.join('{a}'.format(/Volumes/Seagate Backup Plus Drive),'{b}'.format(Recharge_GIS),'{c}'.format(NM_DEM)) #path = 'C:\\Recharge_GIS\\NM_DEM' raster = 'NM_30mDEM_UTM13_clp' dem_open = gdal.open(os.path.join('{a}'.format(a=path),'{b}.tif'.format(b=raster))) #dem_open = gdal.Open('{a}\\{b}.tif'.format(a=path, b=raster)) gt = dem_open.GetGeoTransform() rb = dem_open.GetRasterBand(1) px = abs(int((mx - gt[0]) / gt[1])) py = int((my - gt[3]) / gt[5]) dem_obj = rb.ReadAsArray(px, py, 1, 1) dem_list.append(dem_obj) # path = 'C:\\Recharge_GIS\\Array_Results\\apr21' # raster = 'precip_14yr_mean' # ppt_mn_open = gdal.Open('{a}\\{b}.tif'.format(a=path, b=raster)) # gt = ppt_mn_open.GetGeoTransform() # rb = ppt_mn_open.GetRasterBand(1) # px = abs(int((mx - gt[0]) / gt[1])) # py = int((my - gt[3]) / gt[5])