[udig-devel] Save programatically created layer
Jesse Eichar
jeichar at refractions.net
Mon Feb 18 15:03:10 PST 2008
Hi,
Sounds like a good project. I'm a little disappointed that the
MapEditor doesn't show that the layer is dirty but I think there is an
easy work around. The main thing I think is to open the map before
adding a layer. Also there are ways to create the layer that takes
less code.
Here's a slightly modified version. I'm doing this in my email
program so it probably won't compile but it should give you the idea:
public static Layer CreateNewLayer(Vector<Feature> vf, String name,
java.awt.Color color, IProgressMonitor monitor) {
FeatureType schema = vf.get(0).getFeatureType();
IGeoResource resource =
Catalog.getDefault().createTemporaryResource( schema );
resource.resolve( FeatureStore.class,
monitor ).removeFeatures(Filter.NONE);
List<ILayer> layers = ApplicationGIS.addLayers(null,
Collections.singleList(resource) );
return layers.get(0);
}
I added a line to remove all the features from the layer just in case
it was previously used.
Jesse
Le 18-Feb-08 à 2:43 PM, ragga doll a écrit :
> Hi everyone !
>
> I’m working on a plugin for uDig (hydrology’ stream extraction).
> This plugin extracts some Features (MultiLineString) and creates a
> layer with them, programmatically.
> But after layer’s creation, I can’t save my layer : save and save
> all menu entries are not enable (and I don’t want to save the layer
> programmatically).
> So I wonder how can I enable, programmatically, save and save all
> menu entries after creating layer. Or something to tell to the map :
> there is a modification, you can be saved !
> Because when you add a layer to a map, the map’s name doesn’t have a
> “star” upon herself : map*, to signify that the map is modified,
> even it’s a empty layer or not.
>
> Using Udig 1.1 RC12 code sources, java 6 and eclipse
> 3.3.0
>
> Thanks for your help!
>
> Seb.
>
>
> My code :
>
>
> public static Layer CreateNewLayer(Vector<Feature> vf, Map map,
> String name, java.awt.Color color, IProgressMonitor progressmonitor)
> {
> Layer l = null;
>
> IGeoResource resource = null ;
>
> try
> {
> resource = createGeoResource(vf, true, progressmonitor);
> l = map.getLayerFactory().createLayer(resource);
> }
> catch ( IOException e )
> {
> e.printStackTrace();
> }
>
> map.getLayersInternal().add(l);
>
> l.setName(name);
>
> ChangeCouleurLayer(l, color, progressmonitor);
>
> return l;
> }
>
>
> private static IGeoResource createGeoResource(Vector<Feature>
> features, boolean deleteService, IProgressMonitor progressmonitor)
> throws IOException
> {
> IService service = getService(features, deleteService,
> progressmonitor);
>
> List<? extends IGeoResource> resources =
> service.members(progressmonitor);
> for ( IGeoResource resource : resources )
> {
> if ( resource.resolve(FeatureSource.class,
> progressmonitor
> ).getSchema
> ().getTypeName
> ().equals(features.get(0).getFeatureType().getTypeName()) )
> return resource;
> }
> // hopefully will never happen.
> return null;
> }
>
>
> public static IService getService(Vector<Feature> features, boolean
> deleteService, IProgressMonitor monitor) throws IOException
> {
> ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
> List<IResolve> services = catalog.find(new URL("http://localhost/scratch
> "), monitor);
> IService service = null;
> if ( services.isEmpty() )
> {
> service = createService(catalog);
> }
> else
> {
> IResolve resolve = services.get(0);
> if ( resolve instanceof IGeoResource )
> {
> IGeoResource resource = (IGeoResource) resolve;
> service =
> (IService) resource.parent(null);
> }
> else
> if ( resolve instanceof IService )
> {
> service = (IService) services.get(0);
> }
> }
> if ( deleteService )
> {
> catalog.remove(service);
> service = createService(catalog);
> }
>
> MemoryDataStore ds = service.resolve(MemoryDataStore.class, monitor);
> try
> {
> ds.getSchema(features.get(0).getFeatureType().getTypeName());
>
> if ( deleteService )
> throw new IOException("FeatureType already exists in Service");
> }
> catch ( SchemaNotFoundException exception )
> {
> // verified that schema does not yet exist.
> }
> ds.addFeatures(features);
> return service;
> }
>
> private static IService createService(ICatalog catalog) throws
> MalformedURLException
> {
> IService service;
> MemoryServiceExtensionImpl ext = new MemoryServiceExtensionImpl();
>
> java.util.Map<String, Serializable> params = ext.createParams(new
> URL("http://localhost/scratch"));
> service = (MemoryServiceImpl) ext.createService(new URL("http://localhost/scratch
> "), params);
> catalog.add(service);
> return service;
> }
>
> Ne gardez plus qu'une seule adresse mail ! Copiez vos mails vers
> Yahoo! Mail _______________________________________________
> User-friendly Desktop Internet GIS (uDig)
> http://udig.refractions.net
> http://lists.refractions.net/mailman/listinfo/udig-devel
More information about the udig-devel
mailing list