001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.referencing.factory.wms;
017:
018: // J2SE dependencies
019: import java.util.Map;
020: import java.util.TreeMap;
021: import java.util.Iterator;
022: import java.util.Collections;
023: import java.util.Set;
024: import java.util.LinkedHashSet;
025:
026: // OpenGIS dependencies
027: import org.opengis.util.InternationalString;
028: import org.opengis.metadata.citation.Citation;
029: import org.opengis.referencing.FactoryException;
030: import org.opengis.referencing.IdentifiedObject;
031: import org.opengis.referencing.NoSuchAuthorityCodeException;
032: import org.opengis.referencing.crs.CoordinateReferenceSystem;
033: import org.opengis.referencing.crs.CRSAuthorityFactory;
034: import org.opengis.referencing.crs.ProjectedCRS;
035:
036: // Geotools dependencies
037: import org.geotools.factory.Hints;
038: import org.geotools.util.SimpleInternationalString;
039: import org.geotools.metadata.iso.citation.Citations;
040: import org.geotools.metadata.iso.citation.CitationImpl;
041: import org.geotools.referencing.factory.DirectAuthorityFactory;
042:
043: /**
044: * The factory for {@linkplain ProjectedCRS projected CRS} in the {@code AUTO} and {@code AUTO2}
045: * space.
046: *
047: * @since 2.2
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/main/java/org/geotools/referencing/factory/wms/AutoCRSFactory.java $
049: * @version $Id: AutoCRSFactory.java 27544 2007-10-18 19:16:40Z desruisseaux $
050: * @author Jody Garnett
051: * @author Rueben Schulz
052: * @author Martin Desruisseaux
053: *
054: * @deprecated This class will move in a <code>org.geotools.referencing.factory.<strong>web</strong></code>
055: * package in Geotools 2.5, in order to put together other web-related factories defined
056: * outside the WMS specification. Don't use this class directly. You should not need to
057: * anyway - use {@link org.geotools.referencing.ReferencingFactoryFinder} instead, which
058: * will continue to work no matter where this class is located.
059: */
060: public class AutoCRSFactory extends DirectAuthorityFactory implements
061: CRSAuthorityFactory {
062: /**
063: * The authority code. We use {@code AUTO2} citation, but merge {@code AUTO} and
064: * {@code AUTO2} identifiers in order to use the same factory for both authorities.
065: */
066: private static final Citation AUTHORITY;
067: static {
068: final CitationImpl c = new CitationImpl(Citations.AUTO2);
069: c.getIdentifiers().addAll(Citations.AUTO.getIdentifiers());
070: AUTHORITY = (Citation) c.unmodifiable();
071: }
072:
073: /**
074: * Map of Factlets by integer code (from {@code AUTO:code}).
075: *
076: * @todo Replace this with full FactorySPI system.
077: */
078: private final Map factlets = new TreeMap();
079:
080: /**
081: * Constructs a default factory for the {@code AUTO} authority.
082: */
083: public AutoCRSFactory() {
084: this (null);
085: }
086:
087: /**
088: * Constructs a factory for the {@code AUTO} authority using the specified hints.
089: */
090: public AutoCRSFactory(final Hints hints) {
091: super (hints, NORMAL_PRIORITY);
092: add(Auto42001.DEFAULT);
093: add(Auto42002.DEFAULT);
094: add(Auto42003.DEFAULT);
095: add(Auto42004.DEFAULT);
096: add(Auto42005.DEFAULT);
097: }
098:
099: /**
100: * Add the specified factlet.
101: */
102: private void add(final Factlet f) {
103: final int code = f.code();
104: if (factlets.put(new Integer(code), f) != null) {
105: throw new IllegalArgumentException(String.valueOf(code));
106: }
107: }
108:
109: /**
110: * Returns the {@link Factlet} for the given code.
111: *
112: * @param code The code.
113: * @return The fatclet for the specified code.
114: * @throws NoSuchAuthorityCodeException if no factlet has been found for the specified code.
115: */
116: private Factlet findFactlet(final Code code)
117: throws NoSuchAuthorityCodeException {
118: if (code.authority.equalsIgnoreCase("AUTO")
119: || code.authority.equalsIgnoreCase("AUTO2")) {
120: final Integer key = new Integer(code.code);
121: final Factlet fac = (Factlet) factlets.get(key);
122: if (fac != null) {
123: return fac;
124: }
125: }
126: throw noSuchAuthorityCode(code.type, code.toString());
127: }
128:
129: /**
130: * Returns the authority for this factory.
131: */
132: public Citation getAuthority() {
133: return AUTHORITY;
134: }
135:
136: /**
137: * Provides a complete set of the known codes provided by this authority.
138: * The returned set contains only numeric identifiers like {@code "42001"},
139: * {@code "42002"}, <cite>etc</cite>. The authority name ({@code "AUTO"})
140: * and the {@code lon0,lat0} part are not included. This is consistent with the
141: * {@linkplain org.geotools.referencing.factory.epsg.DirectEpsgFactory#getAuthorityCodes
142: * codes returned by the EPSG factory} and avoid duplication, since the authority is the
143: * same for every codes returned by this factory. It also make it easier for clients to
144: * prepend whatever authority name they wish, as for example in the
145: * {@linkplain org.geotools.referencing.factory.AllAuthoritiesFactory#getAuthorityCodes
146: * all authorities factory}.
147: */
148: public Set getAuthorityCodes(final Class type)
149: throws FactoryException {
150: if (type.isAssignableFrom(ProjectedCRS.class)) {
151: final Set set = new LinkedHashSet();
152: for (final Iterator it = factlets.keySet().iterator(); it
153: .hasNext();) {
154: Integer code = (Integer) it.next();
155: set.add(String.valueOf(code));
156: }
157: return set;
158: } else {
159: return Collections.EMPTY_SET;
160: }
161: }
162:
163: /**
164: * Returns the CRS name for the given code.
165: */
166: public InternationalString getDescriptionText(final String code)
167: throws FactoryException {
168: final Code c = new Code(code, ProjectedCRS.class);
169: return new SimpleInternationalString(findFactlet(c).getName());
170: }
171:
172: /**
173: * Creates an object from the specified code. The default implementation delegates to
174: * <code>{@linkplain #createCoordinateReferenceSystem createCoordinateReferenceSystem}(code)</code>.
175: */
176: public IdentifiedObject createObject(final String code)
177: throws FactoryException {
178: return createCoordinateReferenceSystem(code);
179: }
180:
181: /**
182: * Creates a coordinate reference system from the specified code. The default implementation
183: * delegates to <code>{@linkplain #createProjectedCRS createProjectedCRS}(code)</code>.
184: */
185: public CoordinateReferenceSystem createCoordinateReferenceSystem(
186: final String code) throws FactoryException {
187: return createProjectedCRS(code);
188: }
189:
190: /**
191: * Creates a projected coordinate reference system from the specified code.
192: */
193: public ProjectedCRS createProjectedCRS(final String code)
194: throws FactoryException {
195: final Code c = new Code(code, ProjectedCRS.class);
196: return findFactlet(c).create(c, factories);
197: }
198: }
|