Skip to content

Commit

Permalink
Merge pull request #5786 from leonardehrenfried/move-crs
Browse files Browse the repository at this point in the history
Move expensive `WGS84_XY` constant setup into ElevationModule
  • Loading branch information
leonardehrenfried authored Apr 15, 2024
2 parents 587162b + 70bb4ee commit b176aee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.stream.Stream;
import org.geojson.GeoJsonObject;
import org.geojson.LngLatAlt;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.referencing.CRS;
import org.locationtech.jts.algorithm.ConvexHull;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
Expand All @@ -28,30 +26,12 @@
import org.locationtech.jts.linearref.LengthLocationMap;
import org.locationtech.jts.linearref.LinearLocation;
import org.locationtech.jts.linearref.LocationIndexedLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GeometryUtils {

private static final Logger LOG = LoggerFactory.getLogger(GeometryUtils.class);

private static final CoordinateSequenceFactory csf = new PackedCoordinateSequenceFactory();
private static final GeometryFactory gf = new GeometryFactory(csf);

/** A shared copy of the WGS84 CRS with longitude-first axis order. */
public static final CoordinateReferenceSystem WGS84_XY;

static {
try {
WGS84_XY = CRS.getAuthorityFactory(true).createCoordinateReferenceSystem("EPSG:4326");
} catch (Exception ex) {
LOG.error("Unable to create longitude-first WGS84 CRS", ex);
throw new RuntimeException(
"Could not create longitude-first WGS84 coordinate reference system."
);
}
}

public static <T> Geometry makeConvexHull(
Collection<T> collection,
Function<T, Coordinate> mapToCoordinate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import java.util.concurrent.atomic.AtomicInteger;
import org.geotools.api.coverage.Coverage;
import org.geotools.api.coverage.PointOutsideCoverageException;
import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.geotools.api.referencing.operation.TransformException;
import org.geotools.geometry.Position2D;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.impl.PackedCoordinateSequence;
import org.opentripplanner.framework.geometry.EncodedPolyline;
import org.opentripplanner.framework.geometry.GeometryUtils;
import org.opentripplanner.framework.geometry.SphericalDistanceLibrary;
import org.opentripplanner.framework.lang.IntUtils;
import org.opentripplanner.framework.logging.ProgressTracker;
Expand Down Expand Up @@ -61,6 +62,25 @@
public class ElevationModule implements GraphBuilderModule {

private static final Logger LOG = LoggerFactory.getLogger(ElevationModule.class);
/**
* The WGS84 CRS with longitude-first axis order. The first time a CRS lookup is
* performed is surprisingly expensive (around 500ms), apparently due to initializing
* an HSQLDB JDBC connection. For this reason, the constant is defined in this
* narrower scope rather than a shared utility class, where it was seen to incur the
* initialization cost in a broader range of tests than is necessary.
*/
private static final CoordinateReferenceSystem WGS84_XY;

static {
try {
WGS84_XY = CRS.getAuthorityFactory(true).createCoordinateReferenceSystem("EPSG:4326");
} catch (Exception ex) {
LOG.error("Unable to create longitude-first WGS84 CRS", ex);
throw new RuntimeException(
"Could not create longitude-first WGS84 coordinate reference system."
);
}
}

/** The elevation data to be used in calculating elevations. */
private final ElevationGridCoverageFactory gridCoverageFactory;
Expand Down Expand Up @@ -564,7 +584,7 @@ private double getElevation(Coverage coverage, double x, double y)
// GeoTIFFs in various projections. Note that GeoTools defaults to strict EPSG axis ordering of (lat, long)
// for DefaultGeographicCRS.WGS84, but OTP is using (long, lat) throughout and assumes unprojected DEM
// rasters to also use (long, lat).
coverage.evaluate(new Position2D(GeometryUtils.WGS84_XY, x, y), values);
coverage.evaluate(new Position2D(WGS84_XY, x, y), values);
} catch (PointOutsideCoverageException e) {
nPointsOutsideDEM.incrementAndGet();
throw e;
Expand Down

0 comments on commit b176aee

Please sign in to comment.