I’ve tried to figure this out on my own, but i am having a hard time finding relevant documentation. The Java documentation is great, but I cannot figure out how to convert it into python.
I have the following script to mark out a specific area on a Sentinel-1 image and then generate a png that shows the same thing. However the polygon that is created and added to the product is not shown in the generated image, as it only write out the selected band without any mask overlay.
If I open the new product (TEST_POLYGON.dim) in snap toolbox I can see that the polygon was created. Also if I then use the Export View as Image operation in snap, the image is generated with the polygon added. Is there a way to get the same result in python?
from snappy import ProductIO, GPF, ProgressMonitor
HashMap = snappy.jpy.get_type('java.util.HashMap')
product = ProductIO.readProduct('subTEST.dim')
### MAKING SHAPE
shapeFile = 'geometry_Polygon.shp'
params3 = HashMap()
params3.put('separateShapes', False) # ?
target0 = GPF.createProduct('Import-Vector', params3, product)
ProductIO.writeProduct(target0, "TEST_POLYGON", 'BEAM-DIMAP') # This product has the shape added.
### MAKING IMAGE
JPY = snappy.jpy
imageIO = JPY.get_type('javax.imageio.ImageIO')
File = JPY.get_type('java.io.File')
band = target0.getBand('Amplitude_VV')
image = band.createColorIndexedImage(ProgressMonitor.NULL)
name = File('test.png')
imageIO.write(image, 'PNG', name) # But the shape is not added to the image.
The Export View as Google Earth KMZ also generates an image that has the polygon added. Is there any way to generate a KMZ file using python?
Any help would be greatly appreciated.
When you select Export View as Image or Export View as Google Earth KMZ you are exporting the view, i.e., all raster and vector data layers that are shown. In your code you are only creating an image from the band.
One thing you can do here is to take the mask associated with your shape file and set it as overlay to your band:
mask = target0.getMaskGroup().get(‘geometry’)
Do this before you create the image. If you care about colours, use createRgbImage instead of createColorIndexedImage . The result will not exactly look the same as with the export options above, but hopefully it will also be fine.
Hello @TonioF. Your reply is much appreciated. This was exactly the functions I was looking for.
I can see that the suggested code works because when I now open the TEST_POLYGON.dim-file the mask overlays the selected band (it is already toggled ON in the Layer Manager). However the generated image is still only showing the band.
Can it be that, even though the mask is added, the function band.createColorIndexedImage() only uses the band without any overlay? I’ve also tried with another function: createColoredBandImage(), but with no luck.
Do you maybe have a working code example?
Any functions regarding exporting KMZ files would also help me a lot.
You are correct, createColorIndexedImage does not use the overlay. createRgbImage does, though. See the following snippet (in Java, but easily portable):
final Product product = ProductIO.readProduct(“path/to/product”);
final Band band = product.getBand(“band_name”);
final Mask mask = product.getMaskGroup().get(“geometry”);
final BufferedImage image = band.createRgbImage(ProgressMonitor.NULL);
ImageIO.write(image, “PNG”, new File(“test.png”));
Kmz files can only be exported from SNAP Desktop. When you have the png image you can create the kmz file from it, as the kmz file is a zip containing the png and a simple kml file where you need to set the bounding latitudes and longitudes. Don’t forget to reproject the image to Geographic Lat/lon first.
Thanks again for the further explanation, Tonio. Good clarification regarding the kmz files, they should be easy to generate with some simple scripting, since I have all the necessary information.
I do, however, run into a small issue with the createRgbImage() -function. I have tried this function before and I always get this weird error:
image = band_1.createRgbImage(ProgressMonitor.NULL) RuntimeError: java.util.ServiceConfigurationError: com.bc.ceres.glayer.LayerType: Provider org.esa.s1tbx.sentinel1.rcp.layers.topsbursts.TOPSBurstLayerType not found
I found one older post where the same issue comes up, but I do not know how to fix it. In the mentioned post the issue seems to be fixed by updating the s1tbx repo, but mine is already up to date.
How to overlay a vector polygon when writing an RGB quicklook (java)
I apologize for asking so many questions. I am not a very experienced developer.
There seems to be a bug. If you don’t need need the S1TBX, you can re-install SNAP without the S1TBX. The snappy from that SNAP version will not have this problem. Unfortunately, deactivating the module won’t be enough.
Ok, too bad. If it’s a bug maybe it should be reported to the developers. Thank you for the help anyways. The reason that I wanted an overlay was to give the generated png images some geographical reference. Generating kmz files and using google earth solved my initial problem, but it would be nice to have a png with a gradicule overlay or something.
I am a developer and have created an issue for this bug.
Overlaying a graticule without using SNAP Desktop is not possible, sorry.
Is there any way to export a sentinel product (calibration, speckle filtering, band math and terrain filtering performed) to .kmz format using python?
@marpet, @cop, @TonioF, @falahfakhri