WKT除了有针对几何信息的描述外,也有针对空间参考的描述,以下我直接套用OGC规范中的描述(https://www.osgeo.cn/doc_ogcstd/ogc_standard/ch02_chapter1/chapter.html#wkb):
对于一个地理坐标系,比如最常见的 WGS84 坐标系统,WKT 描述是这样的:
GEOGCS
[
"GCS_WGS_1984",
DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433],
AUTHORITY["EPSG",4326]
]
“GEOGCS”表明其后紧随的“[ ]”中描述的是一个地理坐标系统。该坐标系统名称为“GCS_WGS_1984”;采用的大地基准面为“D_WGS_1984”,该基准面近似椭球体的长轴为 6378137.0 米、扁率为 298.257223563;以格林威治 0 度经线为起始经线;地图单位为度,该单位的转换因子为 0.0174532925199433(π/180);最后,该坐标系统在EPSG5 中的编码为“4326”。对于一个投影坐标系,比如 WGS84 Web Mercator(Auxiliary Sphere)坐标系统,WKT 描述是这样的:
PROJCS
[
"WGS_1984_Web_Mercator_Auxiliary_Sphere",
GEOGCS
[
"GCS_WGS_1984",
DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],
PRIMEM["Greenwich",0.0],
UNIT["Degree",0.0174532925199433]
],
PROJECTION["Mercator_Auxiliary_Sphere"],
PARAMETER["False_Easting",0.0],
PARAMETER["False_Northing",0.0],
PARAMETER["Central_Meridian",0.0],
PARAMETER["Standard_Parallel_1",0.0],
PARAMETER["Auxiliary_Sphere_Type",0.0],
UNIT["Meter",1.0],
AUTHORITY["EPSG",3857]
]
在geotools中读取WKT格式的坐标可以采用如下方法:
String GCS_WGS_1984 = "GEOGCS\r\n" +
"[\r\n" +
"\"GCS_WGS_1984\",\r\n" +
"DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],\r\n" +
"PRIMEM[\"Greenwich\",0.0],\r\n" +
"UNIT[\"Degree\",0.0174532925199433],\r\n" +
"AUTHORITY[\"EPSG\",4326]\r\n" +
"]";
CoordinateReferenceSystem crsTarget = CRS.parseWKT(GCS_WGS_1984);
System.out.println(crsTarget.toWKT());
对于投影转换,可以采用如下方法:
//找到坐标系的WKT文本
String CGCS_2000 = "GEOGCS[\"GCS_China_Geodetic_Coordinate_System_2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137.0,298.257222101]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]";
String CGCS_Z36 = "PROJCS[\"CGCS2000_3_degree_Gauss_Kruger_zone_36\",GEOGCS[\"GCS_China Geodetic Coordinate System 2000\",DATUM[\"D_China_2000\",SPHEROID[\"CGCS2000\",6378137,298.257222101]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",0],PARAMETER[\"central_meridian\",108],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",36500000],PARAMETER[\"false_northing\",0],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"4524\"]]";
//读取几何
String wktPoint = "POINT(102.23489 26.322)";
WKTReader wktReader = new WKTReader();
Point oldGeom = (Point) wktReader.read(wktPoint);
//读取WKT形式的坐标系
CoordinateReferenceSystem sourceCRS= CRS.parseWKT(CGCS_2000 );
CoordinateReferenceSystem targetCRS= CRS.parseWKT(CGCS_Z36 );
//构建两个坐标系的数学转换
MathTransform transform = CRS.findMathTransform(sourceCRS,targetCRS);
//对几何要素进行坐标转换
Geometry newGeom = JTS.transform(oldGeom, transform);
分享一个查询坐标系WKT文本的网址:http://epsg.io/ 该网址可根据epsg编码查询对应坐标系的各种表述形式。坐标转换在使用过程中是有很多注意事项,根据后续文章的推进在一一展开。