平面投影坐标转经纬度坐标

平面投影坐标转经纬度坐标

本人在进行开发时,使用geotools的GeoTiffReader类读取卫星影像的顶点坐标,得到的是平面坐标,单位是米,打开arcGIS,加载影像,右下角会有坐标,在视图->数据框属性->常规->单位,可以更改坐标显示单位,一开始默认单位是,我改成十进制度,如图:这里写图片描述
现想将平面坐标转化为经纬度,在网上看到一片博客:经纬度坐标和投影坐标的转换。参考该代码写类自己的,我的目的正好与博客写的相反。然后我就参考geotools-17的用户文档(
geotools/geotools-17.0-userguide/geotools-17.0/library/referencing/crs.html?highlight=transform
现摘取部分文档():
平面投影坐标转经纬度坐标_第1张图片
程序主要靠这俩方法:CRS.findMathTransform()方法定义转换的坐标系,JTS.transform()进行坐标的转换。

package Test02;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class CoordinateConversion02 {

	/**
	 * 平面坐标转经纬度
	 */
	public static double[] convert(double x, double y,CoordinateReferenceSystem crs) 
            throws FactoryException, MismatchedDimensionException, TransformException {
	Coordinate sourceCoord = new Coordinate(x, y);
    GeometryFactory geoFactory = new GeometryFactory();
    Point sourcePoint = geoFactory.createPoint(sourceCoord);
            
    MathTransform transform = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84, false);
    Point targetPoint = (Point) JTS.transform(sourcePoint, transform);
    double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
    return targetCoord;
	}
	/**
	 * 测试
	 */
	public static void main( String[] args ) throws Exception
    {
        double x = -132202.486;
        double y = 1657014.274;
        //第一种获取方式,imageFile为卫星影像的文件地址,目的是获取影像的投影
//        GeoTiffReader reader = new GeoTiffReader(imageFile);
//        GridCoverage2D coverage = reader.read(null);
//        //获取投影
//        CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
        //这是第二种获取方式(参考博文),
        final String wkt="PROJCS[\"unnamed\"," 
                                +"GEOGCS[\"WGS 84\"," 
                                      + "DATUM[\"World Geodetic System 1984\", "
                                         +"SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]," 
                                        + "AUTHORITY[\"EPSG\",\"6326\"]], "
                                     +  "PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]]," 
                                     + " UNIT[\"degree\", 0.017453292519943295], "
                                      + "AXIS[\"Geodetic longitude\", EAST], "
                                      +" AXIS[\"Geodetic latitude\", NORTH], "
                                      + "AUTHORITY[\"EPSG\",\"4326\"]], "
                                   + " PROJECTION[\"Albers_Conic_Equal_Area\"], "
                                   +  "PARAMETER[\"central_meridian\", 110.0], "
                                   + " PARAMETER[\"latitude_of_origin\", 12.0], "
                                    +" PARAMETER[\"standard_parallel_1\", 25.0], "
                                   +  "PARAMETER[\"false_easting\", 0.0], "
                                   +  "PARAMETER[\"false_northing\", 0.0]," 
                                    + "PARAMETER[\"standard_parallel_2\", 47.0]," 
                                    + "UNIT[\"m\", 1.0], "
                                    + "AXIS[\"Easting\", EAST], "
                                   +  "AXIS[\"Northing\", NORTH]]";
        CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
        double[] coordinate = convert(x, y,crs);
        System.out.println("X: " + coordinate[0] + ", Y: " + coordinate[1]);
    }
}
";

好了,一切完成,用arcGIS进行检验正确。代码还不完整,不断更新中。
更新(2018年1月1日):
当我把这个类移植到另一台电脑上并添加 gt-geotiff-17.0.jar后,恩,就添加了这一个jar包没报错,运行时报错:Exception in thread “main” java.lang.NoClassDefFoundError: javax/media/jai/I。个人觉得这是jar包不全所致,解决办法是先删除全部的import* 引用,然后一个一个的添加jar包,jar包源码在GitHub上有,有的方法名不止一个jar包,这个需特别注意。

本人原创,转载请附上本文连接。
欢迎技术交流。

你可能感兴趣的:(java,geotools,webgis,GIS)