Java+GeoTools实现WKT数据根据EPSG编码进行坐标系转换

场景

Java+GeoTools(开源的Java GIS工具包)快速入门-实现读取shp文件并显示:

Java+GeoTools(开源的Java GIS工具包)快速入门-实现读取shp文件并显示_霸道流氓气质的博客-CSDN博客

在上面实现Java中集成Geotools之后,需求是将WKT数据转换成其他坐标系的WKT。

比如说将EPSG:4524的坐标系转换成EPSG:2334的坐标系数据。

当然如果是数据量较少,可以直接从WKT中复制出来单个点的数据在EPSG的官网进行转换。

EPSG.io: Coordinate Systems Worldwide

但是如果数据量较大,需要通过代码遍历的方式去转换大量数据。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主

实现

1、用到ESPG的转换需要添加依赖

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

否则会提示:

No code "EPSG:4524" from authority "EPSG" found for object of type "EngineeringCRS"

上面也讲过需要设置geotools的仓库,所以完整的pom需要添加的内容

        <!-- GeoTools begin-->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-shapefile</artifactId>
            <version>24-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-swing</artifactId>
            <version>24-SNAPSHOT</version>
        </dependency>
        <!-- GeoTools epsg  need -->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>24-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>osgeo</id>
            <name>OSGeo Release Repository</name>
            <url>https://repo.osgeo.org/repository/release/</url>
            <snapshots><enabled>false</enabled></snapshots>
            <releases><enabled>true</enabled></releases>
        </repository>
        <repository>
            <id>osgeo-snapshot</id>
            <name>OSGeo Snapshot Repository</name>
            <url>https://repo.osgeo.org/repository/snapshot/</url>
            <snapshots><enabled>true</enabled></snapshots>
            <releases><enabled>false</enabled></releases>
        </repository>
    </repositories>

    <!-- GeoTools end-->

2、然后新建类

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

public class WKTTransform {
    public static void main(String[] args) throws ParseException, FactoryException, TransformException {

        //要转换的wkt
        String oldWKT = "LINESTRING (37360817.569479 5127237.510467304, 37360830.13825466 5127118.315033647)";
        WKTReader wktReader = new WKTReader();
        //读取wkt为Geometry 几何对象
        Geometry oldGeometry = wktReader.read(oldWKT);
        //获取CRS权威工厂
        CRSAuthorityFactory crsAuthorityFactory = CRS.getAuthorityFactory(true);
        //创建sourceCRS
        CoordinateReferenceSystem sourceCRS = crsAuthorityFactory.createCoordinateReferenceSystem("EPSG:4524");
        //创建targetCRS
        CoordinateReferenceSystem targetCRS = crsAuthorityFactory.createCoordinateReferenceSystem("EPSG:2334");
        //获取MathTransform
        MathTransform mathTransform = CRS.findMathTransform(sourceCRS, targetCRS, true);
        //转换
        Geometry transform = JTS.transform(oldGeometry, mathTransform);
        //Geometry几何对象转换为WKT
        String newWKT = new WKTWriter().write(transform);
        System.out.println(newWKT);

    }
}

详细说明见代码实现。

相关api参考官方api文档

Overview (Geotools modules 30-SNAPSHOT API)

运行代码输出结果

注意这里转换的数据,将同样的坐标在EPSG网站上转换后对比

发现会存在0.00级别的误差数据。

3、如果以上0.00级别的误差都不能容忍的话,可以采用以下方式。

看一下epsg.io官网坐标系转换的接口,发现是无需任何鉴权,比如上面的转换对应的是get请求。

https://epsg.io/srs/transform/37360817.569479,5127237.510467304.json?key=default&s_srs=4524&t_srs=2334

所以另一种方式就是从WKT获取所有坐标,然后调用上面espg.io的接口进行转换,然后再将转换后的数据转成WKT。

具体流程自行实现。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
非常抱歉,我之前的回答有误。Geotools确实不支持直接的投影转换EPSG:4479坐标系EPSG:4479代表了ETRS89地理坐标系,它使用欧洲地面参考系统1989(ETRS89)作为基准。在Geotools中,大多数投影转换都是基于投影坐标系,而不是地理坐标系。 如果你希望进行EPSG:4479相关的操作,可能需要考虑以下两种方式: 1. 将你的数据从投影坐标系转换EPSG:4326(WGS84)地理坐标系,然后再进行EPSG:4479的转换。这可以通过使用Geotools中的投影转换工具来实现。 2. 考虑使用其他的GIS库或工具,如GDAL(Geospatial Data Abstraction Library),它提供了更广泛的坐标系支持,包括对EPSG:4479的转换。 请注意,无论你选择哪种方式,确保你有正确的Bursa-Wolf参数(七参数或三参数)用于进行转换。这些参数通常用于处理不同基准之间的差异。 对于EPSG:4479,下面是一个示例使用Geotools进行从投影坐标系(如EPSG:3857)到EPSG:4479地理坐标系转换代码: ```java CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3857"); CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4479"); MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true); DirectPosition2D sourcePos = new DirectPosition2D(sourceCRS, x, y); DirectPosition2D targetPos = new DirectPosition2D(); transform.transform(sourcePos, targetPos); double targetX = targetPos.getX(); double targetY = targetPos.getY(); ``` 请注意,以上示例中的转换是从EPSG:3857投影坐标系EPSG:4479地理坐标系。确保你根据实际情况调整源和目标坐标系EPSG代码。 希望这可以帮助到你。再次对之前的回答错误表示抱歉。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

霸道流氓气质

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

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

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

打赏作者

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

抵扣说明:

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

余额充值