Java Code Examples for org.matsim.api.core.v01.network.Network#removeNode()

The following examples show how to use org.matsim.api.core.v01.network.Network#removeNode() . 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 5 votes vote down vote up
public static void cutNetwork(Network network, Coord SW, Coord NE) {
	for(Node n : new HashSet<>(network.getNodes().values())) {
		if(!CoordTools.isInArea(n.getCoord(), SW, NE)) {
			network.removeNode(n.getId());
		}
	}
}
 
Example 2
Source File: NetworkTools.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes all nodes with no in- or outgoing links from a network
 */
public static void removeNotUsedNodes(Network network) {
	for(Node n : new HashSet<>(network.getNodes().values())) {
		if(n.getInLinks().size() == 0 && n.getOutLinks().size() == 0) {
			network.removeNode(n.getId());
		}
	}
}
 
Example 3
Source File: NetworkTools.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes all nodes that are not specified from the network
 */
public static void cutNetwork(Network network, Collection<Node> nodesToKeep) {
	for(Node n : new HashSet<>(network.getNodes().values())) {
		if(!nodesToKeep.contains(n)) {
			network.removeNode(n.getId());
		}
	}
}
 
Example 4
Source File: ScheduleCleaner.java    From pt2matsim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Removes links that are not used by public transit. Links which have a mode defined
 * in modesToKeep are kept regardless of public transit usage. Opposite links of used
 * links are kept if keepOppositeLinks is true.
 */
public static void removeNotUsedTransitLinks(TransitSchedule schedule, Network network, Set<String> modesToKeep, boolean keepOppositeLinks) {
	log.info("... Removing links that are not used by public transit");

	Set<Id<Link>> usedTransitLinkIds = new HashSet<>();

	for(TransitLine line : schedule.getTransitLines().values()) {
		for(TransitRoute route : line.getRoutes().values()) {
			if(route.getRoute() != null)
				usedTransitLinkIds.addAll(ScheduleTools.getTransitRouteLinkIds(route));
		}
	}

	Map<Id<Link>, ? extends Link> links = network.getLinks();
	if(keepOppositeLinks) {
		for(Id<Link> linkId : new HashSet<>(usedTransitLinkIds)) {
			Link oppositeLink = NetworkTools.getOppositeLink(links.get(linkId));
			if(oppositeLink != null) usedTransitLinkIds.add(oppositeLink.getId());
		}
	}

	int linksRemoved = 0;
	for(Link link : new HashSet<>(network.getLinks().values())) {
		// only remove link if there are only modes to remove on it
		if(!org.matsim.pt2matsim.tools.MiscUtils.collectionsShareMinOneStringEntry(link.getAllowedModes(), modesToKeep) && !usedTransitLinkIds.contains(link.getId())) {
			network.removeLink(link.getId());
			linksRemoved++;
		}
		// only retain modes that are actually used
		else if(org.matsim.pt2matsim.tools.MiscUtils.collectionsShareMinOneStringEntry(link.getAllowedModes(), modesToKeep) && !usedTransitLinkIds.contains(link.getId())) {
			link.setAllowedModes(MiscUtils.getSharedSetStringEntries(link.getAllowedModes(), modesToKeep));
		}
	}

	// removing nodes
	for(Node n : new HashSet<>(network.getNodes().values())) {
		if(n.getOutLinks().size() == 0 && n.getInLinks().size() == 0) {
			network.removeNode(n.getId());
		}
	}

	log.info("    " + linksRemoved + " links removed");
}