Andrea thanks again for your help.  A colleague and I cranked this out a little 
while ago.

The following is a Lean(Low Processing) Solution to population of a 
FeatureCollection:
Minimal Testing.....

Below is the following: LeanFeatureCollection.java, LeanFeatureSource.java, and 
a snipped from a Test Cast: 

//LeanFeatureCollection

package org.geotools.demo;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.geotools.feature.FeatureIterator;
import org.geotools.feature.collection.AbstractFeatureCollection;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.geometry.BoundingBox;



@SuppressWarnings("unchecked")
public class LeanFeatureCollection extends AbstractFeatureCollection  {

     private List<SimpleFeature> features = new ArrayList<SimpleFeature>();
     private ReferencedEnvelope bounds = null;
     
     protected LeanFeatureCollection(SimpleFeatureType schema) {
         super(schema);
     }
     
     
     @Override
     public int size() {
         return features.size();
     }
     
     @Override
     protected Iterator openIterator() {
         Iterator it = features.iterator();
         return it;
     }
     
     @Override
     protected void closeIterator(Iterator close) {
         // nothing to do there
     }


    @Override
    public boolean add(SimpleFeature f) {
        
         //maintain the bounds
          BoundingBox boundingBox = f.getBounds();
          if (bounds == null){
              bounds = new ReferencedEnvelope(
                      boundingBox.getMinX(), boundingBox.getMaxX(), 
                      boundingBox.getMinY(), boundingBox.getMaxY(),
                      schema.getCoordinateReferenceSystem());
          } else {
              bounds.expandToInclude(boundingBox.getMinX(), 
boundingBox.getMinY());    
              bounds.expandToInclude(boundingBox.getMaxX(), 
boundingBox.getMaxY());    
          }
          
        return features.add(f);
    }

    @Override
    public void clear() {
        // maintain the bounds
        bounds = null;
        super.clear();
    }

    @Override
    public FeatureIterator<SimpleFeature> features() {
        return new LeanFeatureCollectionIterator(features);
    }

    @Override
    public ReferencedEnvelope getBounds() {
        return bounds;
    }

    @Override
    public boolean isEmpty() {
        return features.isEmpty();
    }
         
    private class LeanFeatureCollectionIterator implements 
FeatureIterator<SimpleFeature> {
        private Iterator<SimpleFeature> iter;
        
        public LeanFeatureCollectionIterator(List<SimpleFeature> features) {
            iter = features.iterator();
        }
        
        public void close() {
            // do nothing
        }

        public boolean hasNext() {
            return iter.hasNext();
        }

        public SimpleFeature next() throws NoSuchElementException {
            return iter.next();
        }
    }
 }


//LeanFeatureSource.java
package org.geotools.demo;

import java.awt.RenderingHints.Key;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.geotools.data.DataAccess;
import org.geotools.data.FeatureListener;
import org.geotools.data.FeatureSource;
import org.geotools.data.Query;
import org.geotools.data.QueryCapabilities;
import org.geotools.data.ResourceInfo;
import org.geotools.feature.CollectionListener;
import org.geotools.feature.FeatureCollection;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.Name;
import org.opengis.filter.Filter;


public class LeanFeatureSource implements FeatureSource<SimpleFeatureType, 
SimpleFeature> {

    private LeanFeatureCollection features;

    public LeanFeatureSource(LeanFeatureCollection featureCollection) {
        this.features = featureCollection;
    }
    
    
    public void addFeatureListener(FeatureListener listener) {
//        throw new UnsupportedOperationException();
    }

    public ReferencedEnvelope getBounds() throws IOException {
        return features.getBounds();
    }

    public ReferencedEnvelope getBounds(Query query) throws IOException {
        return features.getBounds();
    }

    public int getCount(Query query) throws IOException {
        return features.size();
    }

    public DataAccess<SimpleFeatureType, SimpleFeature> getDataStore() {
        return null;
    }

    public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatures()
            throws IOException {
        return features;
    }

    public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatures(
            Query query) throws IOException {
        return features;
    }

    public FeatureCollection<SimpleFeatureType, SimpleFeature> getFeatures(
            Filter filter) throws IOException {
        return features;
    }

    public ResourceInfo getInfo() {
        return null;
    }

    public Name getName() {
        return null;
    }

    public QueryCapabilities getQueryCapabilities() {
        return null;
    }

    public SimpleFeatureType getSchema() {
        return features.getSchema();
    }

    public Set<Key> getSupportedHints() {
        return new HashSet<Key>();
    }

    public void removeFeatureListener(FeatureListener listener) {
//        throw new UnsupportedOperationException();
    }
}


//Test Case Snipped
//let IDE define imports
//getPointStyle set to null
public class DisplayPoint {
    static StyleFactory styleFactory = 
CommonFactoryFinder.getStyleFactory(null);
    static FilterFactory filterFactory = 
CommonFactoryFinder.getFilterFactory(null);
    
    static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
    static final int MAX_NAME_LENGTH = 20;

public static void main(String[] args) throws Exception {
        DisplayPoint displayPoint = new DisplayPoint();
        //TreeMapHandler - Object I have internally for TreeMap Handling
        TreeMapHandler tmHandler = new TreeMapHandler();
        //data.csv file of xyz points
        //allPoints is a TreeMap of XyzPoint Objects
        TreeMap allPoints = tmHandler.getTreeMap("/data.csv");

        LeanFeatureCollection collection = 
displayPoint.getPointFeature(allPoints);
        
        LeanFeatureSource featureSource = new LeanFeatureSource(collection);
        Style pointStyle = displayPoint.createPointStyle();
        MapLayer m = new DefaultMapLayer(featureSource, pointStyle);
        
        MapContext map = new DefaultMapContext();
        map.setTitle("The Point");
        map.addLayer(featureSource, pointStyle);
        
        JMapFrame.showMap(map);
}


public LeanFeatureCollection getPointFeature(TreeMap allPoints) throws 
Exception {
    SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
    typeBuilder.setName("mytype");
    typeBuilder.setCRS(crs);
    typeBuilder.add("route", Point.class);
    typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class);
    final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();

    GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);    
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

    LeanFeatureCollection collection = new LeanFeatureCollection(TYPE);

      Collection cl = allPoints.values();
      Iterator it = cl.iterator();
      while (it.hasNext()){
          XyzPoint xyz = (XyzPoint)it.next();
          Point point = factory.createPoint( new 
Coordinate(xyz.getX(),xyz.getY()));

          featureBuilder.set("name",xyz.getName());
          featureBuilder.add(point);
          
          SimpleFeature f = featureBuilder.buildFeature(null);
          
  
          
          collection.add(f);
      }
      return collection;
}

private Style createPointStyle() {
    return null;
}
}
------------------------------------------------------------------------------
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to