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.
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
I have tried geo.reverse() but it does not work.
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.
Thanks!
You can use a CoordinateFilter to invert all x and y values in a given geometry.
Then apply the filter thusly:
The solution is that the Geometry.getCoordinates() gives a Coordinate[] array which is live. Therefore, I could use the following:
Where myGeometryObject is a Geometry object:
The changes to the Coordinate objects will affect the underlying Geometry permanently.
As important addendum to the existing answers: You should always call yourGeometry.geometryChanged() after you changed a Geometry's geometry!
See http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html#getCoordinates%28%29 and http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html#geometryChanged%28%29
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:
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:
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.NOTE: there are nasty cases where
Coordinate
object you get access to is a copy of what is internally stored (eg. seePackedCoordinateSequence
). In that case, as suggested in the javadocs, you must use the provided setters, that is 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).