svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / geomatches / TestMatcherUtils.java @ 28375
History | View | Annotate | Download (12.1 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.io.File; |
31 |
import java.util.ArrayList; |
32 |
import java.util.HashMap; |
33 |
import java.util.Iterator; |
34 |
import java.util.List; |
35 |
import java.util.Map; |
36 |
|
37 |
import junit.framework.TestCase; |
38 |
|
39 |
import org.gvsig.fmap.dal.DALFileLibrary; |
40 |
import org.gvsig.fmap.dal.DALLibrary; |
41 |
import org.gvsig.fmap.dal.DALLocator; |
42 |
import org.gvsig.fmap.dal.DataManager; |
43 |
import org.gvsig.fmap.dal.exception.DataException; |
44 |
import org.gvsig.fmap.dal.feature.Feature; |
45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
47 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
48 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
49 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
50 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.gvsig.fmap.geom.GeometryLibrary; |
53 |
import org.gvsig.fmap.geom.primitive.Point; |
54 |
import org.gvsig.fmap.geom.util.Converter; |
55 |
import org.gvsig.geocoding.geommatches.MatcherUtils; |
56 |
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary; |
57 |
import org.gvsig.geocoding.result.DissolveResult; |
58 |
import org.gvsig.geocoding.result.ScoredFeature; |
59 |
import org.gvsig.geocoding.result.impl.DefaultScoredFeature; |
60 |
import org.gvsig.tools.ToolsLibrary; |
61 |
import org.gvsig.tools.locator.Library; |
62 |
import org.slf4j.Logger; |
63 |
import org.slf4j.LoggerFactory; |
64 |
|
65 |
/**
|
66 |
* Test
|
67 |
*
|
68 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
69 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
70 |
*/
|
71 |
|
72 |
public class TestMatcherUtils extends TestCase { |
73 |
|
74 |
private static final Logger log = LoggerFactory |
75 |
.getLogger(TestMatcherUtils.class); |
76 |
|
77 |
private DataManager dataManager = null; |
78 |
private FeatureStore provStore = null; |
79 |
private FeatureStore streetsStore = null; |
80 |
private FeatureStore sLinesStore = null; |
81 |
|
82 |
/**
|
83 |
* setUP
|
84 |
*/
|
85 |
public void setUp() { |
86 |
|
87 |
try {
|
88 |
Library tools = new ToolsLibrary();
|
89 |
tools.initialize(); |
90 |
tools.postInitialize(); |
91 |
|
92 |
Library dlib = new DALLibrary();
|
93 |
dlib.initialize(); |
94 |
dlib.postInitialize(); |
95 |
|
96 |
Library libFile = new DALFileLibrary();
|
97 |
libFile.initialize(); |
98 |
libFile.postInitialize(); |
99 |
|
100 |
Library lib = new GeometryLibrary();
|
101 |
lib.initialize(); |
102 |
lib.postInitialize(); |
103 |
|
104 |
Library shpLib = new SHPLibrary();
|
105 |
shpLib.initialize(); |
106 |
shpLib.postInitialize(); |
107 |
|
108 |
Library geocoLib = new DefaultGeocodingLibrary();
|
109 |
geocoLib.initialize(); |
110 |
geocoLib.postInitialize(); |
111 |
|
112 |
} catch (Exception e) { |
113 |
e.printStackTrace(); |
114 |
} |
115 |
|
116 |
dataManager = DALLocator.getDataManager(); |
117 |
|
118 |
try {
|
119 |
// prov file
|
120 |
File prov = new File("./test-data/geocoder/prov.shp"); |
121 |
assertTrue(prov.exists()); |
122 |
SHPStoreParameters provParam = (SHPStoreParameters) dataManager |
123 |
.createStoreParameters(SHPStoreProvider.NAME); |
124 |
provParam.setFile(prov); |
125 |
provStore = (FeatureStore) dataManager.createStore(provParam); |
126 |
assertNotNull(provStore); |
127 |
} catch (Exception e) { |
128 |
log.error("Getting prov.shp store", e);
|
129 |
} |
130 |
try {
|
131 |
// streets file
|
132 |
File streets = new File("./test-data/geocoder/streets.shp"); |
133 |
assertTrue(streets.exists()); |
134 |
SHPStoreParameters strParam = (SHPStoreParameters) dataManager |
135 |
.createStoreParameters(SHPStoreProvider.NAME); |
136 |
strParam.setFile(streets); |
137 |
streetsStore = (FeatureStore) dataManager.createStore(strParam); |
138 |
assertNotNull(streetsStore); |
139 |
|
140 |
} catch (Exception e) { |
141 |
log.error("Getting streets.shp store", e);
|
142 |
} |
143 |
try {
|
144 |
// streets file
|
145 |
File sLines = new File("./test-data/geocoder/simpleLines.shp"); |
146 |
assertTrue(sLines.exists()); |
147 |
SHPStoreParameters sLinesParam = (SHPStoreParameters) dataManager |
148 |
.createStoreParameters(SHPStoreProvider.NAME); |
149 |
sLinesParam.setFile(sLines); |
150 |
sLinesStore = (FeatureStore) dataManager.createStore(sLinesParam); |
151 |
assertNotNull(sLinesStore); |
152 |
|
153 |
} catch (Exception e) { |
154 |
log.error("Getting streets.shp store", e);
|
155 |
} |
156 |
} |
157 |
|
158 |
/**
|
159 |
* tearDown
|
160 |
*/
|
161 |
public void tearDown() { |
162 |
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* Test internal point of geometries
|
167 |
*
|
168 |
* @throws DataException
|
169 |
*/
|
170 |
public void testInternalPointGeometry() throws DataException { |
171 |
|
172 |
FeatureSet feats = provStore.getFeatureSet(); |
173 |
assertNotNull(feats); |
174 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
175 |
Feature fea = iterator.next(); |
176 |
Geometry geom = fea.getDefaultGeometry(); |
177 |
Point pto = MatcherUtils.internalPointGeometry(geom);
|
178 |
assertTrue(geom.contains(pto.getX(), pto.getY())); |
179 |
} |
180 |
} |
181 |
|
182 |
/**
|
183 |
* Parse geometries to gvSIG model to JTS model and return to gvSIG Model
|
184 |
*
|
185 |
* @throws DataException
|
186 |
*/
|
187 |
public void testParseGeomsGVToJTSToGV() throws DataException { |
188 |
FeatureSet feats = provStore.getFeatureSet(); |
189 |
assertNotNull(feats); |
190 |
List<Geometry> geomsGV = new ArrayList<Geometry>(); |
191 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
192 |
Feature fea = iterator.next(); |
193 |
geomsGV.add(fea.getDefaultGeometry()); |
194 |
} |
195 |
List<com.vividsolutions.jts.geom.Geometry> geomsJTS = MatcherUtils
|
196 |
.parseGeomsGVToJTS(geomsGV); |
197 |
List<Geometry> geomsGV2 = MatcherUtils.parseGeomsJTSToGV(geomsJTS);
|
198 |
|
199 |
assertTrue(geomsGV.get(0).getDimension() == geomsGV2.get(0) |
200 |
.getDimension()); |
201 |
assertTrue(geomsGV.get(1).getType() == geomsGV2.get(1).getType()); |
202 |
assertTrue(geomsGV.get(2).getDimension() == geomsGV2.get(2) |
203 |
.getDimension()); |
204 |
assertTrue(geomsGV.get(3).getType() == geomsGV2.get(3).getType()); |
205 |
assertTrue(geomsGV.get(4).getType() == geomsGV2.get(4).getType()); |
206 |
assertTrue(geomsGV.get(5).getBounds().getCenterX() == geomsGV2.get(5) |
207 |
.getBounds().getCenterX()); |
208 |
} |
209 |
|
210 |
/**
|
211 |
* Intersection test
|
212 |
*
|
213 |
* @throws DataException
|
214 |
*/
|
215 |
public void testDissolveLines() throws DataException { |
216 |
|
217 |
List<DissolveResult> results = new ArrayList<DissolveResult>(); |
218 |
|
219 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
220 |
assertNotNull(feats); |
221 |
|
222 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
223 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
224 |
Feature fea = iterator.next(); |
225 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
226 |
sFeat.setFeature(fea); |
227 |
listFeats.add(sFeat); |
228 |
} |
229 |
// group by attribute
|
230 |
HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils |
231 |
.groupScoredFeaturesByAttribute("Campo1", listFeats);
|
232 |
|
233 |
// For each group of features, to do geometries dissolve process getting
|
234 |
// at the end one o more geometries
|
235 |
for (Iterator<Map.Entry<String, List<ScoredFeature>>> iterator = dissolAttributes |
236 |
.entrySet().iterator(); iterator.hasNext();) { |
237 |
Map.Entry<String, List<ScoredFeature>> e = iterator.next(); |
238 |
List<ScoredFeature> inlist = e.getValue();
|
239 |
|
240 |
// dissolve Lines (if first geometry of array is Line)
|
241 |
List<DissolveResult> geomsDissol = MatcherUtils.dissolveGeomsJTS(
|
242 |
inlist, MatcherUtils.LINES); |
243 |
|
244 |
// insert each dissolve result in the general array
|
245 |
for (DissolveResult dissolveResult : geomsDissol) {
|
246 |
results.add(dissolveResult); |
247 |
} |
248 |
} |
249 |
assertNotNull(results); |
250 |
assertEquals(results.size(), 4);
|
251 |
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Intersection test
|
256 |
*
|
257 |
* @throws DataException
|
258 |
*/
|
259 |
public void testDissolvePolys() throws DataException { |
260 |
List<DissolveResult> results = new ArrayList<DissolveResult>(); |
261 |
|
262 |
FeatureSet feats = provStore.getFeatureSet(); |
263 |
assertNotNull(feats); |
264 |
|
265 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
266 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
267 |
Feature fea = iterator.next(); |
268 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
269 |
sFeat.setFeature(fea); |
270 |
sFeat.setScore(Math.random());
|
271 |
listFeats.add(sFeat); |
272 |
} |
273 |
// group by attribute
|
274 |
HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils |
275 |
.groupScoredFeaturesByAttribute("COM", listFeats);
|
276 |
|
277 |
// For each group of features, to do geometries dissolve process getting
|
278 |
// at the end one o more geometries
|
279 |
for (Iterator<Map.Entry<String, List<ScoredFeature>>> iterator = dissolAttributes |
280 |
.entrySet().iterator(); iterator.hasNext();) { |
281 |
Map.Entry<String, List<ScoredFeature>> e = iterator.next(); |
282 |
List<ScoredFeature> inlist = e.getValue();
|
283 |
|
284 |
// dissolve Polys (if first geometry of array is Line)
|
285 |
List<DissolveResult> geomsDissol = MatcherUtils.dissolveGeomsJTS(
|
286 |
inlist, MatcherUtils.POLYS); |
287 |
|
288 |
// insert each dissolve result in the general array
|
289 |
for (DissolveResult dissolveResult : geomsDissol) {
|
290 |
results.add(dissolveResult); |
291 |
} |
292 |
} |
293 |
assertNotNull(results); |
294 |
assertEquals(results.size(), 57);
|
295 |
} |
296 |
|
297 |
/**
|
298 |
* Intersection test
|
299 |
*
|
300 |
* @throws DataException
|
301 |
*/
|
302 |
public void testIntersectLines() throws DataException { |
303 |
|
304 |
// FeatureSet feats = sLinesStore.getFeatureSet();
|
305 |
// assertNotNull(feats);
|
306 |
//
|
307 |
// List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>();
|
308 |
// for (Iterator<Feature> iterator = feats.iterator();
|
309 |
// iterator.hasNext();) {
|
310 |
// Feature fea = iterator.next();
|
311 |
// ScoredFeature sFeat = new ScoredFeature();
|
312 |
// sFeat.setFeature(fea);
|
313 |
// listFeats.add(sFeat);
|
314 |
// }
|
315 |
// // group by attribute
|
316 |
// FeatureAttributeDescriptor field =
|
317 |
// sLinesStore.getDefaultFeatureType().getAttributeDescriptor("Campo1");
|
318 |
// HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils
|
319 |
// .groupScoredFeaturesByAttribute(field, listFeats);
|
320 |
|
321 |
} |
322 |
|
323 |
public void testPositionOverLines() throws DataException { |
324 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
325 |
assertNotNull(feats); |
326 |
|
327 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
328 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
329 |
Feature fea = iterator.next(); |
330 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
331 |
sFeat.setFeature(fea); |
332 |
listFeats.add(sFeat); |
333 |
} |
334 |
|
335 |
for (ScoredFeature sFeat : listFeats) {
|
336 |
Geometry geo = sFeat.getFeature().getDefaultGeometry(); |
337 |
com.vividsolutions.jts.geom.Geometry geomJTS = Converter |
338 |
.geometryToJts(geo); |
339 |
Point pto = MatcherUtils.getLinePositionFromDistance(geomJTS, 1000); |
340 |
com.vividsolutions.jts.geom.Geometry ptoJTS = Converter |
341 |
.geometryToJts(pto); |
342 |
assertTrue(ptoJTS.intersects(geomJTS) || ptoJTS.touches(geomJTS)); |
343 |
} |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Calculate the relative position ( 50% of the geometry)
|
348 |
*
|
349 |
* @throws DataException
|
350 |
*/
|
351 |
public void testRelativePositionOverLines() throws DataException { |
352 |
FeatureSet feats = sLinesStore.getFeatureSet(); |
353 |
assertNotNull(feats); |
354 |
|
355 |
List<ScoredFeature> listFeats = new ArrayList<ScoredFeature>(); |
356 |
for (Iterator<Feature> iterator = feats.iterator(); iterator.hasNext();) { |
357 |
Feature fea = iterator.next(); |
358 |
ScoredFeature sFeat = new DefaultScoredFeature();
|
359 |
sFeat.setFeature(fea); |
360 |
listFeats.add(sFeat); |
361 |
} |
362 |
|
363 |
for (ScoredFeature sFeat : listFeats) {
|
364 |
Geometry geo = sFeat.getFeature().getDefaultGeometry(); |
365 |
com.vividsolutions.jts.geom.Geometry geomJTS = Converter |
366 |
.geometryToJts(geo); |
367 |
Point pto = MatcherUtils.getLinePositionFromRelativeDistance(
|
368 |
geomJTS, 50);
|
369 |
com.vividsolutions.jts.geom.Geometry ptoJTS = Converter |
370 |
.geometryToJts(pto); |
371 |
assertTrue(ptoJTS.intersects(geomJTS) || ptoJTS.touches(geomJTS)); |
372 |
} |
373 |
|
374 |
} |
375 |
|
376 |
} |