1

I am trying to transform a Polygon from EPSG:32643 to EPSG:4326 using GeoTools version 15.1.

However doing so, I seem to get incorrect values, and I cannot work out what is going wrong.

The source polygon I want to transform is: POLYGON ((-673624.76 -9998082.58, -673624.76 9999900, 9999900 9999900, 9999900 -9998082.58, -673624.76 -9998082.58))

My approach is below:

WKTReader reader = new WKTReader(geometryFactory);
    Polygon polygon = (Polygon) reader.read(
            "POLYGON ((-673624.76 -9998082.58, -673624.76 9999900, 9999900 9999900, 9999900 -9998082.58, -673624.76 -9998082.58))");

    CRSAuthorityFactory   factory = CRS.getAuthorityFactory(true);
    CoordinateReferenceSystem sourceCRS = factory.createCoordinateReferenceSystem("EPSG:32643");
    CoordinateReferenceSystem targetCRS = factory.createCoordinateReferenceSystem("EPSG:4326");
    MathTransform mTrans = CRS.findMathTransform(sourceCRS, targetCRS, true);
    Geometry transformed = JTS.transform(polygon, mTrans);
    System.out.println(transformed.toText());

The result I get is: POLYGON ((75.00000000000001 -90, 75.00000000000001 90, 75.00000000000001 90, 75.00000000000001 -90, 75.00000000000001 -90))

which is not a valid Polygon (too few points).

I have compared the results with qGIS, which produces the following POLYGON: POLYGON((-15.00571089231461919 -79.54560544451550186, -15.093940630657249 79.54559128417886882, 165.00830898510739075 25.28552079066640701, 165.00050512410689407 -25.28552203835479517, -15.00571089231461919 -79.54560544451550186)).

I have also tested with epsg.io, transforming the first coordinate of the Polygon: https://epsg.io/transform#s_srs=32643&t_srs=4326&x=-673624.7600000&y=-9998082.5800000

EPSG.IO Transformation qGIS and EPSG.IO seems to agree.

What am I doing wrong?

1

Your input polygon exceeds the valid extent of the projection (check http://epsg.io/32643) so you get nonsense back.

This works fine:

GeometryFactory geometryFactory = new GeometryFactory();
CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:32643");
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
WKTReader reader = new WKTReader(geometryFactory );
Polygon polygon = (Polygon) reader.read(
       "POLYGON ((-673624.76 -9998082.58, -673624.76 9999900, 9999900 9999900, 9999900 -9998082.58, -673624.76 -9998082.58))");

ReferencedEnvelope valid = new ReferencedEnvelope(sourceCRS);
valid.expandToInclude(new Coordinate(166021.44, 0.00));
valid.expandToInclude(new Coordinate(534994.66, 9329005.18));
Polygon validPoly = JTS.toGeometry(valid);
polygon = (Polygon) polygon.intersection(validPoly);
System.out.println(polygon.toString());

MathTransform mTrans = CRS.findMathTransform(sourceCRS, targetCRS);
Geometry transformed = JTS.transform(polygon, mTrans);
System.out.println(transformed.toText());

Giving me:

POLYGON ((166021.44 0, 166021.44 9329005.18, 534994.66 9329005.18, 534994.66 0, 166021.44 0))
POLYGON ((0 71.99999997232004, 83.30604397918667 48.4273218982485, 83.99999997577616 78.00000041167887, 0 75.31448658492026, 0 71.99999997232004))

Which agrees with epsg.io's wgs84 bounds of (72.0 0.0),(78.0 84.0) allowing for axis order and rounding.

  • Thank you Ian, I get your point. But since you establish a valid Envelope, why do you expand it prior to intersecting it with the source polygon? And the coordinates you expand with, where do you get them from? Wouldn’t the correct approach then be to use the valid extent as is, and intersect the source-polygon with this to produce a «as large match as possible» to the original polygon to be transformed? Sorry if I was unclear here. – Eivind Rønnevik Apr 5 '18 at 21:40
  • Also, both qGis and Epsg.io seem to be able to transform the coordinates despite the fact that they are outside the valid area of the CRS? – Eivind Rønnevik Apr 5 '18 at 21:48
  • ReferencedEnvelope valid = new ReferencedEnvelope(sourceCRS); just creates an empty envelope, so I expand it to the valid area. Those values come from epsg.io – Ian Turton Apr 6 '18 at 8:33
  • transforming points outside the valid area may or may not return a result it will almost certainly be very wrong though. – Ian Turton Apr 6 '18 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.