Hello Andrea

Andrea Aime a écrit :
> I'm trying to solve the following problem. I have a CRS object,
> and I need to go back to its id, but this time with a twist,
> in that I need to generate a code like "epsg:4326" if the CRS
> is lon/lat ordered, and like "urn:x-ogc:def:crs:EPSG:6.11.2:4326"
> if the axis order is the opposite.

If this is something similar to what CRS.lookupIdentifier(...) tries to do, a
possible approach may be (assuming that epsg-hsql is in the class path rather
than epsg-wkt):

Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
CRSAuthorityFactory xyOrder = FactoryFinder.getAuthorityFactory("EPSG", hints);

hints.put(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.FALSE);
CRSAuthorityFactory standardOrder = FactoryFinder.getAuthorityFactory("EPSG",
hints);

CoordinateReferenceSystem crs = ...,
String code = standardOrder.findIdentifier(crs);
if (code != null) {
    // We have latitude first.
} else {
    code = xyOrder.findIdentifier(crs);
    if (code != null) {
        // Longitude first.
    }
}


I have not tested, but it should work if there is no bug... Note that the
explicit FORCE_LONGITUDE_FIRST_AXIS_ORDER hint overrides anything that may have
been specified by Hints.putSystemDefault(...) or as system properties. This is
why setting this hint to false is not the same than not setting this hint.



> What's the best way to spot the axis order of a crs? I thought
> about looking at the name of the first axis in the coordinate
> system, yet it seems to be error prone... can I assume the
> axis names or a geographic crs are always Lat and Lon?

GeographicCRS are often lat/lon, but there is nothing enforcing that (and I
believe that some are not).

If we just want to determine axis order without the overhead of the above
approach, I suggest to look at axis direction (NORTH, SOUTH, EAST, WEST,
SOUTH_WEST, NORTH_NORTH_EAST, etc.). The code below consider only NORTH, SOUTH,
EAST and WEST.

CoordinateSystem cs = crs.getCoordinateSystem();
int dimension = cs.getDimension();
int longitudeDim = -1;
int latitudeDim = -1;
for (int i=0; i<dimension; i++) {
    AxisDirection dir = cs.getAxis(i).getDirection().absolute();
    if (dir.equals(AxisDirection.EAST)) {
        longitudeDim = i;
    }
    if (dir.equals(AxisDirection.NORTH)) {
        latitudeDim = i;
    }
}
if (longitudeDim >= 0 && latitudeDim >= 0) {
    if (longitudeDim < latitudeDim) {
        // longitude before latitude
    } else {
        // latitude before longitude
    }
}


Note that you could remove the loop and look immediately at axis 0 and 1 if they
are the specific axis you want to look at.

    Martin

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Geotools-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to