001: package org.geotools.feature.iso.simple;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Date;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.geotools.feature.iso.AttributeFactoryImpl;
010: import org.geotools.feature.iso.AttributeImpl;
011: import org.geotools.feature.iso.Types;
012: import org.geotools.feature.iso.attribute.GeometricAttribute;
013: import org.geotools.feature.iso.attribute.TextualAttribute;
014: import org.geotools.feature.iso.type.AttributeDescriptorImpl;
015: import org.opengis.feature.Attribute;
016: import org.opengis.feature.Feature;
017: import org.opengis.feature.simple.BooleanAttribute;
018: import org.opengis.feature.simple.NumericAttribute;
019: import org.opengis.feature.simple.SimpleFeature;
020: import org.opengis.feature.simple.SimpleFeatureCollection;
021: import org.opengis.feature.simple.SimpleFeatureCollectionType;
022: import org.opengis.feature.simple.SimpleFeatureFactory;
023: import org.opengis.feature.simple.SimpleFeatureType;
024: import org.opengis.feature.simple.TemporalAttribute;
025: import org.opengis.feature.simple.TextAttribute;
026: import org.opengis.feature.type.AttributeDescriptor;
027: import org.opengis.feature.type.AttributeType;
028: import org.opengis.feature.type.Name;
029:
030: import com.vividsolutions.jts.geom.Geometry;
031:
032: /**
033: * Construct specific types for SimpleFeatures.
034: * <p>
035: * Please note that this factory is <b>direct</b> and will implement what
036: * you ask for - if you need asistence use a builder.
037: * </p>
038: * @author Jody Garnett
039: */
040: public class SimpleFeatureFactoryImpl extends AttributeFactoryImpl
041: implements SimpleFeatureFactory {
042:
043: /**
044: * Create a list of properties from provided type + values.
045: * <p>
046: * Package visible for use by SimpleFeatureImpl constructors.
047: * </p>
048: * @return List<Attribute> based on provided values
049: */
050: public static List attributes(SimpleFeatureType type,
051: Object values[]) {
052: if (values == null) {
053: values = new Object[type.getTypes().size()];
054: }
055: List attributes = new ArrayList(values.length);
056: int index = 0;
057: for (Iterator i = type.getProperties().iterator(); i.hasNext(); index++) {
058: AttributeDescriptor descriptor = (AttributeDescriptor) i
059: .next();
060: Class binding = ((AttributeType) descriptor.type())
061: .getBinding();
062: Object value = index < values.length ? values[index] : null;
063: Attribute attribute;
064: if (Geometry.class.isAssignableFrom(binding)) {
065: attribute = new GeometricAttribute(value, descriptor,
066: null, null);
067: } else {
068: attribute = new AttributeImpl(value, descriptor, null);
069: }
070: attributes.add(attribute);
071: }
072: return attributes;
073: }
074:
075: public BooleanAttribute createBooleanAttribute(Boolean value,
076: AttributeDescriptor descriptor) {
077: return new org.geotools.feature.iso.attribute.BooleanAttribute(
078: value, descriptor);
079: }
080:
081: public NumericAttribute createNumericAttribute(Number value,
082: AttributeDescriptor descriptor) {
083: return new org.geotools.feature.iso.attribute.NumericAttribute(
084: value, descriptor);
085: }
086:
087: public TemporalAttribute createTemporalAttribute(Date value,
088: AttributeDescriptor descriptor) {
089: return new org.geotools.feature.iso.attribute.TemporalAttribute(
090: value, descriptor);
091: }
092:
093: public TextAttribute createTextAttribute(CharSequence value,
094: AttributeDescriptor descriptor) {
095: return new TextualAttribute(value, descriptor);
096: }
097:
098: public Feature createFeature(Collection value,
099: AttributeDescriptor desc, String id) {
100: throw new UnsupportedOperationException(
101: "SimpleFeature cannot be nested");
102: }
103:
104: public SimpleFeature createSimpleFeature(SimpleFeatureType type,
105: String id, Object[] values) {
106: return new SimpleFeatureImpl(type, id, values);
107: }
108:
109: public SimpleFeature createSimpleFeature(List attributes,
110: SimpleFeatureType type, String id) {
111: return new SimpleFeatureImpl(attributes, type, id);
112: }
113:
114: public SimpleFeatureCollection createSimpleFeatureCollection(
115: SimpleFeatureCollectionType type, String id) {
116: return new SimpleFeatureCollectionImpl(type, id);
117: }
118:
119: final static AttributeDescriptor find(List descriptors, Name name) {
120: if (name == null)
121: return null;
122: for (Iterator i = descriptors.iterator(); i.hasNext();) {
123: AttributeDescriptor attributeType = (AttributeDescriptor) i
124: .next();
125: if (name.equals(attributeType.type().getName())) {
126: return attributeType;
127: }
128: }
129: return null; // no default geometry here?
130: }
131:
132: /** Create AttributeDescriptorImpl for this simple type */
133: final static List descriptors(List typeList) {
134: List descriptors = new ArrayList(typeList.size());
135: for (Iterator i = typeList.iterator(); i.hasNext();) {
136: AttributeType attributeType = (AttributeType) i.next();
137: Name typeName = attributeType.getName();
138: Name descriptorName = Types.typeName(typeName);
139: AttributeDescriptor attribute = new AttributeDescriptorImpl(
140: attributeType, descriptorName, 1, 1, true, null);
141: descriptors.add(attribute);
142: }
143: return descriptors;
144: }
145:
146: final static Name geometryName(AttributeType geom) {
147: if (geom == null)
148: return null;
149: return geom.getName();
150:
151: }
152: }
|