-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor facility index helper class
- Loading branch information
Showing
2 changed files
with
63 additions
and
31 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
src/main/java/org/matsim/prepare/population/FacilityIndex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.matsim.prepare.population; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.locationtech.jts.index.strtree.STRtree; | ||
import org.matsim.api.core.v01.Id; | ||
import org.matsim.core.utils.geometry.geotools.MGC; | ||
import org.matsim.facilities.ActivityFacilities; | ||
import org.matsim.facilities.ActivityFacility; | ||
import org.matsim.facilities.FacilitiesUtils; | ||
import org.matsim.facilities.MatsimFacilitiesReader; | ||
import org.matsim.run.RunOpenBerlinScenario; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.NavigableMap; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Spatial index for facilities. | ||
*/ | ||
final class FacilityIndex { | ||
|
||
private static final Logger log = LogManager.getLogger(FacilityIndex.class); | ||
|
||
final ActivityFacilities all = FacilitiesUtils.createActivityFacilities(); | ||
|
||
/** | ||
* Maps activity type to spatial index. | ||
*/ | ||
final Map<String, STRtree> index = new HashMap<>(); | ||
|
||
public FacilityIndex(String facilityPath) { | ||
|
||
new MatsimFacilitiesReader(RunOpenBerlinScenario.CRS, RunOpenBerlinScenario.CRS, all) | ||
.readFile(facilityPath); | ||
|
||
Set<String> activities = all.getFacilities().values().stream() | ||
.flatMap(a -> a.getActivityOptions().keySet().stream()) | ||
.collect(Collectors.toSet()); | ||
|
||
log.info("Found activity types: {}", activities); | ||
|
||
for (String act : activities) { | ||
|
||
NavigableMap<Id<ActivityFacility>, ActivityFacility> afs = all.getFacilitiesForActivityType(act); | ||
for (ActivityFacility af : afs.values()) { | ||
STRtree index = this.index.computeIfAbsent(act, k -> new STRtree()); | ||
index.insert(MGC.coord2Point(af.getCoord()).getEnvelopeInternal(), af); | ||
} | ||
} | ||
|
||
// Build all trees | ||
index.values().forEach(STRtree::build); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters