Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[udig-devel] Create new layer and features from code

Hi,
 
I'm new to both uDig and GeoTools.  I've been able to use the various examples and lists from around the web to make progress in a number of areas, but I've hit a snag in trying to create a new feature collection to be displayed as a new layer on an existing map.  I'm using the uDig 1.2.0 release and coding in a plugin based on the distance tool example to trigger a few things with a button click.  Here is what I currently have, pulled together from various examples on the web.  When I run this code I see the following:
 
* New layer called Flag in the Layers View
* Flag layer has these properties
    * ID http://localhost/scratch#Flag
    * Bounds (0,0) (-1,-1)
    * Filter.Exclude
    * Feature Type has correct fields
* Entry called Scratch with child Flag in Catalog tree
* Nothing in the Table view when Flag layer is selected
* Column headers in the Table view called FID, ID, and Name (ID and Name come from my feature construction, FID not sure?)
 
Questions:
 
1) Am I even close?
2) I saw some references to SimpleFeatureCollection code, but think this is from a newer GeoTools than uDig is using?
3) How do I get my supplied attributes to show up in the table and a point to show up on the map?
 
Thanks for any help.  Many uDig features are meeting my needs.  Figuring out how to add points from code will be very useful and hopefully help some others sort out the API in this area, since it seems it has been in transition.
 
- Ned
 
==========================================================
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName( "Flag" );
builder.setNamespaceURI( "http://localhost/" );
builder.setCRS( DefaultGeographicCRS.WGS84 );
 
//add attributes in order
builder.add( "Location", Point.class );
builder.add( "ID", Integer.class );
builder.add( "Name", String.class );
 
//build the type
final SimpleFeatureType FLAG = builder.buildFeatureType();
 
//create the builder
SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(FLAG);
GeometryFactory gf = new com.vividsolutions.jts.geom.GeometryFactory();
Coordinate[] coords = {new Coordinate(114.4456894350221,-33.0429697801666, 0.0)};
 
//add the attributes
fbuilder.set("Location", new Point(CoordinateArraySequenceFactory.instance().create(coords), gf));
fbuilder.set("Name",  "theName" );
fbuilder.set("ID", 99);
 
//build the feature
SimpleFeature feature = fbuilder.buildFeature( "test" );
 
// get the selected layer
IMap map = ApplicationGIS.getActiveMap();
 
IGeoResource stationGeoResource = CatalogPlugin .getDefault ().getLocalCatalog().createTemporaryResource(FLAG);
 
// create list of IGeoResource
List <IGeoResource> geoResourceList = new LinkedList<IGeoResource>();
geoResourceList.add(stationGeoResource);
 
List layerList = ApplicationGIS.addLayersToMap(map, geoResourceList, -1);
 
FeatureSource stationFeatureSource;
try {
   // get feature sourc
   stationFeatureSource = ((ILayer) layerList.get(map.getMapLayers().size())).getResource(FeatureStore.class, null);
   
   //get FeatureCollection
   FeatureCollection features = stationFeatureSource.getFeatures();
 
   //add new feature
   features.add(feature);
   ((ILayer) layerList.get(map.getMapLayers().size())).getResource(FeatureStore.class, null).addFeatures(features);
 

} catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
}
==========================================================

Back to the top