001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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: */
017: package org.geotools.data.feature.adapter;
018:
019: import java.util.ArrayList;
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.commons.jxpath.JXPathIntrospector;
027: import org.geotools.feature.GeometryAttributeType;
028: import org.geotools.feature.IllegalAttributeException;
029: import org.geotools.feature.iso.AttributeImpl;
030: import org.geotools.feature.iso.Types;
031: import org.geotools.feature.iso.simple.SimpleFeatureFactoryImpl;
032: import org.geotools.feature.iso.xpath.AttributePropertyHandler;
033: import org.geotools.geometry.jts.ReferencedEnvelope;
034: import org.opengis.feature.Attribute;
035: import org.opengis.feature.Feature;
036: import org.opengis.feature.GeometryAttribute;
037: import org.opengis.feature.simple.SimpleFeature;
038: import org.opengis.feature.simple.SimpleFeatureFactory;
039: import org.opengis.feature.simple.SimpleFeatureType;
040: import org.opengis.feature.type.AttributeDescriptor;
041: import org.opengis.feature.type.AttributeType;
042: import org.opengis.feature.type.Name;
043: import org.opengis.feature.type.PropertyDescriptor;
044: import org.opengis.geometry.BoundingBox;
045: import org.opengis.referencing.crs.CoordinateReferenceSystem;
046:
047: import com.vividsolutions.jts.geom.Geometry;
048:
049: /**
050: *
051: * @author Gabriel Roldan, Axios Engineering
052: * @version $Id: ISOFeatureAdapter.java 27002 2007-09-17 03:01:53Z jdeolive $
053: * @source $URL:
054: * http://svn.geotools.org/geotools/branches/2.4.x/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/adapter/ISOFeatureAdapter.java $
055: * @since 2.4
056: */
057: public class ISOFeatureAdapter implements Feature, SimpleFeature {
058:
059: static {
060: JXPathIntrospector
061: .registerDynamicClass(ISOFeatureAdapter.class,
062: AttributePropertyHandler.class);
063: }
064:
065: private org.geotools.feature.Feature adaptee;
066:
067: private SimpleFeatureType featureType;
068:
069: private SimpleFeatureFactory attributeFactory;
070:
071: private AttributeDescriptor descriptor;
072:
073: public ISOFeatureAdapter(org.geotools.feature.Feature feature,
074: SimpleFeatureType ftype,
075: SimpleFeatureFactory attributeFactory) {
076: this (feature, ftype, attributeFactory,
077: (AttributeDescriptor) null);
078: }
079:
080: public ISOFeatureAdapter(org.geotools.feature.Feature feature,
081: SimpleFeatureType ftype,
082: SimpleFeatureFactory attributeFactory,
083: AttributeDescriptor descriptor) {
084: if (adaptee instanceof GTFeatureAdapter) {
085: throw new IllegalArgumentException(
086: "No need to adapt GTFeaureAdapter, use getAdaptee() instead");
087: }
088: this .attributeFactory = attributeFactory;
089: if (attributeFactory == null) {
090: this .attributeFactory = new SimpleFeatureFactoryImpl();
091: }
092: this .adaptee = feature;
093: this .featureType = ftype;
094: this .descriptor = descriptor;
095: }
096:
097: public org.geotools.feature.Feature getAdaptee() {
098: return adaptee;
099: }
100:
101: public BoundingBox getBounds() {
102: ReferencedEnvelope env = new ReferencedEnvelope(
103: (CoordinateReferenceSystem) null);
104: env.init(adaptee.getBounds());
105: return env;
106: }
107:
108: public CoordinateReferenceSystem getCRS() {
109: GeometryAttributeType defaultGeometry = adaptee
110: .getFeatureType().getDefaultGeometry();
111: return defaultGeometry == null ? null : defaultGeometry
112: .getCoordinateSystem();
113: }
114:
115: public GeometryAttribute getDefaultGeometry() {
116: AttributeDescriptor defaultGeometry = featureType
117: .getDefaultGeometry();
118: Geometry geom = adaptee.getDefaultGeometry();
119: Attribute attribute;
120: attribute = attributeFactory.createAttribute(geom,
121: defaultGeometry, null);
122: return (GeometryAttribute) attribute;
123: }
124:
125: public String getID() {
126: return adaptee.getID();
127: }
128:
129: public Object getUserData(Object key) {
130: return null;
131: }
132:
133: public void putUserData(Object key, Object value) {
134: // do nothing?
135: }
136:
137: public void setCRS(CoordinateReferenceSystem crs) {
138: Geometry defaultGeometry = adaptee.getDefaultGeometry();
139: defaultGeometry.setUserData(crs);
140: }
141:
142: public void setDefaultGeometry(GeometryAttribute geometryAttribute) {
143: Object value = geometryAttribute.getValue();
144: try {
145: adaptee.setDefaultGeometry((Geometry) value);
146: } catch (IllegalAttributeException e) {
147: throw (RuntimeException) new RuntimeException()
148: .initCause(e);
149: }
150: }
151:
152: public Collection associations() {
153: return Collections.EMPTY_SET;
154: }
155:
156: public Collection attributes() {
157: return getAttributes();
158: }
159:
160: public List getAttributes() {
161: Object[] attributes = this .adaptee.getAttributes(null);
162: Collection descriptors = this .featureType.attributes();
163:
164: assert attributes.length == descriptors.size();
165:
166: List atts = new ArrayList(attributes.length);
167: int current = 0;
168: for (Iterator it = descriptors.iterator(); it.hasNext(); current++) {
169: Object content = attributes[current];
170: AttributeDescriptor descriptor = (AttributeDescriptor) it
171: .next();
172: String id = null;
173: atts.add(new AttributeImpl(content, descriptor, id));
174: }
175:
176: return atts;
177: }
178:
179: public Object getValue() {
180: Collection properties = featureType.getProperties();
181: Object[] values = this .adaptee.getAttributes((Object[]) null);
182: List attributes = new ArrayList(properties.size());
183: AttributeDescriptor descriptor;
184: int i = 0;
185: for (Iterator it = properties.iterator(); it.hasNext(); i++) {
186: descriptor = (AttributeDescriptor) it.next();
187: Object value = values[i];
188: Attribute att = attributeFactory.createAttribute(value,
189: descriptor, null);
190: attributes.add(att);
191: }
192: return attributes;
193: }
194:
195: public List get(Name name) {
196: Object value = getValue(name.getLocalPart());
197: if (value == null) {
198: return Collections.EMPTY_LIST;
199: }
200: AttributeDescriptor attDescriptor;
201: attDescriptor = (AttributeDescriptor) Types.descriptor(
202: featureType, name.getLocalPart());
203: Attribute attribute = attributeFactory.createAttribute(value,
204: attDescriptor, null);
205: return Collections.singletonList(attribute);
206: }
207:
208: public AttributeDescriptor getDescriptor() {
209: return descriptor;
210: }
211:
212: public void setValue(Object atts) throws IllegalArgumentException {
213: Collection attributes = (Collection) atts;
214: int i = 0;
215: Attribute att;
216: for (Iterator it = attributes.iterator(); it.hasNext(); i++) {
217: att = (Attribute) it.next();
218: setValue(i, att.getValue());
219: }
220: }
221:
222: public AttributeType getType() {
223: return featureType;
224: }
225:
226: public boolean nillable() {
227: return true;
228: }
229:
230: public Object operation(Name name, List parameters) {
231: throw new UnsupportedOperationException("not implemented yet");
232: }
233:
234: public PropertyDescriptor descriptor() {
235: return null;
236: }
237:
238: public Name name() {
239: return this .featureType.getName();
240: }
241:
242: public Object getDefaultGeometryValue() {
243: return adaptee.getDefaultGeometry();
244: }
245:
246: public Object getValue(String name) {
247: return adaptee.getAttribute(name);
248: }
249:
250: public Object getValue(int index) {
251: return adaptee.getAttribute(index);
252: }
253:
254: public int getNumberOfAttributes() {
255: return adaptee.getNumberOfAttributes();
256: }
257:
258: public Object operation(String name, Object parameters) {
259: return null;
260: }
261:
262: public void setValue(String name, Object value) {
263: try {
264: adaptee.setAttribute(name, value);
265: } catch (IllegalAttributeException e) {
266: throw (RuntimeException) new RuntimeException()
267: .initCause(e);
268: }
269: }
270:
271: public void setValue(int index, Object value) {
272: try {
273: adaptee.setAttribute(index, value);
274: } catch (IllegalAttributeException e) {
275: throw (RuntimeException) new RuntimeException()
276: .initCause(e);
277: }
278: }
279:
280: /**
281: * TODO implement as a data view
282: */
283: public List getTypes() {
284: org.geotools.feature.AttributeType[] types = adaptee
285: .getFeatureType().getAttributeTypes();
286: return Arrays.asList(types);
287: }
288:
289: /**
290: * TODO implement as a data view as per {@link SimpleFeature#values()}
291: */
292: public List values() {
293: Object[] attributes = adaptee.getAttributes(null);
294: return Arrays.asList(attributes);
295: }
296:
297: public List getValues() {
298: // TODO Auto-generated method stub
299: return null;
300: }
301:
302: public void setDefaultGeometryValue(Object geometry) {
303: try {
304: adaptee.setDefaultGeometry((Geometry) geometry);
305: } catch (IllegalAttributeException e) {
306: throw (RuntimeException) new RuntimeException()
307: .initCause(e);
308: }
309: }
310:
311: public void setValue(List values) {
312: setValues(values);
313: }
314:
315: public void setValues(List values) {
316: int attCount = adaptee.getNumberOfAttributes();
317: Object[] contents = new Object[attCount];
318: for (int i = 0; i < attCount; i++) {
319: contents[i] = values.get(i);
320: }
321: setValues(contents);
322: }
323:
324: public void setValues(Object[] values) {
325: for (int i = 0; i < values.length; i++) {
326: try {
327: adaptee.setAttribute(i, values[i]);
328: } catch (IllegalAttributeException e) {
329: throw (RuntimeException) new RuntimeException()
330: .initCause(e);
331: }
332: }
333: }
334:
335: }
|