当前位置: 代码迷 >> 综合 >> java geotools 坐标转换
  详细解决方案

java geotools 坐标转换

热度:60   发布时间:2024-02-08 14:49:13.0

这里写自定义目录标题

  • Java geotools 坐标转换
    • 点坐标转换
      • pom文件
      • 代码
    • shp坐标系转换

Java geotools 坐标转换

点坐标转换

我点坐标转换为4326到3857,虽然我平时都是4546到3857,都一样。主要时因为我手里没有4546的坐标,

pom文件

这个geotools的依赖比较“皮干”,要是下载不成功,就去官网geotools下的quickstart 的Mavenquick quickstart下面找仓库地址。

  <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><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>

代码

package com.wangkang.gts4vect;import org.geotools.geometry.jts.JTS;import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;import java.text.DecimalFormat;public class MyGeotools {public static void main(String[] args) {
// GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
// WKTReader reader = new WKTReader( geometryFactory );//某点的经纬度Double y = 34.26095856;Double x = 108.94237668;Double[] coordinate = getCoordinate(x, y);System.out.println(coordinate[0] + " " + 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:3857");//建立转换,下面两个我屏掉的转换方式会报出需要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;}
}

结果如下
在这里插入图片描述

在这个转换类中,x,y 的位置调换了,这个是我们国家和国外的差异,约定成俗的东西,记住就好,为什么呢?因为不记住接下来的shp坐标系转换图像就飘了,飘到。。。。
如果说整个科学技术不好看,好吧,我再换种写法

package com.wangkang.gts4vect;import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;import java.text.DecimalFormat;public class MyGeotools2 {static  GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();static  WKTReader reader = new WKTReader( geometryFactory );public static void main(String[] args) {//某点的经纬度Double y=34.26095856;Double x=108.94237668;try {//封装点,x,y 要反过来,不反过来就报纬度超过90的异常String point="POINT("+y+" "+x+")";Geometry geometry = reader.read(point);//这里要选择转换的坐标系是可以随意更换的CoordinateReferenceSystem source = CRS.decode("EPSG:4326");CoordinateReferenceSystem target = CRS.decode("EPSG:3857");//建立转换,下面两个我屏掉的转换方式会报出需要3/7参数的异常// MathTransform mathTransform = CRS.findMathTransform(source, target);//MathTransform mathTransform1 = CRS.findMathTransform(source, target, false);MathTransform transform = CRS.findMathTransform(source, target,true);//转换Geometry transform1 = JTS.transform(geometry, transform);System.out.println(transform1);} catch (FactoryException | TransformException | ParseException e) {e.printStackTrace();}}}

结果如下
在这里插入图片描述
其实整个转化就两句代码
MathTransform transform = CRS.findMathTransform(source, target,true);
Geometry transform1 = JTS.transform(geometry, transform);
就这两句。

shp坐标系转换

下回写。
如果有兄弟看我代码,有垂足顿胸欲掐死我后快而不得,难以却心头之气者,莫慌,别伤了身体。第一次写博客,处女作,多担待,谢谢。

  相关解决方案