generated from matsim-scenarios/matsim-scenario-template
-
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.
- Loading branch information
Showing
13 changed files
with
520 additions
and
235 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
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,52 @@ | ||
library(matsim) | ||
library(tidyverse) | ||
library(readr) | ||
library(sf) | ||
|
||
#FILES | ||
FILE_DIR = "../../shared-svn/projects/DiTriMo/data/commuters-by-town" | ||
SIM <- paste0(FILE_DIR, "lausitz-v1.0-commuter.csv") | ||
GEMEINDE <- paste0(FILE_DIR, "pgemeinden.csv") | ||
COMMUTER <- paste0(FILE_DIR, "commuter.csv") | ||
SHP <- paste0(FILE_DIR, "VG5000_GEM/VG5000_GEM.shp") | ||
LAUSITZ.SHP <- paste0(FILE_DIR, "network-area/network-area.shp") | ||
|
||
shp <- st_read(SHP) | ||
lausitz.shp <- st_read(LAUSITZ.SHP) | ||
|
||
sim <- read_csv(file = SIM) | ||
gemeinden <- read_csv( file = GEMEINDE) %>% | ||
mutate(code = str_remove(string = code, pattern = "P")) | ||
|
||
commuter <- read_csv(file = COMMUTER) %>% | ||
mutate(key = paste0(from, "-", to)) | ||
|
||
sim.1 <- sim %>% | ||
filter(!is.na(from) & !is.na(to)) %>% | ||
left_join(gemeinden, by = c("from" = "code")) %>% | ||
left_join(gemeinden, by = c("from" = "code"), suffix = c("_from", "_to")) %>% | ||
mutate(key = paste0(from, "-", to)) %>% | ||
select(-c(from, to)) %>% | ||
left_join(commuter, by = "key", suffix = c("_sim", "_real")) %>% | ||
filter(!is.na(from)) %>% | ||
# pivot_longer(cols = starts_with("n_"), names_to = "src", values_to = "n", names_prefix = "n_") %>% | ||
arrange(desc(n_real)) | ||
|
||
breaks <- c(-Inf, 0.8, 1.2, Inf) | ||
labels <- c("less", "exakt", "more") | ||
|
||
sim.2 <- sim.1 %>% | ||
filter(n_sim > 10) %>% | ||
select(from, to, starts_with("n_"), starts_with("name_")) %>% | ||
mutate(n_rel = n_sim / n_real, | ||
quality = cut(n_rel, breaks = breaks, labels = labels)) | ||
|
||
ggplot(sim.2, aes(x = n_real, y = n_sim, col = quality)) + | ||
|
||
geom_point() + | ||
|
||
scale_x_log10() + | ||
|
||
scale_y_log10() + | ||
|
||
theme_bw() |
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
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
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
80 changes: 80 additions & 0 deletions
80
src/main/java/org/matsim/run/analysis/CommunityFilter.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,80 @@ | ||
package org.matsim.run.analysis; | ||
|
||
import org.apache.commons.csv.CSVPrinter; | ||
import org.geotools.api.feature.simple.SimpleFeature; | ||
import org.locationtech.jts.geom.Geometry; | ||
import org.locationtech.jts.geom.Point; | ||
import org.matsim.application.MATSimAppCommand; | ||
import org.matsim.application.options.CsvOptions; | ||
import org.matsim.core.utils.collections.Tuple; | ||
import org.matsim.core.utils.gis.GeoFileReader; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Path; | ||
import java.util.*; | ||
|
||
@CommandLine.Command(name = "community-filter", description = "creates a csv with commuity keys within shape") | ||
public class CommunityFilter implements MATSimAppCommand { | ||
|
||
@CommandLine.Option(names = "--community-shp", description = "path to VG5000_GEM.shp", required = true) | ||
private static Path communityShapePath; | ||
|
||
@CommandLine.Option(names = "--dilution-area", description = "path to area-of-interest-shape file", required = true) | ||
private static Path dilutionAreaShapePath; | ||
|
||
@CommandLine.Option(names = "--output", description = "output csv filepath", required = true) | ||
private static Path output; | ||
|
||
@CommandLine.Mixin | ||
CsvOptions csvOptions = new CsvOptions(); | ||
private final Map<String, Tuple<Double, Double>> filtered = new HashMap<>(); | ||
|
||
public static void main(String[] args) { | ||
new CommunityFilter().execute(args); | ||
} | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
|
||
Collection<SimpleFeature> communities = readShapeFile(communityShapePath); | ||
Collection<SimpleFeature> dilutionArea = readShapeFile(dilutionAreaShapePath); | ||
|
||
List<Geometry> geometries = dilutionArea.stream().map(feature -> (Geometry) feature.getDefaultGeometry()).toList(); | ||
|
||
for(var community: communities){ | ||
|
||
String attribute = (String) community.getAttribute("ARS"); | ||
|
||
if(geometries.stream() | ||
.anyMatch(geometry -> geometry.covers((Geometry) community.getDefaultGeometry()))){ | ||
|
||
Point centroid = ((Geometry) community.getDefaultGeometry()).getCentroid(); | ||
Tuple<Double, Double> coordinates = Tuple.of(centroid.getX(), centroid.getY()); | ||
|
||
filtered.put(attribute, coordinates); | ||
} | ||
|
||
} | ||
|
||
try (CSVPrinter printer = csvOptions.createPrinter(output)) { | ||
printer.print("ars"); | ||
printer.print("x"); | ||
printer.print("y"); | ||
printer.println(); | ||
|
||
for (Map.Entry<String, Tuple<Double, Double>> entry : filtered.entrySet()) { | ||
printer.print(entry.getKey()); | ||
printer.print(entry.getValue().getFirst()); | ||
printer.print(entry.getValue().getSecond()); | ||
printer.println(); | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private static Collection<SimpleFeature> readShapeFile(Path filepath){ | ||
|
||
return GeoFileReader.getAllFeatures(filepath.toString()); | ||
} | ||
} |
Oops, something went wrong.