Java WGS84 to GCJ-02 Coordinate Conversion with GeoTools - Code Example
Java WGS84 to GCJ-02 Coordinate Conversion with GeoTools
This article provides a comprehensive guide on transforming WGS84 coordinates to GCJ-02 coordinates in Java using the GeoTools library. We will walk through a complete code example and provide the essential Maven dependencies for a smooth implementation.
Using GeoTools for Coordinate Conversion
GeoTools is a powerful Java library designed for geospatial data handling, including coordinate transformations. It simplifies the process of converting between different coordinate reference systems, such as WGS84 and GCJ-02.
Code Example
import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.referencing.CRS; import org.geotools.referencing.ReferencingFactoryFinder; import org.opengis.geometry.DirectPosition; import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; public class CoordinateTransform { public static void main(String[] args) { GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); // Define WGS84 Coordinate System CoordinateReferenceSystem wgs84Crs = null; try { wgs84Crs = CRS.decode("EPSG:4326", true); } catch (FactoryException e) { e.printStackTrace(); } // Define GCJ-02 Coordinate System CoordinateReferenceSystem gcj02Crs = null; try { gcj02Crs = CRS.decode("EPSG:4490", true); } catch (FactoryException e) { e.printStackTrace(); } // Define Coordinate Point Coordinate coordinate = new Coordinate(116.404, 39.915); Geometry point = geometryFactory.createPoint(coordinate); // Perform Coordinate Conversion MathTransform transform = null; try { transform = CRS.findMathTransform(wgs84Crs, gcj02Crs, true); } catch (FactoryException e) { e.printStackTrace(); } Geometry transformedPoint = null; try { transformedPoint = JTS.transform(point, transform); } catch (TransformException e) { e.printStackTrace(); } // Output the Transformed Coordinates System.out.println("WGS84: " + point); System.out.println("GCJ-02: " + transformedPoint); } } Maven Dependencies
To utilize GeoTools in your Java project, add the following dependencies to your pom.xml file:
<dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geometry</artifactId> <version>24.0</version> </dependency> <dependency> <groupId>org.locationtech.jts</groupId> <artifactId>jts-core</artifactId> <version>1.18.0</version> </dependency>
Remember to adjust the versions of GeoTools and JTS based on your project requirements.
Conclusion
This guide has demonstrated how to seamlessly convert WGS84 coordinates to GCJ-02 coordinates in Java using the GeoTools library. By utilizing this robust and versatile library, you can confidently handle coordinate transformations and other geospatial operations in your Java applications.
原文地址: https://www.cveoy.top/t/topic/qrOl 著作权归作者所有。请勿转载和采集!