Statistics
| Revision:

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

History | View | Annotate | Download (4.86 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 Prodevelop S.L  main development
26
 */
27

    
28
package org.gvsig.geocoding.geommatches;
29

    
30

    
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Set;
38

    
39
import org.apache.log4j.Logger;
40
import org.gvsig.fmap.geom.Geometry;
41
import org.gvsig.fmap.geom.primitive.Point2D;
42

    
43
/**
44
 * This class searchs positions in cross lines (cross ways)
45
 * 
46
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
47
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
48
 * 
49
 */
50

    
51
public class CrossLineMatcher extends GeomMatcher {
52

    
53
        private Logger log = Logger.getLogger(CrossLineMatcher.class);
54

    
55
        /**
56
         * Search the position in the cross lines
57
         * 
58
         * @return cross points
59
         */
60

    
61
        @SuppressWarnings("unchecked")
62
        public Point2D[] match() {
63

    
64
                // Group first features collection by one field
65
                HashMap geomstreets1 = groupFeaturesByAttribute(this
66
                                .getGroupFieldPosition(), (List) this
67
                                .getFeatures().get(0));
68
                List streets1 = new ArrayList();
69
                Iterator itt1 = geomstreets1.entrySet().iterator();
70
                while (itt1.hasNext()) {
71
                        Map.Entry e = (Map.Entry) itt1.next();
72
                        ArrayList geomss1 = (ArrayList) e.getValue();
73
                        // Insert geometries of one street into array
74
                        Geometry[] arrGeoms1 = new Geometry[geomss1.size()];
75
                        for (int i = 0; i < geomss1.size(); i++) {
76
                                arrGeoms1[i] = (Geometry) geomss1.get(i);
77
                        }
78
                        // Parse geometries to JTS
79
                        com.vividsolutions.jts.geom.Geometry[] geomsjts1 = parseGeomsToJTS(
80
                                        geomss1.get(0).getClass(), arrGeoms1);
81
                        // Make the UNION of the all geometries with the same attribute
82
                        com.vividsolutions.jts.geom.Geometry geomstreetjts1 = unionLinesJTS(geomsjts1);
83
                        streets1.add(geomstreetjts1);
84
                }
85

    
86
                // Group second features collection by one field
87
                HashMap geomstreets2 = groupFeaturesByAttribute(this
88
                                .getGroupFieldPosition(), (List) this
89
                                .getFeatures().get(1));
90
                List streets2 = new ArrayList();
91
                Iterator itt2 = geomstreets2.entrySet().iterator();
92
                while (itt2.hasNext()) {
93
                        Map.Entry e = (Map.Entry) itt2.next();
94
                        ArrayList geomss = (ArrayList) e.getValue();
95
                        // Insert geometries of one street into array
96
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
97
                        for (int i = 0; i < geomss.size(); i++) {
98
                                arrGeoms[i] = (Geometry) geomss.get(i);
99
                        }
100
                        // Parse geometries to JTS
101
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
102
                                        geomss.get(0).getClass(), arrGeoms);
103
                        // Make the UNION of the all geometries with the same attribute
104
                        com.vividsolutions.jts.geom.Geometry geomstreetjts = unionLinesJTS(geomsjts);
105
                        streets2.add(geomstreetjts);
106
                }
107

    
108
                // Proof the intersection of geometries
109
                com.vividsolutions.jts.geom.Point pto = null;
110
                Set interPoints = new HashSet();
111
                for (int i = 0; i < streets1.size(); i++) {
112
                        com.vividsolutions.jts.geom.Geometry geomJts1 = (com.vividsolutions.jts.geom.Geometry) streets1
113
                                        .get(i);
114
                        for (int j = 0; j < streets2.size(); j++) {
115
                                com.vividsolutions.jts.geom.Geometry geomJts2 = (com.vividsolutions.jts.geom.Geometry) streets2
116
                                                .get(j);
117

    
118
                                pto = intersectTwoLinesJTS(geomJts1, geomJts2);
119
                                if (pto != null) {
120
                                        log.debug("Intersecction point. X= " + pto.getX() + " Y= "
121
                                                        + pto.getY());
122
                                        interPoints.add(pto);
123
                                        log.debug("Point added ");
124
                                } else {
125
                                        log.info("No intersection between two lines");
126
                                }
127
                                log.debug("-------------------");
128
                        }
129
                }
130

    
131
                // Parse HashSet to Point2D Array
132
                Point2D[] points = new Point2D[interPoints.size()];
133
                Iterator itt5 = interPoints.iterator();
134
                int cont = 0;
135
                while (itt5.hasNext()) {
136
                        com.vividsolutions.jts.geom.Point po = (com.vividsolutions.jts.geom.Point) itt5
137
                                        .next();
138
                        points[cont] = new Point2D(po.getCoordinate().x,
139
                                        po.getCoordinate().y);
140
                        cont++;
141
                }
142
                return points;
143
        }
144
}