GeoTools是在java下的gis开源软件,以下介绍坐标转换的两种方法:投影转换和仿射变换
投影转换
这里以xian80经纬度坐标转xian80,3度分带 111中央经线平面坐标为例
转换函数如下:
1 Point pointXian80 = projectTransform(lon, lat, "EPSG:4610", "EPSG:2382");
1 /**
2 * 投影转换, lon=经度,lat=纬度,ESPG格式(例):EPSG:46103 */
4 public static Point projectTransform(double lon, doublelat,5 String epsgSource, String epsgTarget) throwsFactoryException,6 MismatchedDimensionException, TransformException {7 //原始坐标点8 //PS:通常逻辑上理解经度应该是横坐标x,纬度是y,可是这里经度要填到y,纬度x,否则会报错
9 Point sourcePoint =JtsHelper.createPoint(lat, lon);10
11 //定义转换前和转换后的投影,可以用ESPG或者wkt12 //"PROJCS[\"Xian_1980_3_Degree_GK_CM_111E\",GEOGCS[\"GCS_Xian_1980\",DATUM[\"D_Xian_1980\",SPHEROID[\"Xian_1980\",6378140.0,298.257]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",111.0],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]";13 //CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
14 CoordinateReferenceSystem crsSource =CRS.decode(epsgSource);15 CoordinateReferenceSystem crsTarget =CRS.decode(epsgTarget);16 //投影转换
17 MathTransform transform =CRS.findMathTransform(crsSource, crsTarget);18 Point pointTarget =(Point) JTS.transform(sourcePoint, transform);19
20 returnpointTarget;21 }
关于定义坐标系的说明,GeoTools中定义坐标系有两种方法,一是坐标系的wkt,二是ESPG
坐标系的wkt可以从arcgis的prj文件中过去,用记事本打开prj文件,里面的内容就是该坐标系的wkt格式内容,例如:PROJCS[\"Xian_1980_3_Degree_GK_CM_111E\",GEOGCS[\"GCS_Xian_1980\",DATUM[\"D_Xian_1980\",SPHEROID[\"Xian_1980\",6378140.0,298.257]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Gauss_Kruger\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",111.0],PARAMETER[\"Scale_Factor\",1.0],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]
获取到wkt后,可以通过 CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator); 获取到坐标系对象
另一种方法是ESPG,意思是每一个坐标系都有一个ESPG标准的号码,查询坐标系的ESPG可通过以下网站 外链网址已屏蔽
PS:当转换的两种坐标系的datum不同,会报错Bursa-Wolf parameters,暂时未解决
仿射变换
代码如下:
首先是输入三组参考点,前三个是转换前,后三个是转换后
最后结果是pointReuslt
1 //参考点坐标
2 Coordinate s1 = new Coordinate(429275.549, 2801455.153);3 Coordinate s2 = new Coordinate(428110.626, 2792148.620);4 Coordinate s3 = new Coordinate(428966.479, 2800016.622);5
6 Coordinate t1 = new Coordinate(4628.339, 801349.338);7 Coordinate t2 = new Coordinate(3515.906, 792036.308);8 Coordinate t3 = new Coordinate(4327.381, 799909.069);9 //建立仿射变换对象
10 AffineTransformationBuilder afb = newAffineTransformationBuilder(s1,11 s2, s3, t1, t2, t3);12 AffineTransformation atf =afb.getTransformation();13
14 Point pointReuslt =JtsHelper.createPoint(x,y);15
16 //坐标转换
17 pointReuslt.apply(atf);