Skip to content

Commit

Permalink
round randomly generated coordinates (#2765)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Sep 13, 2023
1 parent c80717b commit bf455e5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public Integer call() throws Exception {
v = new Coord(coord.getX() - y * m, coord.getY() + x * m);
}

v = CoordUtils.round(v);

mapping.put(act.getCoord(), v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
import org.matsim.application.MATSimAppCommand;
import org.matsim.application.options.CsvOptions;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.geometry.CoordUtils;
import picocli.CommandLine;

import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Map;

@CommandLine.Command(
name = "extract-home-coordinates",
description = "Extract the home coordinates of a person"
name = "extract-home-coordinates",
description = "Extract the home coordinates of a person"
)
public class ExtractHomeCoordinates implements MATSimAppCommand {
public final class ExtractHomeCoordinates implements MATSimAppCommand {

private static final Logger log = LogManager.getLogger(ExtractHomeCoordinates.class);

Expand All @@ -34,6 +35,29 @@ public class ExtractHomeCoordinates implements MATSimAppCommand {
@CommandLine.Mixin
private CsvOptions options = new CsvOptions();

/**
* Set and return home coordinate of this person. Can be null if no home activity is known.
*/
public static Coord setHomeCoordinate(Person person) {
for (Plan plan : person.getPlans()) {
for (PlanElement planElement : plan.getPlanElements()) {
if (planElement instanceof Activity) {
String actType = ((Activity) planElement).getType();
if (actType.startsWith("home")) {
Coord homeCoord = CoordUtils.round(((Activity) planElement).getCoord());

person.getAttributes().putAttribute("home_x", homeCoord.getX());
person.getAttributes().putAttribute("home_y", homeCoord.getY());

return homeCoord;
}
}
}
}

return null;
}

@Override
public Integer call() throws Exception {

Expand All @@ -42,24 +66,9 @@ public Integer call() throws Exception {
Map<Person, Coord> coords = new LinkedHashMap<>();

for (Person person : population.getPersons().values()) {
outer:
for (Plan plan : person.getPlans()) {
for (PlanElement planElement : plan.getPlanElements()) {
if (planElement instanceof Activity) {
String actType = ((Activity) planElement).getType();
if (actType.startsWith("home")) {
Coord homeCoord = ((Activity) planElement).getCoord();
coords.put(person, homeCoord);

person.getAttributes().putAttribute("home_x", homeCoord.getX());
person.getAttributes().putAttribute("home_y", homeCoord.getY());

break outer;
}
}
}

}
Coord coord = setHomeCoordinate(person);
if (coord != null)
coords.put(person, coord);
}

if (csv != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.matsim.application.options.ShpOptions;
import org.matsim.core.network.NetworkUtils;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.utils.geometry.CoordUtils;
import org.matsim.core.utils.geometry.geotools.MGC;
import picocli.CommandLine;

Expand Down Expand Up @@ -93,7 +94,7 @@ public Integer call() throws Exception {

Coord newCoord = mapping.getOrDefault(coord, null);
if (newCoord == null) {
newCoord = landuse.select(crs.getInputCRS(),
newCoord = CoordUtils.round(landuse.select(crs.getInputCRS(),
() -> {
double x = rnd.nextDouble(-gridResolution / 2, gridResolution / 2);
double y = rnd.nextDouble(-gridResolution / 2, gridResolution / 2);
Expand All @@ -103,7 +104,7 @@ public Integer call() throws Exception {
else
return new Coord(coord.getX() + x, coord.getY() + y);
}
);
));
mapping.put(coord, newCoord);
}

Expand Down

0 comments on commit bf455e5

Please sign in to comment.