Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / geommatches / PolygonMatcher.java @ 26883

History | View | Annotate | Download (1.94 KB)

1
package org.gvsig.geocoding.geommatches;
2

    
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.Map;
8

    
9
import org.gvsig.fmap.geom.Geometry;
10
import org.gvsig.fmap.geom.primitive.Point2D;
11

    
12
import com.vividsolutions.jts.algorithm.InteriorPointArea;
13

    
14
public class PolygonMatcher extends GeomMatcher {
15

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

    
23
                HashMap polys = groupFeaturesByAttribute(this.getGroupFieldPosition(),
24
                                (List) this.getFeatures().get(0));
25
                Iterator it = polys.entrySet().iterator();
26
                Geometry[] mPoints = new Geometry[polys.size()];
27
                int position = 0;
28
                while (it.hasNext()) {
29
                        Map.Entry e = (Map.Entry) it.next();
30
                        ArrayList geomss = (ArrayList) e.getValue();
31

    
32
                        // Insert geometries of one street into array
33
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
34
                        for (int i = 0; i < geomss.size(); i++) {
35
                                Geometry ggeom = (Geometry) geomss.get(i);
36
                                arrGeoms[i] = ggeom;
37
                        }
38
                        // Parse geometries to JTS
39
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
40
                                        geomss.get(0).getClass(), arrGeoms);
41
                        // Get centroid of polygon or polygons
42
                        for (int j = 0; j < geomsjts.length; j++) {
43
                                Point2D pts = getCenterOfPolygon(geomsjts[j]);
44
                                mPoints[position] = pts;
45
                                position++;
46
                        }
47
                }
48
                return mPoints;
49
        }
50

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

    
61
                InteriorPointArea centro = new InteriorPointArea(geomjts);
62
                com.vividsolutions.jts.geom.Point pto = geomjts.getInteriorPoint();
63

    
64
                return new Point2D(pto.getX(), pto.getY());
65
        }
66

    
67
}