geotools实现坐标系转换

转WGS84坐标系

 坐标网址:Beijing 1954 / Gauss-Kruger 21N - EPSG:21481

下面写的 String c2 里面的东西就是在官网里面找的,,我用的是这个21481,但是手动修改了一下,如果需要别的也可以去官网找,里面都有

不需要修改的,可以直接使用官网提供的EPSG:xxxx

所需依赖:

 <!--geotools-->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-opengis</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <!--EOSG格式-->
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-extension</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-wkt</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.gdal</groupId>
            <artifactId>gdal</artifactId>
            <version>3.1.0</version>
        </dependency>

import org.geotools.referencing.CRS;
import org.locationtech.jts.io.ParseException;
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 GeoUtil {

    public Coordinate getWGS84Yt(double srcLon, double srcLat) throws FactoryException, TransformException, ParseException {

        String sourceCRS = "EPSG:4326";
        String c2 ="PROJCS[\"unnamed\",\n" +
                "    GEOGCS[\"GRS 1980(IUGG, 1980)\",\n" +
                "        DATUM[\"unknown\",\n" +
                "            SPHEROID[\"GRS80\",6378137,298.2572221010002,\n" +
                "                AUTHORITY[\"EPSG\",\"7019\"]],\n" +
                "            AUTHORITY[\"EPSG\",\"6269\"]],\n" +
                "        PRIMEM[\"Greenwich\",0],\n" +
                "        UNIT[\"degree\",0.0174532925199433],\n" +
                "        AXIS[\"Geodetic longitude\",EAST],\n" +
                "        AXIS[\"Geodetic latitude\",NORTH],\n" +
                "        AUTHORITY[\"EPSG\",\"4269\"]],\n" +
                "    PROJECTION[\"Transverse_Mercator\"],\n" +
                "    PARAMETER[\"latitude_of_origin\",0],\n" +
                "    PARAMETER[\"central_meridian\",120],\n" +
                "    PARAMETER[\"scale_factor\",1],\n" +
                "    PARAMETER[\"false_easting\",499040],\n" +
                "    PARAMETER[\"false_northing\",-720],\n" +
                "    UNIT[\"meters\",1],\n" +
                "    AXIS[\"Easting\",EAST],\n" +
                "    AXIS[\"Northing\",NORTH]]";
        CoordinateReferenceSystem sourceCRSObj = CRS.decode(sourceCRS);
        CoordinateReferenceSystem targetCRSObj = CRS.parseWKT(c2);
/*        CoordinateReferenceSystem targetCRSObj = CRS.decode("EPSG:4549");*/
        // 定义待转换坐标点
/*        double srcLon = 37.42318; //x
        double srcLat = 121.249782; //y*/
        double[] srcCoords = new double[]{srcLat, srcLon};


        MathTransform transform = CRS.findMathTransform(sourceCRSObj, targetCRSObj, true);
        double[] tgtCoords = new double[2];
        transform.transform(srcCoords, 0, tgtCoords, 0, 1);

        // 打印转换结果
/*        System.out.println("Source Coordinates: " + srcLon + ", " + srcLat);
        System.out.println("Target Coordinates: " + tgtCoords[0] + ", " + tgtCoords[1]);*/
        return new Coordinate(tgtCoords[0], tgtCoords[1]);
    }

调用:

  @PostMapping("/getWGS84Yt")
    public Result<Coordinate> getWGS84Yt(@RequestParam("lon") double lon,
                                         @RequestParam("lat") double lat) throws FactoryException, ParseException, TransformException {
        return Result.success(new GeoUtil().getWGS84Yt(lon, lat));
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答有误。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代码。 希望这可以帮助到你。再次对之前的回答错误表示抱歉。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值