1. 添加依赖
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>24.1</version>
</dependency>
  1. 创建投影转换对象
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.opengis.geometry.coordinate.*;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.opengis.referencing.operation.TransformFactory;

public class CoordinateTransformUtil {

    /**
     * 创建投影转换对象
     */
    public static MathTransform createTransform(String sourceCrs, String targetCrs) throws Exception {
        // 创建源坐标参考系对象
        CoordinateReferenceSystem sourceCRS = CRS.decode(sourceCrs);
        // 创建目标坐标参考系对象
        CoordinateReferenceSystem targetCRS = CRS.decode(targetCrs);
        // 获取转换工厂对象
        TransformFactory factory = CRS.getAuthorityFactory(true);
        // 创建投影转换对象
        MathTransform transform = factory.createTransform(sourceCRS, targetCRS);
        return transform;
    }
}
  1. 坐标转换
import org.geotools.geometry.jts.JTS;
import org.opengis.geometry.coordinate.*;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

public class CoordinateTransformUtil {

    /**
     * 坐标转换
     */
    public static Coordinate[] transform(Coordinate[] coords, MathTransform transform) throws TransformException {
        // 创建空的目标坐标数组
        Coordinate[] targetCoords = new Coordinate[coords.length];
        // 循环转换每个坐标点
        for (int i = 0; i < coords.length; i++) {
            // 创建源坐标对象
            DirectPosition sourcePosition = new DirectPosition2D(coords[i].getX(), coords[i].getY());
            // 创建目标坐标对象
            DirectPosition targetPosition = new DirectPosition2D();
            // 转换坐标
            transform.transform(sourcePosition, targetPosition);
            // 将目标坐标保存到目标坐标数组中
            targetCoords[i] = new Coordinate(targetPosition.getOrdinate(0), targetPosition.getOrdinate(1));
        }
        return targetCoords;
    }
}
  1. 使用示例
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.referencing.operation.MathTransform;

import java.util.Arrays;

public class App {

    public static void main(String[] args) throws Exception {
        // 创建源坐标对象
        String wkt = "POINT (116.402 39.917)";
        Point sourcePoint = createPoint(wkt);
        // 创建源坐标数组
        Coordinate[] sourceCoords = sourcePoint.getCoordinates();
        // 创建投影转换对象
        String sourceCrs = "EPSG:4326";
        String targetCrs = "EPSG:3857";
        MathTransform transform = CoordinateTransformUtil.createTransform(sourceCrs, targetCrs);
        // 坐标转换
        Coordinate[] targetCoords = CoordinateTransformUtil.transform(sourceCoords, transform);
        // 创建目标坐标对象
        Point targetPoint = createPoint("POINT (" + targetCoords[0].getX() + " " + targetCoords[0].getY() + ")");
        // 输出结果
        System.out.println("源坐标:" + sourcePoint);
        System.out.println("目标坐标:" + targetPoint);
    }

    /**
     * 创建点对象
     */
    public static Point createPoint(String wkt) throws ParseException {
        WKTReader reader = new WKTReader();
        Geometry geometry = reader.read(wkt);
        if (geometry instanceof Point) {
            return (Point) geometry;
        } else if (geometry instanceof Polygon) {
            return ((Polygon) geometry).getCentroid();
        } else {
            throw new IllegalArgumentException("Geometry type not supported: " + geometry.getGeometryType());
        }
    }
}
``
java使用geotools迭代仿射变换实现坐标转换

原文地址: http://www.cveoy.top/t/topic/fI7z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录