Java Code Examples for org.matsim.core.network.NetworkUtils#getNearestNodes()

The following examples show how to use org.matsim.core.network.NetworkUtils#getNearestNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetworkTools.java    From pt2matsim with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Looks for nodes within search radius of <tt>coord</tt> (using {@link NetworkUtils#getNearestNodes},
 * fetches all in- and outlinks and sorts them ascending by their
 * distance to the coordinates given. A map with the distance as key and a set as value is used
 * to (1) return the already calculated distance to the coord and (2) store two opposite links under
 * the same distance.
 *
 * @param network               The network
 * @param coord                 the coordinate from which the closest links are
 *                              to be searched
 * @param nodeSearchRadius      Only links from and to nodes within this radius are considered.
 * @param allowedTransportModes Only links with at least one of these transport modes are considered. All links are considered if <tt>null</tt>.
 */
 public static Map<Double, Set<Link>> findClosestLinks(Network network, Coord coord, double nodeSearchRadius, Set<String> allowedTransportModes) {
	 Collection<Node> nearestNodes = NetworkUtils.getNearestNodes(network, coord, nodeSearchRadius);
	 SortedMap<Double, Set<Link>> closestLinksSortedByDistance = new TreeMap<>();

	 if(nearestNodes.size() != 0) {
		 // fetch every in- and outlink of each node
		 HashSet<Link> links = new HashSet<>();
		 for(Node node : nearestNodes) {
			 links.addAll(node.getOutLinks().values());
			 links.addAll(node.getInLinks().values());
		 }

		 // calculate lineSegmentDistance for all links
		 for(Link link : links) {
			 // only use links with a viable network transport mode
			 if(allowedTransportModes == null || MiscUtils.collectionsShareMinOneStringEntry(link.getAllowedModes(), allowedTransportModes)) {
				 double lineSegmentDistance = CoordUtils.distancePointLinesegment(link.getFromNode().getCoord(), link.getToNode().getCoord(), coord);
				 MapUtils.getSet(lineSegmentDistance, closestLinksSortedByDistance).add(link);
			 }
		 }
	 }
	 return closestLinksSortedByDistance;
}
 
Example 2
Source File: NetworkTools.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the nearest link for the given coordinate.
 * Looks for nodes within search radius of coord (using {@link NetworkUtils#getNearestNodes},
 * fetches all in- and outlinks returns the link with the smallest distance
 * to the given coordinate. If there are two opposite links, the link with
 * the coordinate on its right side is returned.<p/>
 *
 * NOTE: In contrast to {@link NetworkUtils#getNearestLink}, this method looks for the
 * nearest link passing the coordinate from a reasonable set of nearby nodes instead
 * of the closest link originating from or ending in the closest node.
 *
 * @param network network
 * @param coord   the coordinate
 * @param nodeSearchRadius links from/to nodes within this radius are considered
 */
public static Link getNearestLink(Network network, Coord coord, double nodeSearchRadius) {
	Link closestLink = null;
	double minDistance = Double.MAX_VALUE;

	Collection<Node> nearestNodes = NetworkUtils.getNearestNodes(network, coord, nodeSearchRadius);

	while(nearestNodes.size() == 0) {
		nodeSearchRadius *= 2;
		nearestNodes = NetworkUtils.getNearestNodes(network, coord, nodeSearchRadius);
	}
	// check every in- and outlink of each node
	for(Node node : nearestNodes) {
		Set<Link> links = new HashSet<>(node.getOutLinks().values());
		links.addAll(node.getInLinks().values());
		double lineSegmentDistance;

		for(Link link : links) {
			// only use links with a viable network transport mode
			lineSegmentDistance = CoordUtils.distancePointLinesegment(link.getFromNode().getCoord(), link.getToNode().getCoord(), coord);

			if(lineSegmentDistance < minDistance) {
				minDistance = lineSegmentDistance;
				closestLink = link;
			}

		}
	}

	// check for opposite link
	Link oppositeLink = getOppositeLink(closestLink);
	if(oppositeLink != null && !coordIsOnRightSideOfLink(coord, closestLink)) {
		return oppositeLink;
	} else {
		return closestLink;
	}
}