1 |
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 |
|
*/
|
|
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 |
22 |
|
23 |
23 |
/*
|
24 |
|
* AUTHORS (In addition to CIT):
|
25 |
|
* 2008 Software Colaborativo (www.scolab.es) development
|
26 |
|
*/
|
27 |
|
|
|
24 |
* AUTHORS (In addition to CIT):
|
|
25 |
* 2008 Software Colaborativo (www.scolab.es) development
|
|
26 |
*/
|
|
27 |
|
28 |
28 |
package org.gvsig.graph.solvers;
|
29 |
29 |
|
30 |
30 |
import java.util.ArrayList;
|
... | ... | |
60 |
60 |
protected Route route = new Route();
|
61 |
61 |
private int fieldIndexStreetName;
|
62 |
62 |
private IFeatureExtractor featExtractor = null;
|
63 |
|
|
64 |
|
public abstract Route calculateRoute() throws GraphException;
|
65 |
63 |
|
|
64 |
public abstract Route calculateRoute() throws GraphException;
|
|
65 |
|
66 |
66 |
public void setFielStreetName(String name) {
|
67 |
67 |
try {
|
68 |
|
int aux = net.getLayer().getRecordset().getFieldIndexByName(name);
|
69 |
|
if (aux == -1)
|
70 |
|
throw new RuntimeException("Field " + name + " not found.");
|
71 |
|
fieldIndexStreetName = aux;
|
|
68 |
if (net.getLayer() != null) {
|
|
69 |
int aux = net.getLayer().getRecordset().getFieldIndexByName(
|
|
70 |
name);
|
|
71 |
if (aux == -1)
|
|
72 |
throw new RuntimeException("Field " + name + " not found.");
|
|
73 |
fieldIndexStreetName = aux;
|
|
74 |
} else {
|
|
75 |
fieldIndexStreetName = 0;
|
|
76 |
}
|
72 |
77 |
} catch (BaseException e) {
|
73 |
78 |
// TODO Auto-generated catch block
|
74 |
79 |
e.printStackTrace();
|
75 |
80 |
}
|
76 |
|
|
|
81 |
|
77 |
82 |
}
|
78 |
83 |
|
79 |
84 |
private void populateRouteSimple(int idStart, int idEnd)
|
80 |
85 |
throws BaseException {
|
81 |
|
int idEnlace;
|
82 |
|
GvNode node;
|
83 |
|
GvEdge link;
|
84 |
|
double costeEntrada;
|
85 |
|
|
86 |
|
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los Enlaces
|
87 |
|
double Coste = 0;
|
88 |
|
double Coste2 = 0;
|
89 |
|
IGraph graph = net.getGraph();
|
90 |
|
node = graph.getNodeByID(idEnd);
|
91 |
|
int from_link = node.get_best_from_link();
|
92 |
|
VectorialAdapter va = (VectorialAdapter) net.getLayer().getSource();
|
93 |
|
while (node.getIdNode() != idStart)
|
94 |
|
{
|
95 |
|
if (from_link == -1)
|
96 |
|
{
|
97 |
|
throw new RuntimeException("Fallo al recorrer de manera inversa la soluci?n. Encontrado arco con -1");
|
98 |
|
// break; // FALLO!!
|
99 |
|
}
|
100 |
|
link = graph.getEdgeByID(from_link);
|
101 |
|
IFeature feat = va.getFeature(link.getIdArc());
|
102 |
|
route.addRouteFeature(feat.getGeometry(), link.getIdArc(),
|
103 |
|
link.getWeight(), link.getDistance(), feat.getAttribute(getFieldIndexStreetName()).toString());
|
104 |
|
|
105 |
|
node = graph.getNodeByID(link.getIdNodeOrig());
|
106 |
|
|
107 |
|
Coste = Coste + link.getWeight();
|
108 |
|
Coste2 = Coste2 + link.getDistance();
|
109 |
|
|
110 |
|
// TODO:
|
111 |
|
// from_link = node.get_from_link(idEnlace, &costeEntrada);
|
112 |
|
from_link = node.get_from_link(link.getIdEdge());
|
113 |
|
|
114 |
|
}
|
115 |
|
System.out.println("Salgo con node = " + node.getIdNode());
|
|
86 |
int idEnlace;
|
|
87 |
GvNode node;
|
|
88 |
GvEdge link;
|
|
89 |
double costeEntrada;
|
|
90 |
|
|
91 |
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los
|
|
92 |
// Enlaces
|
|
93 |
double Coste = 0;
|
|
94 |
double Coste2 = 0;
|
|
95 |
IGraph graph = net.getGraph();
|
|
96 |
node = graph.getNodeByID(idEnd);
|
|
97 |
int from_link = node.get_best_from_link();
|
|
98 |
VectorialAdapter va = (VectorialAdapter) net.getLayer().getSource();
|
|
99 |
while (node.getIdNode() != idStart) {
|
|
100 |
if (from_link == -1) {
|
|
101 |
throw new RuntimeException(
|
|
102 |
"Fallo al recorrer de manera inversa la soluci?n. Encontrado arco con -1");
|
|
103 |
// break; // FALLO!!
|
116 |
104 |
}
|
|
105 |
link = graph.getEdgeByID(from_link);
|
|
106 |
IFeature feat = va.getFeature(link.getIdArc());
|
|
107 |
route.addRouteFeature(feat.getGeometry(), link.getIdArc(), link
|
|
108 |
.getWeight(), link.getDistance(), feat.getAttribute(
|
|
109 |
getFieldIndexStreetName()).toString());
|
117 |
110 |
|
|
111 |
node = graph.getNodeByID(link.getIdNodeOrig());
|
|
112 |
|
|
113 |
Coste = Coste + link.getWeight();
|
|
114 |
Coste2 = Coste2 + link.getDistance();
|
|
115 |
|
|
116 |
// TODO:
|
|
117 |
// from_link = node.get_from_link(idEnlace, &costeEntrada);
|
|
118 |
from_link = node.get_from_link(link.getIdEdge());
|
|
119 |
|
|
120 |
}
|
|
121 |
System.out.println("Salgo con node = " + node.getIdNode());
|
|
122 |
}
|
|
123 |
|
118 |
124 |
protected void populateRoute(GvFlag origin, GvFlag dest, int idStart,
|
119 |
125 |
int idEnd) throws BaseException {
|
120 |
|
int idEnlace;
|
121 |
|
GvNode node;
|
122 |
|
GvEdge link;
|
123 |
|
double costeEntrada;
|
124 |
|
|
125 |
|
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los Enlaces
|
126 |
|
IGraph graph = net.getGraph();
|
127 |
|
node = graph.getNodeByID(idEnd);
|
128 |
|
int from_link = node.get_best_from_link();
|
|
126 |
int idEnlace;
|
|
127 |
GvNode node;
|
|
128 |
GvEdge link;
|
|
129 |
double costeEntrada;
|
129 |
130 |
|
130 |
|
if (featExtractor == null)
|
131 |
|
featExtractor = new DefaultFeatureExtractor(net.getLayer());
|
|
131 |
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los
|
|
132 |
// Enlaces
|
|
133 |
IGraph graph = net.getGraph();
|
|
134 |
node = graph.getNodeByID(idEnd);
|
|
135 |
int from_link = node.get_best_from_link();
|
132 |
136 |
|
133 |
|
VectorialAdapter va = (VectorialAdapter) net.getLayer().getSource();
|
134 |
|
|
135 |
|
/* Miramos los nodos de los tramos inicio y final, y cogemos el nodo que tenga el from_link rellenado. E IGUAL A NUMSOLUCGLOBAL!!!!
|
136 |
|
A partir de ah?, recorremos hacia atr?s y tenemos el cuerpo principal del shape. Luego, para
|
137 |
|
las puntas hay que tener en cuenta los porcentajes, viendo el trozo que est? pegando a los nodos
|
138 |
|
que sabemos que est?n en el path
|
139 |
|
*/
|
140 |
|
|
141 |
|
/* 22/9/2003 Corregimos el fallo que hab?a de escribir el shape de atr?s adelante.
|
142 |
|
* Guardamos lo que necesitamos en listaShapes y recorremos esa lista guardando los tramos
|
143 |
|
* con el sentido adecuado.
|
144 |
|
*/
|
145 |
|
|
146 |
|
/* 3/Febrero/2005 Limpieza de c?digo superfluo y correci?n de un fallo raro. Ahora es m?s simple y parece
|
147 |
|
* que no falla. A ver si dura. IDEA: quiz?s se necesite meter el porcentaje en los arcos partidos.
|
148 |
|
*/
|
149 |
|
|
150 |
|
// Define a template class for a vector of IDs.
|
151 |
|
InfoShp infoShp;
|
152 |
|
Stack pilaShapes = new Stack();
|
153 |
|
// typedef stack<CInfoShp> PILAINFO;
|
154 |
|
// PILAINFO pilaShapes;
|
155 |
|
|
156 |
|
double costeTramoFinal=-1;
|
157 |
|
GvNode nodeEnd = graph.getNodeByID(idEnd);
|
158 |
|
GvEdge finalEdge = graph.getEdgeByID(nodeEnd.get_best_from_link());
|
159 |
|
costeTramoFinal = finalEdge.getWeight();
|
160 |
|
|
161 |
|
GvNode pNodo;
|
162 |
|
GvEdge pEnlace;
|
163 |
|
|
164 |
|
boolean bFlipearShape = false;
|
165 |
|
|
166 |
|
double pctMax, pctMin;
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
//////////////////////////////////////////////////////////////////////////////////////
|
171 |
|
// Trozo del final
|
172 |
|
// El shape va de idStop1 a idStop2, y el porcentaje viene en ese sentido.
|
173 |
|
// Si el idEnd es idStop1, quiere decir que el tramo que falta va en ese sentido tambi?n,
|
174 |
|
// as? que recorremos ese shape desde idStop1 hasta que rebasemos o igualemos ese porcentaje.
|
175 |
|
// Si no, hemos pasado por idStop2 y la parte que hay que meter es desde el pto interior a idStop2
|
176 |
|
///////////////////////////////////////////////////////////////////////////////////////
|
177 |
|
// IFeature feat = va.getFeature(finalEdge.getIdArc());
|
178 |
|
IGeometry g = featExtractor.getGeometry(finalEdge.getIdArc());
|
179 |
|
Value nameStreet = featExtractor.getFieldValue(finalEdge.getIdArc(), getFieldIndexStreetName());
|
180 |
|
|
181 |
|
MultiLineString jtsGeom = (MultiLineString) g.toJTSGeometry();
|
182 |
|
// CoordinateFilter removeDuplicates = new UniqueCoordinateArrayFilter();
|
183 |
|
// jtsGeom.apply(removeDuplicates);
|
184 |
|
// jtsGeom.geometryChanged();
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
// SI ESTAMOS SOBRE EL MISMO TRAMO, CASO PARTICULAR
|
189 |
|
// y el sentido de circulaci?n es correcto
|
190 |
|
if ((origin.getIdArc() == dest.getIdArc()) && (nodeEnd.getBestCost() <= costeTramoFinal))
|
191 |
|
{
|
192 |
|
if (dest.getPct() > origin.getPct())
|
193 |
|
{
|
194 |
|
pctMax = dest.getPct();
|
195 |
|
pctMin = origin.getPct();
|
196 |
|
}
|
197 |
|
else
|
198 |
|
{
|
199 |
|
pctMax = origin.getPct();
|
200 |
|
pctMin = dest.getPct();
|
201 |
|
bFlipearShape = true;
|
202 |
|
}
|
203 |
|
|
204 |
|
LineString line = NetworkUtils.getPartialLineString(jtsGeom,
|
205 |
|
pctMax, 1);
|
206 |
|
|
207 |
|
|
208 |
|
pctMin = pctMin / pctMax; // Porque ha cambiado la longitud
|
209 |
|
// del shape
|
210 |
|
|
211 |
|
line = NetworkUtils.getPartialLineString(line, pctMin, 0);
|
212 |
|
|
213 |
|
if (bFlipearShape)
|
214 |
|
line = line.reverse();
|
215 |
|
|
216 |
|
IGeometry geom = FConverter.jts_to_igeometry(line);
|
217 |
|
// TODO: Calcular bien el length de este arco,
|
218 |
|
// basandonos en el porcentaje costeTramoFinal / costeOriginal
|
219 |
|
route.addRouteFeature(geom, origin.getIdArc(),
|
220 |
|
nodeEnd.getBestCost(), nodeEnd.getAccumulatedLength(), nameStreet.toString());
|
221 |
|
|
222 |
|
|
223 |
|
return ; // Deber?a sacar el coste
|
224 |
|
}
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los Enlaces
|
229 |
|
pNodo = graph.getNodeByID(idEnd);
|
230 |
|
|
231 |
|
from_link = pNodo.get_best_from_link();
|
232 |
|
|
233 |
|
long t1 = System.currentTimeMillis();
|
234 |
|
|
235 |
|
while ((pNodo.getIdNode() != idStart))
|
236 |
|
{
|
237 |
|
idEnlace = from_link;
|
238 |
|
|
239 |
|
pEnlace = graph.getEdgeByID(idEnlace);
|
240 |
|
// System.err.println("from_link=" + from_link + " idTramo=" + pEnlace.getIdArc());
|
241 |
|
|
242 |
|
pNodo = graph.getNodeByID(pEnlace.getIdNodeOrig());
|
243 |
|
|
244 |
|
infoShp = new InfoShp();
|
245 |
|
infoShp.distance = pEnlace.getDistance();
|
246 |
|
infoShp.cost = pEnlace.getWeight();
|
247 |
|
|
248 |
|
if ((pEnlace.getIdArc() == origin.getIdArc()) || (pEnlace.getIdArc() == dest.getIdArc()))
|
249 |
|
{
|
250 |
|
if (pEnlace.getIdArc() == origin.getIdArc())
|
251 |
|
{
|
252 |
|
infoShp.pct = origin.getPct();
|
253 |
|
infoShp.idArc = origin.getIdArc();
|
254 |
|
if (pEnlace.getDirec() == 0)
|
255 |
|
{
|
256 |
|
infoShp.direction = 1;
|
257 |
|
infoShp.bFlip = true;
|
258 |
|
}
|
259 |
|
else // Hemos pasado por el 2
|
260 |
|
{
|
261 |
|
infoShp.direction = 0;
|
262 |
|
infoShp.bFlip = false;
|
263 |
|
} // if else */
|
264 |
|
}
|
265 |
|
else
|
266 |
|
{
|
267 |
|
infoShp.pct = dest.getPct();
|
268 |
|
infoShp.idArc = dest.getIdArc();
|
269 |
|
if (pEnlace.getDirec() == 0)
|
270 |
|
{
|
271 |
|
infoShp.direction = 0;
|
272 |
|
infoShp.bFlip = true;
|
273 |
|
}
|
274 |
|
else
|
275 |
|
{
|
276 |
|
infoShp.direction = 1;
|
277 |
|
infoShp.bFlip = false;
|
278 |
|
} // if else */
|
279 |
|
}
|
280 |
|
}
|
281 |
|
else
|
282 |
|
{
|
283 |
|
infoShp.pct = 1.0;
|
284 |
|
infoShp.idArc = pEnlace.getIdArc();
|
285 |
|
|
286 |
|
infoShp.direction =1;
|
287 |
|
if (pEnlace.getDirec() == 1)
|
288 |
|
infoShp.bFlip = false;
|
289 |
|
else
|
290 |
|
infoShp.bFlip = true;
|
291 |
|
}
|
292 |
|
|
293 |
|
pilaShapes.push(infoShp);
|
294 |
|
if (pNodo.getIdNode() != idStart)
|
295 |
|
from_link = pNodo.get_from_link(idEnlace);
|
296 |
|
}
|
297 |
|
long t2 = System.currentTimeMillis();
|
298 |
|
System.out.println("T populate 1 = " + (t2-t1));
|
299 |
|
|
300 |
|
// Y ahora recorremos hacia atr?s el vector y escribimos los shapes.
|
301 |
|
// VECTORINFO::iterator theIterator;
|
302 |
|
int auxC = 0;
|
303 |
|
|
304 |
|
t1 = System.currentTimeMillis();
|
305 |
|
|
306 |
|
while (!pilaShapes.empty())
|
307 |
|
{
|
308 |
|
infoShp = (InfoShp) pilaShapes.peek();
|
309 |
|
g = featExtractor.getGeometry(infoShp.idArc);
|
310 |
|
nameStreet = featExtractor.getFieldValue(infoShp.idArc, getFieldIndexStreetName());
|
|
137 |
if (featExtractor == null)
|
|
138 |
featExtractor = new DefaultFeatureExtractor(net.getLayer());
|
311 |
139 |
|
312 |
|
MultiLineString line = (MultiLineString) g.toJTSGeometry();
|
313 |
|
|
314 |
|
LineString aux = null;
|
315 |
|
if (infoShp.pct < 1.0)
|
316 |
|
aux = NetworkUtils.getPartialLineString(line, infoShp.pct,
|
317 |
|
infoShp.direction);
|
318 |
|
|
319 |
|
|
320 |
|
IGeometry geom = null;
|
321 |
|
if (aux == null)
|
322 |
|
{
|
323 |
|
if (infoShp.bFlip)
|
324 |
|
line = line.reverse();
|
325 |
|
geom = FConverter.jts_to_igeometry(line);
|
326 |
|
}
|
327 |
|
else
|
328 |
|
{
|
329 |
|
if (infoShp.bFlip)
|
330 |
|
aux = aux.reverse();
|
331 |
|
geom = FConverter.jts_to_igeometry(aux);
|
332 |
|
}
|
333 |
|
|
334 |
|
route.addRouteFeature(geom, infoShp.idArc,
|
335 |
|
infoShp.cost, infoShp.distance, nameStreet.toString());
|
336 |
|
|
337 |
|
|
338 |
|
pilaShapes.pop();
|
339 |
|
auxC++;
|
340 |
|
|
341 |
|
}
|
342 |
|
t2 = System.currentTimeMillis();
|
343 |
|
System.out.println("T populate 2 = " + (t2-t1));
|
344 |
|
return;
|
345 |
|
|
346 |
|
|
|
140 |
// VectorialAdapter va = (VectorialAdapter) net.getLayer().getSource();
|
|
141 |
// va.start();
|
|
142 |
|
|
143 |
/*
|
|
144 |
* Miramos los nodos de los tramos inicio y final, y cogemos el nodo que
|
|
145 |
* tenga el from_link rellenado. E IGUAL A NUMSOLUCGLOBAL!!!! A partir
|
|
146 |
* de ah?, recorremos hacia atr?s y tenemos el cuerpo principal del
|
|
147 |
* shape. Luego, para las puntas hay que tener en cuenta los
|
|
148 |
* porcentajes, viendo el trozo que est? pegando a los nodos que sabemos
|
|
149 |
* que est?n en el path
|
|
150 |
*/
|
|
151 |
|
|
152 |
/*
|
|
153 |
* 22/9/2003 Corregimos el fallo que hab?a de escribir el shape de atr?s
|
|
154 |
* adelante. Guardamos lo que necesitamos en listaShapes y recorremos
|
|
155 |
* esa lista guardando los tramos con el sentido adecuado.
|
|
156 |
*/
|
|
157 |
|
|
158 |
/*
|
|
159 |
* 3/Febrero/2005 Limpieza de c?digo superfluo y correci?n de un fallo
|
|
160 |
* raro. Ahora es m?s simple y parece que no falla. A ver si dura. IDEA:
|
|
161 |
* quiz?s se necesite meter el porcentaje en los arcos partidos.
|
|
162 |
*/
|
|
163 |
|
|
164 |
// Define a template class for a vector of IDs.
|
|
165 |
InfoShp infoShp;
|
|
166 |
Stack pilaShapes = new Stack();
|
|
167 |
// typedef stack<CInfoShp> PILAINFO;
|
|
168 |
// PILAINFO pilaShapes;
|
|
169 |
|
|
170 |
double costeTramoFinal = -1;
|
|
171 |
GvNode nodeEnd = graph.getNodeByID(idEnd);
|
|
172 |
GvEdge finalEdge = graph.getEdgeByID(nodeEnd.get_best_from_link());
|
|
173 |
costeTramoFinal = finalEdge.getWeight();
|
|
174 |
|
|
175 |
GvNode pNodo;
|
|
176 |
GvEdge pEnlace;
|
|
177 |
|
|
178 |
boolean bFlipearShape = false;
|
|
179 |
|
|
180 |
double pctMax, pctMin;
|
|
181 |
|
|
182 |
// ////////////////////////////////////////////////////////////////////////////////////
|
|
183 |
// Trozo del final
|
|
184 |
// El shape va de idStop1 a idStop2, y el porcentaje viene en ese
|
|
185 |
// sentido.
|
|
186 |
// Si el idEnd es idStop1, quiere decir que el tramo que falta va en ese
|
|
187 |
// sentido tambi?n,
|
|
188 |
// as? que recorremos ese shape desde idStop1 hasta que rebasemos o
|
|
189 |
// igualemos ese porcentaje.
|
|
190 |
// Si no, hemos pasado por idStop2 y la parte que hay que meter es desde
|
|
191 |
// el pto interior a idStop2
|
|
192 |
// /////////////////////////////////////////////////////////////////////////////////////
|
|
193 |
// IFeature feat = va.getFeature(finalEdge.getIdArc());
|
|
194 |
IGeometry g = featExtractor.getGeometry(finalEdge.getIdArc());
|
|
195 |
Value nameStreet = featExtractor.getFieldValue(finalEdge.getIdArc(),
|
|
196 |
getFieldIndexStreetName());
|
|
197 |
|
|
198 |
MultiLineString jtsGeom = (MultiLineString) g.toJTSGeometry();
|
|
199 |
// CoordinateFilter removeDuplicates = new
|
|
200 |
// UniqueCoordinateArrayFilter();
|
|
201 |
// jtsGeom.apply(removeDuplicates);
|
|
202 |
// jtsGeom.geometryChanged();
|
|
203 |
|
|
204 |
// SI ESTAMOS SOBRE EL MISMO TRAMO, CASO PARTICULAR
|
|
205 |
// y el sentido de circulaci?n es correcto
|
|
206 |
if ((origin.getIdArc() == dest.getIdArc())
|
|
207 |
&& (nodeEnd.getBestCost() <= costeTramoFinal)) {
|
|
208 |
if (dest.getPct() > origin.getPct()) {
|
|
209 |
pctMax = dest.getPct();
|
|
210 |
pctMin = origin.getPct();
|
|
211 |
} else {
|
|
212 |
pctMax = origin.getPct();
|
|
213 |
pctMin = dest.getPct();
|
|
214 |
bFlipearShape = true;
|
|
215 |
}
|
|
216 |
|
|
217 |
LineString line = NetworkUtils.getPartialLineString(jtsGeom,
|
|
218 |
pctMax, 1);
|
|
219 |
|
|
220 |
pctMin = pctMin / pctMax; // Porque ha cambiado la longitud
|
|
221 |
// del shape
|
|
222 |
|
|
223 |
line = NetworkUtils.getPartialLineString(line, pctMin, 0);
|
|
224 |
|
|
225 |
if (bFlipearShape)
|
|
226 |
line = line.reverse();
|
|
227 |
|
|
228 |
IGeometry geom = FConverter.jts_to_igeometry(line);
|
|
229 |
// TODO: Calcular bien el length de este arco,
|
|
230 |
// basandonos en el porcentaje costeTramoFinal / costeOriginal
|
|
231 |
route.addRouteFeature(geom, origin.getIdArc(), nodeEnd
|
|
232 |
.getBestCost(), nodeEnd.getAccumulatedLength(), nameStreet
|
|
233 |
.toString());
|
|
234 |
|
|
235 |
return; // Deber?a sacar el coste
|
|
236 |
}
|
|
237 |
|
|
238 |
// Trazar el camino desde idEnd hasta idStart hacia atr?s marcando los
|
|
239 |
// Enlaces
|
|
240 |
pNodo = graph.getNodeByID(idEnd);
|
|
241 |
|
|
242 |
from_link = pNodo.get_best_from_link();
|
|
243 |
|
|
244 |
long t1 = System.currentTimeMillis();
|
|
245 |
|
|
246 |
while ((pNodo.getIdNode() != idStart)) {
|
|
247 |
idEnlace = from_link;
|
|
248 |
|
|
249 |
pEnlace = graph.getEdgeByID(idEnlace);
|
|
250 |
// System.err.println("from_link=" + from_link + " idTramo=" +
|
|
251 |
// pEnlace.getIdArc());
|
|
252 |
|
|
253 |
pNodo = graph.getNodeByID(pEnlace.getIdNodeOrig());
|
|
254 |
|
|
255 |
infoShp = new InfoShp();
|
|
256 |
infoShp.distance = pEnlace.getDistance();
|
|
257 |
infoShp.cost = pEnlace.getWeight();
|
|
258 |
|
|
259 |
if ((pEnlace.getIdArc() == origin.getIdArc())
|
|
260 |
|| (pEnlace.getIdArc() == dest.getIdArc())) {
|
|
261 |
if (pEnlace.getIdArc() == origin.getIdArc()) {
|
|
262 |
infoShp.pct = origin.getPct();
|
|
263 |
infoShp.idArc = origin.getIdArc();
|
|
264 |
if (pEnlace.getDirec() == 0) {
|
|
265 |
infoShp.direction = 1;
|
|
266 |
infoShp.bFlip = true;
|
|
267 |
} else // Hemos pasado por el 2
|
|
268 |
{
|
|
269 |
infoShp.direction = 0;
|
|
270 |
infoShp.bFlip = false;
|
|
271 |
} // if else */
|
|
272 |
} else {
|
|
273 |
infoShp.pct = dest.getPct();
|
|
274 |
infoShp.idArc = dest.getIdArc();
|
|
275 |
if (pEnlace.getDirec() == 0) {
|
|
276 |
infoShp.direction = 0;
|
|
277 |
infoShp.bFlip = true;
|
|
278 |
} else {
|
|
279 |
infoShp.direction = 1;
|
|
280 |
infoShp.bFlip = false;
|
|
281 |
} // if else */
|
347 |
282 |
}
|
|
283 |
} else {
|
|
284 |
infoShp.pct = 1.0;
|
|
285 |
infoShp.idArc = pEnlace.getIdArc();
|
348 |
286 |
|
|
287 |
infoShp.direction = 1;
|
|
288 |
if (pEnlace.getDirec() == 1)
|
|
289 |
infoShp.bFlip = false;
|
|
290 |
else
|
|
291 |
infoShp.bFlip = true;
|
|
292 |
}
|
|
293 |
|
|
294 |
pilaShapes.push(infoShp);
|
|
295 |
if (pNodo.getIdNode() != idStart)
|
|
296 |
from_link = pNodo.get_from_link(idEnlace);
|
|
297 |
}
|
|
298 |
long t2 = System.currentTimeMillis();
|
|
299 |
System.out.println("T populate 1 = " + (t2 - t1));
|
|
300 |
|
|
301 |
// Y ahora recorremos hacia atr?s el vector y escribimos los shapes.
|
|
302 |
// VECTORINFO::iterator theIterator;
|
|
303 |
int auxC = 0;
|
|
304 |
|
|
305 |
t1 = System.currentTimeMillis();
|
|
306 |
|
|
307 |
while (!pilaShapes.empty()) {
|
|
308 |
infoShp = (InfoShp) pilaShapes.peek();
|
|
309 |
g = featExtractor.getGeometry(infoShp.idArc);
|
|
310 |
nameStreet = featExtractor.getFieldValue(infoShp.idArc,
|
|
311 |
getFieldIndexStreetName());
|
|
312 |
|
|
313 |
MultiLineString line = (MultiLineString) g.toJTSGeometry();
|
|
314 |
|
|
315 |
LineString aux = null;
|
|
316 |
if (infoShp.pct < 1.0)
|
|
317 |
aux = NetworkUtils.getPartialLineString(line, infoShp.pct,
|
|
318 |
infoShp.direction);
|
|
319 |
|
|
320 |
IGeometry geom = null;
|
|
321 |
if (aux == null) {
|
|
322 |
if (infoShp.bFlip)
|
|
323 |
line = line.reverse();
|
|
324 |
geom = FConverter.jts_to_igeometry(line);
|
|
325 |
} else {
|
|
326 |
if (infoShp.bFlip)
|
|
327 |
aux = aux.reverse();
|
|
328 |
geom = FConverter.jts_to_igeometry(aux);
|
|
329 |
}
|
|
330 |
|
|
331 |
route.addRouteFeature(geom, infoShp.idArc, infoShp.cost,
|
|
332 |
infoShp.distance, nameStreet.toString());
|
|
333 |
|
|
334 |
pilaShapes.pop();
|
|
335 |
auxC++;
|
|
336 |
|
|
337 |
}
|
|
338 |
// va.stop();
|
|
339 |
t2 = System.currentTimeMillis();
|
|
340 |
System.out.println("T populate 2 = " + (t2 - t1));
|
|
341 |
return;
|
|
342 |
|
|
343 |
}
|
|
344 |
|
349 |
345 |
LineString SituaSobreTramo(Geometry geom, int idArc, double pct, int parte) {
|
350 |
346 |
int j, numVertices;
|
351 |
347 |
double longAcum, longReal, longBuscada, distSobre, miniPorcentaje;
|
352 |
348 |
double nuevaX, nuevaY; // Por cuestiones de claridad al programar
|
353 |
|
double dist=0;
|
354 |
|
|
|
349 |
double dist = 0;
|
|
350 |
|
355 |
351 |
longAcum = 0;
|
356 |
352 |
longReal = geom.getLength();
|
357 |
353 |
longBuscada = longReal * pct;
|
358 |
354 |
Coordinate[] coords = geom.getCoordinates();
|
359 |
355 |
Coordinate c1 = null, c2 = null;
|
360 |
356 |
ArrayList savedCoords = new ArrayList();
|
361 |
|
|
362 |
|
if (parte > 0) // Hemos entrado por el 1 hacia el 2 (al 2 no llegamos)
|
|
357 |
|
|
358 |
if (parte > 0) // Hemos entrado por el 1 hacia el 2 (al 2 no llegamos)
|
363 |
359 |
{
|
364 |
|
for( j = 0; j < coords.length-1; j++ )
|
365 |
|
{
|
|
360 |
for (j = 0; j < coords.length - 1; j++) {
|
366 |
361 |
c1 = coords[j];
|
367 |
|
c2 = coords[j+1];
|
|
362 |
c2 = coords[j + 1];
|
368 |
363 |
dist = c1.distance(c2);
|
369 |
364 |
longAcum += dist;
|
370 |
365 |
savedCoords.add(c1);
|
371 |
|
if (longAcum >= longBuscada)
|
372 |
|
{
|
|
366 |
if (longAcum >= longBuscada) {
|
373 |
367 |
// Hasta aqu?. Ahora ahi que poner el punto sobre el tramo
|
374 |
368 |
distSobre = dist - (longAcum - longBuscada);
|
375 |
|
miniPorcentaje = distSobre/dist;
|
376 |
|
|
377 |
|
nuevaX = c1.x + (c2.x-c1.x) * miniPorcentaje;
|
378 |
|
nuevaY = c1.y + (c2.y-c1.y) * miniPorcentaje;
|
379 |
|
|
|
369 |
miniPorcentaje = distSobre / dist;
|
|
370 |
|
|
371 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje;
|
|
372 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje;
|
|
373 |
|
380 |
374 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
381 |
375 |
break;
|
382 |
376 |
} // if longAcum >= longBuscada
|
383 |
377 |
} // for j
|
384 |
|
|
385 |
|
}
|
386 |
|
else // Hemos entrado por el 2 hacia el 1
|
|
378 |
|
|
379 |
} else // Hemos entrado por el 2 hacia el 1
|
387 |
380 |
{
|
388 |
381 |
numVertices = 0;
|
389 |
|
for( j = 0; j < coords.length; j++ )
|
390 |
|
{
|
391 |
|
////////////////////////////////////////////////////////////////
|
|
382 |
for (j = 0; j < coords.length; j++) {
|
|
383 |
// //////////////////////////////////////////////////////////////
|
392 |
384 |
// 13_ene_2005: Si el ?ltimo punto es el ?ltimo punto no
|
393 |
385 |
// podemos acceder al elemento j+1 porque nos salimos del shape
|
394 |
|
/////////////////////////////////////////////////////////////////
|
|
386 |
// ///////////////////////////////////////////////////////////////
|
395 |
387 |
c1 = coords[j];
|
396 |
|
if (j < coords.length -1)
|
397 |
|
{
|
398 |
|
c2 = coords[j+1];
|
399 |
|
|
|
388 |
if (j < coords.length - 1) {
|
|
389 |
c2 = coords[j + 1];
|
|
390 |
|
400 |
391 |
dist = c1.distance(c2);
|
401 |
392 |
longAcum += dist;
|
402 |
393 |
}
|
403 |
|
|
404 |
|
if (longAcum >= longBuscada)
|
405 |
|
{
|
|
394 |
|
|
395 |
if (longAcum >= longBuscada) {
|
406 |
396 |
// Hasta aqu?. Empezamos a meter puntos
|
407 |
|
|
408 |
|
if (numVertices == 0)
|
409 |
|
{
|
|
397 |
|
|
398 |
if (numVertices == 0) {
|
410 |
399 |
distSobre = dist - (longAcum - longBuscada);
|
411 |
|
miniPorcentaje = distSobre/dist;
|
412 |
|
nuevaX = c1.x + (c2.x-c1.x) * miniPorcentaje;
|
413 |
|
nuevaY = c1.y + (c2.y-c1.y) * miniPorcentaje;
|
414 |
|
|
|
400 |
miniPorcentaje = distSobre / dist;
|
|
401 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje;
|
|
402 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje;
|
|
403 |
|
415 |
404 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
416 |
|
}
|
417 |
|
else
|
418 |
|
{
|
|
405 |
} else {
|
419 |
406 |
savedCoords.add(c2);
|
420 |
407 |
}
|
421 |
|
numVertices ++;
|
|
408 |
numVertices++;
|
422 |
409 |
// break;
|
423 |
410 |
} // if longAcum >= longBuscada
|
424 |
411 |
} // for j
|
425 |
|
|
|
412 |
|
426 |
413 |
// savedCoords.add(c2);
|
427 |
|
|
|
414 |
|
428 |
415 |
} // if else
|
429 |
|
|
430 |
|
|
431 |
|
return geomFactory.createLineString((Coordinate[] )savedCoords.toArray(new Coordinate[0]));
|
|
416 |
|
|
417 |
return geomFactory.createLineString((Coordinate[]) savedCoords
|
|
418 |
.toArray(new Coordinate[0]));
|
432 |
419 |
}
|
433 |
420 |
|
434 |
421 |
private int getFieldIndexStreetName() {
|
... | ... | |
443 |
430 |
this.featExtractor = featExtractor;
|
444 |
431 |
}
|
445 |
432 |
|
446 |
|
|
447 |
433 |
}
|
448 |
|
|