svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / geomatches / TestMatcherUtils.java @ 32479
History | View | Annotate | Download (10.4 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.geomatches; |
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.LayerServer; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.feature.Feature; |
39 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
40 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
43 |
import org.gvsig.fmap.geom.primitive.Point; |
44 |
import org.gvsig.fmap.geom.util.Converter; |
45 |
import org.gvsig.geocoding.geommatches.MatcherUtils; |
46 |
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary; |
47 |
import org.gvsig.geocoding.result.DissolveResult; |
48 |
import org.gvsig.geocoding.result.ScoredFeature; |
49 |
import org.gvsig.geocoding.result.impl.DefaultScoredFeature; |
50 |
import org.gvsig.tools.library.Library; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
* Test
|
56 |
*
|
57 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
58 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
|
59 |
*/
|
60 |
|
61 |
public class TestMatcherUtils extends |
62 |
org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase { |
63 |
|
64 |
private static final Logger log = LoggerFactory |
65 |
.getLogger(TestMatcherUtils.class); |
66 |
|
67 |
@Override
|
68 |
protected void doSetUp() throws Exception { |
69 |
|
70 |
Library geocoLib = new DefaultGeocodingLibrary();
|
71 |
geocoLib.initialize(); |
72 |
geocoLib.postInitialize(); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* tearDown
|
77 |
*/
|
78 |
public void tearDown() { |
79 |
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Test internal point of geometries
|
84 |
*
|
85 |
* @throws DataException
|
86 |
*/
|
87 |
public void testInternalPointGeometry() throws Exception { |
88 |
FeatureStore provStore = LayerServer.getSHPStore(LayerServer |
89 |
.getRegionsFile()); |
90 |
FeatureSet feats = provStore.getFeatureSet(); |
91 |
assertNotNull(feats); |
92 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
93 |
Feature fea = iterator.next(); |
94 |
Geometry geom = fea.getDefaultGeometry(); |
95 |
Point pto = MatcherUtils.internalPointGeometry(geom);
|
96 |
assertTrue(geom.contains(pto.getX(), pto.getY())); |
97 |
} |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Parse geometries to gvSIG model to JTS model and return to gvSIG Model
|
102 |
*
|
103 |
* @throws DataException
|
104 |
* @throws CreateGeometryException
|
105 |
*/
|
106 |
public void testParseGeomsGVToJTSToGV() throws Exception { |
107 |
FeatureStore provStore = LayerServer.getSHPStore(LayerServer |
108 |
.getRegionsFile()); |
109 |
FeatureSet feats = provStore.getFeatureSet(); |
110 |
assertNotNull(feats); |
111 |
List<Geometry> geomsGV = new ArrayList<Geometry>(); |
112 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
113 |
Feature fea = iterator.next(); |
114 |
geomsGV.add(fea.getDefaultGeometry()); |
115 |
} |
116 |
List<com.vividsolutions.jts.geom.Geometry> geomsJTS = MatcherUtils
|
117 |
.parseGeomsGVToJTS(geomsGV); |
118 |
List<Geometry> geomsGV2 = MatcherUtils.parseGeomsJTSToGV(geomsJTS);
|
119 |
|
120 |
assertTrue(geomsGV.get(0).getDimension() == geomsGV2.get(0) |
121 |
.getDimension()); |
122 |
assertTrue(geomsGV.get(1).getType() == geomsGV2.get(1).getType()); |
123 |
assertTrue(geomsGV.get(2).getDimension() == geomsGV2.get(2) |
124 |
.getDimension()); |
125 |
assertTrue(geomsGV.get(3).getType() == geomsGV2.get(3).getType()); |
126 |
assertTrue(geomsGV.get(4).getType() == geomsGV2.get(4).getType()); |
127 |
assertTrue(geomsGV.get(5).getBounds().getCenterX() == geomsGV2.get(5) |
128 |
.getBounds().getCenterX()); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Intersection test
|
133 |
*
|
134 |
* @throws DataException
|
135 |
*/
|
136 |
public void testDissolveLines() throws Exception { |
137 |
|
138 |
List<DissolveResult> results = new ArrayList<DissolveResult>(); |
139 |
FeatureStore sLinesStore = LayerServer.getSHPStore(LayerServer |
140 |
.getSimpleLinesFile()); |
141 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
142 |
assertNotNull(feats); |
143 |
|
144 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
145 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
146 |
Feature fea = iterator.next(); |
147 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
148 |
sFeat.setFeature(fea); |
149 |
listFeats.add(sFeat); |
150 |
} |
151 |
// group by attribute
|
152 |
HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils |
153 |
.groupScoredFeaturesByAttribute("Campo1", listFeats);
|
154 |
|
155 |
// For each group of features, to do geometries dissolve process getting
|
156 |
// at the end one o more geometries
|
157 |
for (Iterator<Map.Entry<String, List<ScoredFeature>>> iterator = dissolAttributes |
158 |
.entrySet().iterator(); iterator.hasNext();) { |
159 |
Map.Entry<String, List<ScoredFeature>> e = iterator.next(); |
160 |
List<ScoredFeature> inlist = e.getValue();
|
161 |
|
162 |
// dissolve Lines (if first geometry of array is Line)
|
163 |
List<DissolveResult> geomsDissol = MatcherUtils.dissolveGeomsJTS(
|
164 |
inlist, MatcherUtils.LINES); |
165 |
|
166 |
// insert each dissolve result in the general array
|
167 |
for (DissolveResult dissolveResult : geomsDissol) {
|
168 |
results.add(dissolveResult); |
169 |
} |
170 |
} |
171 |
assertNotNull(results); |
172 |
assertEquals(results.size(), 4);
|
173 |
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Intersection test
|
178 |
*
|
179 |
* @throws DataException
|
180 |
*/
|
181 |
public void testDissolvePolys() throws Exception { |
182 |
List<DissolveResult> results = new ArrayList<DissolveResult>(); |
183 |
|
184 |
FeatureStore provStore = LayerServer.getSHPStore(LayerServer |
185 |
.getRegionsFile()); |
186 |
FeatureSet feats = provStore.getFeatureSet(); |
187 |
assertNotNull(feats); |
188 |
|
189 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
190 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
191 |
Feature fea = iterator.next(); |
192 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
193 |
sFeat.setFeature(fea); |
194 |
sFeat.setScore(Math.random());
|
195 |
listFeats.add(sFeat); |
196 |
} |
197 |
// group by attribute
|
198 |
HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils |
199 |
.groupScoredFeaturesByAttribute("COM", listFeats);
|
200 |
|
201 |
// For each group of features, to do geometries dissolve process getting
|
202 |
// at the end one o more geometries
|
203 |
for (Iterator<Map.Entry<String, List<ScoredFeature>>> iterator = dissolAttributes |
204 |
.entrySet().iterator(); iterator.hasNext();) { |
205 |
Map.Entry<String, List<ScoredFeature>> e = iterator.next(); |
206 |
List<ScoredFeature> inlist = e.getValue();
|
207 |
|
208 |
// dissolve Polys (if first geometry of array is Line)
|
209 |
List<DissolveResult> geomsDissol = MatcherUtils.dissolveGeomsJTS(
|
210 |
inlist, MatcherUtils.POLYS); |
211 |
|
212 |
// insert each dissolve result in the general array
|
213 |
for (DissolveResult dissolveResult : geomsDissol) {
|
214 |
results.add(dissolveResult); |
215 |
} |
216 |
} |
217 |
assertNotNull(results); |
218 |
assertEquals(results.size(), 57);
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* Intersection test
|
223 |
*
|
224 |
* @throws DataException
|
225 |
*/
|
226 |
public void testIntersectLines() throws DataException { |
227 |
|
228 |
// FeatureSet feats = sLinesStore.getFeatureSet();
|
229 |
// assertNotNull(feats);
|
230 |
//
|
231 |
// List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>();
|
232 |
// for (Iterator<Feature> iterator = feats.iterator();
|
233 |
// iterator.hasNext();) {
|
234 |
// Feature fea = iterator.next();
|
235 |
// ScoredFeature sFeat = new ScoredFeature();
|
236 |
// sFeat.setFeature(fea);
|
237 |
// listFeats.add(sFeat);
|
238 |
// }
|
239 |
// // group by attribute
|
240 |
// FeatureAttributeDescriptor field =
|
241 |
// sLinesStore.getDefaultFeatureType().getAttributeDescriptor("Campo1");
|
242 |
// HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils
|
243 |
// .groupScoredFeaturesByAttribute(field, listFeats);
|
244 |
|
245 |
} |
246 |
|
247 |
public void testPositionOverLines() throws Exception { |
248 |
FeatureStore sLinesStore = LayerServer.getSHPStore(LayerServer |
249 |
.getSimpleLinesFile()); |
250 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
251 |
assertNotNull(feats); |
252 |
|
253 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
254 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
255 |
Feature fea = iterator.next(); |
256 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
257 |
sFeat.setFeature(fea); |
258 |
listFeats.add(sFeat); |
259 |
} |
260 |
|
261 |
for (ScoredFeature sFeat : listFeats) {
|
262 |
Geometry geo = sFeat.getFeature().getDefaultGeometry(); |
263 |
com.vividsolutions.jts.geom.Geometry geomJTS = Converter |
264 |
.geometryToJts(geo); |
265 |
Point pto = MatcherUtils.getLinePositionFromDistance(geomJTS, 1000); |
266 |
com.vividsolutions.jts.geom.Geometry ptoJTS = Converter |
267 |
.geometryToJts(pto); |
268 |
log.debug("JTS Point: x= "+ptoJTS.getCoordinate().x+" y= "+ptoJTS.getCoordinate().y); |
269 |
log.debug("Intersect = "+ptoJTS.intersects(geomJTS));
|
270 |
log.debug("Touches = "+ptoJTS.touches(geomJTS));
|
271 |
|
272 |
assertTrue(ptoJTS.intersects(geomJTS) || ptoJTS.touches(geomJTS)); |
273 |
} |
274 |
} |
275 |
|
276 |
/**
|
277 |
* Calculate the relative position ( 50% of the geometry)
|
278 |
*
|
279 |
* @throws DataException
|
280 |
*/
|
281 |
public void testRelativePositionOverLines() throws Exception { |
282 |
|
283 |
FeatureStore sLinesStore = LayerServer.getSHPStore(LayerServer |
284 |
.getSimpleLinesFile()); |
285 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
286 |
assertNotNull(feats); |
287 |
|
288 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
289 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
290 |
Feature fea = iterator.next(); |
291 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
292 |
sFeat.setFeature(fea); |
293 |
listFeats.add(sFeat); |
294 |
} |
295 |
|
296 |
for (ScoredFeature sFeat : listFeats) {
|
297 |
Geometry geo = sFeat.getFeature().getDefaultGeometry(); |
298 |
com.vividsolutions.jts.geom.Geometry geomJTS = Converter |
299 |
.geometryToJts(geo); |
300 |
Point pto = MatcherUtils.getLinePositionFromRelativeDistance(
|
301 |
geomJTS, 50);
|
302 |
com.vividsolutions.jts.geom.Geometry ptoJTS = Converter |
303 |
.geometryToJts(pto); |
304 |
assertTrue(ptoJTS.intersects(geomJTS) || ptoJTS.touches(geomJTS)); |
305 |
} |
306 |
|
307 |
} |
308 |
|
309 |
} |