2

Is there a way with GeoTools to know whether a CRS is geographic (in degrees) or projected (in meter) ?

I have tried empirically with the following code (based on a check of the CRS unit) but I am not sure at all if it is going to work for all CRSs... Is there a better and more correct way ?

public enum CRSType { GEOG, CARTO, UNKNOWN }

private static CRSType getCRSType(Unit<?> unit) {
    if(unit == null) return CRSType.UNKNOWN;
    switch (unit.toString()) {
    case "": return CRSType.UNKNOWN;
    case "°": return CRSType.GEOG;
    case "deg": return CRSType.GEOG;
    case "dms": return CRSType.GEOG;
    case "degree": return CRSType.GEOG;
    case "m": return CRSType.CARTO;
    default:
        LOGGER.warn("Unexpected unit of measure for projection: "+unit);
        return CRSType.UNKNOWN;
    }
}

public static CRSType getCRSType(CoordinateReferenceSystem crs) {
    return getCRSType(CRSUtilities.getUnit(crs.getCoordinateSystem()));
}

1 Answer 1

3

I think a more reliable method (since you've overlooked feet and chains to name but two) is to check if your projection is an instance of DefaultGeographicCRS or DefaultProjectedCRS. Theoretically, it could also be a DefaultEngineeringCRS (which I think is neither), a DefaultGeocentricCRS, a DefaultImageCRS (that I've never seen in the wild) or a temporal or vertical CRS (again probably not what you want anyway).

So I came up with the following test code which seems to give the expected answers:

ArrayList<CoordinateReferenceSystem> crsList = new ArrayList<>();
CoordinateReferenceSystem wgs84 = DefaultGeographicCRS.WGS84;
crsList.add(wgs84);
String[] codes = { "4326", "27700", "5243", "5673", "4087", "42309", "26956", "8857" };
for (String code : codes) {
  CoordinateReferenceSystem crs = CRS.decode("EPSG:" + code);
  crsList.add(crs);
}
for (CoordinateReferenceSystem crs : crsList) {
  System.out.println(crs);
  if ((crs instanceof DefaultGeographicCRS))
    System.out.println(crs.getName().getCode() + " is geographic");
  if ((crs instanceof DefaultProjectedCRS))
    System.out.println(crs.getName().getCode() + " is projected");
}

Update

A slightly cleaner method would be to look for org.opengis.referencing.crs.ProjectedCRS and org.opengis.referencing.crs.GeographicCRS

2
  • It might be worth proposing this as an enhancement to the CRS class if you get it to work.
    – Ian Turton
    Commented Apr 27, 2020 at 15:24
  • Thanks a lo Ian. It tested it on few other known CRSs and it worked as well. It is more reliable than what I was doing so far.
    – julien
    Commented Apr 29, 2020 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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