转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));
}