001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.catalog.ui.export;
016:
017: import net.refractions.udig.catalog.CatalogPlugin;
018:
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.geotools.feature.AttributeType;
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureCollection;
024: import org.geotools.feature.FeatureType;
025: import org.geotools.feature.GeometryAttributeType;
026: import org.geotools.feature.IllegalAttributeException;
027: import org.geotools.feature.type.GeometricAttributeType;
028:
029: import com.vividsolutions.jts.geom.Envelope;
030: import com.vividsolutions.jts.geom.Geometry;
031:
032: /**
033: * Adapts an existing feature of one Feature type to another of a "compatible" feature type.
034: * <p>
035: * A compatible feature type are feature types that have the same attributes types but maybe in a different order
036: * and the second (the type being adapted to) may have fewer attribute types.
037: * </p>
038: *
039: * @author Jesse
040: */
041: class FeatureWrapper implements Feature {
042: protected final Feature wrapped;
043: protected final FeatureType featureType;
044: protected final Geometry[] geometry;
045: protected final String[] geomAttNames;
046: protected Geometry defaultGeometry;
047:
048: /**
049: * New instance
050: * @param wrapped Feature that needs to be adapted to new FeatureType
051: * @param featureType the new feature type. Must be "compatible" with featureType of wrapped.
052: * See javadocs for class for more information
053: * @param geometries the geometries to use instead of the feature's geometries this allows the geometries to be transformed
054: * @param geomAttNames the attribute names of the geometries so we know how to put them in the attribute array.
055: *
056: * @throws IllegalAttributeException Thrown if the default geometry does not exist
057: */
058: public FeatureWrapper(final Feature wrapped,
059: final FeatureType featureType, Geometry[] geometries,
060: String[] geomAttNames) throws IllegalArgumentException {
061: super ();
062: this .wrapped = wrapped;
063: this .featureType = featureType;
064: this .geometry = geometries;
065: this .geomAttNames = geomAttNames;
066: String defaultGeomName = featureType.getDefaultGeometry()
067: .getName();
068: this .defaultGeometry = findTransformedGeometry(defaultGeomName);
069: }
070:
071: private Geometry findTransformedGeometry(String defaultGeomName)
072: throws IllegalArgumentException {
073: for (int i = 0; i < geomAttNames.length; i++) {
074: String attName = geomAttNames[i];
075: if (attName.equals(defaultGeomName))
076: return geometry[i];
077: }
078: throw new IllegalArgumentException(
079: "Attribute does not exist:" + defaultGeomName); //$NON-NLS-1$
080: }
081:
082: public final Object getAttribute(int index) {
083: return getAttribute(featureType.getAttributeType(index)
084: .getName());
085: }
086:
087: public Object getAttribute(String xPath) {
088: AttributeType type = featureType.getAttributeType(xPath);
089:
090: if (type instanceof GeometryAttributeType) {
091: return findTransformedGeometry(xPath);
092: }
093:
094: return wrapped.getAttribute(xPath);
095: }
096:
097: public Object[] getAttributes(Object[] attributes) {
098: if (attributes == null || attributes.length == 0) {
099: attributes = new Object[featureType.getAttributeCount()];
100: }
101: if (attributes.length < featureType.getAttributeCount())
102: throw new IllegalArgumentException(
103: "There needs to be at least " + featureType.getAttributeCount() + " elements in the provided array"); //$NON-NLS-1$ //$NON-NLS-2$
104:
105: Object[] atts = new Object[wrapped.getNumberOfAttributes()];
106: atts = wrapped.getAttributes(atts);
107: for (int i = 0; i < atts.length; i++) {
108: Object object = atts[i];
109: AttributeType attributeType = wrapped.getFeatureType()
110: .getAttributeType(i);
111: int index = featureType.find(attributeType.getName());
112: if (index == -1)
113: continue;
114: if (attributeType instanceof GeometricAttributeType) {
115: attributes[index] = findTransformedGeometry(attributeType
116: .getName());
117: } else {
118: attributes[index] = object;
119: }
120: }
121: return attributes;
122: }
123:
124: public Envelope getBounds() {
125: return defaultGeometry.getEnvelopeInternal();
126: }
127:
128: public Geometry getDefaultGeometry() {
129: return defaultGeometry;
130: }
131:
132: public FeatureType getFeatureType() {
133: return featureType;
134: }
135:
136: public String getID() {
137: return wrapped.getID();
138: }
139:
140: public int getNumberOfAttributes() {
141: return featureType.getAttributeCount();
142: }
143:
144: @SuppressWarnings("deprecation")
145: public FeatureCollection getParent() {
146: return wrapped.getParent();
147: }
148:
149: public void setAttribute(int position, Object val)
150: throws IllegalAttributeException,
151: ArrayIndexOutOfBoundsException {
152: throw new UnsupportedOperationException();
153: }
154:
155: public void setAttribute(String xPath, Object attribute)
156: throws IllegalAttributeException {
157: throw new UnsupportedOperationException();
158: }
159:
160: public void setDefaultGeometry(Geometry geometry)
161: throws IllegalAttributeException {
162: throw new UnsupportedOperationException();
163: }
164:
165: public void setParent(FeatureCollection collection) {
166: throw new UnsupportedOperationException();
167: }
168: }
|