-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two new tools and extending an existing one (#187)
* feat: ExportActivitiesToShapeFile * feat: ExportPopulationToCSV * feat: extending ExportTransitLinesToShapefile Two features: - It is now possible to specify which items are exported by one of transit line id, transit route id or mode. - The line name is now included in the exported shapefile * fix: renamed ExportActivitiesToShapefile to have consistent naming of tools
- Loading branch information
Showing
4 changed files
with
235 additions
and
16 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
core/src/main/java/org/eqasim/core/tools/ExportActivitiesToShapefile.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,92 @@ | ||
package org.eqasim.core.tools; | ||
|
||
import org.locationtech.jts.geom.Coordinate; | ||
import org.matsim.api.core.v01.Scenario; | ||
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.config.CommandLine; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.population.io.PopulationReader; | ||
import org.matsim.core.scenario.ScenarioUtils; | ||
import org.matsim.core.utils.geometry.geotools.MGC; | ||
import org.matsim.core.utils.gis.PointFeatureFactory; | ||
import org.matsim.core.utils.gis.ShapeFileWriter; | ||
import org.opengis.feature.simple.SimpleFeature; | ||
import org.opengis.referencing.crs.CoordinateReferenceSystem; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExportActivitiesToShapefile { | ||
|
||
public static void exportActivitiesToShapeFile(Population population, String crsString, Set<String> ignoredActivityTypesSet, String outputPath) { | ||
|
||
CoordinateReferenceSystem crs = MGC.getCRS(crsString); | ||
|
||
PointFeatureFactory pointFactory = new PointFeatureFactory.Builder() // | ||
.setCrs(crs).setName("id") // | ||
.addAttribute("personId", String.class) | ||
.addAttribute("activityIndex", Integer.class) | ||
.addAttribute("type", String.class)// | ||
.addAttribute("linkId", String.class) | ||
.addAttribute("facilityId", String.class) | ||
.addAttribute("startTime", Double.class) | ||
.addAttribute("endTime", Double.class)// | ||
.create(); | ||
|
||
Collection<SimpleFeature> features = new LinkedList<>(); | ||
|
||
for(Person person: population.getPersons().values()) { | ||
if(person.getSelectedPlan() == null) { | ||
continue; | ||
} | ||
int activityIndex = -1; | ||
for(PlanElement planElement: person.getSelectedPlan().getPlanElements()) { | ||
if (!(planElement instanceof Activity)) { | ||
continue; | ||
} | ||
Activity a = (Activity) planElement; | ||
activityIndex++; | ||
if(ignoredActivityTypesSet.contains(a.getType())) { | ||
continue; | ||
} | ||
Coordinate coordinate = new Coordinate(a.getCoord().getX(), a.getCoord().getY()); | ||
SimpleFeature feature = pointFactory.createPoint(coordinate, | ||
new Object[] { | ||
person.getId().toString(), | ||
activityIndex, | ||
a.getType(), | ||
a.getLinkId().toString(), | ||
a.getFacilityId() == null ? null : a.getFacilityId().toString(), | ||
a.getStartTime().orElse(Double.NaN), | ||
a.getEndTime().orElse(Double.NaN) | ||
}, | ||
null); | ||
features.add(feature); | ||
} | ||
} | ||
ShapeFileWriter.writeGeometries(features, outputPath); | ||
} | ||
|
||
|
||
public static void main(String[] args) throws CommandLine.ConfigurationException { | ||
CommandLine commandLine = new CommandLine.Builder(args).requireOptions("plans-path", "output-path", "crs") | ||
.allowOptions("ignored-activity-types").build(); | ||
|
||
String plansPath = commandLine.getOptionStrict("plans-path"); | ||
String outputPath = commandLine.getOptionStrict("output-path"); | ||
String crs = commandLine.getOptionStrict("crs"); | ||
Set<String> ignoredActivityTypes = Arrays.stream(commandLine.getOption("ignored-activity-types").orElse("").split(",")) | ||
.map(String::trim) | ||
.filter(s -> s.length()>0) | ||
.collect(Collectors.toSet()); | ||
|
||
Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); | ||
PopulationReader populationReader = new PopulationReader(scenario); | ||
populationReader.readFile(plansPath); | ||
|
||
exportActivitiesToShapeFile(scenario.getPopulation(), crs, ignoredActivityTypes, outputPath); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/eqasim/core/tools/ExportPopulationToCSV.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,64 @@ | ||
package org.eqasim.core.tools; | ||
|
||
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.core.config.CommandLine; | ||
import org.matsim.core.config.ConfigUtils; | ||
import org.matsim.core.population.io.PopulationReader; | ||
import org.matsim.core.scenario.ScenarioUtils; | ||
|
||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class ExportPopulationToCSV { | ||
|
||
public static final String[] IGNORED_ATTRIBUTES = new String[]{"vehicles"}; | ||
|
||
public static void exportPopulationToCSV(Population population, String filePath) { | ||
|
||
List<String> ignoredAttributes = List.of(IGNORED_ATTRIBUTES); | ||
|
||
List<String> attributes = population.getPersons().values().stream() | ||
.flatMap(p -> p.getAttributes().getAsMap().keySet().stream()) | ||
.distinct() | ||
.filter(attribute -> !ignoredAttributes.contains(attribute)) | ||
.collect(Collectors.toList()); | ||
|
||
String[] header = new String[attributes.size()+1]; | ||
header[0] = "person_id"; | ||
for(int i=0; i<attributes.size(); i++) { | ||
header[i+1] = attributes.get(i); | ||
} | ||
|
||
try { | ||
FileWriter fileWriter = new FileWriter(filePath); | ||
fileWriter.write(String.join(";", header) + "\n"); | ||
for(Person person: population.getPersons().values()) { | ||
String[] line = new String[attributes.size()+1]; | ||
line[0] = person.getId().toString(); | ||
for(int i=0; i<attributes.size(); i++) { | ||
line[i+1] = String.valueOf(person.getAttributes().getAsMap().getOrDefault(attributes.get(i), null)); | ||
} | ||
fileWriter.write(String.join(";", line) + "\n"); | ||
} | ||
fileWriter.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
|
||
public static void main(String[] args) throws CommandLine.ConfigurationException { | ||
CommandLine commandLine = new CommandLine.Builder(args).requireOptions("plans-path", "output-path").build(); | ||
|
||
String plansPath = commandLine.getOptionStrict("plans-path"); | ||
Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); | ||
|
||
new PopulationReader(scenario).readFile(plansPath); | ||
|
||
exportPopulationToCSV(scenario.getPopulation(), commandLine.getOptionStrict("output-path")); | ||
} | ||
} |
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