Skip to content

Commit

Permalink
attempt to reduce auto-magic // read directly from shape
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Feb 1, 2024
1 parent e878900 commit a916ea6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void processNetworkForStopCreation(Network network, boolean modeFilteredN

for (Node node : network.getNodes().values()) {
//we dont want pt nodes included as pt has a separate network + no dead ends
//ts feb' 24: why more than 2 links and not >=2 links?? also, the spatial lookup is made before in the network preprocessing...
if (MGC.coord2Point(node.getCoord()).within(drtServiceArea) && (node.getInLinks().size() + node.getOutLinks().size() > 2)
&& !node.getId().toString().contains("pt_")) {

Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/matsim/run/prepare/DrtCaseSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,29 @@ public static void prepareScenario(Scenario scenario, DrtCase drtCase, ShpOption
scenario.getPopulation().getFactory().getRouteFactories().setRouteFactory(DrtRoute.class, new DrtRouteFactory());
// (matsim core does not know about DRT routes. This makes it possible to read them before the controler is there.)

String drtMode = null;
Integer noVehicles = null;


CreateDrtStopsFromNetwork drtStopsCreator = new CreateDrtStopsFromNetwork();
MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(scenario.getConfig(), MultiModeDrtConfigGroup.class);

String drtMode;
switch (drtCase) {
case twoSeparateServiceAreas -> {
//flexa case with 2 separate drt bubbles (north and southeast) -> 2 separate drt modes

log.info("reading " + drtArea.getShapeFile().toString());
for (SimpleFeature feature : drtArea.readFeatures()) {
String name = (String) feature.getAttribute("Name");
if (name.equals("Nord")) {
drtMode = "drtNorth";
noVehicles = 3;

} else if (name.equals("Suedost")) {
drtMode = "drtSoutheast";
noVehicles = 2;
} else {
log.fatal("Invalid shp feature name. Shp features must be named 'Nord' or 'Suedost'!");
// String name = (String) feature.getAttribute("Name");
drtMode = String.valueOf(feature.getAttribute("mode"));
if (drtMode.equals("null")) {
throw new IllegalArgumentException("could not find 'mode' attribute in the given shape file at " + drtArea.getShapeFile().toString());
}
Integer noVehicles = (Integer) feature.getAttribute("noVehicles");
if (noVehicles.equals(null)){
throw new IllegalArgumentException("could not find 'noVehicles' attribute in the given shape file at " + drtArea.getShapeFile().toString());
}

log.info("attempting to create " + noVehicles + " drt vehicles for mode " + drtMode + " in feature " + name);
log.info("attempting to create " + noVehicles + " drt vehicles for mode " + drtMode);

new LeipzigDrtVehicleCreator().createDrtVehiclesForSingleArea(scenario.getVehicles(), scenario.getNetwork(),
feature, noVehicles, drtMode);
Expand Down
47 changes: 14 additions & 33 deletions src/main/java/org/matsim/run/prepare/PrepareNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,53 +59,34 @@ public Integer call() throws Exception {
*/
static void prepareDRT(Network network, ShpOptions shp, String modes) {

Set<String> modesToAdd = new HashSet<>(Arrays.asList(modes.split(",")));
Geometry drtOperationArea = null;
Geometry avOperationArea = null;

List<SimpleFeature> features = shp.readFeatures();
Map<String, Geometry> modeGeoms = new HashMap<>();
for (SimpleFeature feature : features) {
if (feature.getAttribute("mode").equals("drt")) {
if (drtOperationArea == null) {
drtOperationArea = (Geometry) feature.getDefaultGeometry();
} else {
drtOperationArea = drtOperationArea.union((Geometry) feature.getDefaultGeometry());
}
} else {
drtOperationArea = avOperationArea.getFactory().createPoint();
}

if (feature.getAttribute("mode").equals("av")) {
if (avOperationArea == null) {
avOperationArea = (Geometry) feature.getDefaultGeometry();
} else {
avOperationArea = avOperationArea.union((Geometry) feature.getDefaultGeometry());
}
} else {
avOperationArea = drtOperationArea.getFactory().createPoint();
String mode = String.valueOf( feature.getAttribute("mode") );
if (mode.equals("null")) {
throw new IllegalArgumentException("could not find 'mode' attribute in the given shape file at " + shp.getShapeFile().toString());
}
modeGeoms.compute(mode, (m,geom) -> geom == null ? ((Geometry) feature.getDefaultGeometry()) : geom.union((Geometry) feature.getDefaultGeometry()));

}

for (Link link : network.getLinks().values()) {
if (!link.getAllowedModes().contains("car")) {
continue;
}

boolean isDrtAllowed = MGC.coord2Point(link.getFromNode().getCoord()).within(drtOperationArea) &&
MGC.coord2Point(link.getToNode().getCoord()).within(drtOperationArea);
boolean isAvAllowed = MGC.coord2Point(link.getFromNode().getCoord()).within(avOperationArea) &&
MGC.coord2Point(link.getToNode().getCoord()).within(avOperationArea);

if (isDrtAllowed || isAvAllowed) {
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
allowedModes.addAll(modesToAdd);
link.setAllowedModes(allowedModes);
for (Map.Entry<String, Geometry> modeGeometryEntry : modeGeoms.entrySet()) {
if(MGC.coord2Point(link.getFromNode().getCoord()).within(modeGeometryEntry.getValue()) &&
MGC.coord2Point(link.getToNode().getCoord()).within(modeGeometryEntry.getValue())){
Set<String> allowedModes = new HashSet<>(link.getAllowedModes());
allowedModes.add(modeGeometryEntry.getKey());
link.setAllowedModes(allowedModes);
}
}
}
MultimodalNetworkCleaner multimodalNetworkCleaner = new MultimodalNetworkCleaner(network);
multimodalNetworkCleaner.run(modesToAdd);

log.log(Level.INFO, "The following modes have been added to the network: {}", modes);
log.log(Level.INFO, "The following modes have been added to the network: {}", modeGeoms.keySet());
}

/**
Expand Down

0 comments on commit a916ea6

Please sign in to comment.