root / trunk / extensions / extGraph / src / org / gvsig / graph / core / NetworkUtils.java @ 31808
History | View | Annotate | Download (24.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.graph.core; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.geom.Point2D; |
45 |
import java.awt.geom.Rectangle2D; |
46 |
import java.io.File; |
47 |
import java.io.FileNotFoundException; |
48 |
import java.io.FileReader; |
49 |
import java.util.ArrayList; |
50 |
import java.util.Iterator; |
51 |
|
52 |
import javax.swing.ImageIcon; |
53 |
|
54 |
import org.apache.log4j.Logger; |
55 |
import org.exolab.castor.xml.MarshalException; |
56 |
import org.exolab.castor.xml.ValidationException; |
57 |
import org.gvsig.exceptions.BaseException; |
58 |
import org.gvsig.graph.solvers.Route; |
59 |
|
60 |
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException; |
61 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
62 |
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException; |
63 |
import com.iver.cit.gvsig.fmap.MapControl; |
64 |
import com.iver.cit.gvsig.fmap.ViewPort; |
65 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
66 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
67 |
import com.iver.cit.gvsig.fmap.core.ShapeFactory; |
68 |
import com.iver.cit.gvsig.fmap.core.styles.ArrowDecoratorStyle; |
69 |
import com.iver.cit.gvsig.fmap.core.styles.ILineStyle; |
70 |
import com.iver.cit.gvsig.fmap.core.styles.SimpleLineStyle; |
71 |
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol; |
72 |
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol; |
73 |
import com.iver.cit.gvsig.fmap.core.symbols.SimpleLineSymbol; |
74 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
75 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
76 |
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes; |
77 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
78 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
79 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
80 |
import com.iver.cit.gvsig.fmap.layers.GraphicLayer; |
81 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
82 |
import com.iver.cit.gvsig.fmap.layers.XMLException; |
83 |
import com.iver.cit.gvsig.fmap.rendering.FGraphic; |
84 |
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend; |
85 |
import com.iver.cit.gvsig.fmap.rendering.LegendFactory; |
86 |
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex; |
87 |
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts; |
88 |
import com.iver.utiles.XMLEntity; |
89 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
90 |
import com.vividsolutions.jts.geom.Coordinate; |
91 |
import com.vividsolutions.jts.geom.Geometry; |
92 |
import com.vividsolutions.jts.geom.GeometryFactory; |
93 |
import com.vividsolutions.jts.geom.LineSegment; |
94 |
import com.vividsolutions.jts.geom.LineString; |
95 |
import com.vividsolutions.jts.geom.MultiPoint; |
96 |
import com.vividsolutions.jts.geom.Point; |
97 |
|
98 |
public class NetworkUtils { |
99 |
static int idSymbolFlag = -1; |
100 |
|
101 |
static Logger logger = Logger.getLogger(NetworkUtils.class); |
102 |
|
103 |
static private GeometryFactory geomFactory = new GeometryFactory(); |
104 |
|
105 |
private static IMarkerSymbol simFlag2; |
106 |
private static FSymbol simFlag; |
107 |
|
108 |
public static void clearBarriersFromGraphics(MapControl mc) { |
109 |
GraphicLayer graphics = mc.getMapContext().getGraphicsLayer(); |
110 |
for (int i = graphics.getNumGraphics() - 1; i >= 0; i--) { |
111 |
FGraphic aux = graphics.getGraphic(i); |
112 |
if (aux.getTag() != null) |
113 |
if (aux.getTag().equalsIgnoreCase("BARRIER")) |
114 |
graphics.removeGraphic(aux); |
115 |
} |
116 |
} |
117 |
|
118 |
public static void clearFlagsFromGraphics(MapControl mc) { |
119 |
GraphicLayer graphics = mc.getMapContext().getGraphicsLayer(); |
120 |
for (int i = graphics.getNumGraphics() - 1; i >= 0; i--) { |
121 |
FGraphic aux = graphics.getGraphic(i); |
122 |
if (aux.getTag() != null) |
123 |
if (aux.getTag().equalsIgnoreCase("FLAG")) |
124 |
graphics.removeGraphic(aux); |
125 |
} |
126 |
} |
127 |
|
128 |
public static void clearFlagFromGraphics(MapControl mc, GvFlag flag) { |
129 |
GraphicLayer graphics = mc.getMapContext().getGraphicsLayer(); |
130 |
FGraphic graphic = graphics.getGraphicByObjectTag(flag); |
131 |
graphics.removeGraphic(graphic); |
132 |
} |
133 |
|
134 |
public static void clearRouteFromGraphics(MapControl mc) { |
135 |
GraphicLayer graphics = mc.getMapContext().getGraphicsLayer(); |
136 |
for (int i = graphics.getNumGraphics() - 1; i >= 0; i--) { |
137 |
FGraphic aux = graphics.getGraphic(i); |
138 |
if (aux.getTag() != null) |
139 |
if (aux.getTag().equalsIgnoreCase("ROUTE")) |
140 |
graphics.removeGraphic(aux); |
141 |
} |
142 |
} |
143 |
|
144 |
public static void centerGraphicsOnFlags(MapControl mc, GvFlag[] flags) { |
145 |
ViewPort vp = mc.getViewPort(); |
146 |
Rectangle2D extent = vp.getAdjustedExtent();
|
147 |
/*
|
148 |
* double xNC = flag.getOriginalPoint().getX(); double yNC =
|
149 |
* flag.getOriginalPoint().getY();
|
150 |
*/
|
151 |
double width = extent.getWidth();
|
152 |
double height = extent.getHeight();
|
153 |
|
154 |
Rectangle2D.Double r = null;// new Rectangle2D.Double(); |
155 |
|
156 |
/*
|
157 |
* r.width = width; r.height = height; r.x = xNC - width/2; r.y = yNC -
|
158 |
* height/2;
|
159 |
*/
|
160 |
|
161 |
for (int i = 0; i < flags.length; i++) { |
162 |
if (flags[i] != null) { |
163 |
Point2D p = flags[i].getOriginalPoint();
|
164 |
if (r == null) |
165 |
r = new Rectangle2D.Double(p.getX(), p.getY(), 0, 0); |
166 |
else
|
167 |
r.add(flags[i].getOriginalPoint()); |
168 |
} |
169 |
} |
170 |
|
171 |
double realTol = mc.getViewPort().toMapDistance(15); |
172 |
|
173 |
r.height = r.height + realTol; |
174 |
r.width = r.width + realTol; |
175 |
r.x = r.x - (realTol / 2.0);// r.getMaxX() - r.width/2; |
176 |
r.y = r.y - (realTol / 2.0);// r.getMaxY() - r.height/2; |
177 |
|
178 |
vp.setExtent(r); |
179 |
mc.getMapContext().invalidate(); |
180 |
} |
181 |
|
182 |
public static void centerGraphicsOnFlag(MapControl mc, GvFlag flag) { |
183 |
ViewPort vp = mc.getViewPort(); |
184 |
Rectangle2D extent = vp.getAdjustedExtent();
|
185 |
double xNC = flag.getOriginalPoint().getX();
|
186 |
double yNC = flag.getOriginalPoint().getY();
|
187 |
double width = extent.getWidth();
|
188 |
double height = extent.getHeight();
|
189 |
|
190 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
191 |
|
192 |
r.width = width; |
193 |
r.height = height; |
194 |
r.x = xNC - width / 2;
|
195 |
r.y = yNC - height / 2;
|
196 |
|
197 |
vp.setExtent(r); |
198 |
} |
199 |
|
200 |
public static void drawRouteOnGraphics(MapControl mc, Route route) { |
201 |
Iterator it = route.getFeatureList().iterator();
|
202 |
int idSymbolLine = -1; |
203 |
GraphicLayer graphicLayer = mc.getMapContext().getGraphicsLayer(); |
204 |
// if (idSymbolLine == -1)
|
205 |
{ |
206 |
SimpleLineSymbol arrowSymbol = new SimpleLineSymbol();
|
207 |
// FSymbol arrowSymbol = new FSymbol(FConstant.SYMBOL_TYPE_LINE);
|
208 |
arrowSymbol.setLineColor(Color.RED);
|
209 |
arrowSymbol.setUnit(-1); // pixels |
210 |
arrowSymbol.setLineWidth(3.0f);
|
211 |
ILineStyle lineStyle = new SimpleLineStyle();
|
212 |
|
213 |
ArrowDecoratorStyle arrowDecoratorStyle = new ArrowDecoratorStyle();
|
214 |
arrowDecoratorStyle.setArrowMarkerCount(1);
|
215 |
lineStyle.setArrowDecorator(arrowDecoratorStyle); |
216 |
lineStyle.setLineWidth(3.0f);
|
217 |
arrowSymbol.setLineStyle(lineStyle); |
218 |
idSymbolLine = graphicLayer.addSymbol(arrowSymbol); |
219 |
|
220 |
} |
221 |
// Para evitar hacer reallocate de los elementos de la
|
222 |
// graphicList cada vez, creamos primero la lista
|
223 |
// y la insertamos toda de una vez.
|
224 |
ArrayList graphicsRoute = new ArrayList(); |
225 |
while (it.hasNext()) {
|
226 |
IFeature feat = (IFeature) it.next(); |
227 |
IGeometry gAux = feat.getGeometry(); |
228 |
FGraphic graphic = new FGraphic(gAux, idSymbolLine);
|
229 |
graphic.setTag("ROUTE");
|
230 |
graphicsRoute.add(graphic); |
231 |
// graphicLayer.insertGraphic(0, graphic);
|
232 |
} |
233 |
// Lo insertamos al principio de la lista para que los
|
234 |
// pushpins se dibujen despu?s.
|
235 |
|
236 |
graphicLayer.inserGraphics(0, graphicsRoute);
|
237 |
mc.drawGraphics(); |
238 |
} |
239 |
|
240 |
public static File getNetworkFile(FLayer lyr) { |
241 |
String directoryName = System.getProperty("java.io.tmpdir"); |
242 |
String aux = lyr.getName().replaceAll("\\Q.shp\\E", ".net"); |
243 |
File newFile = new File(directoryName + File.separator + aux); |
244 |
return newFile;
|
245 |
|
246 |
} |
247 |
|
248 |
public static void addGraphicFlag(MapControl mapControl, GvFlag flag) { |
249 |
addGraphicFlag(mapControl, flag, getDefaultSymbolFlag()); |
250 |
} |
251 |
|
252 |
/**
|
253 |
* @return
|
254 |
*/
|
255 |
public static ISymbol getDefaultSymbolFlag() { |
256 |
if (simFlag == null) { |
257 |
|
258 |
simFlag = new FSymbol(FConstant.SYMBOL_TYPE_ICON);
|
259 |
ImageIcon icon = new ImageIcon(NetworkUtils.class.getClassLoader() |
260 |
.getResource("images/pushpin.png"));
|
261 |
simFlag.setIcon(icon.getImage()); |
262 |
simFlag.setSize(24);
|
263 |
simFlag.setSizeInPixels(true);
|
264 |
simFlag.setStyle(FConstant.SYMBOL_STYLE_MARKER_IMAGEN); |
265 |
} |
266 |
return simFlag;
|
267 |
} |
268 |
|
269 |
public static void addGraphicFlag(MapControl mapControl, GvFlag flag, |
270 |
ISymbol sym) { |
271 |
GraphicLayer graphicLayer = mapControl.getMapContext() |
272 |
.getGraphicsLayer(); |
273 |
int idSymbol = graphicLayer.getSymbol(sym);
|
274 |
if (idSymbol == -1) { // El s?mbolo no existe todav?a en la lista de |
275 |
// gr?ficos
|
276 |
idSymbol = graphicLayer.addSymbol(sym); |
277 |
} |
278 |
IGeometry gAux = ShapeFactory.createPoint2D(flag.getOriginalPoint() |
279 |
.getX(), flag.getOriginalPoint().getY()); |
280 |
FGraphic graphic = new FGraphic(gAux, idSymbol);
|
281 |
graphic.setTag("FLAG");
|
282 |
graphic.setObjectTag(flag); |
283 |
graphicLayer.addGraphic(graphic); |
284 |
|
285 |
} |
286 |
|
287 |
public static double getPercentAlong(Geometry jtsGeom, double x, double y) { |
288 |
// Primero calculamos la longitud total del shape.
|
289 |
// Luego calculamos el punto m?s cercano y su distancia para cada
|
290 |
// segmento del shape.
|
291 |
// Nos quedamos con el que est? m?s cerca y luego recorremos hasta ?l
|
292 |
// acumulando distancia.
|
293 |
// Finalmente, dividimos esa distancia por la longitud total.
|
294 |
|
295 |
Coordinate[] coords = jtsGeom.getCoordinates();
|
296 |
|
297 |
Coordinate userCoord = new Coordinate(x, y);
|
298 |
|
299 |
double longReal = 0; |
300 |
// Le pegamos una primera pasada para saber su longitud real.
|
301 |
// OJO, NO TRABAJAMOS CON SHAPES MULTIPARTE, NO TIENE SENTIDO CON LAS
|
302 |
// REDES (CREO)
|
303 |
// POR ESO SUPONEMOS UNA ?NICA PARTE (L?NEA CONT?NUA)
|
304 |
// A la vez calculamos el punto m?s cercano y su distancia para cada
|
305 |
// segmento.
|
306 |
double minDist = Double.MAX_VALUE; |
307 |
double distTo = 0; |
308 |
double dist = 0; |
309 |
Coordinate cOrig = null;
|
310 |
Coordinate closestPoint = null;
|
311 |
for (int j = 0; j < coords.length - 1; j++) { |
312 |
Coordinate c1 = coords[j]; |
313 |
Coordinate c2 = coords[j + 1];
|
314 |
LineSegment line = new LineSegment(c1, c2);
|
315 |
|
316 |
Coordinate auxPoint = line.closestPoint(userCoord); |
317 |
dist = userCoord.distance(auxPoint); |
318 |
if ((dist < minDist)) {
|
319 |
minDist = dist; |
320 |
cOrig = c1; |
321 |
closestPoint = auxPoint; |
322 |
distTo = longReal; |
323 |
} |
324 |
longReal += line.getLength(); |
325 |
} |
326 |
dist = cOrig.distance(closestPoint); |
327 |
double longBuscada = distTo + dist;
|
328 |
|
329 |
double pct;
|
330 |
if (longReal > 0) |
331 |
pct = longBuscada / longReal; |
332 |
else
|
333 |
pct = 0.0;
|
334 |
|
335 |
return pct;
|
336 |
} |
337 |
|
338 |
/**
|
339 |
* Receives jtsgeom, percentage of desired linestring and direction.
|
340 |
*
|
341 |
* @param geom
|
342 |
* @param pct
|
343 |
* @param direction
|
344 |
* 1=> same as geometry. 0=> Inversed
|
345 |
* @return partial linestring
|
346 |
*/
|
347 |
public static LineString getPartialLineString(Geometry geom, double pct, |
348 |
int direction)
|
349 |
// Si parte vale cero, los v?lidos son los primeros. Si no, los segundos.
|
350 |
{ |
351 |
int j, numVertices;
|
352 |
double longAcum, longReal, longBuscada, distSobre, miniPorcentaje;
|
353 |
double nuevaX, nuevaY; // Por cuestiones de claridad al programar |
354 |
double dist = 0; |
355 |
|
356 |
longAcum = 0;
|
357 |
longReal = geom.getLength(); |
358 |
longBuscada = longReal * pct; |
359 |
Coordinate[] coords = geom.getCoordinates();
|
360 |
Coordinate c1 = null, c2 = null; |
361 |
ArrayList savedCoords = new ArrayList(); |
362 |
Coordinate lastCoord = null;
|
363 |
|
364 |
if (direction > 0) // Hemos entrado por el 1 hacia el 2 (al 2 no |
365 |
// llegamos)
|
366 |
{ |
367 |
for (j = 0; j < coords.length - 1; j++) { |
368 |
c1 = coords[j]; |
369 |
c2 = coords[j + 1];
|
370 |
dist = c1.distance(c2); |
371 |
longAcum += dist; |
372 |
if ((lastCoord == null) || (!c1.equals2D(lastCoord))) { |
373 |
savedCoords.add(c1); |
374 |
lastCoord = c1; |
375 |
} |
376 |
|
377 |
if (longAcum >= longBuscada) {
|
378 |
// Hasta aqu?. Ahora ahi que poner el punto sobre el tramo
|
379 |
distSobre = dist - (longAcum - longBuscada); |
380 |
miniPorcentaje = distSobre / dist; |
381 |
|
382 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje; |
383 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje; |
384 |
|
385 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
386 |
break;
|
387 |
} // if longAcum >= longBuscada
|
388 |
} // for j
|
389 |
|
390 |
} else // Hemos entrado por el 2 hacia el 1 |
391 |
{ |
392 |
numVertices = 0;
|
393 |
for (j = 0; j < coords.length; j++) { |
394 |
// //////////////////////////////////////////////////////////////
|
395 |
// 13_ene_2005: Si el ?ltimo punto es el ?ltimo punto no
|
396 |
// podemos acceder al elemento j+1 porque nos salimos del shape
|
397 |
// ///////////////////////////////////////////////////////////////
|
398 |
c1 = coords[j]; |
399 |
if (j < coords.length - 1) { |
400 |
c2 = coords[j + 1];
|
401 |
|
402 |
dist = c1.distance(c2); |
403 |
longAcum += dist; |
404 |
} |
405 |
|
406 |
if (longAcum >= longBuscada) {
|
407 |
// Hasta aqu?. Empezamos a meter puntos
|
408 |
|
409 |
if (numVertices == 0) { |
410 |
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 |
|
415 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
416 |
savedCoords.add(c2); |
417 |
} else {
|
418 |
if ((lastCoord == null) || (!c2.equals2D(lastCoord))) { |
419 |
savedCoords.add(c2); |
420 |
lastCoord = c2; |
421 |
} |
422 |
|
423 |
} |
424 |
numVertices++; |
425 |
// break;
|
426 |
} // if longAcum >= longBuscada
|
427 |
} // for j
|
428 |
|
429 |
// savedCoords.add(c2);
|
430 |
|
431 |
} // if else
|
432 |
|
433 |
return geomFactory.createLineString((Coordinate[]) savedCoords |
434 |
.toArray(new Coordinate[0])); |
435 |
} |
436 |
|
437 |
/**
|
438 |
* Retrieves a sub-linestring from pct1 and length = pct2
|
439 |
*
|
440 |
* @param geom
|
441 |
* @param pct1 =>
|
442 |
* from pct
|
443 |
* @param pct2 => distance (in percentage) of second point
|
444 |
* @param direction
|
445 |
* 1=> same as geometry. 0=> Inversed
|
446 |
* @return partial linestring
|
447 |
*/
|
448 |
public static LineString getPartialLineString(Geometry geom, double pct1, |
449 |
double pct2, int direction) |
450 |
// Si parte vale cero, los v?lidos son los primeros. Si no, los segundos.
|
451 |
{ |
452 |
int j, numVertices;
|
453 |
double longAcum, longReal, longFrom, longTo, distSobre, miniPorcentaje;
|
454 |
double nuevaX, nuevaY; // Por cuestiones de claridad al programar |
455 |
double dist = 0; |
456 |
|
457 |
longAcum = 0;
|
458 |
longReal = geom.getLength(); |
459 |
longFrom = longReal * pct1; |
460 |
|
461 |
Coordinate[] coords = geom.getCoordinates();
|
462 |
Coordinate c1 = null, c2 = null; |
463 |
ArrayList savedCoords = new ArrayList(); |
464 |
Coordinate lastCoord = null;
|
465 |
|
466 |
if (direction > 0) // Hemos entrado por el 1 hacia el 2 (al 2 no |
467 |
// llegamos)
|
468 |
{ |
469 |
longTo = longReal * (pct1 + pct2); |
470 |
numVertices = 0;
|
471 |
for (j = 0; j < coords.length - 1; j++) { |
472 |
c1 = coords[j]; |
473 |
c2 = coords[j + 1];
|
474 |
dist = c1.distance(c2); |
475 |
longAcum += dist; |
476 |
if ((lastCoord == null) || (!c1.equals2D(lastCoord))) { |
477 |
if (longAcum >= longFrom) {
|
478 |
if (numVertices == 0) { |
479 |
distSobre = dist - (longAcum - longFrom); |
480 |
miniPorcentaje = distSobre / dist; |
481 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje; |
482 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje; |
483 |
|
484 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
485 |
} else {
|
486 |
savedCoords.add(c1); |
487 |
} |
488 |
numVertices++; |
489 |
} |
490 |
lastCoord = c1; |
491 |
} |
492 |
|
493 |
if (longAcum >= longTo) {
|
494 |
// Hasta aqu?. Ahora ahi que poner el punto sobre el tramo
|
495 |
distSobre = dist - (longAcum - longTo); |
496 |
miniPorcentaje = distSobre / dist; |
497 |
|
498 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje; |
499 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje; |
500 |
|
501 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
502 |
break;
|
503 |
} // if longAcum >= longBuscada
|
504 |
} // for j
|
505 |
|
506 |
} else // Hemos entrado por el 2 hacia el 1 |
507 |
{ |
508 |
numVertices = 0;
|
509 |
|
510 |
longTo = longFrom; |
511 |
longFrom = longFrom - (longReal * pct2); |
512 |
|
513 |
for (j = 0; j < coords.length; j++) { |
514 |
// //////////////////////////////////////////////////////////////
|
515 |
// 13_ene_2005: Si el ?ltimo punto es el ?ltimo punto no
|
516 |
// podemos acceder al elemento j+1 porque nos salimos del shape
|
517 |
// ///////////////////////////////////////////////////////////////
|
518 |
c1 = coords[j]; |
519 |
if (j < coords.length - 1) { |
520 |
c2 = coords[j + 1];
|
521 |
|
522 |
dist = c1.distance(c2); |
523 |
longAcum += dist; |
524 |
} |
525 |
|
526 |
if (longAcum >= longFrom) {
|
527 |
// Desde aqu?. Empezamos a meter puntos
|
528 |
|
529 |
if (numVertices == 0) { |
530 |
distSobre = dist - (longAcum - longFrom); |
531 |
miniPorcentaje = distSobre / dist; |
532 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje; |
533 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje; |
534 |
|
535 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
536 |
savedCoords.add(c2); |
537 |
} else {
|
538 |
if ((lastCoord == null) || (!c2.equals2D(lastCoord))) { |
539 |
if (longAcum <= longTo)
|
540 |
savedCoords.add(c2); |
541 |
lastCoord = c2; |
542 |
} |
543 |
|
544 |
} |
545 |
numVertices++; |
546 |
if (longAcum >= longTo) {
|
547 |
// Hasta aqu?. Ahora ahi que poner el punto sobre el
|
548 |
// tramo
|
549 |
distSobre = dist - (longAcum - longTo); |
550 |
miniPorcentaje = distSobre / dist; |
551 |
|
552 |
nuevaX = c1.x + (c2.x - c1.x) * miniPorcentaje; |
553 |
nuevaY = c1.y + (c2.y - c1.y) * miniPorcentaje; |
554 |
|
555 |
savedCoords.add(new Coordinate(nuevaX, nuevaY));
|
556 |
break;
|
557 |
} // if longAcum >= longTo
|
558 |
|
559 |
} // if longAcum >= longFrom
|
560 |
} // for j
|
561 |
|
562 |
// savedCoords.add(c2);
|
563 |
|
564 |
} // if else
|
565 |
|
566 |
return geomFactory.createLineString((Coordinate[]) savedCoords |
567 |
.toArray(new Coordinate[0])); |
568 |
} |
569 |
|
570 |
public static double[] string2doubleArray(String str, String separator) |
571 |
throws NumberFormatException { |
572 |
str = str.replace(" ", ""); |
573 |
|
574 |
if (!str.contains(separator)) {
|
575 |
try {
|
576 |
double[] result = new double[1]; |
577 |
result[0] = Double.parseDouble(str); |
578 |
return result;
|
579 |
} catch (NumberFormatException except) { |
580 |
if (str.startsWith("[") && str.endsWith("]")) { |
581 |
String[] tokens = str.substring(1, str.length() - 1).split( |
582 |
":");
|
583 |
if (tokens.length == 3) { |
584 |
try {
|
585 |
Double ini = Double.valueOf(tokens[0]); |
586 |
Double end = Double.valueOf(tokens[1]); |
587 |
Double interval = Double.valueOf(tokens[2]); |
588 |
|
589 |
int length = ((int) ((end - ini) / interval)) + 1; |
590 |
if (length > 0) { |
591 |
double[] result = new double[length]; |
592 |
|
593 |
for (int i = 0; i < result.length; i++) { |
594 |
result[i] = ini; |
595 |
ini += interval; |
596 |
} |
597 |
|
598 |
return result;
|
599 |
} else {
|
600 |
return new double[0]; |
601 |
} |
602 |
} catch (NumberFormatException except2) { |
603 |
throw except2;
|
604 |
} |
605 |
} else {
|
606 |
throw except;
|
607 |
} |
608 |
} else {
|
609 |
throw except;
|
610 |
} |
611 |
} |
612 |
} else {
|
613 |
String[] parts; |
614 |
parts = str.split(separator); |
615 |
ArrayList ret = new ArrayList(); |
616 |
|
617 |
for (int i = 0; i < parts.length; i++) { |
618 |
try {
|
619 |
ret.add(Double.valueOf(parts[i]));
|
620 |
} catch (NumberFormatException except) { |
621 |
if (parts[i].startsWith("[") && parts[i].endsWith("]")) { |
622 |
String[] tokens = parts[i].substring(1, |
623 |
parts[i].length() - 1).split(":"); |
624 |
if (tokens.length == 3) { |
625 |
try {
|
626 |
Double ini = Double.valueOf(tokens[0]); |
627 |
Double end = Double.valueOf(tokens[1]); |
628 |
Double interval = Double.valueOf(tokens[2]); |
629 |
|
630 |
for (double j = ini; j <= end; j += interval) { |
631 |
ret.add(Double.valueOf(j));
|
632 |
} |
633 |
} catch (NumberFormatException except2) { |
634 |
throw except2;
|
635 |
} |
636 |
} |
637 |
} else {
|
638 |
throw except;
|
639 |
} |
640 |
} |
641 |
} |
642 |
|
643 |
double[] result = new double[ret.size()]; |
644 |
for (int i = 0; i < result.length; i++) { |
645 |
result[i] = ((Double) ret.get(i)).doubleValue();
|
646 |
} |
647 |
|
648 |
return result;
|
649 |
} |
650 |
} |
651 |
|
652 |
/**
|
653 |
* Truco sucio para evitar la inquisici?n. Cuando funcione, habr? que hacer
|
654 |
* una propuesta para ver si es aceptado....
|
655 |
*
|
656 |
* @param mapCtrl
|
657 |
* @param x
|
658 |
* @param y
|
659 |
*/
|
660 |
public static void flashPoint(MapControl mapCtrl, double x, double y) { |
661 |
flashPoint(mapCtrl, Color.RED, 5, x, y); |
662 |
} |
663 |
|
664 |
/**
|
665 |
* @param mapCtrl
|
666 |
* @param color
|
667 |
* @param x
|
668 |
* @param y
|
669 |
*/
|
670 |
public static void flashPoint(MapControl mapCtrl, Color color, |
671 |
int maxCount, double x, double y) { |
672 |
int delay = 100; // milliseconds |
673 |
MyTask task = new MyTask(mapCtrl, color, maxCount, x, y);
|
674 |
|
675 |
java.util.Timer timer = new java.util.Timer();
|
676 |
timer.schedule(task, delay, 60);
|
677 |
|
678 |
} |
679 |
|
680 |
public static GvFlag[] putFlagsOnNetwork(FLyrVect layer, Network net, |
681 |
double tolerance) throws BaseException { |
682 |
ReadableVectorial reader = layer.getSource(); |
683 |
reader.start(); |
684 |
IFeatureIterator it = reader.getFeatureIterator(); |
685 |
int i = 0; |
686 |
ArrayList<GvFlag> flags = new ArrayList(); |
687 |
while (it.hasNext()) {
|
688 |
IFeature feat = it.next(); |
689 |
Geometry geo = feat.getGeometry().toJTSGeometry(); |
690 |
if (!((geo instanceof Point) || (geo instanceof MultiPoint))) |
691 |
continue;
|
692 |
|
693 |
Coordinate[] coords = geo.getCoordinates();
|
694 |
if (coords.length > 1) { |
695 |
logger.warn("The record " + i + " has " + coords.length + "coordinates. Pay attention!!"); |
696 |
logger.warn("Only one point will be used.");
|
697 |
} |
698 |
for (int j = 0; j < coords.length; j++) { |
699 |
GvFlag flag = net.addFlag(coords[j].x, coords[j].y, tolerance); |
700 |
if (flag == null) { |
701 |
GraphException e = new GraphException("Punto " + i |
702 |
+ " fuera de la red. Tolerancia=" + tolerance);
|
703 |
e.setCode(GraphException.FLAG_OUT_NETWORK); |
704 |
throw e;
|
705 |
// NotificationManager.addError("No se puedo situar el
|
706 |
// registro " + i +
|
707 |
// " Por favor, compruebe que est? encima de la red o
|
708 |
// aumente la tolerancia.",
|
709 |
// e);
|
710 |
} |
711 |
System.out.println("Situando flag " + i + " de la capa " + layer.getName() + " en idArc=" + flag.getIdArc()); |
712 |
flags.add(flag); |
713 |
flag.getProperties().put("rec", new Integer(i)); |
714 |
break;
|
715 |
} |
716 |
i++; |
717 |
} |
718 |
it.closeIterator(); |
719 |
reader.stop(); |
720 |
return flags.toArray(new GvFlag[0]); |
721 |
} |
722 |
|
723 |
/**
|
724 |
* Parche hasta corregir QuadtreGT2 y createSpatialIndex (lo de menos de
|
725 |
* 65.000 niveles)
|
726 |
*
|
727 |
* @param lyrVect
|
728 |
* @throws ReadDriverException
|
729 |
* @throws InitializeDriverException
|
730 |
*/
|
731 |
public static ISpatialIndex createJtsQuadtree(FLyrVect lyrVect) throws InitializeDriverException, ReadDriverException { |
732 |
ReadableVectorial va = lyrVect.getSource(); |
733 |
if (!(va.getDriver() instanceof BoundedShapes)) |
734 |
return null; |
735 |
va.start(); |
736 |
QuadtreeJts spatialIndex = new QuadtreeJts();
|
737 |
BoundedShapes shapeBounds = (BoundedShapes) va.getDriver(); |
738 |
int to = va.getShapeCount();
|
739 |
for (int i = 0; i < to; i++) { |
740 |
Rectangle2D r = shapeBounds.getShapeBounds(i);
|
741 |
if (r != null) |
742 |
spatialIndex.insert(r, i); |
743 |
if ((i % 100000) == 0) |
744 |
System.out.println("Inserting " + i + " bounding of " + to); |
745 |
|
746 |
} // for
|
747 |
va.stop(); |
748 |
// vectorial adapter needs a reference to the spatial index, to
|
749 |
// solve
|
750 |
// request for feature iteration based in spatial queries
|
751 |
va.setSpatialIndex(spatialIndex); |
752 |
return spatialIndex;
|
753 |
} |
754 |
|
755 |
public static void loadLegend(FLyrVect lyr, String gvlPath) { |
756 |
File xmlFile = new File(gvlPath); |
757 |
|
758 |
FileReader reader = null; |
759 |
|
760 |
try {
|
761 |
reader = new FileReader(xmlFile); |
762 |
|
763 |
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
764 |
IVectorLegend myLegend = LegendFactory.createFromXML(new XMLEntity(tag));
|
765 |
|
766 |
if(myLegend != null ) { |
767 |
lyr.setLegend(myLegend); |
768 |
} |
769 |
} catch (FileNotFoundException e) { |
770 |
// TODO Auto-generated catch block
|
771 |
e.printStackTrace(); |
772 |
} catch (MarshalException e) { |
773 |
// TODO Auto-generated catch block
|
774 |
e.printStackTrace(); |
775 |
} catch (ValidationException e) {
|
776 |
// TODO Auto-generated catch block
|
777 |
e.printStackTrace(); |
778 |
} catch (XMLException e) {
|
779 |
// TODO Auto-generated catch block
|
780 |
e.printStackTrace(); |
781 |
} catch (LegendLayerException e) {
|
782 |
// TODO Auto-generated catch block
|
783 |
e.printStackTrace(); |
784 |
} |
785 |
|
786 |
|
787 |
} |
788 |
|
789 |
|
790 |
} |