java中JTS对空间数据Geometry进行坐标系投影转换

java中JTS对空间数据Geometry进行坐标系投影转换

代码:

/**
     * @Description: 用JTS对Geometry空间数据进行坐标系投影转换
     *
     * @Param:  [params]
     * @Return: Geometry
     * @Author yanghaoxing
     * @Date 2024/9/10 14:54
     */
    public Geometry getGeometryForlong(Geometry geometry) {
        String srcCrsCode = "EPSG:4490";
        String tarCrsCode = "EPSG:4522";

        CoordinateReferenceSystem sourceCRS = null;
        CoordinateReferenceSystem targetCRS = null;

        try{
            sourceCRS = CRS.decode(srcCrsCode, true);
            targetCRS = CRS.decode(tarCrsCode, true);
        }catch (FactoryException e){
            System.out.println(e);
        }

        Geometry reprojectGeometry = null;

        try {
            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
            reprojectGeometry = JTS.transform(geometry, transform);
        }catch (Exception e){
            System.out.println(e);
        }

        return reprojectGeometry;
    }

使用:

Geometry regionGeometry = null;
        try {
            regionGeometry = getGeometryForlong(reader.read(wkt));
        } catch (ParseException e) {
            e.printStackTrace();
        }

完结!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将Java投影坐标系转换为地理坐标系,可以使用Java的地理信息系统(GIS)库,如GeoTools或JTSJava Topology Suite)。下面是使用GeoTools库进行投影坐标系转换的简要步骤: 1. 首先,需要导入GeoTools库。可以使用Maven或手动下载并设置类路径。 2. 创建CoordinateReferenceSystem(CRS)对象,表示投影坐标系。可以使用EPSG(European Petroleum Survey Group)代码指定常用的投影坐标系,例如EPSG:4326代表WGS84地理坐标系。 3. 使用CRS类的静态方法findMathTransform(),通过源坐标系和目标坐标系创建MathTransform对象。MathTransform对象可用于执行实际的坐标转换。 4. 创建源投影坐标的Coordinate对象,指定x和y坐标值。 5. 调用MathTransform对象的transform()方法,将源坐标转换为目标坐标。 6. 创建目标地理坐标的Coordinate对象,获取转换后的经纬度值。 下面是使用GeoTools进行投影坐标系转换的示例代码: ```java import org.geotools.referencing.CRS; import org.geotools.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; public class CoordinateConversion { public static void main(String[] args) { try { // 创建投影坐标系对象 CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3857"); // 使用EPSG:3857作为投影坐标系 // 创建地理坐标系对象 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); // 使用EPSG:4326作为地理坐标系 // 创建坐标转换对象 MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS); // 创建源投影坐标 org.geotools.geometry.DirectPosition2D sourcePos = new org.geotools.geometry.DirectPosition2D(sourceCRS, 100, 200); // 使用100, 200作为源投影坐标的x和y值 // 执行坐标转换 org.geotools.geometry.DirectPosition2D targetPos = new org.geotools.geometry.DirectPosition2D(); transform.transform(sourcePos, targetPos); // 获取转换后的地理坐标 double longitude = targetPos.getOrdinate(0); // 经度 double latitude = targetPos.getOrdinate(1); // 纬度 System.out.println("转换后的地理坐标:(" + longitude + ", " + latitude + ")"); } catch (FactoryException | TransformException e) { e.printStackTrace(); } } } ``` 这是使用GeoTools库将Java投影坐标系转换为地理坐标系的简要示例。实际应用,还应根据具体的数据和需求进行适当的调整和错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星月前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值