Skip to content

Commit

Permalink
minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed Apr 2, 2024
1 parent 1ba2c9c commit 71fccea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.matsim.contrib.common.zones;

import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdCollectors;
import org.matsim.api.core.v01.IdMap;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.contrib.common.zones.util.NetworkWithZonesUtils;
import org.matsim.contrib.common.zones.util.ZoneFinder;

import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ZoneSystemImpl implements ZoneSystem {

Expand All @@ -31,8 +26,8 @@ public ZoneSystemImpl(Collection<Zone> zones) {
public ZoneSystemImpl(Collection<Zone> zones, ZoneFinder zoneFinder, Network network) {
zones.forEach(zone -> this.zones.put(zone.getId(), zone));

IdMap<Node, Zone> nodeToZoneMap = NetworkWithZonesUtils.createNodeToZoneMap(network, zoneFinder);
IdMap<Link, Zone> linkToZoneMap = NetworkWithZonesUtils.createLinkToZoneMap(network, zoneFinder);
IdMap<Node, Zone> nodeToZoneMap = ZoneSystemUtils.createNodeToZoneMap(network, zoneFinder);
IdMap<Link, Zone> linkToZoneMap = ZoneSystemUtils.createLinkToZoneMap(network, zoneFinder);
this.nodeToZoneMap.putAll(nodeToZoneMap);
this.link2zone.putAll(linkToZoneMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import org.locationtech.jts.geom.prep.PreparedPolygon;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.IdCollectors;
import org.matsim.api.core.v01.IdMap;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.api.core.v01.network.Link;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.network.Node;
import org.matsim.contrib.common.zones.io.ZoneShpReader;
import org.matsim.contrib.common.zones.io.ZoneXmlReader;
import org.matsim.contrib.common.zones.util.ZoneFinder;
import org.matsim.contrib.common.zones.util.ZoneFinderImpl;
import org.matsim.core.utils.geometry.geotools.MGC;

Expand Down Expand Up @@ -89,4 +92,20 @@ public static Map<Id<Zone>, Zone> readZones(URL zonesXmlUrl, URL zonesShpUrl) {
shpReader.readZones(zonesShpUrl);
return zones;
}

// if CRSs of the network and zones are different, zoneFinder should convert between CRSs
public static IdMap<Link, Zone> createLinkToZoneMap(Network network, ZoneFinder zoneFinder) {
return EntryStream.of(network.getLinks())
.mapValues(link -> zoneFinder.findZone(link.getToNode().getCoord()))
.filterValues(Objects::nonNull)
.collect(IdCollectors.toIdMap(Link.class, Map.Entry::getKey, Map.Entry::getValue));
}

public static IdMap<Node, Zone> createNodeToZoneMap(Network network, ZoneFinder zoneFinder) {
return EntryStream.of(network.getNodes())
.mapValues(node -> zoneFinder.findZone(node.getCoord()))
.filterValues(Objects::nonNull)
.collect(IdCollectors.toIdMap(Node.class, Map.Entry::getKey, Map.Entry::getValue));

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.matsim.contrib.taxi.optimizer.rules.ZonalRegisters;
import org.matsim.contrib.taxi.scheduler.TaxiScheduler;
import org.matsim.contrib.zone.ZonalSystemParams;
import org.matsim.contrib.common.zones.util.NetworkWithZonesUtils;
import org.matsim.contrib.common.zones.util.ZoneFinderImpl;
import org.matsim.core.config.ConfigGroup;
import org.matsim.core.mobsim.framework.MobsimTimer;
Expand Down Expand Up @@ -78,7 +77,7 @@ public ZonalRequestInserter(Fleet fleet, TaxiScheduler scheduler, MobsimTimer ti
ConfigGroup.getInputFileURL(context, zonalSystemParams.zonesShpFile));
// TODO No conversion of SRS is done

this.linkToZone = NetworkWithZonesUtils.createLinkToZoneMap(network, new ZoneFinderImpl(zones, zonalSystemParams.expansionDistance));
this.linkToZone = ZoneSystemUtils.createLinkToZoneMap(network, new ZoneFinderImpl(zones, zonalSystemParams.expansionDistance));

// FIXME zonal system used in RuleBasedTaxiOptim (for registers) should be equivalent to
// the zones used in ZonalTaxiOptim (for dispatching)
Expand Down

0 comments on commit 71fccea

Please sign in to comment.