GeoTools WKT坐标系转换

参考文章:
java geotools 坐标转换
Maven中GeoTools的引入 - Maven 的 repository 与 mirror的关系

背景

使用geotools 对WKT字符串进行坐标系的转换。

pom包引入

  <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>
    <!-- gt-epsg-hsql 这个是epsgcode的,一定要引入,不然会出现坐标系找不到的问题-->
     <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-epsg-hsql</artifactId>
            <version>20.0</version>
        </dependency>
	<dependency>
         <groupId>org.geotools</groupId>
         <artifactId>gt-referencing</artifactId>
         <version>20.0</version>
     </dependency>
     <dependency>
         <groupId>org.geotools</groupId>
         <artifactId>gt-api</artifactId>
         <version>20.0</version>
     </dependency>
代码实现
/**
     * 单个geom转换
     * geotools的安装包中的x,y坐标是反着的,因此,直接用geomtry转换,导出的数据是错误,先的将
     * x,y的坐标换过来。所以,我选择拼装geomtry。
     * @param geom
     * @return
     */
    public static Geometry geometryTransform2(Geometry geom, int sourceEPSGCode,int targetEPSGCode ){

        try{
            //CoordinateReferenceSystem的生成方法有两种,EPSG和WKT,然而,我发现把我需要的坐标系的prj文件中的内容用来转换会报“不存在。。。。什么这样的。。”
            //所以采用EPSG,即使使用了EPSG,导出的转换后的投影文件的prj仍然不对,arcgis 无法识别。当然,同一个坐标系下的prj 文件的内容是相同的,用别的替换即可。
            CoordinateReferenceSystem crsresource = CRS.decode("EPSG:"+ epsgCode);
            CoordinateReferenceSystem crsTarget = CRS.decode("EPSG:"+ targetEPSGCode);
            // 投影转换
            MathTransform transform = CRS.findMathTransform(crsresource, crsTarget,true);
            String wktString = geom.toString();
            log.debug("转换前的WKT字符串:" + wktString);
            //将WKTString中的坐标进行正则匹配,投影转换后再将字符串中对应的坐标给替换了
            Pattern pattern = compile("[1-9]\\d*\\.?\\d*\\s[1-9]\\d*\\.?\\d*");
            Matcher matcher = pattern.matcher(wktString);
            while (matcher.find()){
                String cordStr = matcher.group();
                String[] cord = cordStr.split("\\s+");
                double x= Double.parseDouble(cord[0]);
                double y= Double.parseDouble(cord[1]);
                //使用coordinate时,产生的结果dest中的x坐标的位数是科学计数,所以不建议采用
                /*Coordinate source = new Coordinate(y, x);
                Coordinate dest = new Coordinate();
                JTS.transform(source,dest, transform);
                String[] str = dest.toString().substring(1, dest.toString().length() - 1).split(",");*/
                String point="POINT(" + y + " " + x + ")";
                Geometry pointGeom = reader.read(point);
                Geometry resGeom = JTS.transform(pointGeom, transform);
                String[] str = resGeom.toString().substring(resGeom.toString().lastIndexOf("(")+1, resGeom.toString().length() - 1).split("\\s+");
                String cordStr2 = str[1]+" "+str[0];
                wktString = wktString.replaceAll(cordStr,cordStr2);
            }
            log.debug("转换后的WKT字符串:" + wktString);
            Geometry read = reader.read(wktString);
            return read;
        } catch (Exception e) {
            log.debug("坐标系转换出错:" + e.getMessage());
            return null;
        }
    }
测试
private static WKTReader reader = new WKTReader();

public static void main(String[] args) {
	 String wktStr = "POLYGON((40535321.97750524 3276858.489317663,40534838.475054964 3276742.437636584,40534885.506722644 3276050.3168770154,40535410.02098725 3276621.3582205614,40535321.97750524 3276858.489317663))";
	 Geometry geometry = reader.read(wktStr);
     Geometry targetGeometry = WKTUtil.geometryTransform2(geometry,4528,4326);
     System.out.println(targetGeometry.toString());
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值