-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quick analysis to extract home locations
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
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,61 @@ | ||
package org.matsim.analysis; | ||
|
||
|
||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVPrinter; | ||
import org.locationtech.jts.geom.Geometry; | ||
import org.matsim.api.core.v01.Coord; | ||
import org.matsim.api.core.v01.population.Activity; | ||
import org.matsim.api.core.v01.population.Person; | ||
import org.matsim.api.core.v01.population.PlanElement; | ||
import org.matsim.api.core.v01.population.Population; | ||
import org.matsim.core.population.PopulationUtils; | ||
import org.matsim.core.utils.geometry.geotools.MGC; | ||
import org.matsim.core.utils.gis.ShapeFileReader; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import picocli.CommandLine; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
@CommandLine.Command( | ||
name = "analyze-population", | ||
description = "Extract the home location of the persons in the population file and write it into a csv" | ||
) | ||
|
||
|
||
public class Person2Home { | ||
|
||
public static void main(String[] args) throws IOException { | ||
String shapeFileZones = "../../shared-svn/projects/rvr-metropole-ruhr/data/shapeFiles/dvg2krs_ruhrgebiet-rvr/dvg2krs_ruhrgebiet-rvr.shp"; | ||
Collection<SimpleFeature> features = ShapeFileReader.getAllFeatures(shapeFileZones); | ||
analyzeHomeLocation(PopulationUtils.readPopulation("../../respos/shared-svn/projects/matsim-metropole-ruhr/metropole-ruhr-v1.0/input/metropole-ruhr-v1.4-3pct.plans.xml.gz"), features); | ||
} | ||
|
||
|
||
private static void analyzeHomeLocation(Population population, Collection<SimpleFeature> analyzedArea) throws IOException { | ||
CSVPrinter csvWriter = new CSVPrinter(new FileWriter("persons-home-locations.csv"), CSVFormat.TDF); | ||
csvWriter.printRecord("person", "home_x", "home_y", "home_location"); | ||
for (SimpleFeature feature : analyzedArea) { | ||
Geometry defaultGeometry = (Geometry) feature.getDefaultGeometry(); | ||
|
||
for (Person p : population.getPersons().values()) { | ||
for (PlanElement planElement : p.getSelectedPlan().getPlanElements()) { | ||
if (planElement instanceof Activity) { | ||
String actType = ((Activity) planElement).getType(); | ||
if (actType.startsWith("home")) { | ||
Coord homeCoord = ((Activity) planElement).getCoord(); | ||
if (MGC.coord2Point(homeCoord).within(defaultGeometry)) { | ||
csvWriter.printRecord(p.getId().toString(), | ||
Double.toString(homeCoord.getX()), Double.toString(homeCoord.getY()), feature.getAttribute("GN").toString()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|