栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用java geotools进行坐标转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用java geotools进行坐标转换

java geotools支持不同的坐标系之间进行转换,只需要转换的时候指定坐标系即可。

pom依赖文件如下:



    4.0.0
    cr.cdrb
    geo1
    1.0-SNAPSHOT
    jar
    
        UTF-8
        1.8
        1.8
        20.0
    
    
       

        
            osgeo
            OSGeo Release Repository
            https://repo.osgeo.org/repository/release/
            false
            true
        
    
    

        
            org.geotools
            gt-api
            ${geotools.version}
            
                
                    javax.media
                    jai_core
                
                
                    jgridshift
                    jgridshift
                
                
                    commons-pool
                    commons-pool
                
                
                    net.sf.geographiclib
                    GeographicLib-Java
                
            
        

        
            org.geotools
            gt-epsg-hsql
            ${geotools.version}
        
    

如果依赖下不下来,也可以去仓库自行下载,地址:Nexus Repository Manager

JAVA代码:

package geo1;

import org.locationtech.jts.geom.Coordinate;
import org.geotools.geometry.jts.JTS;

import org.geotools.referencing.CRS;

import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MyGeotools {
    public static void main(String[] args) {
        //某点的度分秒格式的经纬度,如果经纬度本身就是double格式的,就不需要再行计算
        Double y = Double.parseDouble(getPosition("32°11′38.977458″"));//纬度
        Double x = Double.parseDouble(getPosition("119°31′06.853758″"));//经度
        Double[] coordinate = getCoordinate(x, y);
        System.out.println("Y:"+coordinate[0] + " " + "X:"+coordinate[1]);

    }

    //坐标转换
    public static Double[] getCoordinate(Double x, Double y) {
        Double[] res = new Double[2];
        Coordinate tar = null;
        try {
            //封装点,这个是通用的,也可以用POINT(y,x)
            // private static WKTReader reader = new WKTReader( geometryFactory );
            Coordinate sour = new Coordinate(y, x);
            //这里要选择转换的坐标系是可以随意更换的
            CoordinateReferenceSystem source = CRS.decode("EPSG:4326");
            CoordinateReferenceSystem target = CRS.decode("EPSG:4549");
            //建立转换,下面两个我屏掉的转换方式会报出需要3/7参数的异常
            // MathTransform mathTransform = CRS.findMathTransform(source, target);
            //MathTransform mathTransform1 = CRS.findMathTransform(source, target, false);
            MathTransform transform = CRS.findMathTransform(source, target, true);
            tar = new Coordinate();
            //转换
            JTS.transform(sour, tar, transform);
        } catch (FactoryException | org.opengis.referencing.operation.TransformException e) {
            e.printStackTrace();
        }
        String[] split = (tar.toString().substring(1, tar.toString().length() - 1)).split(",");
        //经纬度精度
        DecimalFormat fm = new DecimalFormat("0.0000000");
        res[0] = Double.valueOf(fm.format(Double.valueOf(split[0])));
        res[1] = Double.valueOf(fm.format(Double.valueOf(split[1])));
        return res;
    }

    private static String getPosition(String position){
        String[] degree = position.split("\°");
        if(degree.length == 1){
            return position;
        }
        String d = degree[0];
        String[] one = degree[1].split("\′");
        String a = one[0];
        if(!a.contains(".")){
            if(a.substring(0,1).equals("0")){
                a = a.substring(1);
            }
        }else if(a.contains(".") && a.split("\.").length > 1){
            a = a.substring(1);
        }
        String[] two = one[1].split("\″");
        String b= two[0];
        if(!b.contains(".")){
            if(b.substring(0,1).equals("0")){
                b = b.substring(1);
            }
        }else if(b.contains(".") && b.split("\.").length > 1){
            if(b.substring(0,1).equals("0")){
                b = b.substring(1);
            }
        }
        BigDecimal fen = new BigDecimal(a);
        BigDecimal miao = new BigDecimal(b);
        BigDecimal du = new BigDecimal(d);
//		float f = Float.valueOf(a)+ Float.valueOf(Float.valueOf(b)/60);
//		float du = Float.valueOf(f/60)+Float.valueOf(d);
        BigDecimal add = fen.add(miao.divide(new BigDecimal("60"),6, BigDecimal.ROUND_HALF_UP)).divide(new BigDecimal("60"),6, BigDecimal.ROUND_HALF_UP).add(du);
        return String.valueOf(add);
    }
}

结果如图:

注意,如果运行的时候报错如下:

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

那就是查找坐标系的数据库依赖没有引入,引入就可以了:

 
            org.geotools
            gt-epsg-hsql
            ${geotools.version}
        

 参考文档:

java geotools 坐标转换_不知工匠的博客-CSDN博客

No code "EPSG:4490" from authority "EPSG" found for object of type "EngineeringCRS".解决方案_徐先森的博客-CSDN博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号