Peppe, some comments inline below ..ede

On 9/22/2020 11:30, jump-pilot-svn--- via Jump-pilot-devel wrote:
Revision: 6508
           http://sourceforge.net/p/jump-pilot/code/6508
Author:   ma15569
Date:     2020-09-22 09:30:06 +0000 (Tue, 22 Sep 2020)
Log Message:
-----------
added two method to valid and to union by attribute a FeatureCollection

Modified Paths:
--------------
     core/trunk/src/org/openjump/sigle/utilities/geom/FeatureCollectionUtil.java

Modified: 
core/trunk/src/org/openjump/sigle/utilities/geom/FeatureCollectionUtil.java
===================================================================
--- core/trunk/src/org/openjump/sigle/utilities/geom/FeatureCollectionUtil.java 
2020-09-22 09:26:19 UTC (rev 6507)
+++ core/trunk/src/org/openjump/sigle/utilities/geom/FeatureCollectionUtil.java 
2020-09-22 09:30:06 UTC (rev 6508)
@@ -8,12 +8,23 @@
SNIP
/**
   * This class can check if a FeatureCollection has only one Geometry Type
@@ -22,7 +33,8 @@
   * @author Erwan Bocher
   * @author Olivier Bedel
   * @version 2005-08-10
- *
+ * @author Giuseppe Aruta [2020-07-22]
+ * added two method to valid and to union by attribute a FeatureCollection
   */
public class FeatureCollectionUtil {
@@ -90,4 +102,88 @@
                        return AttributesList;
        
  }
+       /**
+        * Method to make a FeatureCollection valid
+        * @param fc
+        */

can you add to the javadoc exactly what is the method doing to make it valid? 
why wasn't it valid before?

+       public static void validFeatureCollection(FeatureCollection fc) {
+               MakeValidOp makeValidOp = new MakeValidOp();
+                makeValidOp.setPreserveGeomDim(true);
+            makeValidOp.setPreserveDuplicateCoord(false);
+            for (Feature feature  : fc.getFeatures()) {
+                 Geometry validGeom = 
makeValidOp.makeValid(feature.getGeometry());
+                 feature.setGeometry(validGeom);
+               }
+         // return fc;
+          }
+       
+       /**
+        * Mathod to merge geometries of a FeaureCollection according

probably "Method to merge geometries of a FeatureCollection according"

+        * to an attribute
+        * @param featureCollection
+        * @param value

what is the value. looks like the attribute name to me. please javadoc it

+        * @throws Exception
+        */
+        public static void unionByAttributeValue(FeatureCollection 
featureCollection, String value) throws Exception {
+                 FeatureSchema schema = featureCollection.getFeatureSchema();
+             if (featureCollection.getFeatures().size() > 1 &&
+                               
featureCollection.getFeatures().get(0).getGeometry() != null) {
+                       
featureCollection.getFeatures().get(0).getGeometry().getFactory();
+               }
+               else {
+                       Logger.error(
+                           
I18N.get("ui.plugin.analysis.DissolvePlugIn.needs-two-features-or-more"));
+                 // return null;
+               }
+               FeatureSchema newSchema;
+                   newSchema = schema;
+               Map<Object, FeatureCollection> map = new HashMap<Object, 
FeatureCollection>();
+               for (Feature feature  : featureCollection.getFeatures()) {
+                    Object key = feature.getAttribute(value);
+                    if (!map.containsKey(key)) {
+                        FeatureCollection fd = new 
FeatureDataset(featureCollection.getFeatureSchema());
+                        fd.add(feature);
+                        map.put(key, fd);
+                    }  else {
+                        map.get(key).add(feature);
+                    }
+                }
+               featureCollection.removeAll(featureCollection.getFeatures());
+                   for (Iterator<Object> i = map.keySet().iterator() ; 
i.hasNext() ; ) {
+                       Object key = i.next();
+                       FeatureCollection fca = map.get(key);
+                       if (fca.size() > 0) {
+                         Feature feature = union(fca);
+                         feature.setAttribute(value, key);
+                         Feature newFeature = new BasicFeature(newSchema);
+                           // Copy feature attributes in newFeature
+                         for (int j = 0, max = newSchema.getAttributeCount() ; j 
< max ; j++) {
+                               newFeature.setAttribute(j, 
feature.getAttribute(newSchema.getAttributeName(j)));
+                        }
+                         featureCollection.add(newFeature);
+                       }
+                   }
+              // return featureCollection;
+            }
+
+       
+            private static Feature union(FeatureCollection fc) {
+                GeometryFactory factory = new GeometryFactory();
+                Collection<Geometry> geometries  = new ArrayList<Geometry>();
+                for (Feature f :  fc.getFeatures()) {
+                    Geometry g = f.getGeometry();
+                    geometries.add(g);
+                }
+                Geometry unioned = UnaryUnionOp.union(geometries);
+                 FeatureSchema schema = fc.getFeatureSchema();
+                Feature feature = new BasicFeature(schema);
+                if (geometries.size()==0) {
+                    feature.setGeometry(factory.createGeometryCollection(new 
Geometry[]{}));
+                }
+                else {
+                    feature.setGeometry(unioned);
+                }
+                return feature;
+            }
+       
  }



_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel




_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to