我有以下gml片段:
<gml:LineString srsName="EPSG:25832"><gml:coordinates>663957.75944074022118,5103981.64908889029175 663955.915655555087142,5103991.151674075052142</gml:coordinates></gml:LineString>
我想用EPSG:4326 srs将其转换为wkt字符串。
答案 0 :(得分:0)
我发现了一个有问题的解决方案,主要问题是,输入只是gml片段,而不是有效的文档。因此,无法使用xpath提取srsName属性,因为未绑定名称空间。
我用正则表达式搜索了srsName属性,并转换了几何形状。
try {
String gml = "<gml:LineString srsName=\"EPSG:25832\"><gml:coordinates>663957.75944074022118,5103981.64908889029175 663955.915655555087142,5103991.151674075052142</gml:coordinates></gml:LineString>";
Geometry geometry = gmlReader.read(gml, geometryFactory);
Pattern p = Pattern.compile("srsName=\\\"([^\"]*)\\\"");
Matcher m = p.matcher(gml);
if (m.find()) {
String srs = m.group(1);
CoordinateReferenceSystem crsSource = CRS.decode(srs);
GeographicCRS crsTarget =
org.geotools.referencing.crs.DefaultGeographicCRS.WGS84;
MathTransform transform = CRS.findMathTransform(crsSource, crsTarget, false);
geometry = JTS.transform(geometry, transform);
}
String wktString = wktWriter.write(geometry);
} catch (Exception e) {
throw new RuntimeException(e);
}
丑陋为****,有没有更清洁的方法?