First of all great example! We get asked all the time on the GeoTools list 
about how to parse GML … the docs must be especially bad :(  

The createTemporaryResource is just that - a temporary holding area for the 
results of an operation, which the user can choose to save or throw away. When 
saving to a shapefile any maps that were using the temp resource are updated) 
or throw away.

There are two cheap ways to avoid this:

1. When importing it the first time, invite the user to save (said it was 
cheap).

2. Do the trick we used for TAB format, convert the file each time they ask.

   You need to make a new IService, and when the user asks for a DataStore copy 
your GML content
   into a temp shapefile (this is best) or MemoryDataStore.

  Because the Service is hanging out in the catalog it will be restored each 
time, and it can create a
  FeatureSource when asked (allowing the content to be drawn).

Here is the example:
- 
https://github.com/uDig-Community/udig-community/tree/master/lreed/net.refractions.udig.catalog.mitab

The good bit is in MTABService 
(https://github.com/uDig-Community/udig-community/blob/master/lreed/net.refractions.udig.catalog.mitab/src/net/refractions/udig/catalog/mitab/MITABService.java)
 where it uses a utility class to stage the data into a temporary shapefile:

    private synchronized DataStore getDS() throws IOException {
        if (null == this.dataStore) {
            MITABReader reader = null;

            try {
                reader = new MITABReader(this.file);
            } catch(IOException e) {
                this.message = e;
                throw e;
            } catch(Throwable t) {
                this.message = t;
                throw new IOException("Could not connect to to datastore.", t);
            }

            Map<String, Object> connect = new HashMap<String, Object>();
            connect.put("url", reader.getShapeFile().toURL());
            this.dataStore = DataStoreFinder.getDataStore(connect);

            this.message = null;
        }

        return this.dataStore;
    }
  
When you do get this to work please contribute it back - it would be great to 
have GML support in uDig.

(When GeoTools implemented a a GMLDataStore we were able to support GML out of 
the box, but the maintainer stepped down due to the volume of questions about 
GML)  

--  
Jody Garnett


On Saturday, 27 April 2013 at 10:17 PM, Amit Shukla wrote:

> Hello there, I want to add local gml to map. I am able to add file locally 
> but when i close udig it will ask to save file how to avoid it.... I think it 
> is due to createTemporaryResource. How to load gml permanently so that it 
> will no ask file to save and automatically load while application start next 
> time
>  
> Here is my sample code    
>  
> String workDirPath = "/home/amit/Music/sample.gml";
>                 org.geotools.xml.Configuration configuration = new 
> org.geotools.gml2.GMLConfiguration();  
>                 org.geotools.xml.Parser parser = new org.geotools.xml.Parser( 
> configuration );
>                 InputStream xml = null;
>                         try {
>                             GML gml = new GML(Version.WFS1_0);  
>                             xml = new FileInputStream(workDirPath);
>                             //FeatureCollection<SimpleFeatureType, 
> SimpleFeature> featureCollection = gml.decodeFeatureCollection(xml);
>                             SimpleFeatureCollection featureCollection = 
> gml.decodeFeatureCollection(xml);
>                             IGeoResource resource = 
> CatalogPlugin.getDefault().getLocalCatalog()
>                                     
> .createTemporaryResource(featureCollection.getSchema());
>                             FeatureStore<SimpleFeatureType, SimpleFeature> 
> store = resource.resolve(FeatureStore.class, new NullProgressMonitor());
>                             store.addFeatures(featureCollection);
>                             
> ApplicationGIS.addLayersToMap(ApplicationGIS.getActiveMap(), 
> Collections.singletonList(resource), -1);
>                         } catch (FileNotFoundException e1) {
>                             // TODO Auto-generated catch block
>                             e1.printStackTrace();
>                         } catch (IOException e1) {
>                             // TODO Auto-generated catch block
>                             e1.printStackTrace();
>                         } catch (SAXException e1) {
>                             // TODO Auto-generated catch block
>                             e1.printStackTrace();
>                         } catch (ParserConfigurationException 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
>  
>  


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

Reply via email to