svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / styles / DoubleRange.java @ 27064
History | View | Annotate | Download (9.95 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.styles; |
29 |
|
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
import java.util.Set; |
33 |
import java.util.TreeSet; |
34 |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
36 |
import org.gvsig.fmap.geom.Geometry; |
37 |
import org.gvsig.fmap.geom.primitive.Point2D; |
38 |
import org.gvsig.geocoding.Address; |
39 |
import org.gvsig.geocoding.NumberAddress; |
40 |
import org.gvsig.geocoding.RelationsComponent; |
41 |
import org.gvsig.geocoding.geommatches.MatcherUtils; |
42 |
import org.gvsig.geocoding.result.GeocodingResult; |
43 |
import org.gvsig.geocoding.result.GeomMatchResult; |
44 |
import org.gvsig.geocoding.result.ScoredFeature; |
45 |
import org.gvsig.tools.persistence.AbstractPersistenceManager; |
46 |
import org.gvsig.tools.persistence.PersistenceException; |
47 |
import org.gvsig.tools.persistence.PersistentState; |
48 |
|
49 |
/**
|
50 |
*
|
51 |
*
|
52 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
53 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
54 |
*/
|
55 |
|
56 |
public class DoubleRange extends AbstractRange { |
57 |
|
58 |
private static final String FIRSTLEFTNUMBER = "firstleftnumber"; |
59 |
private static final String FIRSTRIGHTNUMBER = "firstrightnumber"; |
60 |
private static final String LASTLEFTNUMBER = "lastleftnumber"; |
61 |
private static final String LASTRIGHTNUMBER = "firstrightnumber"; |
62 |
|
63 |
private RelationsComponent firstLeftNumber;
|
64 |
private RelationsComponent firstRightNumber;
|
65 |
private RelationsComponent lastLeftNumber;
|
66 |
private RelationsComponent lastRightNumber;
|
67 |
|
68 |
/**
|
69 |
* Get relation component that define the field of data source that it has
|
70 |
* the first left number of the feature
|
71 |
*
|
72 |
* @return
|
73 |
*/
|
74 |
public RelationsComponent getFirstLeftNumber() {
|
75 |
return firstLeftNumber;
|
76 |
} |
77 |
|
78 |
/**
|
79 |
* Set relation component that define the field of data source that it has
|
80 |
* the first left number of the feature
|
81 |
*
|
82 |
* @param firstLeftNumber
|
83 |
*/
|
84 |
public void setFirstLeftNumber(RelationsComponent firstLeftNumber) { |
85 |
this.firstLeftNumber = firstLeftNumber;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* Get relation component that define the field of data source that it has
|
90 |
* the first right number of the feature
|
91 |
*
|
92 |
* @return
|
93 |
*/
|
94 |
public RelationsComponent getFirstRightNumber() {
|
95 |
return firstRightNumber;
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Set relation component that define the field of data source that it has
|
100 |
* the first right number of the feature
|
101 |
*
|
102 |
* @param firstRightNumber
|
103 |
*/
|
104 |
public void setFirstRightNumber(RelationsComponent firstRightNumber) { |
105 |
this.firstRightNumber = firstRightNumber;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
* Get relation component that define the field of data source that it has
|
110 |
* the last left number of the feature
|
111 |
*
|
112 |
* @return
|
113 |
*/
|
114 |
public RelationsComponent getLastLeftNumber() {
|
115 |
return lastLeftNumber;
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* Set relation component that define the field of data source that it has
|
120 |
* the last left number of the feature
|
121 |
*
|
122 |
* @param lastLeftNumber
|
123 |
*/
|
124 |
public void setLastLeftNumber(RelationsComponent lastLeftNumber) { |
125 |
this.lastLeftNumber = lastLeftNumber;
|
126 |
} |
127 |
|
128 |
/**
|
129 |
* Get relation component that define the field of data source that it has
|
130 |
* the last right number of the feature
|
131 |
*
|
132 |
* @return
|
133 |
*/
|
134 |
public RelationsComponent getLastRightNumber() {
|
135 |
return lastRightNumber;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Set relation component that define the field of data source that it has
|
140 |
* the last right number of the feature
|
141 |
*
|
142 |
* @param lastRightNumber
|
143 |
*/
|
144 |
public void setLastRightNumber(RelationsComponent lastRightNumber) { |
145 |
this.lastRightNumber = lastRightNumber;
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Get the persistent state of the doublerange and append to the passed
|
150 |
* state.
|
151 |
*
|
152 |
* @param state
|
153 |
*/
|
154 |
@Override
|
155 |
public PersistentState getState() throws PersistenceException { |
156 |
return AbstractPersistenceManager.getState(this); |
157 |
} |
158 |
|
159 |
/**
|
160 |
* Load the state of the doublerange over the passed state.
|
161 |
*
|
162 |
* @param state
|
163 |
*/
|
164 |
@Override
|
165 |
public void loadState(PersistentState state) throws PersistenceException { |
166 |
state.setTheClass(this);
|
167 |
state.set(FIRSTLEFTNUMBER, this.firstLeftNumber);
|
168 |
state.set(FIRSTRIGHTNUMBER, this.firstRightNumber);
|
169 |
state.set(LASTLEFTNUMBER, this.lastLeftNumber);
|
170 |
state.set(LASTRIGHTNUMBER, this.lastRightNumber);
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Set the state of the doublerange from the state passed as parameter.
|
175 |
*
|
176 |
* @param state
|
177 |
*/
|
178 |
@Override
|
179 |
public void setState(PersistentState state) throws PersistenceException { |
180 |
this.firstLeftNumber = (RelationsComponent) state.get(FIRSTLEFTNUMBER);
|
181 |
this.firstRightNumber = (RelationsComponent) state
|
182 |
.get(FIRSTRIGHTNUMBER); |
183 |
this.lastLeftNumber = (RelationsComponent) state.get(LASTLEFTNUMBER);
|
184 |
this.lastRightNumber = (RelationsComponent) state.get(LASTRIGHTNUMBER);
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* spatial search over the geometries
|
189 |
*
|
190 |
* @param lists
|
191 |
* list of lists with ScoredFeatures
|
192 |
* @param address
|
193 |
* @return
|
194 |
*/
|
195 |
@Override
|
196 |
public Set<GeocodingResult> match(List<List<ScoredFeature>> inLists, |
197 |
Address address) { |
198 |
|
199 |
Set<GeocodingResult> outFeats = new TreeSet<GeocodingResult>(); |
200 |
|
201 |
FeatureAttributeDescriptor firstLeftNumberDesc = this
|
202 |
.getFirstLeftNumber().getValue(); |
203 |
FeatureAttributeDescriptor lastLeftNumberDesc = this
|
204 |
.getLastLeftNumber().getValue(); |
205 |
FeatureAttributeDescriptor firstRightNumberDesc = this
|
206 |
.getFirstRightNumber().getValue(); |
207 |
FeatureAttributeDescriptor lastRightNumberDesc = this
|
208 |
.getLastRightNumber().getValue(); |
209 |
|
210 |
// get the list
|
211 |
List<ScoredFeature> inFeats = inLists.get(0); |
212 |
|
213 |
int number = ((NumberAddress) address).getNumber();
|
214 |
|
215 |
for (ScoredFeature feat : inFeats) {
|
216 |
|
217 |
int firstL = feat.getInt(firstLeftNumberDesc.getName());
|
218 |
int lastL = feat.getInt(lastLeftNumberDesc.getName());
|
219 |
int firstR = feat.getInt(firstRightNumberDesc.getName());
|
220 |
int lastR = feat.getInt(lastRightNumberDesc.getName());
|
221 |
|
222 |
// Number even (Par)
|
223 |
if (number % 2 == 0) { |
224 |
// Left numbers - EVEN // right numbers - ODD
|
225 |
if (firstL % 2 == 0) { |
226 |
if (number >= firstL && number <= lastL) {
|
227 |
// create result
|
228 |
GeomMatchResult res = calculatePosition(feat, number, |
229 |
firstL, lastL); |
230 |
// add result to the list
|
231 |
outFeats.add(res); |
232 |
} |
233 |
} |
234 |
// Left numbers - ODD // right numbers - EVEN
|
235 |
else {
|
236 |
if (number >= firstR && number <= lastR) {
|
237 |
// create result
|
238 |
GeomMatchResult res = calculatePosition(feat, number, |
239 |
firstR, lastR); |
240 |
// add result to the list
|
241 |
outFeats.add(res); |
242 |
} |
243 |
} |
244 |
} |
245 |
// number odd (Impar)
|
246 |
else {
|
247 |
// Left numbers - EVEN // right numbers - ODD
|
248 |
if (firstL % 2 == 0) { |
249 |
if (number >= firstR && number <= lastR) {
|
250 |
// create result
|
251 |
GeomMatchResult res = calculatePosition(feat, number, |
252 |
firstR, lastR); |
253 |
// add result to the list
|
254 |
outFeats.add(res); |
255 |
} |
256 |
} |
257 |
// Left numbers - ODD // right numbers - EVEN
|
258 |
else {
|
259 |
if (number >= firstL && number <= lastL) {
|
260 |
// create result
|
261 |
GeomMatchResult res = calculatePosition(feat, number, |
262 |
firstL, lastL); |
263 |
// add result to the list
|
264 |
outFeats.add(res); |
265 |
} |
266 |
} |
267 |
} |
268 |
} |
269 |
return outFeats;
|
270 |
} |
271 |
|
272 |
/**
|
273 |
* Calculate the relative distance in line
|
274 |
*
|
275 |
* @param number
|
276 |
* @param first
|
277 |
* @param last
|
278 |
* @return
|
279 |
*/
|
280 |
private int calculateRelativeDistance(int number, int first, int last) { |
281 |
int total = Math.abs(last - first); |
282 |
int ini = Math.abs(number - first); |
283 |
return (ini * 100) / total; |
284 |
} |
285 |
|
286 |
/**
|
287 |
* calculate the position
|
288 |
*
|
289 |
* @param feat
|
290 |
* @param number
|
291 |
* @param first
|
292 |
* @param last
|
293 |
* @return
|
294 |
*/
|
295 |
private GeomMatchResult calculatePosition(ScoredFeature feat, int number, |
296 |
int first, int last) { |
297 |
|
298 |
Geometry geom = feat.getDefaultGeometry(); |
299 |
com.vividsolutions.jts.geom.Geometry geJTS = MatcherUtils |
300 |
.parseGeomGVToJTS(geom); |
301 |
int relative = calculateRelativeDistance(number, first, last);
|
302 |
Point2D position = MatcherUtils.getLinePositionFromRelativeDistance(
|
303 |
geJTS, relative); |
304 |
// create result
|
305 |
GeomMatchResult res = new GeomMatchResult();
|
306 |
List<ScoredFeature> list = new ArrayList<ScoredFeature>(); |
307 |
list.add(feat); |
308 |
res.setGeom(position); |
309 |
res.setSources(list); |
310 |
|
311 |
return res;
|
312 |
} |
313 |
|
314 |
/**
|
315 |
* Calculate the position with a offset positive or negative
|
316 |
*
|
317 |
* @param feat
|
318 |
* @param number
|
319 |
* @param first
|
320 |
* @param last
|
321 |
* @param offset
|
322 |
* @return
|
323 |
*/
|
324 |
private GeomMatchResult calculateOffsetPosition(ScoredFeature feat,
|
325 |
int number, int first, int last, int offset) { |
326 |
|
327 |
Geometry geom = feat.getDefaultGeometry(); |
328 |
com.vividsolutions.jts.geom.Geometry geJTS = MatcherUtils |
329 |
.parseGeomGVToJTS(geom); |
330 |
int relative = calculateRelativeDistance(number, first, last);
|
331 |
Point2D position = MatcherUtils.getLinePositionFromRelativeDistance(
|
332 |
geJTS, relative); |
333 |
// create result
|
334 |
GeomMatchResult res = new GeomMatchResult();
|
335 |
List<ScoredFeature> list = new ArrayList<ScoredFeature>(); |
336 |
list.add(feat); |
337 |
res.setGeom(position); |
338 |
res.setSources(list); |
339 |
|
340 |
return res;
|
341 |
} |
342 |
|
343 |
} |