This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch remove-proj4-epsg
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git

commit 86361aab4c170282c0de3ca6fff5104e2d25400f
Author: Bertil Chapuis <[email protected]>
AuthorDate: Fri Mar 1 22:47:55 2024 +0100

    Remove the dependency to proj4j-epsg
---
 baremaps-core/pom.xml                              |  5 --
 .../java/org/apache/baremaps/utils/CRSUtils.java   | 53 ++++++++++++++++++++++
 .../org/apache/baremaps/utils/GeometryUtils.java   |  6 +--
 .../baremaps/utils/ProjectionTransformer.java      |  3 +-
 baremaps-core/src/main/resources/proj4/nad/srid    |  4 ++
 .../EntityDataTypeGeometryBuilderTest.java         |  3 +-
 pom.xml                                            |  5 --
 7 files changed, 62 insertions(+), 17 deletions(-)

diff --git a/baremaps-core/pom.xml b/baremaps-core/pom.xml
index 5552c114..accf8138 100644
--- a/baremaps-core/pom.xml
+++ b/baremaps-core/pom.xml
@@ -118,11 +118,6 @@ limitations under the License.
       <groupId>org.locationtech.proj4j</groupId>
       <artifactId>proj4j</artifactId>
     </dependency>
-    <!-- TODO: Remove this dependency due to license incompatibility -->
-    <dependency>
-      <groupId>org.locationtech.proj4j</groupId>
-      <artifactId>proj4j-epsg</artifactId>
-    </dependency>
     <dependency>
       <groupId>org.postgresql</groupId>
       <artifactId>postgresql</artifactId>
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/utils/CRSUtils.java 
b/baremaps-core/src/main/java/org/apache/baremaps/utils/CRSUtils.java
new file mode 100644
index 00000000..a29ea0d9
--- /dev/null
+++ b/baremaps-core/src/main/java/org/apache/baremaps/utils/CRSUtils.java
@@ -0,0 +1,53 @@
+/*
+ * 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.baremaps.utils;
+
+import org.locationtech.proj4j.CRSFactory;
+import org.locationtech.proj4j.CoordinateReferenceSystem;
+import org.locationtech.proj4j.UnknownAuthorityCodeException;
+
+/**
+ * Utility methods for creating coordinate reference systems.
+ */
+public class CRSUtils {
+
+  /**
+   * Creates a coordinate reference system from the provided SRID.
+   * <p>
+   * This method first looks into the minimal set of authority codes stored in 
the proj4/nad/srid
+   * resource file.
+   * <p>
+   * If the authority code is not found, it tries to create a CRS from the 
EPSG code that may be
+   * stored in the proj4/nad/epsg resource file of the proj4j-epsg module.
+   * <p>
+   * The proj4j-epsg module is not included in baremaps by default due to 
licensing issues. It can
+   * be added to the classpath to enable the creation of CRS from EPSG codes.
+   *
+   * @param srid the SRID
+   * @return the coordinate reference system
+   */
+  public static CoordinateReferenceSystem createFromSrid(int srid) {
+    var factory = new CRSFactory();
+    try {
+
+      return factory.createFromName(String.format("SRID:%s", srid));
+    } catch (UnknownAuthorityCodeException e) {
+      return factory.createFromName(String.format("EPSG:%s", srid));
+    }
+  }
+}
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java 
b/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java
index 58da9902..96ebd207 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/utils/GeometryUtils.java
@@ -77,10 +77,8 @@ public class GeometryUtils {
    */
   public static CoordinateTransform coordinateTransform(Integer sourceSrid, 
Integer targetSrid) {
     CRSFactory crsFactory = new CRSFactory();
-    CoordinateReferenceSystem sourceCRS =
-        crsFactory.createFromName(String.format("EPSG:%d", sourceSrid));
-    CoordinateReferenceSystem targetCRS =
-        crsFactory.createFromName(String.format("EPSG:%d", targetSrid));
+    CoordinateReferenceSystem sourceCRS = CRSUtils.createFromSrid(sourceSrid);
+    CoordinateReferenceSystem targetCRS = CRSUtils.createFromSrid(targetSrid);
     CoordinateTransformFactory coordinateTransformFactory = new 
CoordinateTransformFactory();
     return coordinateTransformFactory.createTransform(sourceCRS, targetCRS);
   }
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/utils/ProjectionTransformer.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/utils/ProjectionTransformer.java
index 01d14823..5ca14833 100644
--- 
a/baremaps-core/src/main/java/org/apache/baremaps/utils/ProjectionTransformer.java
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/utils/ProjectionTransformer.java
@@ -24,7 +24,6 @@ import java.util.stream.Stream;
 import org.locationtech.jts.geom.*;
 import org.locationtech.jts.geom.impl.CoordinateArraySequence;
 import org.locationtech.jts.geom.util.GeometryTransformer;
-import org.locationtech.proj4j.CRSFactory;
 import org.locationtech.proj4j.CoordinateTransform;
 import org.locationtech.proj4j.ProjCoordinate;
 import org.slf4j.Logger;
@@ -56,7 +55,7 @@ public class ProjectionTransformer extends 
GeometryTransformer {
     this.targetSrid = targetSrid;
     this.transform = GeometryUtils.coordinateTransform(sourceSrid, targetSrid);
 
-    var targetCRS = new CRSFactory().createFromName(String.format("EPSG:%s", 
targetSrid));
+    var targetCRS = CRSUtils.createFromSrid(targetSrid);
     var lonlatTranform = GeometryUtils.coordinateTransform(4326, sourceSrid);
     min = lonlatTranform
         .transform(new 
ProjCoordinate(Math.toDegrees(targetCRS.getProjection().getMinLongitude()),
diff --git a/baremaps-core/src/main/resources/proj4/nad/srid 
b/baremaps-core/src/main/resources/proj4/nad/srid
new file mode 100644
index 00000000..d0dc9b25
--- /dev/null
+++ b/baremaps-core/src/main/resources/proj4/nad/srid
@@ -0,0 +1,4 @@
+# WGS 84
+<4326> +proj=longlat +datum=WGS84 +no_defs  <>
+# WGS 84 / Pseudo-Mercator
+<3857> +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 
+k=1.0 +units=m +nadgrids=@null +wktext +no_defs  <>
\ No newline at end of file
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/geometry/EntityDataTypeGeometryBuilderTest.java
 
b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/geometry/EntityDataTypeGeometryBuilderTest.java
index 182775d4..c9fb84f3 100644
--- 
a/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/geometry/EntityDataTypeGeometryBuilderTest.java
+++ 
b/baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/geometry/EntityDataTypeGeometryBuilderTest.java
@@ -34,6 +34,7 @@ import org.apache.baremaps.openstreetmap.model.Node;
 import org.apache.baremaps.openstreetmap.model.Relation;
 import org.apache.baremaps.openstreetmap.model.Way;
 import org.apache.baremaps.openstreetmap.store.MockDataMap;
+import org.apache.baremaps.utils.CRSUtils;
 import org.junit.jupiter.api.Test;
 import org.locationtech.jts.geom.Coordinate;
 import org.locationtech.jts.geom.GeometryFactory;
@@ -52,7 +53,7 @@ class EntityDataTypeGeometryBuilderTest {
 
   static final CRSFactory CRS_FACTORY = new CRSFactory();
 
-  static final CoordinateReferenceSystem EPSG_4326 = 
CRS_FACTORY.createFromName("EPSG:4326");
+  static final CoordinateReferenceSystem EPSG_4326 = 
CRSUtils.createFromSrid(4326);
 
   static final CoordinateTransform COORDINATE_TRANSFORM = new 
CoordinateTransform() {
     @Override
diff --git a/pom.xml b/pom.xml
index d68733bd..ad5ac0ae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -356,11 +356,6 @@ limitations under the License.
         <artifactId>proj4j</artifactId>
         <version>${version.lib.proj4j}</version>
       </dependency>
-      <dependency>
-        <groupId>org.locationtech.proj4j</groupId>
-        <artifactId>proj4j-epsg</artifactId>
-        <version>${version.lib.proj4j}</version>
-      </dependency>
       <dependency>
         <groupId>org.postgresql</groupId>
         <artifactId>postgresql</artifactId>

Reply via email to