I just started playing around with SharpMap last night but here are some snippets from the test code I built that will let you load dteds (and most other GDAL formats). There may very well be a more elegant way of doing this, but I'm used to working with
GDAL directly for other applications...
1) reference sharpmap extensions (in addition to the other base requirements)
2) Use GDAL extension to load the dted raster. If you rebuild sharpmap with GDAL binaries from
, you gain access to additional GDAL plugins that allow you to open a few more formats (MrSid read support is particularly useful for the project I plan on using it
3) I use Sharpmap through WPF, and am using the SharpMap WPF Map control, so you also need to capture mousedown event of the underlying WinForm control so that you can handle it from your WPF window...
//First you need to initialize GDAL in your constructor using:
// Only needed if you're using WPF, creating a trigger for mousedown received on
// sharpmap winform control, MapBox...
map1.Child.MouseDown += Child_MouseDown;
var result = new System.Windows.Forms.OpenFileDialog();
if (result.ShowDialog() == System.Windows.Forms.DialogResult.OK)
// The following line adds the layer to the map, assigning the base filename as the title of the layer...
map1.MapLayers.Add(new SharpMap.Layers.GdalRasterLayer(System.IO.Path.GetFileNameWithoutExtension(result.FileName), result.FileName));
// zoom to map extent
void Child_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
// get mouse coordinate...
var pt = map1.CurrentMouseCoordinate;
// Get row and column (assuming raster projection same as that of map!)
// otherwise you'll need to use OSR (or something else) to project coordinate first...
var env = rLayer.Envelope; // get extent of raster
// open raster using gdal...
var ds = OSGeo.GDAL.Gdal.Open(rLayer.Filename, OSGeo.GDAL.Access.GA_ReadOnly);
double geotrans = new double;
// get geo transform for GDAL (see format of array at: http://www.gdal.org/gdal_tutorial.html)
//getting row and column (remember 0,0, is top left of raster...
int c = (int)((pt.X - env.MinX) / geotrans);
int r = (int)((env.MaxY - pt.Y) / (-geotrans));
//create an array to hold cell value....
double value = new double;
//fetch raster band (for dted we only have a single band, hence band = 1)
var b = ds.GetRasterBand(1); //note gdal uses base 1 index for raster indices...
//read a single value from the raster, starting at the row and column
// we calculated above...
b.ReadRaster(c, r, 1, 1, value, 1, 1, 0, 0);
System.Windows.Forms.MessageBox.Show("Value is " + value.ToString());
// get rid of gdal dataset now that we're done