-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from matsim-scenarios/addGladbeckUtils
Add gladbeck utils
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/main/java/org/matsim/prepare/AssignPersonAttributes.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,47 @@ | ||
package org.matsim.prepare; | ||
|
||
import org.locationtech.jts.geom.prep.PreparedGeometry; | ||
import org.matsim.api.core.v01.Scenario; | ||
import org.matsim.api.core.v01.population.Person; | ||
import org.matsim.api.core.v01.population.Population; | ||
import org.matsim.application.analysis.HomeLocationFilter; | ||
import org.matsim.application.options.ShpOptions; | ||
import org.matsim.core.gbl.MatsimRandom; | ||
import org.matsim.run.RunGladbeckScenario; | ||
import org.matsim.utils.gis.shp2matsim.ShpGeometryUtils; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class AssignPersonAttributes { | ||
|
||
private static final Logger log = LogManager.getLogger(AssignPersonAttributes.class); | ||
|
||
public static void assigningDifferentCitizenship(Scenario scenario, ShpOptions shp) { | ||
|
||
for (Person p: scenario.getPopulation().getPersons().values()) { | ||
if (shp != null) { | ||
HomeLocationFilter homeLocationFilter = new HomeLocationFilter(shp, scenario.getConfig().global().getCoordinateSystem(), scenario.getPopulation()); | ||
if (homeLocationFilter.test(p) == true) { | ||
log.info("person p is resident in Gladbeck" + p); | ||
double random = MatsimRandom.getRandom().nextDouble(); | ||
if (random <= GladbeckUtils.getShareOfDiffrentCitizenship()) { | ||
log.info("set person " + p + "to different citizenship"); | ||
GladbeckUtils.setPersonToDifferentCitizenship(p); | ||
} | ||
} | ||
} else { | ||
log.warn("no shape file defined assigning attribute to everyone in the population"); | ||
double random = MatsimRandom.getRandom().nextDouble(); | ||
if (random <= GladbeckUtils.getShareOfDiffrentCitizenship()) { | ||
log.info("set person " + p + "to different citizenship"); | ||
GladbeckUtils.setPersonToDifferentCitizenship(p); | ||
} | ||
} | ||
|
||
|
||
} | ||
} | ||
} |
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,21 @@ | ||
package org.matsim.prepare; | ||
|
||
import org.matsim.api.core.v01.population.Person; | ||
|
||
|
||
|
||
public class GladbeckUtils{ | ||
private GladbeckUtils(){} // do not instantiate | ||
|
||
|
||
public static void setPersonToDifferentCitizenship(Person person) { | ||
person.getAttributes().putAttribute("citizenship", "diffrent"); | ||
} | ||
|
||
public static double getShareOfDiffrentCitizenship() { | ||
// this number is derived from: respos/shared-svn/projects/GlaMoBi/data/sozio-demographischen_Daten/2023-18-04_Auswertung_Staatsangehoerigkeiten.xlsx | ||
return 0.19; | ||
} | ||
|
||
|
||
} |
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
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,44 @@ | ||
package org.matsim.run; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.locationtech.jts.util.Assert; | ||
import org.matsim.api.core.v01.Scenario; | ||
import org.matsim.api.core.v01.population.Person; | ||
import org.matsim.core.config.Config; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.controler.OutputDirectoryHierarchy; | ||
import org.matsim.core.scenario.ScenarioUtils; | ||
import org.matsim.examples.ExamplesUtils; | ||
import org.matsim.prepare.AssignPersonAttributes; | ||
import org.matsim.testcases.MatsimTestUtils; | ||
|
||
public class TestPersonAttributes { | ||
|
||
@Rule | ||
public MatsimTestUtils utils = new MatsimTestUtils(); | ||
|
||
@Test | ||
public final void runTestPersonAttributes() { | ||
String inputPath = String.valueOf(ExamplesUtils.getTestScenarioURL("equil-mixedTraffic")); | ||
Config config = ConfigUtils.loadConfig(inputPath + "config-with-mode-vehicles.xml"); | ||
config.controler().setLastIteration(0); | ||
config.controler().setOutputDirectory("output/TestPersonAttributes/"); | ||
config.global().setNumberOfThreads(1); | ||
config.qsim().setNumberOfThreads(1); | ||
config.controler().setOverwriteFileSetting(OutputDirectoryHierarchy.OverwriteFileSetting.deleteDirectoryIfExists); | ||
|
||
Scenario scenario = ScenarioUtils.loadScenario(config); | ||
AssignPersonAttributes.assigningDifferentCitizenship(scenario, null); | ||
|
||
scenario.getPopulation().getPersons(); | ||
int count = 0; | ||
for (Person p: scenario.getPopulation().getPersons().values()) { | ||
if( p.getAttributes().getAttribute("citizenship") != null) { | ||
count++; | ||
} | ||
} | ||
int correctNr = (int) Math.floor(scenario.getPopulation().getPersons().size() * 0.19); | ||
Assert.equals(correctNr, count); | ||
} | ||
} |