如何在JTS中交换来自Lat,Long到Long,Lat的jts.geom.Geometry对象的坐标

aru*_*uuu 1 java jts geotools

我有一个类型的几何对象(com.vividsolutions.jts.geom.Geometry).它目前处于纬度,经度形式,我想翻转坐标使其经度纬度,以便我可以使用GeoJSON格式为mongodb.

我看到的约束是:a)我想要翻转坐标的输入是Geometry对象.b)Geometry对象可以是Polygon类型或Multipolygon.c)我想在类型转换为多边形/多边形之前翻转坐标

我试过geo.reverse()但它不起作用.

同样,我尝试过使用:CRSAuthorityFactory factory = CRS.getAuthorityFactory(true); CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");

另一种选择,我没有看到它的工作.

谢谢!

小智 8

您可以使用CoordinateFilter反转给定几何体中的所有x和y值.

private static class InvertCoordinateFilter implements CoordinateFilter {
    public void filter(Coordinate coord) {
        double oldX = coord.x;
        coord.x = coord.y;
        coord.y = oldX;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后应用过滤器:

// Invert Coordinates since GeoJSON likes them that way
myGeometryObj.apply(new InvertCoordinateFilter());
Run Code Online (Sandbox Code Playgroud)

  • 优秀而优雅的解决方案 这应该是公认的答案. (3认同)