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.Collection;
020: import java.util.Iterator;
021:
022: import org.geotools.feature.FeatureType;
023: import org.geotools.feature.IllegalAttributeException;
024: import org.geotools.feature.iso.AttributeBuilder;
025: import org.geotools.feature.iso.AttributeFactoryImpl;
026: import org.geotools.geometry.jts.ReferencedEnvelope;
027: import org.opengis.feature.Attribute;
028: import org.opengis.feature.GeometryAttribute;
029: import org.opengis.feature.simple.SimpleFeature;
030: import org.opengis.feature.simple.SimpleFeatureType;
031: import org.opengis.feature.type.AttributeDescriptor;
032: import org.opengis.feature.type.GeometryType;
033:
034: import com.vividsolutions.jts.geom.Geometry;
035:
036: /**
037: * Adapter from ISO SimpleFeature to GeoTools Feature
038: *
039: * @author Gabriel Roldan, Axios Engineering
040: * @version $Id: GTFeatureAdapter.java 28577 2008-01-03 15:44:29Z groldan $
041: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/adapter/GTFeatureAdapter.java $
042: * @since 2.4
043: */
044: public class GTFeatureAdapter implements
045: org.geotools.feature.SimpleFeature {
046:
047: private SimpleFeature adaptee;
048:
049: private FeatureType gtType;
050:
051: public GTFeatureAdapter(SimpleFeature adaptee, FeatureType gtType) {
052: if (adaptee instanceof ISOFeatureAdapter) {
053: throw new IllegalArgumentException(
054: "No need to adapt ISOFEatureAdapter, use getAdaptee() instead");
055: }
056: this .adaptee = adaptee;
057: this .gtType = gtType;
058: }
059:
060: public Object getAttribute(String xPath) {
061: Object value = adaptee.getValue(xPath);
062: return value;
063: }
064:
065: public Object getAttribute(int index) {
066: Object value = adaptee.getValue(index);
067: return value;
068: }
069:
070: public Object[] getAttributes(Object[] attributes) {
071: Collection atts = adaptee.attributes();
072: Object[] values = new Object[atts.size()];
073: int current = 0;
074: for (Iterator it = atts.iterator(); it.hasNext();) {
075: Attribute att = (Attribute) it.next();
076: values[current] = att.getValue();
077: current++;
078: }
079: return values;
080: }
081:
082: public ReferencedEnvelope getBounds() {
083: ReferencedEnvelope envelope = new ReferencedEnvelope(adaptee
084: .getBounds());
085: // envelope.init(adaptee.getBounds());
086: return envelope;
087: }
088:
089: public Geometry getDefaultGeometry() {
090: GeometryAttribute defaultGeometry = adaptee
091: .getDefaultGeometry();
092: return (Geometry) (defaultGeometry == null ? null
093: : defaultGeometry.getValue());
094: }
095:
096: public FeatureType getFeatureType() {
097: return gtType;
098: }
099:
100: public String getID() {
101: return adaptee.getID();
102: }
103:
104: public int getNumberOfAttributes() {
105: return adaptee.getNumberOfAttributes();
106: }
107:
108: public void setAttribute(int position, Object val)
109: throws IllegalAttributeException,
110: ArrayIndexOutOfBoundsException {
111: adaptee.setValue(position, val);
112: }
113:
114: public void setAttribute(String xPath, Object attribute)
115: throws IllegalAttributeException {
116: adaptee.setValue(xPath, attribute);
117: }
118:
119: public void setDefaultGeometry(Geometry geometry)
120: throws IllegalAttributeException {
121: SimpleFeatureType type = (SimpleFeatureType) adaptee.getType();
122: AttributeDescriptor descriptor = type.getDefaultGeometry();
123: GeometryType defGeomType = (GeometryType) descriptor.type();
124:
125: AttributeFactoryImpl attributeFactory = new AttributeFactoryImpl();
126: AttributeBuilder builder = new AttributeBuilder(
127: attributeFactory);
128: builder.setType(defGeomType);
129: builder.add(geometry, defGeomType.getName());
130:
131: GeometryAttribute geometryAttribute;
132: geometryAttribute = (GeometryAttribute) builder.build();
133: adaptee.setDefaultGeometry(geometryAttribute);
134: }
135:
136: public void setAttributes(Object[] attributes)
137: throws IllegalAttributeException {
138:
139: Collection atts = adaptee.attributes();
140:
141: int current = 0;
142: for (Iterator it = atts.iterator(); it.hasNext();) {
143: Attribute att = (Attribute) it.next();
144: att.setValue(attributes[current]);
145: current++;
146: }
147:
148: }
149:
150: }
|