Extract_information


#1

Is it possible to use extract Pixel values but instead of writing them in txt store them in a variable e.g. list, array in python? My point is that i have multiple products and relevant coordinates (in some products i have two or more coordinates to extract) but i do not want to create many txt/.


#2

take a look
this may help
def product2array(src_product, band_names=[] ):
width = src_product.getSceneRasterWidth()
height = src_product.getSceneRasterHeight()
name = src_product.getName()
desc = src_product.getDescription()
if not band_names:
band_names = src_product.getBandNames()
# band_names = [b for b in band_names if “Sigma” in b]
print(‘We are in product2array extracting this {} \n from this {}’. format(band_names,name ))
print('product2array width:{} height:{} name:{} /n desc:{} /n band_names: {} '.format(width, height, name, desc, band_names))

‘’’ creating empty aprray ‘’’
filename = path.join(mkdtemp(), ‘dataStorageFile.dat’)
# Create a memmap with dtype and shape that matches our data:
data = np.memmap(filename, shape=(height, width, len(band_names)), dtype=np.float32, mode=‘w+’)
tempname = path.join(mkdtemp(), ‘tempStorageFile.dat’)
temp = np.memmap(tempname, shape=(height, width), dtype=np.float32, mode=‘w+’)
for b in range(len(band_names)):
# print(‘reading band:’, band_name)
print(’ reading: ', band_names[b])
band = src_product.getBand(band_names[b])
band.readPixels(0, 0, width, height, temp)
data[:, :, b] = temp
return data


#4

You can use the PixEx operator, it writes to a text file. But you can easily read it in again.
Another option is you do the extraction your self. You can iterate over all bands of a product and get the value at the specific location. To convert your geo-location into a pixel location and read the value you can do:

gc = product.getSceneGeoCoding()
pixelPos = gc.getPixelPos(GeoPos(lat, lon), None)
value = band.getPixelDouble(pixel.Pos.x, pixelPos.y)

This is roughly what you need to do.


#5

Trying to adopt your code

product=ProductIO.readProduct(folder+‘S3A_OL_2_WFR____20160430T093820_20160430T094020_20171030T225518_0119_003_307______MR1_R_NT_002.SEN3/xfdumanifest.xml’)
gc = product.getSceneGeoCoding()
pixelPos = gc.getPixelPos(GeoPos(40.36734009, 10.64342976), None)
band = product.getBand(‘CHL_OC4ME’)
value = band.getPixelDouble(pixelPos.x, pixelPos.y)
print value

but i get the following error File “E:/seo_dwarf/chl_scripts/extract_multiple_products1.py”, line 93, in
value = band.getPixelDouble(pixelPos.x, pixelPos.y)
RuntimeError: no matching Java method overloads found

Any ideas?


#6

The variables pixelPos.x and pixelPos.yare floating point. And getPixelDouble() expects integers.
The values need to be cast to integer. This should do the trick.

value = band.getPixelDouble(int(pixelPos.x), int(pixelPos.y))

#7

Thanks for your answers but now i get this error

value = band.getPixelDouble(int(pixelPos.x), int(pixelPos.y))
RuntimeError: java.lang.NullPointerException

Any ideas?


#8

Oh, yes. My mistake. The getPixelDouble method expects that the data is already loaded.

Either do

band.loadRasterData() 

before calling getPixelDouble (once per band is sufficient) or

data = array.array('d', [0]) 
# or with numpy
# import numpy as np
# data =np.zeros(1)
readPixels(int(pixelPos.x), int(pixelPos.y), 1, 1, data)

#9

Is there any possibility to use the float and not convert the coordinates to integers?


#10

no, and it makes no difference.
You will get the same pixel value either if you use

x = 34.567
y = 123.456

or
x = 34
y = 123


#11

Did what you said but i get the following error

Traceback (most recent call last):
File “<pyshell#3>”, line 1, in
value = band.getPixelDouble(int(pixelPos.x), int(pixelPos.y))
RuntimeError: java.lang.ArrayIndexOutOfBoundsException: 6027866

Any ideas?


#12

Is the pixel not anymore in the bounds of the image?

catch the exception and check at which x,y coordinate this happens. Check also the image bounds. Similar to this.

try:
    value = band.getPixelDouble(int(pixelPos.x), int(pixelPos.y))
except RuntimeError:
    print "x-coord:", int(pixelPos.x)
    print "y-coord:", int(pixelPos.y)
    print "band width:", band.getRasterWidth()
    print "band height:", band.getRasterHeight()