Revision 15509 trunk/extensions/extGraph/src/com/iver/cit/gvsig/graph/solvers/ServiceAreaExtractor2.java
ServiceAreaExtractor2.java | ||
---|---|---|
45 | 45 |
|
46 | 46 |
import java.io.File; |
47 | 47 |
import java.sql.Types; |
48 |
import java.util.ArrayList; |
|
48 | 49 |
import java.util.HashMap; |
49 | 50 |
import java.util.Map; |
50 | 51 |
import java.util.Set; |
... | ... | |
58 | 59 |
import com.hardcode.gdbms.engine.values.Value; |
59 | 60 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
60 | 61 |
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException; |
62 |
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException; |
|
61 | 63 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException; |
62 | 64 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
63 | 65 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
... | ... | |
71 | 73 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
72 | 74 |
import com.iver.cit.gvsig.fmap.edition.writers.shp.ShpWriter; |
73 | 75 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
76 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
|
74 | 77 |
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial; |
75 | 78 |
import com.iver.cit.gvsig.graph.NetworkUtils; |
76 | 79 |
import com.iver.cit.gvsig.graph.core.EdgePair; |
... | ... | |
152 | 155 |
fields[6] = fieldDesc; |
153 | 156 |
|
154 | 157 |
} |
158 |
|
|
159 |
static FieldDescription[] fieldsPol = new FieldDescription[2]; |
|
160 |
static { |
|
161 |
FieldDescription fieldDesc = new FieldDescription(); |
|
162 |
fieldDesc.setFieldName("COST"); |
|
163 |
fieldDesc.setFieldType(Types.DOUBLE); |
|
164 |
fieldDesc.setFieldLength(20); |
|
165 |
fieldDesc.setFieldDecimalCount(5); |
|
166 |
fieldsPol[0] = fieldDesc; |
|
167 |
|
|
168 |
fieldDesc = new FieldDescription(); |
|
169 |
fieldDesc.setFieldName("IDFLAG"); |
|
170 |
fieldDesc.setFieldType(Types.INTEGER); |
|
171 |
fieldDesc.setFieldLength(20); |
|
172 |
fieldDesc.setFieldDecimalCount(5); |
|
173 |
fieldsPol[1] = fieldDesc; |
|
155 | 174 |
|
175 |
} |
|
176 |
|
|
177 |
|
|
156 | 178 |
private Network net; |
157 | 179 |
|
158 | 180 |
private ShpWriter shpWriter; |
181 |
private ShpWriter shpWriterPol; |
|
182 |
private File fTempPol; |
|
183 |
private SHPLayerDefinition layerDefPol; |
|
159 | 184 |
|
185 |
|
|
160 | 186 |
private HashMap<Integer, GvEdge> visitedEdges = new HashMap(); |
161 | 187 |
|
162 | 188 |
private File fTemp; |
... | ... | |
167 | 193 |
|
168 | 194 |
private ReadableVectorial adapter; |
169 | 195 |
|
170 |
private double maxCost; |
|
196 |
// private double maxCost;
|
|
171 | 197 |
|
172 | 198 |
private Geometry serviceArea; |
199 |
private ArrayList <Geometry> serviceAreaPolygons; |
|
173 | 200 |
|
201 |
private double[] costs = null; |
|
202 |
|
|
174 | 203 |
/** |
175 | 204 |
* @param net |
176 | 205 |
* @throws InitializeWriterException |
... | ... | |
180 | 209 |
public ServiceAreaExtractor2(Network net) throws BaseException { |
181 | 210 |
this.net = net; |
182 | 211 |
int aux = (int) (Math.random() * 1000); |
183 |
fTemp = new File(tempDirectoryPath + "/tmpShp" + aux + ".shp"); |
|
184 |
|
|
212 |
|
|
213 |
|
|
214 |
String nameLine = "tmpServiceAreaLine" + aux + ".shp"; |
|
215 |
String namePol = "tmpServiceAreaPol" + aux + ".shp"; |
|
216 |
fTemp = new File(tempDirectoryPath + "/" + nameLine ); |
|
217 |
fTempPol = new File(tempDirectoryPath + "/" + namePol ); |
|
218 |
|
|
185 | 219 |
layerDef = new SHPLayerDefinition(); |
186 | 220 |
layerDef.setFile(fTemp); |
187 |
layerDef.setName("TestLayer");
|
|
221 |
layerDef.setName(nameLine);
|
|
188 | 222 |
layerDef.setFieldsDesc(fields); |
189 | 223 |
layerDef.setShapeType(FShape.LINE); |
190 | 224 |
|
225 |
layerDefPol = new SHPLayerDefinition(); |
|
226 |
layerDefPol.setFile(fTempPol); |
|
227 |
layerDefPol.setName(namePol); |
|
228 |
layerDefPol.setFieldsDesc(fieldsPol); |
|
229 |
layerDefPol.setShapeType(FShape.POLYGON); |
|
230 |
|
|
191 | 231 |
shpWriter = new ShpWriter(); |
192 | 232 |
shpWriter.setFile(fTemp); |
193 | 233 |
shpWriter.initialize(layerDef); |
234 |
|
|
235 |
shpWriterPol = new ShpWriter(); |
|
236 |
shpWriterPol.setFile(fTempPol); |
|
237 |
shpWriterPol.initialize(layerDefPol); |
|
238 |
shpWriter.preProcess(); |
|
239 |
shpWriterPol.preProcess(); |
|
194 | 240 |
FLyrVect lyr = net.getLayer(); |
195 | 241 |
adapter = lyr.getSource(); |
196 | 242 |
adapter.start(); |
197 |
shpWriter.preProcess(); |
|
243 |
|
|
244 |
serviceAreaPolygons = new ArrayList<Geometry>(); |
|
198 | 245 |
|
199 | 246 |
} |
200 | 247 |
|
248 |
/** |
|
249 |
* Devuelve el ?ndice del intervalo m?s alto que contiene a ese valor. |
|
250 |
* @param bestCost |
|
251 |
* @param costs |
|
252 |
* @return |
|
253 |
*/ |
|
254 |
private int getCostInterval(double bestCost, double[] costs) { |
|
255 |
int ret = 0; |
|
256 |
if (bestCost > costs[costs.length-1]) |
|
257 |
return -1; |
|
258 |
for (int i=costs.length-1; i>=0; i--) { |
|
259 |
if (bestCost > costs[i]) |
|
260 |
{ |
|
261 |
ret = i+1; |
|
262 |
break; |
|
263 |
} |
|
264 |
} |
|
265 |
return ret; |
|
266 |
} |
|
201 | 267 |
|
268 |
/** |
|
269 |
* We process each edge and prepare a list of polygons, classified by |
|
270 |
* cost |
|
271 |
* @param edge |
|
272 |
* @param nodeOrig |
|
273 |
* @param nodeEnd |
|
274 |
* @param geom |
|
275 |
* @param costs |
|
276 |
*/ |
|
277 |
private void processEdgeForPolygon(GvEdge edge, GvNode nodeOrig, GvNode nodeEnd, IGeometry geom, double[] costs) { |
|
278 |
if (nodeEnd.getBestCost() > nodeOrig.getBestCost()) |
|
279 |
{ |
|
280 |
// miramos en qu? pol?gono cae ese edge POR COMPLETO |
|
281 |
// El coste de su punto final es menor que uno de los costes. |
|
282 |
int indexInterval = getCostInterval(nodeEnd.getBestCost(), costs); |
|
283 |
// Un pol?gono por cada zona |
|
284 |
Geometry jtsGeom = geom.toJTSGeometry(); |
|
285 |
if (indexInterval != -1) |
|
286 |
{ |
|
287 |
for (int i=costs.length-1; i >= indexInterval; i--) { |
|
288 |
calculateConvexHull(jtsGeom, i); |
|
289 |
} |
|
290 |
} |
|
291 |
double maxCost = costs[costs.length-1]; |
|
292 |
// Es -1 si caso l?mite externo |
|
293 |
if (indexInterval < costs.length-1) |
|
294 |
{ |
|
295 |
// Caso l?mite externo |
|
296 |
if ((nodeEnd.getBestCost() > maxCost) && |
|
297 |
(nodeOrig.getBestCost() < maxCost)) |
|
298 |
{ |
|
299 |
double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight(); |
|
300 |
LineString partial = NetworkUtils.getPartialLineString(jtsGeom, pct, edge.getDirec()); |
|
301 |
calculateConvexHull(partial, costs.length-1); |
|
302 |
return; |
|
303 |
} |
|
304 |
// Parcial interno |
|
305 |
maxCost = costs[indexInterval+1]; |
|
306 |
if ((nodeOrig.getBestCost() < maxCost) && |
|
307 |
(nodeEnd.getBestCost() > maxCost)) |
|
308 |
{ |
|
309 |
// A ese tramo hemos llegado parcialmente |
|
310 |
|
|
311 |
double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight(); |
|
312 |
try { |
|
313 |
LineString partial = NetworkUtils.getPartialLineString(jtsGeom, pct, edge.getDirec()); |
|
314 |
calculateConvexHull(partial, indexInterval+1); |
|
315 |
} |
|
316 |
catch (Exception e) |
|
317 |
{ |
|
318 |
e.printStackTrace(); |
|
319 |
} |
|
320 |
|
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
|
|
325 |
|
|
326 |
} |
|
327 |
|
|
328 |
/** |
|
329 |
* @param jtsGeom |
|
330 |
* @param i |
|
331 |
*/ |
|
332 |
private void calculateConvexHull(Geometry jtsGeom, int i) { |
|
333 |
if (serviceAreaPolygons.size() <= i) { // se crea por primera vez |
|
334 |
Geometry gIni = jtsGeom; |
|
335 |
serviceAreaPolygons.add(i, gIni); |
|
336 |
} |
|
337 |
else |
|
338 |
{ |
|
339 |
Geometry antG = serviceAreaPolygons.get(i); |
|
340 |
if (antG == null) |
|
341 |
antG = jtsGeom; |
|
342 |
else |
|
343 |
{ |
|
344 |
antG = antG.union(jtsGeom); |
|
345 |
} |
|
346 |
antG = antG.convexHull(); |
|
347 |
serviceAreaPolygons.set(i, antG); |
|
348 |
} |
|
349 |
} |
|
350 |
|
|
351 |
|
|
202 | 352 |
private void writePartialEdge(int i, IGeometry geom, GvEdge edge, GvNode nodeOrig, GvNode nodeEnd, int idFlag, double maxCost) throws ProcessWriterVisitorException { |
203 | 353 |
Geometry jtsGeom = geom.toJTSGeometry(); |
204 | 354 |
double pct = (maxCost - nodeOrig.getBestCost())/ edge.getWeight(); |
... | ... | |
272 | 422 |
this.idFlag = idFlag; |
273 | 423 |
} |
274 | 424 |
|
275 |
public void endExtraction() throws BaseException { |
|
425 |
/** |
|
426 |
* Write edges and polygons associated with active flag and costs |
|
427 |
* @param costs |
|
428 |
* @throws BaseException |
|
429 |
*/ |
|
430 |
public void writeServiceArea() throws BaseException { |
|
276 | 431 |
Set<Map.Entry<Integer, GvEdge>> keySet = visitedEdges.entrySet(); |
432 |
|
|
277 | 433 |
GvEdge edge; |
278 | 434 |
IGraph g = net.getGraph(); |
279 |
Integer idEdge; |
|
435 |
// Integer idEdge; |
|
436 |
double maxCost = costs[costs .length-1]; |
|
437 |
serviceAreaPolygons = new ArrayList<Geometry>(costs.length); |
|
438 |
for (int i=0; i < costs.length-1; i++) |
|
439 |
serviceAreaPolygons.add(null); |
|
440 |
|
|
280 | 441 |
for (Map.Entry<Integer, GvEdge> entry : keySet) { |
281 |
idEdge = entry.getKey(); |
|
442 |
// idEdge = entry.getKey();
|
|
282 | 443 |
edge = entry.getValue(); |
283 | 444 |
GvNode nodeEnd = g.getNodeByID(edge.getIdNodeEnd()); |
284 | 445 |
GvNode nodeOrig = g.getNodeByID(edge.getIdNodeOrig()); |
285 | 446 |
IGeometry geom; |
286 | 447 |
try { |
287 | 448 |
geom = adapter.getShape(edge.getIdArc()); |
288 |
|
|
449 |
processEdgeForPolygon(edge, nodeOrig, nodeEnd, geom, costs); |
|
289 | 450 |
if (nodeEnd.getBestCost() > nodeOrig.getBestCost()) |
290 | 451 |
{ |
291 | 452 |
if (nodeEnd.getBestCost() < maxCost) { |
... | ... | |
309 | 470 |
throw new RuntimeException(e); |
310 | 471 |
} |
311 | 472 |
|
473 |
} // for |
|
474 |
for (int j=serviceAreaPolygons.size()-1; j>=0; j--) { |
|
475 |
Geometry jtsGeom = serviceAreaPolygons.get(j); |
|
476 |
writePolygon(idFlag, costs[j], jtsGeom); |
|
312 | 477 |
} |
478 |
|
|
313 | 479 |
|
480 |
} |
|
481 |
|
|
482 |
private void writePolygon(int idFlag, double maxCost, Geometry jtsGeom) throws ProcessWriterVisitorException { |
|
483 |
Value[] values = new Value[2]; |
|
484 |
values[0] = ValueFactory.createValue(maxCost); |
|
485 |
values[1] = ValueFactory.createValue(idFlag); |
|
486 |
|
|
487 |
IGeometry geom = FConverter.jts_to_igeometry(jtsGeom); |
|
488 |
DefaultFeature feat = new DefaultFeature(geom, values); |
|
489 |
IRowEdited row = new DefaultRowEdited(feat, DefaultRowEdited.STATUS_ADDED, idFlag); |
|
490 |
shpWriterPol.process(row); |
|
491 |
} |
|
492 |
|
|
493 |
/** |
|
494 |
* Close writers. |
|
495 |
* @throws BaseException |
|
496 |
*/ |
|
497 |
public void closeFiles() throws BaseException { |
|
498 |
for (int j=serviceAreaPolygons.size()-1; j>=0; j--) { |
|
499 |
Geometry jtsGeom = serviceAreaPolygons.get(j); |
|
500 |
writePolygon(idFlag, costs[j], jtsGeom); |
|
501 |
} |
|
502 |
|
|
314 | 503 |
shpWriter.postProcess(); |
504 |
shpWriterPol.postProcess(); |
|
505 |
|
|
315 | 506 |
adapter.stop(); |
507 |
|
|
508 |
|
|
509 |
|
|
316 | 510 |
} |
511 |
public double[] getCosts() { |
|
512 |
return costs; |
|
513 |
} |
|
317 | 514 |
|
515 |
public void setCosts(double[] costs) { |
|
516 |
this.costs = costs; |
|
517 |
} |
|
318 | 518 |
|
319 |
public void setMaxCost(double maxCost) { |
|
320 |
this.maxCost = maxCost; |
|
519 |
public FLyrVect getPolygonLayer() throws LoadLayerException { |
|
520 |
FLyrVect lyr = (FLyrVect) LayerFactory.createLayer(layerDefPol.getName(), "gvSIG shp driver", |
|
521 |
layerDefPol.getFile(), null); |
|
522 |
return lyr; |
|
321 | 523 |
} |
322 | 524 |
|
525 |
public FLyrVect getLineLayer() throws LoadLayerException { |
|
526 |
FLyrVect lyr = (FLyrVect) LayerFactory.createLayer(layerDef.getName(), "gvSIG shp driver", |
|
527 |
layerDef.getFile(), null); |
|
528 |
return lyr; |
|
529 |
} |
|
530 |
|
|
323 | 531 |
} |
Also available in: Unified diff