001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation;
010: * version 2.1 of the License.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * This package contains documentation from OpenGIS specifications.
018: * OpenGIS consortium's work is fully acknowledged here.
019: */
020: package org.geotools.metadata.iso.extent;
021:
022: // J2SE dependencies
023: import java.util.Iterator;
024: import java.util.Collection;
025:
026: // OpenGIS dependencies
027: import org.opengis.metadata.extent.Extent;
028: import org.opengis.metadata.extent.BoundingPolygon;
029: import org.opengis.metadata.extent.GeographicExtent;
030: import org.opengis.metadata.extent.GeographicBoundingBox;
031: import org.opengis.metadata.extent.TemporalExtent;
032: import org.opengis.metadata.extent.VerticalExtent;
033: import org.opengis.util.InternationalString;
034:
035: // Geotools dependencies
036: import org.geotools.metadata.iso.MetadataEntity;
037:
038: /**
039: * Information about spatial, vertical, and temporal extent.
040: * This interface has four optional attributes
041: * ({@linkplain #getGeographicElements geographic elements},
042: * {@linkplain #getTemporalElements temporal elements}, and
043: * {@linkplain #getVerticalElements vertical elements}) and an element called
044: * {@linkplain #getDescription description}.
045: * At least one of the four shall be used.
046: *
047: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/metadata/iso/extent/ExtentImpl.java $
048: * @version $Id: ExtentImpl.java 25189 2007-04-17 13:23:47Z desruisseaux $
049: * @author Martin Desruisseaux
050: * @author Touraïvane
051: *
052: * @since 2.1
053: */
054: public class ExtentImpl extends MetadataEntity implements Extent {
055: /**
056: * Serial number for interoperability with different versions.
057: */
058: private static final long serialVersionUID = 7812213837337326257L;
059:
060: /**
061: * A geographic extent ranging from 180°W to 180°E and 90°S to 90°N.
062: *
063: * @since 2.2
064: */
065: public static final Extent WORLD;
066: static {
067: final ExtentImpl world = new ExtentImpl();
068: world.getGeographicElements().add(
069: GeographicBoundingBoxImpl.WORLD);
070: world.freeze();
071: WORLD = world;
072: }
073:
074: /**
075: * Returns the spatial and temporal extent for the referring object.
076: */
077: private InternationalString description;
078:
079: /**
080: * Provides geographic component of the extent of the referring object
081: */
082: private Collection/*<GeographicExtent>*/geographicElements;
083:
084: /**
085: * Provides temporal component of the extent of the referring object
086: */
087: private Collection/*<TemporalExtent>*/temporalElements;
088:
089: /**
090: * Provides vertical component of the extent of the referring object
091: */
092: private Collection/*<VerticalExtent>*/verticalElements;
093:
094: /**
095: * Constructs an initially empty extent.
096: */
097: public ExtentImpl() {
098: }
099:
100: /**
101: * Constructs a metadata entity initialized with the values from the specified metadata.
102: *
103: * @since 2.4
104: */
105: public ExtentImpl(final Extent source) {
106: super (source);
107: }
108:
109: /**
110: * Returns the spatial and temporal extent for the referring object.
111: */
112: public InternationalString getDescription() {
113: return description;
114: }
115:
116: /**
117: * Set the spatial and temporal extent for the referring object.
118: */
119: public synchronized void setDescription(
120: final InternationalString newValue) {
121: checkWritePermission();
122: description = newValue;
123: }
124:
125: /**
126: * Provides geographic component of the extent of the referring object
127: */
128: public synchronized Collection/*<GeographicExtent>*/getGeographicElements() {
129: return geographicElements = nonNullCollection(
130: geographicElements, GeographicExtent.class);
131: }
132:
133: /**
134: * Set geographic component of the extent of the referring object
135: */
136: public synchronized void setGeographicElements(
137: final Collection/*<GeographicExtent>*/newValues) {
138: geographicElements = copyCollection(newValues,
139: geographicElements, GeographicExtent.class);
140: }
141:
142: /**
143: * Provides temporal component of the extent of the referring object
144: */
145: public synchronized Collection/*<TemporalExtent>*/getTemporalElements() {
146: return temporalElements = nonNullCollection(temporalElements,
147: TemporalExtent.class);
148: }
149:
150: /**
151: * Set temporal component of the extent of the referring object
152: */
153: public synchronized void setTemporalElements(
154: final Collection/*<TemporalExtent>*/newValues) {
155: temporalElements = copyCollection(newValues, temporalElements,
156: TemporalExtent.class);
157: }
158:
159: /**
160: * Provides vertical component of the extent of the referring object
161: */
162: public synchronized Collection/*<VerticalExtent>*/getVerticalElements() {
163: return verticalElements = nonNullCollection(verticalElements,
164: VerticalExtent.class);
165: }
166:
167: /**
168: * Set vertical component of the extent of the referring object
169: */
170: public synchronized void setVerticalElements(
171: final Collection/*<VerticalExtent>*/newValues) {
172: verticalElements = copyCollection(newValues, verticalElements,
173: VerticalExtent.class);
174: }
175:
176: /**
177: * Convenience method returning a single geographic bounding box from the specified extent.
178: * If no bounding box was found, then this method returns {@code null}. If more than one box
179: * is found, then boxes are {@linkplain GeographicBoundingBoxImpl#add added} together.
180: *
181: * @since 2.2
182: */
183: public static GeographicBoundingBox getGeographicBoundingBox(
184: final Extent extent) {
185: GeographicBoundingBox candidate = null;
186: if (extent != null) {
187: GeographicBoundingBoxImpl modifiable = null;
188: for (final Iterator it = extent.getGeographicElements()
189: .iterator(); it.hasNext();) {
190: final GeographicExtent element = (GeographicExtent) it
191: .next();
192: final GeographicBoundingBox bounds;
193: if (element instanceof GeographicBoundingBox) {
194: bounds = (GeographicBoundingBox) element;
195: } else if (element instanceof BoundingPolygon) {
196: // TODO: iterates through all polygons and invoke Polygon.getEnvelope();
197: continue;
198: } else {
199: continue;
200: }
201: /*
202: * A single geographic bounding box has been extracted. Now add it to previous
203: * ones (if any). All exclusion boxes before the first inclusion box are ignored.
204: */
205: if (candidate == null) {
206: /*
207: * Reminder: 'inclusion' is a mandatory attribute, so it should never be
208: * null for a valid metadata object. If the metadata object is invalid,
209: * it is better to get an exception than having a code doing silently
210: * some probably inappropriate work.
211: */
212: final Boolean inclusion = bounds.getInclusion();
213: ensureNonNull("inclusion", inclusion);
214: if (inclusion.booleanValue()) {
215: candidate = bounds;
216: }
217: } else {
218: if (modifiable == null) {
219: modifiable = new GeographicBoundingBoxImpl(
220: candidate);
221: candidate = modifiable;
222: }
223: modifiable.add(bounds);
224: }
225: }
226: if (modifiable != null) {
227: modifiable.freeze();
228: }
229: }
230: return candidate;
231: }
232: }
|