This how I do it and it works for esri asc and should also for tiffs
(I had to remove some code, but I think it should compile and work):
private void loadFileInMap( File file ) throws Exception {
URL fileUrl = file.toURI().toURL();
CatalogPlugin cp = CatalogPlugin.getDefault();
IServiceFactory sf = cp.getServiceFactory();
List<IService> services = sf.createService(fileUrl);
List<IGeoResource> resources = new ArrayList<IGeoResource>();
for( IService service : services ) {
List< ? extends IGeoResource> geoResource =
service.resources(new NullProgressMonitor());
if (geoResource != null) {
for( IGeoResource iGeoResource : geoResource ) {
resources.add(iGeoResource);
}
}
}
if (resources.size() == 0) {
return;
}
IMap map = ApplicationGIS.getActiveMap();
LayerFactory layerFactory = map.getLayerFactory();
for( IGeoResource resource : resources ) {
Layer layer = layerFactory.createLayer(resource);
AddLayerCommand cmd = new AddLayerCommand(layer);
map.sendCommandASync(cmd);
}
}
Andrea
On Wed, Jun 29, 2011 at 4:10 PM, Alberto Debiasi
<[email protected]> wrote:
> I tried with the two solutions but nothing works:
>
> 1 Solution:
> I'm only new to this so forgive me if I'm barking up the wrong tree, I
> was digging around in the CatalogPlugin plugin and think that's what
> you're looking for.
>
> You can manually create your service and add it to the catalog like so.
>
> IServiceFactory serviceFactory =
> CatalogPlugin.getDefault().
> getServiceFactory();
> IService service = serviceFactory.createService(new URL("file://"+path
> <file://%22+path/> ));
>
> try {
> IServiceInfo info = service.getInfo( null ); // try connecting to
> make sure the service works
>
> if (info != null) {
> CatalogPlugin.getDefault().getLocalCatalog().add( service ); //
> add service
> }
> }catch (IOException couldNotConnect ){
> service.dispose(new NullProgressMonitor());
> }
>
>
> serviceFactory.createService returns List<IService> and not IService.
>
> And if I write :
>
> List<IService> services = serviceFactory.createService(url);
> IService service=services.iterator().next();
>
> I have an exception.
>
>
>
>
>
> 2 solution.
> However the catalog provides the acquire method that will do all the
> work for you.
>
> IService service =
> CatalogPlugin.getDefault().getLocalCatalog().acquire(new
> URL("file://"+path <file://"+path> ), new NullProgressMonitor());
>
> Simply call this from your menu contribution and provide the path to
> your GeoTiff.
>
> One thing to note is that the acquire method douse not guarantee that a
> geotiff service will be used to render your layer. If it I important
> that your layer use a geotiff render you will have to use the manual
> service creation method.
>
>
> the class ICatalog doesnt' have the method acquire.
> I tried with:
>
> ICatalog icatalog =CatalogPlugin.getDefault().getLocalCatalog();
> icatalog.find(url, new NullProgressMonitor());
>
> icatalog.findService(new URL("file://"+selPath));
>
> but nothing append....I don't see the geotiff image inside the 2d map....
>
> ________________________________
>
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Alberto
> Debiasi
> Sent: Wednesday, 22 June 2011 5:52 PM
> To: ML udig-users; User-friendly Desktop Internet GIS
> Subject: [udig-devel] add GeoTifff Image
>
>
> Hi,
> I wuold like to add a geoTiff image on a 2D map simply pressing
> a button.
>
> The functionality is the same as "right click on a map" -> "add
> files" -> "and select a GeoTiff image"
> but I don't know which classes I should use and in which way.
>
> Thanks.
> Alberto
> _______________________________________________
> 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