在处理空间坐标时经常会遇到不同坐标系之间的转换,比如某些激光点云数据使用的坐标系为EPSG:32649,而我们需要读取成经纬度坐标(EPSG:4326),这时就可以借助于geotools工具进行不同坐标系的转换。

使用之前引入相关依赖:

<dependency>
	<groupId>org.locationtech.jts</groupId>
	<artifactId>jts-core</artifactId>
	<version>1.16.1</version>
</dependency>

这里需要注意的是我们坐标系是EPSG时,需要引入如下依赖(这个包借助hsqldb存储了相关坐标系的转换信息),不然会报错!

No code “EPSG:4326” from authority “EPSG” found for object of type “EngineeringCRS”.

<dependency>
	<groupId>org.geotools</groupId>
	<artifactId>gt-epsg-hsql</artifactId>
	<version>23.0</version>
</dependency>

坐标转换代码如下:

CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32649");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
Coordinate src = new Coordinate(x, y, z);
Coordinate dest = JTS.transform(src, null, transform);