Skip to content

Commit

Permalink
Cleanup small scale traffic generation (#3032)
Browse files Browse the repository at this point in the history
* improve method signatures of small scale traffic generation

* add fixes for usage with arbitrary configs

* fix config errors

* round transformed coordinates

* clear counts file

* use person id for missing vehicles

* use Map interface instead of HashMap implementation for many objects

* correct spelling

* overwrite existing files instead of deleting

* add comment

* use index of a shape from the beginning

* use index for TripDistributionMatrix Builder

* add assert for creating new File

* add error description

* fix input and usage of index for shape

* clean up

* add method for creating indexBuildings

---------

Co-authored-by: Ricardo Ewert <[email protected]>
  • Loading branch information
rakow and rewertvsp authored Jan 2, 2024
1 parent 90e5f29 commit e315fe4
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.matsim.api.core.v01.Coord;
import org.matsim.core.utils.geometry.CoordinateTransformation;
import org.matsim.core.utils.geometry.geotools.MGC;
import org.matsim.core.utils.geometry.transformations.IdentityTransformation;
import org.matsim.core.utils.geometry.transformations.TransformationFactory;
import org.matsim.core.utils.gis.ShapeFileReader;
import org.matsim.core.utils.io.IOUtils;
Expand Down Expand Up @@ -47,6 +48,11 @@
*/
public final class ShpOptions {

/**
* Special value for {@link #createIndex(String, String)} to use the same crs as the shape file.
*/
public static final String SAME_CRS = "same_crs";

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

@CommandLine.Option(names = "--shp", description = "Optional path to shape file used for filtering", required = false)
Expand Down Expand Up @@ -204,10 +210,15 @@ public Index createIndex(String queryCRS, String attr, Set<String> filter) {
if (queryCRS == null)
throw new IllegalArgumentException("Query crs must not be null!");

CoordinateTransformation ct = TransformationFactory.getCoordinateTransformation(queryCRS, detectCRS());

try {
return new Index(ct, attr, filter);
ShapefileDataStore ds = openDataStoreAndSetCRS();
CoordinateTransformation ct;
if (queryCRS.equals(SAME_CRS))
ct = new IdentityTransformation();
else {
ct = TransformationFactory.getCoordinateTransformation(queryCRS, shpCrs);
}
return new Index(ct, ds, attr, filter);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -222,6 +233,15 @@ public Index createIndex(String queryCRS, String attr) {
return createIndex(queryCRS, attr, null);
}

/**
* Create an index without a filter and the same query crs as the shape file.
*
* @see #createIndex(String, String)
*/
public Index createIndex(String attr) {
return createIndex(SAME_CRS, attr, null);
}

/**
* Create a coordinate transformation to the shape file crs. Tries to autodetect the crs of the shape file.
*/
Expand All @@ -247,6 +267,25 @@ private String detectCRS() {
return shpCrs;
}

/**
* Open the shape file for processing and set the crs if not already specified.
*/
private ShapefileDataStore openDataStoreAndSetCRS() throws IOException {
ShapefileDataStore ds = openDataStore(shp);

if (shpCrs == null) {
try {
CoordinateReferenceSystem crs = ds.getSchema().getCoordinateReferenceSystem();
shpCrs = "EPSG:" + CRS.lookupEpsgCode(crs, true);
log.info("Using detected crs for {}: {}", shp, shpCrs);
} catch (FactoryException | NullPointerException e) {
throw new IllegalStateException("Could not determine crs of the shape file. Try to specify it manually using --shp-crs.", e);
}
}

return ds;
}

/**
* Create an inverse coordinate transformation from the shape file crs. Tries to autodetect the crs of the shape file.
*/
Expand All @@ -269,9 +308,7 @@ public final class Index {
* @param ct coordinate transform from query to target crs
* @param attr attribute for the result of {@link #query(Coord)}
*/
Index(CoordinateTransformation ct, String attr, @Nullable Set<String> filter) throws IOException {
ShapefileDataStore ds = openDataStore(shp);

Index(CoordinateTransformation ct, ShapefileDataStore ds, String attr, @Nullable Set<String> filter) throws IOException {
if (shpCharset != null)
ds.setCharset(shpCharset);

Expand Down Expand Up @@ -342,11 +379,10 @@ public boolean contains(Coord coord) {
return false;
}


/**
* Return all features in the index.
*/
public List<SimpleFeature> getAll() {
public List<SimpleFeature> getAllFeatures() {
List<SimpleFeature> result = new ArrayList<>();
itemsTree(result, index.getRoot());

Expand All @@ -372,6 +408,14 @@ private void itemsTree(List<SimpleFeature> list, AbstractNode node) {
public int size() {
return index.size();
}

/**
* Return underlying shp file. Should be used carefully.
*/
public ShpOptions getShp() {
return ShpOptions.this;
}

}

}
Loading

0 comments on commit e315fe4

Please sign in to comment.