Skip to content

Commit

Permalink
feat: Control car departure access (#154)
Browse files Browse the repository at this point in the history
* Only allow access to/from car on viable links

* Update tag
  • Loading branch information
sebhoerl authored Dec 5, 2023
1 parent 71b8e47 commit c2bc9a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import org.eqasim.core.misc.ParallelProgress;
import org.eqasim.core.scenario.cutter.network.RoadNetwork;
import org.matsim.api.core.v01.network.Link;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.utils.collections.QuadTree;
import org.matsim.core.utils.collections.QuadTrees;
import org.matsim.facilities.ActivityFacilities;
import org.matsim.facilities.ActivityFacility;
import org.matsim.facilities.ActivityFacilityImpl;

public class FacilityPlacement {
private final int numberOfThreads;
private final int batchSize;
private final RoadNetwork network;
private final QuadTree<Link> spatialIndex;

public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network) {
this.network = network;
public FacilityPlacement(int numberOfThreads, int batchSize, RoadNetwork network, FacilityPlacementVoter voter) {
this.numberOfThreads = numberOfThreads;
this.batchSize = batchSize;

this.spatialIndex = QuadTrees.createQuadTree(
network.getLinks().values().stream().filter(voter::canPlaceFacility).collect(Collectors.toList()));
}

public void run(ActivityFacilities facilities) throws InterruptedException {
Expand Down Expand Up @@ -69,12 +73,56 @@ public void run() {
}

for (ActivityFacility facility : localTasks) {
Link link = NetworkUtils.getNearestLink(network, facility.getCoord());
Link link = spatialIndex.getClosest(facility.getCoord().getX(), facility.getCoord().getY());
((ActivityFacilityImpl) facility).setLinkId(link.getId());
}

progress.update(localTasks.size());
} while (localTasks.size() > 0);
}
}

static public interface FacilityPlacementVoter {
boolean canPlaceFacility(Link link);
}

static public class OSMFacilityPlacementVoter implements FacilityPlacementVoter {
private final static String HIGHWAY_TAG = "osm:way:highway";

public OSMFacilityPlacementVoter(RoadNetwork network) {
boolean foundAttribute = false;

for (Link link : network.getLinks().values()) {
if (link.getAttributes().getAttribute(HIGHWAY_TAG) != null) {
foundAttribute = true;
break;
}
}

if (!foundAttribute) {
throw new IllegalStateException("Did not find osm:highway attribute in network");
}
}

@Override
public boolean canPlaceFacility(Link link) {
String highway = (String) link.getAttributes().getAttribute(HIGHWAY_TAG);

if (highway != null) {
if (highway.contains("motorway")) {
return false;
}

if (highway.contains("trunk")) {
return false;
}

if (highway.contains("_link")) {
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.eqasim.core.scenario.preparation;

import org.eqasim.core.scenario.cutter.network.RoadNetwork;
import org.eqasim.core.scenario.preparation.FacilityPlacement.OSMFacilityPlacementVoter;
import org.matsim.api.core.v01.Scenario;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.CommandLine.ConfigurationException;
Expand Down Expand Up @@ -33,7 +34,8 @@ static public void main(String[] args) throws ConfigurationException, Interrupte
int batchSize = cmd.getOption("batch-size").map(Integer::parseInt).orElse(10);

RoadNetwork roadNetwork = new RoadNetwork(scenario.getNetwork());
FacilityPlacement facilityPlacement = new FacilityPlacement(numberOfThreads, batchSize, roadNetwork);
OSMFacilityPlacementVoter voter = new OSMFacilityPlacementVoter(roadNetwork);
FacilityPlacement facilityPlacement = new FacilityPlacement(numberOfThreads, batchSize, roadNetwork, voter);
facilityPlacement.run(scenario.getActivityFacilities());

// Fix freight activities (TODO: should go to the pipeline)
Expand Down

0 comments on commit c2bc9a4

Please sign in to comment.