svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / geommatches / CrossLineMatcher.java @ 27057
History | View | Annotate | Download (4.84 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.HashSet; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
import java.util.Map; |
36 |
import java.util.Set; |
37 |
|
38 |
import org.apache.log4j.Logger; |
39 |
import org.gvsig.fmap.geom.Geometry; |
40 |
import org.gvsig.fmap.geom.primitive.Point2D; |
41 |
|
42 |
/**
|
43 |
* This class searchs positions in cross lines (cross ways)
|
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 CrossLineMatcher extends GeomMatcher { |
51 |
|
52 |
private Logger log = Logger.getLogger(CrossLineMatcher.class); |
53 |
|
54 |
/**
|
55 |
* Search the position in the cross lines
|
56 |
*
|
57 |
* @return cross points
|
58 |
*/
|
59 |
|
60 |
@SuppressWarnings("unchecked") |
61 |
public Point2D[] match() { |
62 |
|
63 |
// Group first features collection by one field
|
64 |
HashMap geomstreets1 = groupFeaturesByAttribute(this |
65 |
.getGroupFieldPosition(), (List) this.getFeatures().get(0)); |
66 |
List streets1 = new ArrayList(); |
67 |
Iterator itt1 = geomstreets1.entrySet().iterator();
|
68 |
while (itt1.hasNext()) {
|
69 |
Map.Entry e = (Map.Entry) itt1.next(); |
70 |
ArrayList geomss1 = (ArrayList) e.getValue(); |
71 |
// Insert geometries of one street into array
|
72 |
Geometry[] arrGeoms1 = new Geometry[geomss1.size()]; |
73 |
for (int i = 0; i < geomss1.size(); i++) { |
74 |
arrGeoms1[i] = (Geometry) geomss1.get(i); |
75 |
} |
76 |
// Parse geometries to JTS
|
77 |
com.vividsolutions.jts.geom.Geometry[] geomsjts1 = parseGeomsToJTS(
|
78 |
geomss1.get(0).getClass(), arrGeoms1);
|
79 |
// Make the UNION of the all geometries with the same attribute
|
80 |
com.vividsolutions.jts.geom.Geometry geomstreetjts1 = unionLinesJTS(geomsjts1); |
81 |
streets1.add(geomstreetjts1); |
82 |
} |
83 |
|
84 |
// Group second features collection by one field
|
85 |
HashMap geomstreets2 = groupFeaturesByAttribute(this |
86 |
.getGroupFieldPosition(), (List) this.getFeatures().get(1)); |
87 |
List streets2 = new ArrayList(); |
88 |
Iterator itt2 = geomstreets2.entrySet().iterator();
|
89 |
while (itt2.hasNext()) {
|
90 |
Map.Entry e = (Map.Entry) itt2.next(); |
91 |
ArrayList geomss = (ArrayList) e.getValue(); |
92 |
// Insert geometries of one street into array
|
93 |
Geometry[] arrGeoms = new Geometry[geomss.size()]; |
94 |
for (int i = 0; i < geomss.size(); i++) { |
95 |
arrGeoms[i] = (Geometry) geomss.get(i); |
96 |
} |
97 |
// Parse geometries to JTS
|
98 |
com.vividsolutions.jts.geom.Geometry[] geomsjts = parseGeomsToJTS(
|
99 |
geomss.get(0).getClass(), arrGeoms);
|
100 |
// Make the UNION of the all geometries with the same attribute
|
101 |
com.vividsolutions.jts.geom.Geometry geomstreetjts = unionLinesJTS(geomsjts); |
102 |
streets2.add(geomstreetjts); |
103 |
} |
104 |
|
105 |
// Proof the intersection of geometries
|
106 |
com.vividsolutions.jts.geom.Point pto = null;
|
107 |
Set interPoints = new HashSet(); |
108 |
for (int i = 0; i < streets1.size(); i++) { |
109 |
com.vividsolutions.jts.geom.Geometry geomJts1 = (com.vividsolutions.jts.geom.Geometry) streets1 |
110 |
.get(i); |
111 |
for (int j = 0; j < streets2.size(); j++) { |
112 |
com.vividsolutions.jts.geom.Geometry geomJts2 = (com.vividsolutions.jts.geom.Geometry) streets2 |
113 |
.get(j); |
114 |
|
115 |
pto = intersectTwoLinesJTS(geomJts1, geomJts2); |
116 |
if (pto != null) { |
117 |
log.debug("Intersecction point. X= " + pto.getX() + " Y= " |
118 |
+ pto.getY()); |
119 |
interPoints.add(pto); |
120 |
log.debug("Point added ");
|
121 |
} else {
|
122 |
log.info("No intersection between two lines");
|
123 |
} |
124 |
log.debug("-------------------");
|
125 |
} |
126 |
} |
127 |
|
128 |
// Parse HashSet to Point2D Array
|
129 |
Point2D[] points = new Point2D[interPoints.size()]; |
130 |
Iterator itt5 = interPoints.iterator();
|
131 |
int cont = 0; |
132 |
while (itt5.hasNext()) {
|
133 |
com.vividsolutions.jts.geom.Point po = (com.vividsolutions.jts.geom.Point) itt5 |
134 |
.next(); |
135 |
points[cont] = new Point2D(po.getCoordinate().x, |
136 |
po.getCoordinate().y); |
137 |
cont++; |
138 |
} |
139 |
return points;
|
140 |
} |
141 |
} |