MATSIM
NetworkScenarioCut.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * NetworkScenarioCut.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.network.algorithms;
22 
23 import java.util.HashSet;
24 import java.util.Set;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
33 
34 public final class NetworkScenarioCut implements NetworkRunnable {
35 
36  private enum CutType {RECTANGLE, CIRCLE}
37 
38  private final CutType cutType;
39 
40  private final double minX;
41  private final double maxX;
42  private final double minY;
43  private final double maxY;
44 
45  private final double radius;
46  private final Coord center;
47 
48  private final static Logger log = LogManager.getLogger(NetworkScenarioCut.class);
49 
50  public NetworkScenarioCut(final Coord min, final Coord max) {
51  super();
52 
53  this.cutType = CutType.RECTANGLE;
54 
55  this.minX = min.getX();
56  this.maxX = max.getX();
57  this.minY = min.getY();
58  this.maxY = max.getY();
59 
60  this.radius = Double.MAX_VALUE;
61  this.center = null;
62  }
63 
64  public NetworkScenarioCut(final Coord center, final double radius) {
65  super();
66 
67  this.cutType = CutType.CIRCLE;
68 
69  this.center = center;
70  this.radius = radius;
71 
72  this.minX = Double.MIN_VALUE;
73  this.maxX = Double.MAX_VALUE;
74  this.minY = Double.MIN_VALUE;
75  this.maxY = Double.MAX_VALUE;
76  }
77 
78  @Override
79  public void run(final Network network) {
80 
81  Set<Node> nodesToRemove;
82 
83  if (this.cutType == CutType.RECTANGLE) nodesToRemove = rectangularCut(network);
84  else if (this.cutType == CutType.CIRCLE) nodesToRemove = circularCut(network);
85  else return;
86 
87  int nofLinksRemoved = 0;
88  for (Node n : nodesToRemove) {
89  nofLinksRemoved += n.getInLinks().size() + n.getOutLinks().size();
90  network.removeNode(n.getId());
91  }
92 
93  log.info("number of nodes removed: "+nodesToRemove.size());
94  log.info("number of links removed: "+nofLinksRemoved);
95  log.info("number of nodes remaining: "+network.getNodes().size());
96  log.info("number of links remaining: "+network.getLinks().size());
97  }
98 
99  private Set<Node> rectangularCut(Network network) {
100  Set<Node> nodesToRemove = new HashSet<>();
101  for (Node n : network.getNodes().values()) {
102  Coord coord = n.getCoord();
103  double x = coord.getX();
104  double y = coord.getY();
105  if (!((x < this.maxX) && (this.minX < x) && (y < this.maxY) && (this.minY < y))) {
106  nodesToRemove.add(n);
107  }
108  }
109  return nodesToRemove;
110  }
111 
112  private Set<Node> circularCut(Network network) {
113  Set<Node> nodesToRemove = new HashSet<>();
114  for (Node n : network.getNodes().values()) {
115  Coord coord = n.getCoord();
116  double distance = CoordUtils.calcEuclideanDistance(coord, center);
117  if (distance > radius) {
118  nodesToRemove.add(n);
119  }
120  }
121  return nodesToRemove;
122  }
123 }
NetworkScenarioCut(final Coord center, final double radius)
Map< Id< Node >, ? extends Node > getNodes()
static double calcEuclideanDistance(Coord coord, Coord other)
Node removeNode(final Id< Node > nodeId)
Map< Id< Link >, ? extends Link > getLinks()