本文介绍了如何在JTS中从纬度,经度到经度,纬度交换jts.geom.Geometry对象的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a geometry object of type (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.

我看到的约束是:a)我想翻转坐标的输入是一个Geometry对象.b)几何对象将是Polygon类型或Multipolygon.c)我想在将类型强制转换为Polygon/multipolygon

My constraints that I am seeing are:a) the input that I would like to flip coordinates for is a Geometry object.b) The Geometry object will be either a Polygon type or Multipolygon.c) I would like to flip the coordinates before the type cast to Polygon/multipolygon

我尝试了geo.reverse(),但是它不起作用.

I have tried geo.reverse() but it does not work.

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

As well, I have tried using:CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);CoordinateReferenceSystem crs = factory.createCoordinateReferenceSystem("EPSG:4326");

还有另一种选择,但我认为它没有用.

And another option and I did not see it work.

谢谢!

推荐答案

对此的一种可能的解决方案是扩展类,以提供一个附加功能,该功能可以通过一些便捷的方式输出所需的数据:

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.

一旦有了反向坐标,就可以创建线性环类型的重复几何.一种方法是使用工厂使用几何工厂:

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!

这篇关于如何在JTS中从纬度,经度到经度,纬度交换jts.geom.Geometry对象的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 02:02