Problem adding the whole FeatureCollection to VectorDataNode with snappy


#1

Hi I have a collection of SimpleFeature inside org.geotools.data.collection.ListFeatureCollection

When I try to add the whole ListFeatureCollection to VectorDataNode with
node = VectorDataNode('shape', newCollection)

only the first item in ListFeatureCollection is added. I need to add the entire ListFeatureCollection.

Casting ListFeatureCollection as FeatureCollection to match the API exactly ( https://step.esa.int/docs/v3.0/apidoc/engine/org/esa/snap/core/datamodel/VectorDataNode.html ) didnt help.

Can someone please help?

Here is the code

from readradar import FileManager
from readradar import ProductType
from snappy import (ProgressMonitor, VectorDataNode,
                    WKTReader, ProductIO, PlainFeatureFactory,
                    SimpleFeatureBuilder, DefaultGeographicCRS,
                    ListFeatureCollection, FeatureUtils)
import jpy
PixelPos = jpy.get_type('org.esa.snap.core.datamodel.PixelPos')
SimpleFeature = jpy.get_type('org.opengis.feature.simple.SimpleFeature')
FeatureCollection = jpy.get_type('org.geotools.feature.FeatureCollection')

#load inputs
grid_width = 900
grid_height = 900
products = FileManager.load_all_dim_from('../test_data', ProductType.HhHvVhVv)
print(products)

#create workspace
FileManager.create_workspace()

#draw grids
product_height = products[0].bands[0].h
product_width = products[0].bands[0].w
num_grids_in_row = product_width // grid_width
num_grids_in_col = product_height // grid_height

for px_product in products:
    product = px_product.product
    wktFeatureType = PlainFeatureFactory.createDefaultFeatureType(DefaultGeographicCRS.WGS84)
    newCollection = ListFeatureCollection(wktFeatureType)

    for c in range(0, num_grids_in_col):
        for r in range(0, num_grids_in_row):
            top_left = (r * grid_width, c * grid_height)
            top_right = ((r + 1) * grid_width, c * grid_height)
            bot_left = (r * grid_width, (c + 1) * grid_height)
            bot_right = ((r + 1) * grid_width, (c + 1) * grid_height)
            polygon = "POLYGON((" + str(top_left[0]) + " " + str(top_left[1]) + "," + str(top_right[0]) + " " + str(top_right[1]) + "," + str(bot_right[0]) + " " + str(bot_right[1]) + "," + str(bot_left[0]) + " " + str(bot_left[1]) + "," + str(top_left[0]) + " " + str(top_left[1]) + "))"
            try:
                geometry = WKTReader().read(polygon)
                print(geometry)
            except Exception as e:
                geometry = None
                print('Failed to convert WKT into geometry')
                print(e)
                
            featureBuilder = SimpleFeatureBuilder(wktFeatureType)
            wktFeature = featureBuilder.buildFeature('shape')
            wktFeature = jpy.cast(wktFeature, SimpleFeature)
            wktFeature.setDefaultGeometry(geometry)

            try:
                newCollection.add(wktFeature)
                print(newCollection.size())
            except Exception as e:
                print(e)

    #adding collection to vector data node
    print(type(newCollection)) # collection type <class 'org.geotools.data.collection.ListFeatureCollection'>
    print(newCollection.size()) # collection size > 1
    
    node = VectorDataNode('shape', newCollection)
    print ('Num features = ', node.getFeatureCollection().size()) # Num features = 1

    newCollection = jpy.cast(newCollection, FeatureCollection)
    print(type(newCollection)) # collection type <class 'org.geotools.feature.FeatureCollection'>

    node = VectorDataNode('shape', newCollection)
    print ('Num features = ', node.getFeatureCollection().size()) # Num features = 1

    print(type(node.getFeatureCollection())) # <class 'org.geotools.feature.DefaultFeatureCollection'>
    node.getFeatureCollection().addAll(newCollection) 
    print ('Num features = ', node.getFeatureCollection().size()) # Num features = 1

    product.getVectorDataGroup().add(node)
    ProductIO.writeProduct(product, FileManager.WORKSPACE + "/snappy_subset_output.dim", "BEAM-DIMAP")

This is in python