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

The following examples show how to use org.matsim.api.core.v01.network.Network#addLink() . 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: PTMapperTools.java    From pt2matsim with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a node and dummy/loop link on the coordinate of the stop facility and
 * adds both to the network. The stop facility is NOT referenced.
 *
 * @return the new link or the existing link if it's already present in the network
 */
public static Link createArtificialStopFacilityLink(TransitStopFacility stopFacility, Network network, String prefix, double freespeed, Set<String> transportModes) {
	Id<Link> dummyLinkId = createArtificialLinkId(stopFacility);

	Link dummyLink = network.getLinks().get(dummyLinkId);
	if(dummyLink != null) {
		return dummyLink;
	} else {
		Node dummyNode = NetworkUtils.createNode(Id.createNodeId(prefix + stopFacility.getId()), stopFacility.getCoord());
		network.addNode(dummyNode);
		dummyLink = NetworkUtils.createLink(dummyLinkId, dummyNode, dummyNode, network, 10, freespeed, 9999, 1);
		dummyLink.setAllowedModes(transportModes);
		network.addLink(dummyLink);

		return dummyLink;
	}
}
 
Example 2
Source File: PseudoRoutingImpl.java    From pt2matsim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds the artificial links to the network.
 *
 * Not thread safe.
 */
@Override
public void addArtificialLinks(Network network) {
	for(ArtificialLink a : necessaryArtificialLinks) {
		if(!network.getLinks().containsKey(a.getId())) {
			network.addLink(a);
		}
	}
}
 
Example 3
Source File: StandardMATSimScenarioTest.java    From amodeus with GNU General Public License v2.0 4 votes vote down vote up
private static void makeMultimodal(Scenario scenario) {
    // Add pt-links to the network to test a multimodal network as it appears in standard MATSim use cases

    Network network = scenario.getNetwork();
    NetworkFactory factory = network.getFactory();

    // Let's build a fast track through the scenario
    for (int i = 0; i < 9; i++) {
        Id<Link> ptFowardLinkId = Id.createLinkId(String.format("pt_fwd_%d:%d", i, i));
        Id<Link> ptBackwardLinkId = Id.createLinkId(String.format("pt_bck_%d:%d", i, i));
        Id<Node> fromNodeId = Id.createNodeId(String.format("%d:%d", i, i));
        Id<Node> toNodeId = Id.createNodeId(String.format("%d:%d", i + 1, i + 1));

        Link ptFowardLink = factory.createLink(ptFowardLinkId, network.getNodes().get(fromNodeId), network.getNodes().get(toNodeId));
        ptFowardLink.setFreespeed(100.0 * 1000.0 / 3600.0);
        ptFowardLink.setLength(1000.0);
        ptFowardLink.setAllowedModes(Collections.singleton("pt"));
        network.addLink(ptFowardLink);

        Link ptBackwardLink = factory.createLink(ptBackwardLinkId, network.getNodes().get(toNodeId), network.getNodes().get(fromNodeId));
        ptBackwardLink.setFreespeed(100.0 * 1000.0 / 3600.0);
        ptBackwardLink.setLength(1000.0);
        ptBackwardLink.setAllowedModes(Collections.singleton("pt"));
        network.addLink(ptBackwardLink);
    }

    // Also, a routed population may have "pt interaction" activities, which take place at links that are not part of the road network. Amodeus must be able
    // to
    // handle these cases.

    /* for (Person person : scenario.getPopulation().getPersons().values())
     * for (Plan plan : person.getPlans()) {
     * Activity trickyActivity = PopulationUtils.createActivityFromCoordAndLinkId("pt interaction", new Coord(5500.0, 5500.0), Id.createLinkId("pt_fwd_5:5"));
     * 
     * plan.getPlanElements().add(PopulationUtils.createLeg("walk"));
     * plan.getPlanElements().add(trickyActivity);
     * } */

    // TODO @sebhoerl Difficult to keep this in as handling of "interaction" activities become much smarter in MATSim now. We would need to
    // set up a much more realistic test scenario. There is one in the AV package, so we can use that one!

    for (Link link : network.getLinks().values()) {
        if (link.getAllowedModes().contains("car")) {
            link.setAllowedModes(new HashSet<>(Arrays.asList("car", AmodeusModeConfig.DEFAULT_MODE)));
        }
    }
}
 
Example 4
Source File: TestScenarioGenerator.java    From amodeus with GNU General Public License v2.0 4 votes vote down vote up
static private void generateNetwork(Network network) {
    NetworkFactory networkFactory = network.getFactory();

    for (int i = 0; i < networkSize; i++) {
        for (int j = 0; j < networkSize; j++) {
            network.addNode(networkFactory.createNode(Id.createNodeId(String.format("%d:%d", i, j)), new Coord(i * networkScale, j * networkScale)));
        }
    }

    Node fromNode;
    Node toNode;

    for (int i = 0; i < networkSize; i++) {
        for (int j = 1; j < networkSize; j++) {
            fromNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j - 1)));
            toNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j)));
            network.addLink(networkFactory.createLink(Id.createLinkId(String.format("%s_%s", fromNode.getId(), toNode.getId())), fromNode, toNode));

            fromNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j)));
            toNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j - 1)));
            network.addLink(networkFactory.createLink(Id.createLinkId(String.format("%s_%s", fromNode.getId(), toNode.getId())), fromNode, toNode));
        }
    }

    for (int j = 0; j < networkSize; j++) {
        for (int i = 1; i < networkSize; i++) {
            fromNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i - 1, j)));
            toNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j)));
            network.addLink(networkFactory.createLink(Id.createLinkId(String.format("%s_%s", fromNode.getId(), toNode.getId())), fromNode, toNode));

            fromNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i, j)));
            toNode = network.getNodes().get(Id.createNodeId(String.format("%d:%d", i - 1, j)));
            network.addLink(networkFactory.createLink(Id.createLinkId(String.format("%s_%s", fromNode.getId(), toNode.getId())), fromNode, toNode));
        }
    }

    for (Link link : network.getLinks().values()) {
        link.setFreespeed(freespeed);
        link.setLength(networkScale);
    }
}
 
Example 5
Source File: NetworkTools.java    From pt2matsim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Integrates <tt>network B</tt> into <tt>network A</tt>. Network
 * A contains all links and nodes of both networks
 * after integration.
 */
public static void integrateNetwork(final Network networkA, final Network networkB, boolean mergeModes) {
	final NetworkFactory factory = networkA.getFactory();

	// Nodes
	for(Node node : networkB.getNodes().values()) {
		Id<Node> nodeId = Id.create(node.getId().toString(), Node.class);
		if(!networkA.getNodes().containsKey(nodeId)) {
			Node newNode = factory.createNode(nodeId, node.getCoord());
			networkA.addNode(newNode);
		}
	}

	// Links
	double capacityFactor = networkA.getCapacityPeriod() / networkB.getCapacityPeriod();
	for(Link link : networkB.getLinks().values()) {
		Id<Link> linkId = Id.create(link.getId().toString(), Link.class);
		if(!networkA.getLinks().containsKey(linkId)) {
			Id<Node> fromNodeId = Id.create(link.getFromNode().getId().toString(), Node.class);
			Id<Node> toNodeId = Id.create(link.getToNode().getId().toString(), Node.class);
			Link newLink = factory.createLink(linkId, networkA.getNodes().get(fromNodeId), networkA.getNodes().get(toNodeId));
			newLink.setAllowedModes(link.getAllowedModes());
			newLink.setCapacity(link.getCapacity() * capacityFactor);
			newLink.setFreespeed(link.getFreespeed());
			newLink.setLength(link.getLength());
			newLink.setNumberOfLanes(link.getNumberOfLanes());
			networkA.addLink(newLink);
		} else if (mergeModes) {
			Link existingLink = networkA.getLinks().get(linkId);
			
			Set<String> allowedModes = new HashSet<>();
			allowedModes.addAll(existingLink.getAllowedModes());
			allowedModes.addAll(link.getAllowedModes());
			
			existingLink.setAllowedModes(allowedModes);
			
			if (link.getCapacity() * capacityFactor != existingLink.getCapacity()) {
				throw new IllegalStateException("Capacity must be equal for integration");
			}
			
			if (link.getFreespeed() != existingLink.getFreespeed()) {
				throw new IllegalStateException("Freespeed must be equal for integration");
			}
			
			if (link.getLength() != existingLink.getLength()) {
				throw new IllegalStateException("Length must be equal for integration");
			}
			
			if (link.getNumberOfLanes() != existingLink.getNumberOfLanes()) {
				throw new IllegalStateException("Number of lanes must be equal for integration");
			}
		}
	}
}