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