Extracting single pixel information

Hello,

What would be the easiest method to extract single pixel information from a S3 lvl1 EFR scene using python? I am particularly interested in extracting the Top of Atmosphere radiance and if possible the observational zenith angle. I would be performing this over many downloaded scenes. I have looked through all the examples, forum, and SNAP API but still not discovering anything that quite does what I’m looking for. Thanks.

Maybe you don’t even need python.

You can use the Pixel Extraction tool. You can find it in the menu at Raster -> Export -> Export Pixel Values.
It is also possible to use it from the command line with gpt.
Call gpt PixEx -h to get some more help.
You can use it also from python by calling GPF.createProduct('PixEx', ...)
Look at the examples how to invoke a processor from python. PixEx can be invoked in a similar way.

Thank you for the information. The PixEx operator was exactly what I was looking for, I don’t know how I missed it previously. I was able to successfully implement it using python after calling various other operators.

Hi marpet
I’m hoping to extract values from a Sen2Cor processed image corresponding to locations in an Esri shapefile with SNAP. I’d have thought this would be possible through the ‘Extract Pixel Values’ tool, but can’t work out how. The Coordinates parameter doesn’t allow for shapefiles, and I can’t work out how to convert this file to a placemark format that the tool will accept. I’ve fiddled around with GPSBabel but can’t find a suitable format.
Can you help?

I must confess that the input format is not well documented. But it is pretty similar to the output format described in the help (Sentinel Toolbox Application / Import and Export / Extract Pixel Values).
As an example I have attached a file (pin.txt). It is a simple CSV file with tab separated columns.
The columns necessary are Name, Lat and Lon. Optionally a DateTime column or a Date and a Time column can be added. Additional columns are not interpreted but repeated in the ouput file, if the option is enabled.

3 Likes

Thank you marpet. With the help of your example I can now get coordinates into the extraction tool. When I run the tool it says that it has run successfully and written the result files to my folder of choice, but when I check there is nothing there - it hasn’t produced anything. I have checked that the points intersect with the raster data, and even if they didn’t I would have thought the tool would produce NA output for my points. Do you have any idea what I’m doing wrong? Thanks again

I’ve heard about this behaviour before. But what was the reason…?
No matter what the reason is, you are right. The tool should tell that it wasn’t able to extract any pixels.

Do you use the time constraint? Maybe it the pixels are not in the time range.

Hello,
I am struggling a bit to invoke the PixEx op from python.
How did you succeed to set the coordinates parameter needed by PixEx?
Thanks,
Tristan

Probably you need to get the proper java type first by doing

Coord = snappy.jpy.get_type('org.esa.snap.pixex.Coordinate')
Date  = snappy.jpy.get_type('java.util.Date')

Afterwards you can do:

import datetime
c1 = Coord('name1', 54.0, 10.0, Date(datetime.datetime.utcnow()))

The creation of the coordinates can be a bit tricky. You need to know the Java interface also a bit.
Maybe it is better to use the coordinatesFile parameter and specify the file which contains the coordinates.

Perfect!
Thanks a lot, Marco,
Tristan

I encountered the same problem that SNAP pop-up a message saying the points are successfully extracted , but no result export in the given folder. I am working on Sentinel-2 imagery, using GUI. I tried both “.xml” or “dim” input format, same problem. I think the problem in my case is the S-2 bands are different resolution in each band.

Your right, the multi-resolution could be the problem.
If you do the operation and the go to Help / Show Log Directory and then choose the messages.log file you will probably see the reasons why products have been refused.
The Pixel extraction definitely needs some review.

Hi. Is it possible to overlay a classified map onto the sentinel image, and extract values of the bands for each class of the map, then export these?

regards
Pamela

No, this is not possible.
You might want to read also this thread:

You can import the classified map into the sentinel image, and export altogether. Or you create masks based on the classes via the Band Maths expression and then export the data.

Thanks. let me try this.

Hi marpet, can I read somewhere something more about a PixEx operator in python? If I understand well, I need to put some parmeters here -> GPF.createProduct(‘PixEx’, parameters, product). But what is the name of a parameter in a case of a coordinate (‘org.esa.snap.pixex.Coordinate’)?

And I have also problem with Coord creation. When I try exactly what you write, I got an error:

RuntimeError: no matching Java method overloads found

The code is:

from snappy import jpy

Coord = jpy.get_type(‘org.esa.snap.pixex.Coordinate’)
Date = jpy.get_type(‘java.util.Date’)

import datetime
c1 = Coord(‘name1’, 54.0, 10.0, Date(datetime.datetime.utcnow()))

I have also tried to use ‘java.lang.Float’ and ‘java.lang.String’, but the same error. Do you know where should be a problem? Thank you for reply :slight_smile:

Probably the error comes from thi call

The java Date class does not understand the Python datetime object.
Try, removing datetime.datetime.utcnow() and call only Date()
What might also work is Coord('name1', 54.0, 10.0, none)if you don’t care about the time.
Regarding the parameters of PixEx you can find them when you call on the command line

gpt -h PixEx

For defining the array of Corrdinates you need to do the following:

coords = jpy.array('org.esa.snap.pixex.Coordinate', 8)

This will create an array with 8 elements.
You can use it for the parameters:

parameters.put('coordinates', coords)

Hallo to all

I tried the following replies in order to extract the pixel value but still i got an error.

Traceback (most recent call last):
File “E:\diplomatiki\doriforikes\statistiki_analysi.py”, line 28, in
Coord(‘name1’,25.12,35.72,none)
NameError: name ‘none’ is not defined

The code i am using is
inf=‘E:\diplomatiki\S2A_MSIL1C.SAFE\S2A_MSL2A\S2A_MSIL2A_20170622T090021_N0205_R007_T35SLV_20170622T090154.SAFE\outputs/’
product=ProductIO.readProduct(inf+‘resample.dim’)

HashMap = jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()
Coord=jpy.array(‘org.esa.snap.pixex.Coordinate’,5)
parameters.put(‘PexportBands’,1)
parameters.put(‘PexportExpressionResult’,0)
parameters.put(‘PexportMasks’,0)
parameters.put(‘PexportTiePoints’,0)
parameters.put(‘PoutputDir’,‘C:\Users\user\Desktop\te’)
Coord(‘name1’,25.12,35.72,‘none’)
parameters.put(‘coordinates’, Coord)
c=GPF.createProduct(‘PixEx’,parameters)

Any help ?

Actually it should be like:

c1 = Coord('name1', 54.0, 10.0, None)

And as said in my post before, the ‘coordinates’ parameter needs as value an array of coordinates. But this can be of length one if there is only a single coordinate

I tried that and i get the following error

Traceback (most recent call last):
File “E:\diplomatiki\doriforikes\statistiki_analysi.py”, line 24, in
Coord(‘name1’,25.12,35.72,None)
TypeError: ‘[Lorg.esa.snap.pixex.Coordinate;’ object is not callable