Skip to main content

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

 
Thank you very much, Jesse.  That did work.  I can now see the point on the map and the data under "Scratch" in the catalog.  I guess my next step is to get that data saved off to a file by moving from a memory datastore to a file based one on the disk.
 
Thanks for pointing out the missing part of the API.
 
- Ned


From: Jesse Eichar [mailto:jesse.eichar@xxxxxxxxxxxxxx]
Sent: Thursday, October 28, 2010 4:36 AM
To: User-friendly Desktop Internet GIS
Subject: Re: [udig-devel] Create new layer and features from code

Hi,

You are making good progress.  The 1.2.0 release uses Geotools 2.6.Something which does not have SimpleFeatureCollection like you said.  Instead you need to FeatureCollection<SimpleFeature,SImpleFeatureType> (or something similar.

As for your code you are very close. 

   FeatureCollection features = stationFeatureSource.getFeatures();
 
   //add new feature
   features.add(feature);
   ((ILayer) layerList.get(map.getMapLayers().size())).getResource(FeatureStore.class, null).addFeatures(features);
 
The stationFeatureSource looks suspicious to me.  I would create a new collection containing the new features and add that collection rather than trying to reuse a FeatureCollection.  Something like:

   FeatureCollection features = FeatureCollections.newCollection<...>()
 
   //add new feature
   features.add(feature);
   ((ILayer) layerList.get(map.getMapLayers().size())).getResource(FeatureStore.class, null).addFeatures(features);

That might work,

Jesse



On Wed, Oct 27, 2010 at 10:36 PM, Ned Haubein <ned@xxxxxxxxxxxxxxxxxxxxx> wrote:
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
    * 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();
}
==========================================================

_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel



Back to the top