This project has moved. For the latest updates, please go here.

DTED maps in sharmap 1.1

Topics: SharpMap v0.9 / v1.x, SharpMap v2.0
Jun 23, 2015 at 10:49 AM
how to display DTED maps (.dt1,.dt2) in sharpmap 1.1
I want to use the value of the cordinate in a click event
Jun 23, 2015 at 9:36 PM
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 with)
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...
Sharmap.UI.WPF.SharpMapHost map1;
//First you need to initialize GDAL in your constructor using:
 public MainWindow()
            // Only needed if you're using WPF, creating a trigger for mousedown received on
            // sharpmap winform control, MapBox...
            map1.Child.MouseDown += Child_MouseDown;
void LoadRaster()
     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[6];
            // get geo transform for GDAL (see format of array at:
            //getting row and column (remember 0,0, is top left of raster...
            int c = (int)((pt.X - env.MinX) / geotrans[1]);
            int r = (int)((env.MaxY - pt.Y) / (-geotrans[5]));
            //create an array to hold cell value....
            double[] value = new double[1]; 
           //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[0].ToString());
            // get rid of gdal dataset now that we're done