问题
I want to plot and RGB image (because it is satellite image) from multiband GeoTIFF and project it to specified area extent.
Recently, I was successfully able to do this with single band raster using "georaster". However, when I tried to do the same for multiband raster, after calling image = georaster.MultiBandRaster(file, bands='all', load_data=extent)
to load the subset data the same way as with georaster.SingleBandRaster, I get the following error:
Traceback (most recent call last): File "slovakia.py", line 15, in image = georaster.MultiBandRaster(file, bands='all', load_data=extent) File "/usr/local/lib/python2.7/dist-packages/georaster/georaster.py", line 1249, in init if self.r == None: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When I was unable to load subset of multiband data, I tried to create and RGB image from 3 bands, which are contained in GeoTIFF, without projecting it to basemap. However, the result is quite surprising.
import georaster
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.pyplot import figure
import numpy as np
import matplotlib.cm as cm
file = "/home/lubomir/Desktop/Sentinel3_SLSTR/RGB/SLSTR_201901100859_Natural_Color.tif"
defllon = 16
defllat = 47
defulon = 23
defulat = 50.5
extent = (defllon,defulon,defllat,defulat)
image = georaster.MultiBandRaster(file, bands='all')
resulting_image = np.array(image.r[:,:,:], dtype='float32')
print resulting_image.shape
m = Basemap(epsg=3395,llcrnrlat=47,urcrnrlat=50.5,\
llcrnrlon=16,urcrnrlon=23,resolution='i')
m.drawcoastlines(linewidth=1, color='g', zorder=2)
m.drawcountries(linewidth=1, color='m', zorder=2)
m.imshow(resulting_image, origin='upper', zorder=1, cmap=cm.gray)
plt.savefig('test.tiff',dpi=600,transparent=True,bbox_inches=None,frameon=False)
plt.show()
resulting_image=None
Single band raster projected to specified area: Single_band
RGB image composed from 3 bands: RGB_image
While band 2 of the GeoTIF looks like this: band2_greyscale
I will welcome any help or suggestions regarding subset data loading of MultiBandRaster or RGB ploting of MultiBandRaster. I would be alsothankful for any other python methods on how to do this.
回答1:
I've had a very similar issue when using georaster.MultiBandRaster. After switching to georaster.SingleBandRaster, the error message "ValueError: The truth value of an array with more than one element is ambiguous." went away, and I was able to display a single band plot. To match with the basemap contour, it also required to execute a 180 degree flip operation with the statement: np.flipud(image.r). In summary, there seems to be an issue, likely related to Python >= 3.6 or numpy version, which causes the error message during the function georaster.MultiBandRaster. I will open a new post for it.
来源:https://stackoverflow.com/questions/54178650/how-to-project-rgb-multiband-geotiff-to-specified-area-extent-using-python