001: package org.geotools.feature.iso.simple;
002:
003: import java.util.Arrays;
004: import java.util.Collections;
005: import java.util.List;
006:
007: import org.geotools.feature.iso.Types;
008: import org.geotools.util.GTContainer;
009: import org.opengis.feature.simple.SimpleFeature;
010: import org.opengis.feature.simple.SimpleFeatureCollectionType;
011: import org.opengis.feature.simple.SimpleFeatureFactory;
012: import org.opengis.feature.simple.SimpleFeatureType;
013: import org.opengis.feature.simple.SimpleTypeFactory;
014: import org.picocontainer.defaults.DefaultPicoContainer;
015:
016: /**
017: * Convenience class for working with or implementing with simple features.
018: * <p>
019: * Method that involve creation must be called in a non-static context
020: * and only after the class has been injected with a
021: * {@link org.geotools.feature.simple.SimpleFeatureBuilder}
022: * </p>
023: * FIXME Inject with factory not builder!
024: *
025: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
026: *
027: */
028: public class SimpleFeatures {
029:
030: //
031: // FIXME This code moved from FeatureTypes shares flaw of GTContainer use
032: //
033: final private static SimpleFeatureType emptySimpleFeatureType;
034: final private static SimpleFeatureCollectionType emptySimpleFeatureCollectionType;
035: static {
036: DefaultPicoContainer container = GTContainer.simple();
037: SimpleTypeFactory factory = (SimpleTypeFactory) container
038: .getComponentInstanceOfType(SimpleTypeFactory.class);
039:
040: emptySimpleFeatureType = factory.createSimpleFeatureType(Types
041: .typeName("EmptySimpleFeatureType"),
042: Collections.EMPTY_LIST, null, null,
043: Collections.EMPTY_SET, null);
044: emptySimpleFeatureCollectionType = factory
045: .createSimpleFeatureCollectionType(Types
046: .typeName("EmptySimpleFeatureCollectionType"),
047: emptySimpleFeatureType, null);
048: }
049: /**
050: * Injected factory
051: */
052: SimpleFeatureFactory factory;
053:
054: /**
055: * Builder used to create simple features.
056: */
057: SimpleFeatureBuilder builder;
058:
059: /**
060: * Creates a new instance satisfying the factory dependency.
061: *
062: * @param builder
063: */
064: public SimpleFeatures(SimpleFeatureFactory factory) {
065: this .factory = factory;
066: builder = new SimpleFeatureBuilder(factory);
067: }
068:
069: public void setSimpleFeatureFactory(SimpleFeatureFactory factory) {
070: this .factory = factory;
071: builder.setSimpleFeatureFactory(factory);
072: }
073:
074: /**
075: * Creates a new simple feature from a type and array of values.
076: * <p>
077: * The order of items in <code>values</code> must correspond to the
078: * order of attributes defined in <code>type</code>
079: * </p>
080: * @param type The simple feature type.
081: * @param values The values of the created feature.
082: * @param fid The feature id, may be null.
083: *
084: * @return The created feature.
085: */
086: public SimpleFeature create(SimpleFeatureType type,
087: Object[] values, String fid) {
088: return create(type, Arrays.asList(values), fid);
089: }
090:
091: /**
092: * Creates a new simple feature from a type and list of values.
093: * <p>
094: * The order of items in <code>values</code> must correspond to the
095: * order of attributes defined in <code>type</code>
096: * </p>
097: * @param type The simple feature type.
098: * @param values The values of the created feature.
099: * @param fid The feature id, may be null.
100: *
101: * @return The created feature.
102: */
103: public SimpleFeature create(SimpleFeatureType type, List values,
104: String fid) {
105: builder.init();
106: builder.setType(type);
107:
108: for (int i = 0; i < values.size(); i++) {
109: builder.add(values.get(i));
110: }
111: return builder.feature(fid);
112: }
113:
114: /**
115: * Copies the values from one feature to another.
116: *
117: * @param source The feature being copied from.
118: * @param target The feature being copied to.
119:
120: */
121: public static void copy(SimpleFeature source, SimpleFeature target) {
122: target.setValue(source.getValue());
123: target.setDefaultGeometry(source.getDefaultGeometry());
124: target.setCRS(source.getCRS());
125: }
126: }
|