Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGeocoding / src / org / gvsig / geocoding / geommatches / PolygonMatcher.java @ 22684

History | View | Annotate | Download (2.18 KB)

1
package org.gvsig.geocoding.geommatches;
2

    
3
import java.awt.geom.PathIterator;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Map;
8

    
9
import org.gvsig.data.vectorial.FeatureCollection;
10
import org.gvsig.fmap.geom.Geometry;
11
import org.gvsig.fmap.geom.aggregate.MultiPoint2D;
12
import org.gvsig.fmap.geom.aggregate.MultiSurface2D;
13
import org.gvsig.fmap.geom.primitive.Point2D;
14

    
15
import com.vividsolutions.jts.algorithm.CentroidArea;
16
import com.vividsolutions.jts.algorithm.InteriorPointArea;
17

    
18
public class PolygonMatcher extends GeomMatcher {
19

    
20
        /**
21
         * This method match the centroids (Point2D) of polygons. If there are some
22
         * multipolygon in the result features the centroid will be a MultiPoint
23
         */
24
        @Override
25
        public Geometry[] match() {
26

    
27
                HashMap polys = groupFeaturesByAttribute(this.getGroupFieldPosition(),
28
                                (FeatureCollection) this.getFeatures().get(0));
29
                Iterator it = polys.entrySet().iterator();
30
                Geometry[] mPoints = new Geometry[polys.size()];
31
                int position = 0;
32
                while (it.hasNext()) {
33
                        Map.Entry e = (Map.Entry) it.next();
34
                        ArrayList geomss = (ArrayList) e.getValue();
35
                        
36
                        // Insert geometries of one street into array
37
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
38
                        for (int i = 0; i < geomss.size(); i++) {
39
                                Geometry ggeom = (Geometry) geomss.get(i);                                
40
                                arrGeoms[i] = ggeom;
41
                        }
42
                        // Parse geometries to JTS
43
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
44
                                        geomss.get(0).getClass(), arrGeoms);
45
                        // Get centroid of polygon or polygons
46
                        for (int j = 0; j < geomsjts.length; j++) {
47
                                Point2D pts = getCenterOfPolygon(geomsjts[j]);
48
                                mPoints[position] = pts;
49
                                position++;
50
                        }                        
51
                }
52
                return mPoints;
53
        }
54

    
55
        /**
56
         * This method calculate the centroid of one polygon. Always the centroid
57
         * will be within the polygon.
58
         * 
59
         * @param geomjts
60
         * @return
61
         */
62
        private Point2D getCenterOfPolygon(
63
                        com.vividsolutions.jts.geom.Geometry geomjts) {
64

    
65
                InteriorPointArea centro = new InteriorPointArea(geomjts);
66
                com.vividsolutions.jts.geom.Point pto = geomjts.getInteriorPoint();
67

    
68
                return new Point2D(pto.getX(), pto.getY());
69
        }
70

    
71
}