简体   繁体   English

如何在JTS中从纬度,经度到经度,纬度交换jts.geom.Geometry对象的坐标

[英]How to Swap Coordinates of jts.geom.Geometry object from Lat, Long to Long,Lat in JTS

I have a geometry object of type (com.vividsolutions.jts.geom.Geometry). 我有一个类型为(com.vividsolutions.jts.geom.Geometry)的几何对象。 it is currently in latitude, longitude form and I'd like to flip the coordinates so that its longitude latitude so that I can have it in GeoJSON format for mongodb. 它目前是经度,纬度形式,我想翻转坐标,使其经度纬度,以便可以将其以GeoJSON格式用于mongodb。

My constraints that I am seeing are: a) the input that I would like to flip coordinates for is a Geometry object. 我看到的约束是:a)我想翻转坐标的输入是一个Geometry对象。 b) The Geometry object will be either a Polygon type or Multipolygon. b)几何对象将是多边形类型或多多边形。 c) I would like to flip the coordinates before the type cast to Polygon/multipolygon c)我想在将类型强制转换为Polygon / multipolygon之前翻转坐标

I have tried geo.reverse() but it does not work. 我已经尝试过geo.reverse(),但是它不起作用。

As well, I have tried using: CRSAuthorityFactory factory = CRS.getAuthorityFactory(true); 同样,我尝试使用:CRSAuthorityFactory factory = CRS.getAuthorityFactory(true); CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326"); CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem(“ EPSG:4326”);

And another option and I did not see it work. 和另一个选择,我没有看到它的工作。

Thanks! 谢谢!

You can use a CoordinateFilter to invert all x and y values in a given geometry. 您可以使用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;
    }
}

Then apply the filter thusly: 然后按以下方式应用过滤器:

// Invert Coordinates since GeoJSON likes them that way
myGeometryObj.apply(new InvertCoordinateFilter());

The solution is that the Geometry.getCoordinates() gives a Coordinate[] array which is live. 解决方案是Geometry.getCoordinates()提供了一个实时的Coordinate []数组。 Therefore, I could use the following: 因此,我可以使用以下内容:

Where myGeometryObject is a Geometry object: 其中myGeometryObject是Geometry对象:

Coordinate[] original = myGeometryobject.getCoordinates();
for(int i = 0; i < original.length; i++){
    Double swapValue = original[i].x;
    original[i].x = original[i].y;
    original[i].y = swapValue;
}

The changes to the Coordinate objects will affect the underlying Geometry permanently. 对“坐标”对象的更改将永久影响基础几何。

One potential solution to this is extending the class to provide an additional function that either outputs the data you need in some convenient way: 一种可能的解决方案是扩展类,以提供一个附加功能,该功能可以通过一些便捷的方式输出所需的数据:

public Coordinate[] getReversedCoordinates(){

  Coordinate[] original = this.getCoordinates();
  Coordinate[] ret = new Coordinate[original.length];

  for(int i =0; i<original.length; i++){
      ret[i] = new Coordinate( original[i].x , original[i].y );
  }

  return ret;

}

Alternately you could alter the interpretation of the data. 或者,您可以更改数据的解释。 It's a little harder for me to give you a code snippet for that as I'm not sure how you're using the information specifically. 我很难为您提供一个代码段,因为我不确定您是如何使用这些信息的。

EDIT: 编辑:

Once you have the reversed coordinates,you can create a duplicate Geometry of type linear ring. 一旦有了反向坐标,就可以创建线性环类型的重复几何。 A means of doing this is to use your factory to use your geometry factory: 一种方法是使用工厂使用几何工厂:

GeometryFactory gf = //However this was instantiated;
Coordinate[] reversedCoordinates = getReversedCoordinates();
gf.createLinearRing(reversedCoordinates);

Happy coding and leave a comment if you have any questions! 祝您编程愉快,如果有任何疑问,请留下评论!

As the JTS javadocs suggest, it is best to use a CoordinateSequenceFilter to change the coordinates of a geometry in place. 正如JTS javadocs所建议的那样,最好使用CoordinateSequenceFilter来更改几何的坐标。

/**
 * Swaps the XY coordinates of a geometry.
 */
public void swapCoordinates(Geometry g) {
    g.apply(new CoordinateSequenceFilter() {

        @Override
        public void filter(CoordinateSequence seq, int i) {
            double oldX = seq.getCoordinate(i).x;
            seq.getCoordinate(i).x = seq.getCoordinate(i).y;
            seq.getCoordinate(i).y = oldX;
        }

        @Override
        public boolean isGeometryChanged() {
            return true;
        }

        @Override
        public boolean isDone() {
            return false;
        }
    });
}

NOTE: there are nasty cases where Coordinate object you get access to is a copy of what is internally stored (eg. see PackedCoordinateSequence ). 注意:在某些讨厌的情况下,您可以访问的Coordinate对象是内部存储的副本 (例如,参见PackedCoordinateSequence )。 In that case, as suggested in the javadocs, you must use the provided setters, that is setX() setY() . 在这种情况下,如javadocs中建议的那样,您必须使用提供的setter,即setX() setY()


There are also even nastier cases where there is simply no way to change the coordinates in place, eg. 甚至在更糟糕的情况下,也根本无法更改坐标,例如。 when fetching a geometry from the DB with PostGIS where this Geolatte PackedPositionSequence sequence implementation just won't let you modify the coordinates (afaik). 当使用PostGIS从数据库中获取几何图形时,此Geolatte PackedPositionSequence序列实现只是不允许您修改坐标(afaik)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM