GeoTools : How to create a simple FeatureCollection from Scratch

What if you're just testing or you want to dynamically create features using only Java code?
There is no need to load data from a file or other DataStore.

Here's a handy-dandy little example that creates a custom MapLayer with a FeatureCollection containing one feature, my house:


/*
 * Created on Aug 13, 2004
 */

import java.io.IOException;

import org.geotools.data.DataSourceException;
import org.geotools.feature.AttributeType;
import org.geotools.feature.AttributeTypeFactory;
import org.geotools.feature.DefaultFeature;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.DefaultFeatureType;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureTypeFactory;
import org.geotools.feature.GeometryAttributeType;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.SchemaException;
import org.geotools.map.DefaultMapLayer;
import org.geotools.map.MapLayer;
import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

/**
 * @author knudsen
 */
public class HouseFeatureHelper {
  
  public static final House MY_HOUSE = new House(
    "Keith's House",
    "Oxnard",
    "CA",
    -119.1761,
    34.1975);

  public static MapLayer createLayer() throws IOException, DataSourceException {
    // Create the style
    final StyleBuilder builder = new StyleBuilder();
    final Style style = builder.createStyle(builder.createPointSymbolizer());
    FeatureCollection col = createFeatureCollection();
      
    // add layer to the context
    DefaultMapLayer layer = new DefaultMapLayer(col, style);
    layer.setTitle("Houses");
    return layer;
  }  

  public static FeatureCollection createFeatureCollection() {
    PFeatureCollection col = new PFeatureCollection();
    col.add(createTestFeature());
    return col;
  }

  public static Feature createTestFeature() {
    return createHouseFeature(
        MY_HOUSE.name, "house1",
        MY_HOUSE.city, MY_HOUSE.state,
        MY_HOUSE.longitude, MY_HOUSE.latitude);
  }
  
  public static Feature createHouseFeature(
             String name, String id,
             String city, String state,
             double longitude, double latitude) {
    GeometryFactory gf = new GeometryFactory();
    Point loc = gf.createPoint(new Coordinate(longitude, latitude));
    
    Object[] featureAttributes = { loc, name, city, state };
    Feature f = null;
    try {
      f = new HouseFeature(featureAttributes, id);
    } catch (IllegalAttributeException e) {
      e.printStackTrace();
    }
    return f;
  }

  public static DefaultFeatureType createHouseFeatureType() {
    
    GeometryAttributeType geometryAttribute =
      (GeometryAttributeType)AttributeTypeFactory.newAttributeType("the_geom", Point.class);
    AttributeType nameAttribute = 
      AttributeTypeFactory.newAttributeType("NAME", String.class, true, 48, null);
    AttributeType cityAttribute = 
      AttributeTypeFactory.newAttributeType("CITY", String.class, true, 48, null);
    AttributeType stateAttribute = 
      AttributeTypeFactory.newAttributeType("STATE", String.class, true, 48, null);

    FeatureTypeFactory factory = FeatureTypeFactory.newInstance("House");  
    factory.addType(geometryAttribute);
    factory.addType(nameAttribute);
    factory.addType(cityAttribute);
    factory.addType(stateAttribute);
    factory.setName("House");  
    factory.setNamespace("");  
    DefaultFeatureType ft = null;
    try {
      ft = (DefaultFeatureType)factory.getFeatureType(); 
    } catch (SchemaException e) {
      e.printStackTrace();
    }
        
    return ft;
  }

  
  public static class PFeatureCollection extends DefaultFeatureCollection {
    public PFeatureCollection() {
      super();
    }
  }

  public static class HouseFeature extends DefaultFeature {
    public HouseFeature(Object[] attributes, String featureId) throws IllegalAttributeException {
      super(
        HouseFeatureHelper.createHouseFeatureType(),
        attributes, 
        featureId);
    }
  }
  
  public static class House {
    public String name = "";
     public String city = "";
    public String state = "";
    public double longitude = 0.0;
    public double latitude = 0.0;
    
    public House(String name, String city, String state, double longitude, double latitude) {
      this.name = name;
      this.city = city;
      this.state = state;
      this.longitude = longitude;
      this.latitude = latitude;
    }
  }  
}