Statistics
| Revision:

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

History | View | Annotate | Download (7.42 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
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Map;
35

    
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.fmap.geom.primitive.Curve2D;
39
import org.gvsig.fmap.geom.util.Converter;
40

    
41
/**
42
 * 
43
 * This class gets the line between two lines
44
 * 
45
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
46
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
47
 * 
48
 */
49

    
50
public class DoubleCrossLineMatcher extends GeomMatcher {
51

    
52
        /**
53
         * Search lines between two lines
54
         */
55
        @SuppressWarnings("unchecked")
56
        public Curve2D[] match() {
57

    
58
                // Group first features collection
59
                HashMap geomstreets1 = groupFeaturesByAttribute(this
60
                                .getGroupFieldPosition(), (List) this.getFeatures().get(0));
61
                List streets1 = new ArrayList();
62
                Iterator it1 = geomstreets1.entrySet().iterator();
63
                while (it1.hasNext()) {
64
                        Map.Entry e = (Map.Entry) it1.next();
65
                        ArrayList geomss = (ArrayList) e.getValue();
66
                        // Insert geometries of one street into array
67
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
68
                        for (int i = 0; i < geomss.size(); i++) {
69
                                arrGeoms[i] = (Geometry) geomss.get(i);
70
                        }
71
                        // Parse geometries to JTS
72
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
73
                                        geomss.get(0).getClass(), arrGeoms);
74
                        // Make the UNION of the all geometries with the same attribute
75
                        com.vividsolutions.jts.geom.Geometry geomstreetjts1 = unionLinesJTS(geomsjts);
76
                        streets1.add(geomstreetjts1);
77
                }
78
                // Group second features collection by one field
79
                HashMap geomstreets2 = groupFeaturesByAttribute(this
80
                                .getGroupFieldPosition(), (List) this.getFeatures().get(1));
81
                List streets2 = new ArrayList();
82
                Iterator it2 = geomstreets2.entrySet().iterator();
83
                while (it2.hasNext()) {
84
                        Map.Entry e = (Map.Entry) it2.next();
85
                        ArrayList geomss = (ArrayList) e.getValue();
86
                        // Insert geometries of one street into array
87
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
88
                        for (int i = 0; i < geomss.size(); i++) {
89
                                arrGeoms[i] = (Geometry) geomss.get(i);
90
                        }
91
                        // Parse geometries to JTS
92
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
93
                                        geomss.get(0).getClass(), arrGeoms);
94
                        // Make the UNION of the all geometries with the same attribute
95
                        com.vividsolutions.jts.geom.Geometry geomstreetjts2 = unionLinesJTS(geomsjts);
96
                        streets2.add(geomstreetjts2);
97
                }
98
                // Group third features collection
99
                HashMap ppalstreets = groupFeaturesByAttribute(this
100
                                .getGroupFieldPosition(), (List) this.getFeatures().get(2));
101
                List streets3 = new ArrayList();
102
                Iterator it3 = ppalstreets.entrySet().iterator();
103
                while (it3.hasNext()) {
104
                        Map.Entry e = (Map.Entry) it3.next();
105
                        ArrayList geomss = (ArrayList) e.getValue();
106
                        // Insert geometries of one street into array
107
                        Geometry[] arrGeoms = new Geometry[geomss.size()];
108
                        for (int i = 0; i < geomss.size(); i++) {
109
                                arrGeoms[i] = (Geometry) geomss.get(i);
110
                        }
111
                        // Parse geometries to JTS
112
                        com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
113
                                        geomss.get(0).getClass(), arrGeoms);
114
                        // Make the UNION of the all geometries with the same attribute
115
                        com.vividsolutions.jts.geom.Geometry geomstreetjts3 = unionLinesJTS(geomsjts);
116
                        streets3.add(geomstreetjts3);
117
                }
118

    
119
                // Intersect each main street with all streets of the second group, if
120
                // it intersects with some street proof with the intersection with
121
                // second streets group
122
                List geomStreets = new ArrayList();
123

    
124
                com.vividsolutions.jts.geom.Point[] segment = new com.vividsolutions.jts.geom.Point[2];
125

    
126
                for (int i = 0; i < streets3.size(); i++) {
127
                        log.debug("STREET PPAL: " + i);
128
                        com.vividsolutions.jts.geom.Geometry street3jts = (com.vividsolutions.jts.geom.Geometry) streets3
129
                                        .get(i);
130
                        for (int j = 0; j < streets1.size(); j++) {
131
                                log.debug("STREET: " + i + " SECUNDARY: " + j);
132
                                com.vividsolutions.jts.geom.Geometry street1jts = (com.vividsolutions.jts.geom.Geometry) streets1
133
                                                .get(j);
134
                                com.vividsolutions.jts.geom.Point pto = intersectTwoLinesJTS(
135
                                                street3jts, street1jts);
136
                                if (pto != null) {
137
                                        for (int k = 0; k < streets2.size(); k++) {
138
                                                log.debug("STREET: " + i + " SECUNDARY: " + j
139
                                                                + " TERTIARY: " + k);
140
                                                com.vividsolutions.jts.geom.Geometry street2jts = (com.vividsolutions.jts.geom.Geometry) streets2
141
                                                                .get(k);
142

    
143
                                                com.vividsolutions.jts.geom.Point pto2 = intersectTwoLinesJTS(
144
                                                                street3jts, street2jts);
145
                                                if (pto2 != null) {
146
                                                        segment[0] = pto;
147
                                                        segment[1] = pto2;
148
                                                        geomStreets.add(segment);
149
                                                        log.debug("Segment added. Number: "
150
                                                                        + geomStreets.size());
151
                                                        log.debug("Segment x1= " + pto.getCoordinate().x
152
                                                                        + " y1= " + pto.getCoordinate().y);
153
                                                        log.debug("Segment x2= " + pto2.getCoordinate().x
154
                                                                        + " y1= " + pto2.getCoordinate().y);
155
                                                }
156
                                        }
157
                                }
158
                        }
159
                }
160

    
161
                List ppalFeats = (List) getFeatures().get(2);
162
                List results = new ArrayList();
163

    
164
                Iterator it7 = ppalFeats.iterator();
165
                while (it7.hasNext()) {
166
                        Feature fea = (Feature) it7.next();
167
                        Geometry geomet = (Geometry) fea.getGeometry("GEOMETRY");
168
                        Geometry[] ges = new Geometry[1];
169
                        ges[0] = geomet;
170
                        com.vividsolutions.jts.geom.Geometry[] jtsGeoms = parseGeomsToJTS(
171
                                        ges[0].getClass(), ges);
172
                        for (int i = 0; i < jtsGeoms.length; i++) {
173
                                for (int j = 0; j < geomStreets.size(); j++) {
174
                                        com.vividsolutions.jts.geom.Point[] ptos = (com.vividsolutions.jts.geom.Point[]) geomStreets
175
                                                        .get(j);
176
                                        boolean cont1 = (jtsGeoms[i].intersects(ptos[0]));
177
                                        if (cont1) {
178
                                                boolean cont2 = jtsGeoms[i].intersects(ptos[1]);
179
                                                if (cont2) {
180
                                                        results.add(fea);
181
                                                }
182
                                        }
183
                                }
184
                        }
185
                }
186

    
187
                Curve2D[] sublines = new Curve2D[results.size()];
188
                Geometry[] geome1 = new Geometry[1];
189
                Geometry geome = null;
190

    
191
                for (int i = 0; i < results.size(); i++) {
192

    
193
                        geome = (Geometry) ((Feature) results.get(i))
194
                                        .getGeometry("GEOMETRY");
195

    
196
                        geome1[0] = geome;
197
                        com.vividsolutions.jts.geom.Geometry[] tempsJts = parseGeomsToJTS(
198
                                        geome1[0].getClass(), geome1);
199
                        com.vividsolutions.jts.geom.Geometry tempgeom = unionLinesJTS(tempsJts);
200
                        Geometry geometr = Converter.jtsToGeometry(tempgeom);
201
                        sublines[i] = (Curve2D) geometr;
202

    
203
                }
204

    
205
                return sublines;
206
        }
207

    
208
}