aruuuuu
aruuuuu

Reputation: 1645

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). 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!

Upvotes: 2

Views: 7605

Answers (5)

Campa
Campa

Reputation: 4495

As the JTS javadocs suggest, it is best to use a CoordinateSequenceFilter to change the coordinates of a geometry in place.

/**
 * 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). 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).

Upvotes: 1

aruuuuu
aruuuuu

Reputation: 1645

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:

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.

Upvotes: 3

bugmenot123
bugmenot123

Reputation: 1136

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

Upvotes: 2

spt5007
spt5007

Reputation: 178

You can use a CoordinateFilter to invert all x and y values in a given geometry.

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());

Upvotes: 11

Matt
Matt

Reputation: 5684

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!

Upvotes: 1

Related Questions