最近接触到了一个关于geotools后端坐标转换的工作,手上想自己动手尝试一下,看着官方文档,一路照做下来,无论是建立Java project还是maven project,一样的代码还是一样的导包还是有问题,起先我以为一定要在maven中写代码,开始是maven的环境配置上出了一点问题,后来环境问题搞了很久才解决,到了后面还是报坐标转换先关的包的异常,于是再仔细地看看官方给的文档,果然是忽略了一个关键点,官方建议移除的八个jar我没移除,对比了移除与不移除的结果,得出了那八个包对坐标转换相关的包有影响。
1、eclipse下新建Java工程项目,工程名为coordinatetransform
2、新建包,包名也一样
3、新建一个class,name为prj
4、导包--geotools,选择11.0版本
5、移除以下jar包,以免影响下面的坐标转换相关的包
/*
* 删除以下jar包即可
* gt-epsg-hsql
gt-epsg-oracle
gt-epsg-postgresql
gt-epsg-wkt
gt-arcsde
gt-arcsde-common
gt-jdbc-db2
gt-jdbc-oracle
*/
libraries添加jar包,工程名右键属性property
在order and export中select all
6、工程项目视图如下
7、上代码
package coordinatetransform;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import com.vividsolutions.jts.geom.Coordinate;
/*
* 删除以下jar包即可
* gt-epsg-hsql
gt-epsg-oracle
gt-epsg-postgresql
gt-epsg-wkt
gt-arcsde
gt-arcsde-common
gt-jdbc-db2
gt-jdbc-oracle
*/
public class prj {
static String strWKTMercator="PROJCS[\"World_Mercator\","
+ "GEOGCS[\"GCS_WGS_1984\","
+ "DATUM[\"WGS_1984\","
+ "SPHEROID[\"WGS_1984\",6378137,298.257223563]],"
+ "PRIMEM[\"Greenwich\",0],"
+ "UNIT[\"Degree\",0.017453292519943295]],"
+ "PROJECTION[\"Mercator_1SP\"],"
+ "PARAMETER[\"False_Easting\",0],"
+ "PARAMETER[\"False_Northing\",0],"
+ "PARAMETER[\"Central_Meridian\",0],"
+ "PARAMETER[\"latitude_of_origin\",0],"
+ "UNIT[\"Meter\",1]]";
static String strUTM49="PROJCS[\"WGS 84 / UTM zone 49N\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",111],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"false_easting\",500000],PARAMETER[\"false_northing\",0],AUTHORITY[\"EPSG\",\"32649\"],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]";
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
///CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
//CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
//MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS);
//Coordinate coorDst=new Coordinate();
///JTS.transform(new Coordinate(40, 116),coorDst, transform);
CoordinateReferenceSystem crsTarget = CRS.parseWKT(strWKTMercator);
CoordinateReferenceSystem crsTarget1 = CRS.parseWKT(strUTM49);
MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, crsTarget);
MathTransform transform1 = CRS.findMathTransform( crsTarget1,DefaultGeographicCRS.WGS84);
//String wktPoint = "POINT(100.02715479879 33.462715497945)";
Coordinate coorDst=new Coordinate();
Coordinate coorDst1=new Coordinate();
JTS.transform(new Coordinate(113.367, 23.126), coorDst, transform);
JTS.transform(new Coordinate(742369.57, 2559443.47), coorDst1, transform1);
System.out.println(coorDst);
System.out.println(coorDst1);
}catch(Exception e) {System.out.println(e.toString());}
}
}
8、run as java application console的结果为
(1.2619956712761045E7, 2630487.8566600755, NaN)
(113.36674291511564, 23.126077677299143, NaN)