Skip to content

Commit

Permalink
[SEDONA-347] Centralize transform() usages (apache#949)
Browse files Browse the repository at this point in the history
Co-authored-by: Jia Yu <jiayu@apache.org>
  • Loading branch information
2 people authored and Kontinuation committed Aug 16, 2023
1 parent 816c2ae commit b4b6543
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 4 additions & 4 deletions common/src/main/java/org/apache/sedona/common/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ public static Geometry transform(Geometry geometry, String sourceCRS, String tar
return transform(geometry, sourceCRS, targetCRS, false);
}


public static Geometry transform(Geometry geometry, String sourceCRS, String targetCRS, boolean lenient)
throws FactoryException, TransformException {

CoordinateReferenceSystem sourceCRScode = parseCRSString(sourceCRS);
CoordinateReferenceSystem sourceCRSCode = parseCRSString(sourceCRS);
CoordinateReferenceSystem targetCRScode = parseCRSString(targetCRS);
MathTransform transform = CRS.findMathTransform(sourceCRScode, targetCRScode, lenient);
return JTS.transform(geometry, transform);
return GeomUtils.transform(geometry, sourceCRSCode, targetCRScode, lenient);
}


private static CoordinateReferenceSystem parseCRSString(String CRSString)
throws FactoryException
{
Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/org/apache/sedona/common/utils/GeomUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.apache.sedona.common.utils;

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.geom.impl.CoordinateArraySequence;
import org.locationtech.jts.io.ByteOrderValues;
Expand All @@ -25,6 +27,10 @@
import org.locationtech.jts.algorithm.distance.DiscreteHausdorffDistance;
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
import org.locationtech.spatial4j.shape.jts.JtsGeometry;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import java.nio.ByteOrder;
import java.util.*;
Expand Down Expand Up @@ -244,6 +250,11 @@ public static Geometry buildArea(Geometry geom) {
return outputGeom;
}

public static Geometry transform(Geometry geometry, CoordinateReferenceSystem sourceCRS, CoordinateReferenceSystem targetCRS, boolean lenient) throws FactoryException, TransformException {
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, lenient);
return JTS.transform(geometry, transform);
}

public static int getDimension(Geometry geometry) {
return geometry.getCoordinate() != null && !java.lang.Double.isNaN(geometry.getCoordinate().getZ()) ? 3 : 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.sedona.common.utils;

import com.sun.media.imageioimpl.common.BogusColorSpace;
import org.apache.sedona.common.Functions;
import org.geotools.coverage.CoverageFactoryFinder;
import org.geotools.coverage.GridSampleDimension;
import org.geotools.coverage.grid.GridCoordinates2D;
Expand Down Expand Up @@ -132,8 +133,7 @@ public static Geometry convertCRSIfNeeded(Geometry geometry, CoordinateReference
try {
CoordinateReferenceSystem queryWindowCRS = CRS.decode("EPSG:" + geomSRID);
if (!CRS.equalsIgnoreMetadata(rasterCRS, queryWindowCRS)) {
MathTransform transform = CRS.findMathTransform(queryWindowCRS, rasterCRS, true);
geometry = JTS.transform(geometry, transform);
geometry = GeomUtils.transform(geometry, queryWindowCRS, rasterCRS, true);
}
} catch (FactoryException | TransformException e) {
throw new RuntimeException("Cannot transform CRS of query window", e);
Expand Down

0 comments on commit b4b6543

Please sign in to comment.