Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@
<groupId>org.datasyslab</groupId>
<artifactId>campskeleton</artifactId>
</dependency>
<!-- proj4sedona for CRS transformation (pure Java, no LGPL dependencies) -->
<dependency>
<groupId>org.datasyslab</groupId>
<artifactId>proj4sedona</artifactId>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,24 @@ public static Geometry lineFromText(String geomString) {
}

public static Geometry polygonFromEnvelope(double minX, double minY, double maxX, double maxY) {
return polygonFromEnvelope(minX, minY, maxX, maxY, GEOMETRY_FACTORY);
}

public static Geometry polygonFromEnvelope(
double minX, double minY, double maxX, double maxY, GeometryFactory factory) {
Coordinate[] coordinates = new Coordinate[5];
coordinates[0] = new Coordinate(minX, minY);
coordinates[1] = new Coordinate(minX, maxY);
coordinates[2] = new Coordinate(maxX, maxY);
coordinates[3] = new Coordinate(maxX, minY);
coordinates[4] = coordinates[0];
return GEOMETRY_FACTORY.createPolygon(coordinates);
return factory.createPolygon(coordinates);
}

public static Geometry makeEnvelope(
double minX, double minY, double maxX, double maxY, int srid) {
Geometry envelope = polygonFromEnvelope(minX, minY, maxX, maxY);
envelope.setSRID(srid);
return envelope;
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), srid);
return polygonFromEnvelope(minX, minY, maxX, maxY, geometryFactory);
}

public static Geometry makeEnvelope(double minX, double minY, double maxX, double maxY) {
Expand Down Expand Up @@ -315,10 +319,6 @@ public static Geometry geomFromMySQL(byte[] binary) throws ParseException {

buffer.get(wkb);

Geometry geom = geomFromWKB(wkb);

geom.setSRID(srid);

return geom;
return geomFromWKB(wkb, srid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,7 @@ public static Geometry expand(Geometry geometry, double deltaX, double deltaY, d
newCoords[4] = newCoords[0];
return geometry.getFactory().createPolygon(newCoords);
}
Geometry result = Constructors.polygonFromEnvelope(minX, minY, maxX, maxY);
result.setSRID(geometry.getSRID());
return result;
return Constructors.polygonFromEnvelope(minX, minY, maxX, maxY, geometry.getFactory());
}

public static Geometry buffer(Geometry geometry, double radius) {
Expand Down
194 changes: 194 additions & 0 deletions common/src/main/java/org/apache/sedona/common/FunctionsProj4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sedona.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.datasyslab.proj4sedona.core.Proj;
import org.datasyslab.proj4sedona.jts.JTSGeometryTransformer;
import org.datasyslab.proj4sedona.parser.CRSSerializer;
import org.locationtech.jts.geom.Geometry;

/**
* CRS transformation functions using proj4sedona library.
*
* <p>This class provides coordinate reference system (CRS) transformation for vector geometries
* using the proj4sedona library, which is a pure Java implementation without LGPL dependencies.
*
* <h2>Supported CRS Input Formats</h2>
*
* <ul>
* <li><b>EPSG code</b>: "EPSG:4326", "EPSG:3857"
* <li><b>WKT1</b>: OGC Well-Known Text format (PROJCS[...] or GEOGCS[...])
* <li><b>WKT2</b>: ISO 19162:2019 format (PROJCRS[...] or GEOGCRS[...])
* <li><b>PROJ string</b>: "+proj=longlat +datum=WGS84 +no_defs"
* <li><b>PROJJSON</b>: JSON representation of CRS ({"type": "GeographicCRS", ...})
* </ul>
*
* <h2>NAD Grid Support</h2>
*
* <p>NAD grid files can be specified in PROJ strings using the +nadgrids parameter:
*
* <ul>
* <li><b>Local file</b>: "+nadgrids=/path/to/grid.gsb"
* <li><b>PROJ CDN</b>: "+nadgrids=@us_noaa_conus.tif" (@ prefix means optional)
* <li><b>HTTPS URL</b>: "+nadgrids=https://cdn.proj.org/us_noaa_conus.tif"
* </ul>
*
* <p>Grid files with @ prefix are optional - if not found, the transformation continues without the
* grid. Without @ prefix, the grid is mandatory and an error is thrown if not found.
*
* @since 1.9.0
*/
public class FunctionsProj4 {

/** Pattern to match EPSG codes (e.g., "EPSG:4326", "epsg:2154") */
private static final Pattern EPSG_PATTERN =
Pattern.compile("^EPSG:(\\d+)$", Pattern.CASE_INSENSITIVE);

/**
* Transform a geometry from the source CRS specified by the geometry's SRID to the target CRS.
*
* @param geometry The geometry to transform
* @param targetCRS Target CRS definition (EPSG code, WKT, PROJ string, or PROJJSON)
* @return The transformed geometry with SRID set to the target CRS EPSG code (if identifiable)
* @throws IllegalArgumentException if source CRS cannot be determined from geometry SRID
*/
public static Geometry transform(Geometry geometry, String targetCRS) {
return transform(geometry, null, targetCRS);
}

/**
* Transform a geometry from one CRS to another with lenient parameter.
*
* <p><b>Note:</b> The {@code lenient} parameter is accepted for API compatibility with GeoTools
* but is ignored. proj4sedona always performs strict transformation.
*
* @param geometry The geometry to transform
* @param sourceCRS Source CRS definition (EPSG code, WKT, PROJ string, or PROJJSON), or null to
* use geometry's SRID
* @param targetCRS Target CRS definition (EPSG code, WKT, PROJ string, or PROJJSON)
* @param lenient Ignored parameter, kept for API compatibility
* @return The transformed geometry with SRID set to the target CRS EPSG code (if identifiable)
* @throws IllegalArgumentException if source CRS is null and geometry has no SRID
*/
public static Geometry transform(
Geometry geometry, String sourceCRS, String targetCRS, boolean lenient) {
// lenient parameter is ignored - proj4sedona doesn't support it
return transform(geometry, sourceCRS, targetCRS);
}

/**
* Transform a geometry from one CRS to another.
*
* @param geometry The geometry to transform
* @param sourceCRS Source CRS definition (EPSG code, WKT, PROJ string, or PROJJSON), or null to
* use geometry's SRID
* @param targetCRS Target CRS definition (EPSG code, WKT, PROJ string, or PROJJSON)
* @return The transformed geometry with SRID set to the target CRS EPSG code (if identifiable)
* @throws IllegalArgumentException if source CRS is null and geometry has no SRID
*/
public static Geometry transform(Geometry geometry, String sourceCRS, String targetCRS) {
if (geometry == null) {
return null;
}

// Determine source CRS
String effectiveSourceCRS = sourceCRS;
if (effectiveSourceCRS == null || effectiveSourceCRS.isEmpty()) {
int srid = geometry.getSRID();
if (srid != 0) {
effectiveSourceCRS = "EPSG:" + srid;
} else {
throw new IllegalArgumentException(
"Source CRS must be specified. No SRID found on geometry.");
}
}

// Check if source and target are the same EPSG code
Integer sourceSRID = extractEpsgCode(effectiveSourceCRS);
Integer targetSRID = extractEpsgCode(targetCRS);

if (sourceSRID != null && sourceSRID.equals(targetSRID)) {
// Same CRS, just update SRID if needed
if (geometry.getSRID() != targetSRID) {
Geometry result = geometry.copy();
result = Functions.setSRID(result, targetSRID);
result.setUserData(geometry.getUserData());
return result;
}
return geometry;
}

// Perform transformation using cached projections to avoid per-row overhead.
// JTSGeometryTransformer.cached() uses Proj4.cachedConverter() internally,
// which caches parsed Proj objects for repeated transformations.
JTSGeometryTransformer transformer =
JTSGeometryTransformer.cached(effectiveSourceCRS, targetCRS);
Geometry transformed = transformer.transform(geometry);

// Set SRID on result
if (targetSRID == null) {
try {
Proj targetProj = new Proj(targetCRS);
String epsgCode = CRSSerializer.toEpsgCode(targetProj);
if (epsgCode != null) {
Integer epsg = extractEpsgCode(epsgCode);
if (epsg != null) {
targetSRID = epsg;
}
}
} catch (Exception e) {
// Could not identify EPSG code, leave SRID as 0
}
}

if (targetSRID == null) {
targetSRID = 0;
}
transformed = Functions.setSRID(transformed, targetSRID);

// Preserve user data
transformed.setUserData(geometry.getUserData());

return transformed;
}

/**
* Extract EPSG code from a CRS string if it's in EPSG format.
*
* @param crs CRS string (may be EPSG code, WKT, PROJ string, etc.)
* @return EPSG code as integer, or null if not an EPSG code format
*/
private static Integer extractEpsgCode(String crs) {
if (crs == null || crs.isEmpty()) {
return null;
}

Matcher matcher = EPSG_PATTERN.matcher(crs.trim());
if (matcher.matches()) {
try {
return Integer.parseInt(matcher.group(1));
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
}
Loading
Loading