Skip to content

Commit

Permalink
Merge branch 'master' into emissions-viz
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Jan 3, 2024
2 parents 8e536f3 + e315fe4 commit 7ecdb4e
Show file tree
Hide file tree
Showing 301 changed files with 2,212 additions and 2,199 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 7ecdb4e

Please sign in to comment.