001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gml2.bindings;
017:
018: import java.util.Iterator;
019: import org.opengis.metadata.Identifier;
020: import org.opengis.referencing.crs.CoordinateReferenceSystem;
021: import org.opengis.referencing.crs.GeographicCRS;
022: import org.opengis.referencing.crs.ProjectedCRS;
023: import org.opengis.referencing.cs.AxisDirection;
024: import org.opengis.referencing.cs.CoordinateSystem;
025: import org.geotools.metadata.iso.citation.Citations;
026:
027: /**
028: * Utility methods used by gml2 bindigns when encodding.
029: *
030: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
031: *
032: */
033: public class GML2EncodingUtils {
034: static final int LON_LAT = 0;
035: static final int LAT_LON = 1;
036: static final int INAPPLICABLE = 2;
037:
038: public static String epsgCode(CoordinateReferenceSystem crs) {
039: if (crs == null) {
040: return null;
041: }
042:
043: for (Iterator i = crs.getIdentifiers().iterator(); i.hasNext();) {
044: Identifier id = (Identifier) i.next();
045:
046: //return "EPSG:" + id.getCode();
047: if ((id.getAuthority() != null)
048: && id.getAuthority().getTitle().equals(
049: Citations.EPSG.getTitle())) {
050: return id.getCode();
051: }
052: }
053:
054: return null;
055: }
056:
057: public static String crs(CoordinateReferenceSystem crs) {
058: String code = epsgCode(crs);
059: int axisOrder = axisOrder(crs);
060:
061: if (code != null) {
062: if ((axisOrder == LON_LAT) || (axisOrder == INAPPLICABLE)) {
063: return "http://www.opengis.net/gml/srs/epsg.xml#"
064: + code;
065: } else {
066: return "urn:x-ogc:def:crs:EPSG:6.11.2:" + code;
067: }
068: }
069:
070: return null;
071: }
072:
073: /**
074: * Returns the axis order of the provided {@link CoordinateReferenceSystem} object.
075: * @param crs
076: * @return <ul>
077: * <li>LON_LAT if the axis order is longitude/latitude</li>
078: * <li>LAT_LON if the axis order is latitude/longitude</li>
079: * <li>INAPPLICABLE if the CRS does not deal with longitude/latitude
080: * (such as vertical or engineering CRS)</li>
081: */
082: static int axisOrder(CoordinateReferenceSystem crs) {
083: CoordinateSystem cs = null;
084:
085: if (crs instanceof ProjectedCRS) {
086: ProjectedCRS pcrs = (ProjectedCRS) crs;
087: cs = pcrs.getBaseCRS().getCoordinateSystem();
088: } else if (crs instanceof GeographicCRS) {
089: cs = crs.getCoordinateSystem();
090: } else {
091: return INAPPLICABLE;
092: }
093:
094: int dimension = cs.getDimension();
095: int longitudeDim = -1;
096: int latitudeDim = -1;
097:
098: for (int i = 0; i < dimension; i++) {
099: AxisDirection dir = cs.getAxis(i).getDirection().absolute();
100:
101: if (dir.equals(AxisDirection.EAST)) {
102: longitudeDim = i;
103: }
104:
105: if (dir.equals(AxisDirection.NORTH)) {
106: latitudeDim = i;
107: }
108: }
109:
110: if ((longitudeDim >= 0) && (latitudeDim >= 0)) {
111: if (longitudeDim < latitudeDim) {
112: return LON_LAT;
113: } else {
114: return LAT_LON;
115: }
116: }
117:
118: return INAPPLICABLE;
119: }
120: }
|