Again in case it can help someone, I had to add something to MyGML3Configuration for the "<gml:FeatureCollection><gml:featureMembers>..." to be ordered correctly. The version without an "s" was ok ( "<gml:FeatureCollection><gml:featureMember>..." ).

--------------------------------------
public class MyGML3Configuration extends GMLConfiguration {

    @Override
    protected void registerBindings(MutablePicoContainer container) {
        super.registerBindings(container);
        // Change the Binding implementation
container.registerComponentImplementation(GML.AbstractFeatureCollectionType, PreserveOrderFeatureCollectionTypeBinding.class); container.registerComponentImplementation(GML.featureMembers, PreserveOrderFeatureCollectionTypeBinding.class); // manage "<gml:featureMembers>"
    }
}
--------------------------------------
No need to change  PreserveOrderFeatureCollectionTypeBinding.

Julien


On 9/26/2014 9:37 AM, electrotype wrote:
I guess you're right, I should have read the doc again! What I did though is checked the Type Hierarchy of SimpleFeatureCollection and they were a lot ofpotential implementations! That's why I wasn't too sure.

Anyway, I found a solution. It's quite ugly, but it works. If it can help 
someone :

--------------------------------------
MyGML3Configuration configuration = new MyGML3Configuration();
Parser parser = new Parser(configuration);
// do the parsing...
--------------------------------------
public class MyGML3Configuration extends GMLConfiguration {

    @Override
    protected void registerBindings(MutablePicoContainer container) {
        super.registerBindings(container);
        // Change the Binding implementation
container.registerComponentImplementation(GML.AbstractFeatureCollectionType, PreserveOrderFeatureCollectionTypeBinding.class);
    }
}
--------------------------------------
public class PreserveOrderFeatureCollectionTypeBinding extends 
AbstractFeatureCollectionTypeBinding  {

    /**
     * Sadly, we have to copy/paste this whole method from the parent, only to 
change one line.
     */
    @Override
    public Object parse(ElementInstance instance, Node node, Object value) 
throws Exception {
        SimpleFeatureCollection featureCollection =
            (SimpleFeatureCollection) 
node.getChildValue(FeatureCollection.class);
        if (featureCollection == null) {
            featureCollection = createFeatureCollection();
        }

        //&lt;element maxOccurs="unbounded" minOccurs="0" 
ref="gml:featureMember"/&gt;
        @SuppressWarnings("unchecked")
        List<SimpleFeature> childValues = 
node.getChildValues(SimpleFeature.class);

        // example DefaultFeatureCollections or ListFeatureCollection
        Collection<SimpleFeature> collection = DataUtilities.collectionCast( 
featureCollection );
        collection.addAll(childValues);

        //&lt;element minOccurs="0" ref="gml:featureMembers"/&gt;
        SimpleFeature[] featureMembers = (SimpleFeature[]) 
node.getChildValue(SimpleFeature[].class);

        if (featureMembers != null) {
            for (int i = 0; i < featureMembers.length; i++) {
                collection.add(featureMembers[i]);
            }
        }

        return featureCollection;
    }

    // We use our own SimpleFeatureCollection implementation
    protected SimpleFeatureCollection createFeatureCollection() {
        return new MyFeatureCollection();
    }
}
--------------------------------------
public class MyFeatureCollection extends DefaultFeatureCollection {

    private final List<SimpleFeature> featuresInXmlFileOrder = new 
ArrayList<SimpleFeature>();

    public MyFeatureCollection () {
        super();
    }

    public MyFeatureCollection (FeatureCollection<SimpleFeatureType, 
SimpleFeature> collection) {
        super(collection);
    }

    public MyFeatureCollection (String id) {
        super(id);
    }

    public MyFeatureCollection (String id, SimpleFeatureType memberType) {
        super(id, memberType);
    }

    /**
     * SimpleFeature in the order they were parsed.
     */
    public List<SimpleFeature> getFeaturesInXmlFileOrder() {
        return this.featuresInXmlFileOrder;
    }

    @Override
    protected boolean add(SimpleFeature feature, boolean fire) {
        boolean res = super.add(feature, fire);
        if(res) {
            this.featuresInXmlFileOrder.add(feature);
        }
        return res;
    }
}
--------------------------------------

Julien


On 9/25/2014 5:23 PM, Jody Garnett wrote:
Darn hoped the doc link would of helped answered that.

ListFeatureCollection is the one to use (your choice of java.util.List). Have to be careful not to have duplicate FeatureIds in order to preserve the FeatureCollection contract.
--
Jody

Jody Garnett

On Thu, Sep 25, 2014 at 9:42 AM, electrotype <electrot...@gmail.com <mailto:electrot...@gmail.com>> wrote:

    Jody, let's say I modified the resulting Map of the parse() method and 
replace the
    DefaultFeatureCollection collections by new ones preserving order (I would 
use Xpath and my
    original XML file to "correct" the order). Which implementation of 
SimpleFeatureCollection
    would you suggest me? Is there one available in GeoTools which would work 
OK for that
    purpose, or should I create my own one?

    Thanks,

    Julien



    On 9/25/2014 12:14 PM, Jody Garnett wrote:
    I think you would have to review the parse code, and prepare a pull request 
allowing it to
    preserve order.

    We used to have a global setting defining what implementation to use for 
the default feature
    collection, but it was not being used. I am having more luck with these 
explicit
    FeatureCollection implementations so developers can notice their is a 
choice.

    Jody

    Jody Garnett

    On Thu, Sep 25, 2014 at 4:58 AM, electrotype <electrot...@gmail.com
    <mailto:electrot...@gmail.com>> wrote:

        Thanks for the reply Jody.

        But how can I tell the parse() method to use one implementation instead 
of another?

        I see the Parser's constructor can take a Configuration object, which 
seems to use
        PicoContainer and have a registerBindings() method... Is that the way 
to configure which
        implementation of the FeatureCollection I want it to use?

        Can you provide a small code example?

        Julien




        On 9/24/2014 4:01 PM, Jody Garnett wrote:
        There are several FeatureCollection implementations for different 
purposes. The
        DefaultFeatureCollection implementation is built around a TreeMap and 
sorts the
        contents by FeatureId.

        See the docs for alternatives:
        -
        
http://docs.geotools.org/latest/userguide/library/main/collection.html#performance-options

        Jody Garnett

        On Wed, Sep 24, 2014 at 12:24 PM, electrotype <electrot...@gmail.com
        <mailto:electrot...@gmail.com>> wrote:

            I use org.geotools.xml.Parser#parse(...) to parse a GML based XML 
file. This XML
            contains a
            <gml:FeatureCollection> which contains multiple <gml:featureMember>.

            The problem is that the resulting DefaultFeatureCollection doesn't 
seem to keep the
            order in which
            the featureMembers are declared.

            For example,

            ----------------------
            Iterator<SimpleFeature> iterator = 
defaultFeatureCollection.iterator();
            ----------------------

            ... returns an Iterator with the elements in another order. I think
            DefaultFeatureCollection uses a
            TreeMap to keep the elements, but sort them by internal ID, not by 
the order they
            appear in the XML.

            Our application would require the order of the featureMembers to be 
kept... Is
            there a way to
            achieve this?

            The only way I currently think of is to manually modify the 
generated
            DefaultFeatureCollection after
            GeoTools has parsed the XML file.

            The highest version of GeoTools I can use is 11.2 since our 
application uses Java 6.

            Thanks in advance!

            Julien

            
------------------------------------------------------------------------------
            Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
            Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS 
Reports
            Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
            Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
            
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
            _______________________________________________
            GeoTools-GT2-Users mailing list
            GeoTools-GT2-Users@lists.sourceforge.net
            <mailto:GeoTools-GT2-Users@lists.sourceforge.net>
            https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users




        
------------------------------------------------------------------------------
        Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
        Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
        Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
        Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
        
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
        _______________________________________________
        GeoTools-GT2-Users mailing list
        GeoTools-GT2-Users@lists.sourceforge.net 
<mailto:GeoTools-GT2-Users@lists.sourceforge.net>
        https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users




    
------------------------------------------------------------------------------
    Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
    Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
    Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
    Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
    http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
    _______________________________________________
    GeoTools-GT2-Users mailing list
    GeoTools-GT2-Users@lists.sourceforge.net 
<mailto:GeoTools-GT2-Users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users




------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to