Discussion:
[udig-devel] How to add GeoTiff from code
Ned Haubein
2010-12-20 18:54:31 UTC
Permalink
Hi,

I have a geotiff file that I can drag and drop into uDig with no problems.? I'm trying to add this same file from code, and can't quite figure out how to do it.

I've tried two ways, neither with success:

First (try/catch ommitted for readability)

GeoTiffFormat format = new GeoTiffFormat();
GeoTiffReader geoReader = new GeoTiffReader(rasterFile);
GridCoverage2D gc = (GridCoverage2D) geoReader.read(null);
Style style = getStyle();? // method to return a style
new DefaultMapContext().addLayer( gc , style );

This doesn't seem to do anything in the ui - I think b/c it's not working on the MapContext for my active map - but I can't figure out how to get access to that MapContext.

Second approach:

GeoTiffServiceImpl gtsi = new GeoTiffServiceImpl(uri.toURL(), new GeoTiffFormatFactorySpi());
GeoTiffGeoResourceImpl gtiffgeo = new GeoTiffGeoResourceImpl(gtsi, "Data"); // igeoresource
List <IGeoResource> geoResourceList = new LinkedList<IGeoResource>();
geoResourceList.add(gtiffgeo);
ApplicationGIS.addLayersToMap(ApplicationGIS.getActiveMap(), geoResourceList, -1);

In this case, a layer is added, but it says that no CRS is found. I don't see where to set it, and going into the properties dialog from the layer in the layers view and setting it doesn't make it display anyway.

===============

Can anyone point me to an example or spot what I've done wrong? Seems like it should be doable since I can use drag and drop to insert it, I'm just at a dead end as for how to do it.

Oh,this is all in the 1.2 release and associated SDK.

Thanks for any help,

- Ned
Jody Garnett
2010-12-21 00:30:07 UTC
Permalink
Second approach is good; usually the CRS data has to be baked into your TIFF file (that is what makes it a GeoTIFF).

If you don't have such information you could consider adding a "world file" and a "prj" sidecar file. ie same filename but with different extensions.

You can also right click on your layer and "force" the CRS. Users can do this to make up for data such as this where the CRS was not provided. You may need to look into the code for layer properties to see what was actually set to make this happen. And then you could do the same thing as a programmer.

Do note however that this is a workaround for data that is missing and should be provided.
Jody
Hi,
I have a geotiff file that I can drag and drop into uDig with no problems. I'm trying to add this same file from code, and can't quite figure out how to do it.
First (try/catch ommitted for readability)
GeoTiffFormat format = new GeoTiffFormat();
GeoTiffReader geoReader = new GeoTiffReader(rasterFile);
GridCoverage2D gc = (GridCoverage2D) geoReader.read(null);
Style style = getStyle(); // method to return a style
new DefaultMapContext().addLayer( gc , style );
This doesn't seem to do anything in the ui - I think b/c it's not working on the MapContext for my active map - but I can't figure out how to get access to that MapContext.
GeoTiffServiceImpl gtsi = new GeoTiffServiceImpl(uri.toURL(), new GeoTiffFormatFactorySpi());
GeoTiffGeoResourceImpl gtiffgeo = new GeoTiffGeoResourceImpl(gtsi, "Data"); // igeoresource
List <IGeoResource> geoResourceList = new LinkedList<IGeoResource>();
geoResourceList.add(gtiffgeo);
ApplicationGIS.addLayersToMap(ApplicationGIS.getActiveMap(), geoResourceList, -1);
In this case, a layer is added, but it says that no CRS is found. I don't see where to set it, and going into the properties dialog from the layer in the layers view and setting it doesn't make it display anyway.
===============
Can anyone point me to an example or spot what I've done wrong? Seems like it should be doable since I can use drag and drop to insert it, I'm just at a dead end as for how to do it.
Oh,this is all in the 1.2 release and associated SDK.
Thanks for any help,
- Ned
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net
http://lists.refractions.net/mailman/listinfo/udig-devel
Continue reading on narkive:
Search results for '[udig-devel] How to add GeoTiff from code' (Questions and Answers)
10
replies
what is GIS?
started 2006-06-13 03:41:34 UTC
science & mathematics
Loading...