21 package org.matsim.core.network;
24 import java.util.function.Function;
26 import javax.annotation.Nullable;
28 import org.apache.commons.lang3.StringUtils;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
61 throw new IllegalStateException(
"Utility class");
94 return new NetworkImpl(linkFactory);
101 double[] bBox =
new double[4];
102 bBox[0] = Double.POSITIVE_INFINITY;
103 bBox[1] = Double.POSITIVE_INFINITY;
104 bBox[2] = Double.NEGATIVE_INFINITY;
105 bBox[3] = Double.NEGATIVE_INFINITY;
107 for (
Node n : nodes) {
108 if (n.getCoord().getX() < bBox[0]) {
109 bBox[0] = n.getCoord().getX();
111 if (n.getCoord().getX() > bBox[2]) {
112 bBox[2] = n.getCoord().getX();
114 if (n.getCoord().getY() > bBox[3]) {
115 bBox[3] = n.getCoord().getY();
117 if (n.getCoord().getY() < bBox[1]) {
118 bBox[1] = n.getCoord().getY();
140 return new ArrayList<>(0);
142 String trimmed = nodes.trim();
143 if (trimmed.length() == 0) {
144 return new ArrayList<>(0);
146 String[] parts = trimmed.split(
"[ \t\n]+");
147 final List<Node> nodesList =
new ArrayList<>(parts.length);
149 for (String
id : parts) {
152 throw new IllegalArgumentException(
"no node with id " +
id);
175 return new ArrayList<>(0);
177 String trimmed = links.trim();
178 if (trimmed.length() == 0) {
179 return new ArrayList<>(0);
181 String[] parts = trimmed.split(
"[ \t\n]+");
182 final List<Link> linksList =
new ArrayList<>(parts.length);
184 for (String
id : parts) {
187 throw new IllegalArgumentException(
"no link with id " +
id);
198 public static List<Id<Link>>
getLinkIds(
final String links) {
200 return new ArrayList<>(0);
202 String trimmed = links.trim();
203 if (trimmed.length() == 0) {
204 return new ArrayList<>(0);
206 String[] parts = trimmed.split(
"[ \t\n]+");
207 final List<Id<Link>> linkIdsList =
new ArrayList<>(parts.length);
209 for (String
id : parts) {
216 List<Link> links =
new ArrayList<>();
220 throw new IllegalArgumentException(
"no link with id " + linkId);
227 public static List<Id<Link>>
getLinkIds(
final List<Link> links) {
228 List<Id<Link>> linkIds =
new ArrayList<>();
230 for (
Link link : links) {
231 linkIds.add(link.getId());
245 return Math.max(1, numberOfLanes);
250 return Math.max(1, numberOfLanes);
256 boolean hasEmptyModes =
false;
258 Set<String> modes = link.getAllowedModes();
259 if (modes.size() > 1) {
261 }
else if (modes.size() == 1) {
262 String m2 = modes.iterator().next();
271 if (!m2.equals(mode)) {
284 hasEmptyModes =
true;
294 if (link.getToNode() == toNode) {
315 if(distanceToNode < distanceFromNode)
386 Link nearestRightLink = null;
387 Link nearestOverallLink = null;
390 double[] coordVector =
new double[2];
395 double shortestRightDistance = Double.MAX_VALUE;
396 double shortestOverallDistance = Double.MAX_VALUE;
397 List<Link> incidentLinks =
new ArrayList<>(nearestNode.
getInLinks().values());
398 incidentLinks.addAll(nearestNode.
getOutLinks().values());
399 for (
Link link : incidentLinks) {
401 if (dist <= shortestRightDistance) {
403 double[] linkVector =
new double[2];
404 linkVector[0] = link.getToNode().getCoord().getX()
405 - link.getFromNode().getCoord().getX();
406 linkVector[1] = link.getToNode().getCoord().getY()
407 - link.getFromNode().getCoord().getY();
410 double crossProductZ = coordVector[0]*linkVector[1] - coordVector[1]*linkVector[0];
413 if (crossProductZ < 0) {
414 if (dist < shortestRightDistance) {
415 shortestRightDistance = dist;
416 nearestRightLink = link;
419 if (link.getId().compareTo(nearestRightLink.
getId()) < 0) {
420 shortestRightDistance = dist;
421 nearestRightLink = link;
426 if (dist < shortestOverallDistance) {
427 shortestOverallDistance = dist;
428 nearestOverallLink = link;
430 else if (dist == shortestOverallDistance) {
431 if (link.getId().compareTo(nearestOverallLink.
getId()) < 0) {
432 shortestOverallDistance = dist;
433 nearestOverallLink = link;
440 if (nearestRightLink == null) {
441 return nearestOverallLink;
443 return nearestRightLink;
458 Link nearestLink = null;
460 if ( nearestNode == null ) {
461 log.warn(
"nearestNode not found. Will probably crash eventually. Maybe network for requested mode does not exist (i.e. links not annotated accordingly)? Maybe run NetworkCleaner? " +
467 log.warn(network +
"[found nearest node that has no incident links. Will probably crash eventually ... Maybe run NetworkCleaner?][node = " + nearestNode.
getId() +
"]" ) ;
475 double shortestDistance = Double.MAX_VALUE;
478 if (dist < shortestDistance) {
479 shortestDistance = dist;
483 if ( nearestLink == null ) {
484 log.warn(network +
"[nearestLink not found. Will probably crash eventually ... Maybe run NetworkCleaner?]" ) ;
501 if (result.size() == 0){
504 return result.get(result.firstKey());
527 double thetaInLink = Math.atan2(coordInLink.
getY(), coordInLink.
getX());
528 TreeMap<Double, Link> outLinksByOrientation =
new TreeMap<>();
531 if (!(outLink.getToNode().equals(inLink.
getFromNode()))) {
533 double thetaOutLink = Math.atan2(coordOutLink.
getY(), coordOutLink.
getX());
534 double thetaDiff = thetaOutLink - thetaInLink;
535 if (thetaDiff < -Math.PI) {
536 thetaDiff += 2 * Math.PI;
537 }
else if (thetaDiff > Math.PI) {
538 thetaDiff -= 2 * Math.PI;
540 outLinksByOrientation.put(-thetaDiff, outLink);
543 return outLinksByOrientation;
549 return new Coord(x, y);
554 Map<Id<Node>,
Node> nodes =
new TreeMap<>();
556 Node outNode = link.getToNode();
557 nodes.put(outNode.
getId(), outNode);
571 Map<Id<Node>,
Node> nodes =
new TreeMap<>();
573 Node inNode = link.getFromNode();
574 nodes.put(inNode.
getId(), inNode);
588 return new NodeImpl(
id);
593 return new NodeImpl(
id, coord, type);
598 return new NodeImpl(
id, coord);
604 if ( node instanceof NodeImpl ) {
605 ((NodeImpl)node).setOrigId(
id ) ;
607 throw new RuntimeException(
"wrong implementation of interface Node to do this") ;
614 if ( node instanceof NodeImpl ) {
615 ((NodeImpl)node).setType( type ) ;
617 throw new RuntimeException(
"wrong implementation of interface Node to do this") ;
623 if ( node instanceof NodeImpl ) {
624 return ((NodeImpl) node).getOrigId() ;
626 throw new RuntimeException(
"wrong implementation of interface Node to do this") ;
633 if ( node instanceof NodeImpl ) {
634 return ((NodeImpl) node).getType() ;
636 throw new RuntimeException(
"wrong implementation of interface Node to do this") ;
657 public static final String
TYPE=
"type" ;
664 if ( type != null ) {
686 type = type.replaceFirst(
"^highway\\.",
"");
688 if (type == null || type.isBlank())
689 type =
"unclassified";
703 if (speed instanceof Double s)
705 return Double.parseDouble(speed.toString());
715 return o == null ? null : o.toString();
731 double capacity,
double lanes) {
732 return new LinkImpl(
id, from, to, network, length, freespeed, capacity, lanes);
736 final double capacity,
final double numLanes) {
737 return createAndAddLink(network,
id, fromNode, toNode, length, freespeed, capacity, numLanes, null, null ) ;
741 final double capacity,
final double numLanes,
final String origId,
final String type) {
743 throw new IllegalArgumentException(network+
"[from="+fromNode+
" does not exist]");
747 throw new IllegalArgumentException(network+
"[to="+toNode+
" does not exist]");
766 ((TimeDependentNetwork)network).setNetworkChangeEvents(events);
774 if (network.
getNodes().containsKey(
id)) {
775 throw new IllegalArgumentException(network +
"[id=" +
id +
" already exists]");
785 ((TimeDependentNetwork) network).addNetworkChangeEvent(event);
794 return ((TimeDependentNetwork) network).getNetworkChangeEvents() ;
803 return ((SearchableNetwork) network).getNearestLinkExactly(coord) ;
812 return ((SearchableNetwork)network).getNearestNode(coord);
821 return ((SearchableNetwork)network).getNearestNodes(coord, distance);
827 public static final String
ORIGID =
"origid";
841 if (candidateLink.getToNode().equals(link.
getFromNode())) {
842 return candidateLink;
852 String attribute = ACCESSTIMELINKATTRIBUTEPREFIX+routingMode;
861 String attribute = ACCESSTIMELINKATTRIBUTEPREFIX+routingMode;
866 String attribute = EGRESSTIMELINKATTRIBUTEPREFIX + routingMode;
874 String attribute = EGRESSTIMELINKATTRIBUTEPREFIX + routingMode;
899 var network =
readNetwork(filename, networkConfigGroup);
900 network.getNodes().values().parallelStream()
902 var transformedCoord = transformation.
transform(node.getCoord());
903 node.setCoord(transformedCoord);
926 if (testLink == null)
return false;
932 if (testNode == null)
return false;
938 Link expectedLink = expected.
getLinks().get(link.getId());
939 if (expectedLink == null)
return false;
943 Node expectedNode = expected.
getNodes().get(node.getId());
944 if (expectedNode == null)
return false;
974 && Objects.equals(expectedDnl, actualDnl);
995 List<Node> result =
new ArrayList<>();
999 if (!StringUtils.isBlank(attr)) {
1000 var data = attr.split(
" ");
1001 for (String date : data) {
1002 var values = date.split(
",");
1003 if (values.length != 3)
throw new RuntimeException(
"expected three values per node but found: " + date);
1004 var coord =
new Coord(Double.parseDouble(values[1]), Double.parseDouble(values[2]));
1023 if (disallowedNextLinks == null) {
1027 return disallowedNextLinks;
static Node [] getSortedNodes(final Network network)
static void setOrigId(final Node node, final String id)
static int getNumberOfLanesAsInt(final double time, final Link link)
static final String DISALLOWED_NEXT_LINKS_ATTRIBUTE
static double getAllowedSpeed(Link link)
void setNumberOfLanes(double lanes)
boolean isTimeVariantNetwork()
static Link getConnectingLink(final Node fromNode, final Node toNode)
static Network readNetwork(String filename, NetworkConfigGroup networkConfigGroup)
Attributes getAttributes()
static DisallowedNextLinks getOrCreateDisallowedNextLinks(Link link)
static void setLinkEgressTime(Link link, String routingMode, double egressTime)
Map< Id< Node >, ? extends Node > getNodes()
static boolean addDisallowedNextLinks(Link link, String mode, List< Id< Link >> linkIds)
void setFreespeed(double freespeed)
static final String ACCESSTIMELINKATTRIBUTEPREFIX
static Link getNearestRightEntryLink(Network network, final Coord coord)
void setTimeVariantNetwork(final boolean timeVariantNetwork)
static double calcEuclideanDistance(Coord coord, Coord other)
static Node getCloserNodeOnLink(Coord coord, Link link)
static final String ALLOWED_SPEED
static List< Link > getLinks(final Network network, final String links)
final NetworkConfigGroup network()
static String getHighwayType(Link link)
static double [] getBoundingBox(final Collection<? extends Node > nodes)
static final String EGRESSTIMELINKATTRIBUTEPREFIX
static boolean isMultimodal(final Network network)
static Node createNode(Id< Node > id, Coord coord, String type)
void run(final Network network)
Map< Id< Link >, ? extends Link > getInLinks()
static String getOrigId(Link link)
static Node createAndAddNode(Network network, final Id< Node > id, final Coord coord)
static Link createLink(Id< Link > id, Node from, Node to, Network network, double length, double freespeed, double capacity, double lanes)
static boolean compare(Network expected, Network actual)
static void setLinkAccessTime(Link link, String routingMode, double accessTime)
static String getOrigId(Node node)
static NetworkCollector getCollector(NetworkConfigGroup networkConfigGroup)
static void removeDisallowedNextLinks(Link link)
static Map< Id< Link >, ? extends Link > getIncidentLinks(Node node)
static final String ORIG_GEOM
static Network readNetwork(String filename, Config config)
static OptionalTime getLinkEgressTime(Link link, String routingMode)
NetworkFactory getFactory()
static Coord orthogonalProjectionOnLineSegment(final Coord lineFrom, final Coord lineTo, final Coord point)
boolean addDisallowedLinkSequence(String mode, List< Id< Link >> linkSequence)
double getCapacityPeriod()
void run(Network network)
static Link findLinkInOppositeDirection(Link link)
static Map< Id< Node >, ? extends Node > getIncidentNodes(Node node)
static Link getLeftmostTurnExcludingU(Link inLink)
static final String ORIGID
static List< Node > getOriginalGeometry(Link link)
static Queue< NetworkChangeEvent > getNetworkChangeEvents(Network network)
static< T > Id< T > create(final long key, final Class< T > type)
static double getFreespeedTravelTime(Link link, double time)
static void runNetworkCleaner(Network network)
static Link getNearestLinkExactly(Network network, Coord coord)
static double distancePointLinesegment(final Coord lineFrom, final Coord lineTo, final Coord point)
static Collection< Node > getNearestNodes(Network network, final Coord coord, final double distance)
static Network createNetwork(NetworkConfigGroup networkConfigGroup)
static String getType(Node node)
static DisallowedNextLinks getDisallowedNextLinks(Link link)
static TreeMap< Double, Link > getOutLinksSortedClockwiseByAngle(Link inLink)
Object getAttribute(final String attribute)
static Coord findNearestPointOnLink(Coord coord, Link link)
static NetworkCollector getCollector()
Link createLink(final Id< Link > id, final Node fromNode, final Node toNode)
void run(final Network network)
static Network createNetwork()
static Link [] getSortedLinks(final Network network)
void setLength(double length)
static String getType(Link link)
static Link getNearestLink(Network network, final Coord coord)
static void removeAllowedMode(Link link, String mode)
double getNumberOfLanes()
static List< Id< Link > > getLinkIds(final List< Link > links)
static< T extends Attributable > void copyAttributesFromToExcept(T from, T to, String exceptAttribute)
static NetworkCollector getCollector(Config config)
static void readNetwork(Network network, String string)
static void writeNetwork(Network network, String string)
Object putAttribute(final String attribute, final Object value)
static OptionalTime defined(double seconds)
boolean equals(final Object other)
static List< Id< Link > > getLinkIds(final String links)
static void restrictModesAndCleanNetwork(Network network, Function< Id< Link >, Set< String >> modesToRemoveByLinkId)
static void runNetworkSimplifier(Network network)
static double getEuclideanDistance(Coord origin, Coord destination)
Map< Id< Link >, ? extends Link > getLinks()
static void copyAttributesExceptDisallowedNextLinks(Link from, Link to)
Node createNode(final Id< Node > id, final Coord coord)
static Link createAndAddLink(Network network, final Id< Link > id, final Node fromNode, final Node toNode, final double length, final double freespeed, final double capacity, final double numLanes, final String origId, final String type)
static double getFreespeedTravelTime(Link link)
static Node createNode(Id< Node > id, Coord coord)
static boolean testLinksAreEqual(Link expected, Link actual)
static Link createAndAddLink(Network network, final Id< Link > id, final Node fromNode, final Node toNode, final double length, final double freespeed, final double capacity, final double numLanes)
static final String WRONG_IMPLEMENTATION
final void readFile(final String filename)
static void setType(Node node, final String type)
void setAllowedModes(Set< String > modes)
static int getNumberOfLanesAsInt(final Link link)
static void setNetworkChangeEvents(Network network, List< NetworkChangeEvent > events)
static Map< Id< Node >, ? extends Node > getOutNodes(Node node)
static OptionalTime getLinkAccessTime(Link link, String routingMode)
static void setDisallowedNextLinks(Link link, DisallowedNextLinks disallowedNextLinks)
static List< Link > getLinks(final Network network, final List< Id< Link >> linkIds)
void write(final String filename)
static OptionalTime undefined()
static Network createNetwork(Config config)
static Coord getVector(Link link)
Map< Id< Link >, ? extends Link > getOutLinks()
static Network readNetwork(String filename)
static boolean testNodesAreEqual(Node expected, Node actual)
static Network readNetwork(String filename, NetworkConfigGroup networkConfigGroup, CoordinateTransformation transformation)
static Node createNode(Id< Node > id)
static void addNetworkChangeEvent(Network network, NetworkChangeEvent event)
static List< Node > getNodes(final Network network, final String nodes)
static void setType(Link link, String type)
static void addAllowedMode(Link link, String mode)
Object removeAttribute(final String attribute)
static Id< Node > createNodeId(final long key)
static Node getNearestNode(Network network, final Coord coord)
static void setOrigId(Link link, String id)
void setCapacity(double capacity)
static double getEuclideanDistance(double x1, double y1, double x2, double y2)
Set< String > getAllowedModes()
static Config createConfig(final String context)
static Map< Id< Node >, ? extends Node > getInNodes(Node node)