GdalRasterLayer Improvement part 2 + Automatic Reprojection Patch

Topics: Algorithms, SharpMap v0.9 / v1.x, WinForms Controls
Dec 20, 2007 at 6:50 PM
This is for SharpMap v0.9

I've created a patch that overhauls the current (and my other patches) GdalRasterLayer. This patch has all the same features as my first patch with a new technique for placing the raster pixels in the correct ground coordinate which fixes all the previous problems I was having with my first attempt. I have also hopefully fixed all the build and dependency problems that are associated with the GdalRasterLayer all around. I also added an ElevationLayer that inherites from GdalRasterLayer for elevation display. There is no need to download the FWTools or put its dlls in the executable folder. I have included all the gdal dependencies (I had to rebuild gdal), so using the dlls from FWTools is not advised.

I added a projection attribute to the map which can be set by the programmer, or is automatically set by the first layer with a projection. When being rendered, GdalRasterLayers and VectorLayers with ShapeFile datasources will automatically reproject to the map's projection.

I have included a small win form demo to showcase some of these new features. Make sure to put the files from the Dependencies folder into the same folder as the executable to be able to use the Gdal layers.

I made a fix in ProjNET for the reprojection stuff, so I have included ProjNET in the solutions. I forgot to note the change in ProjNET in the readme, so for everyone who is curious, I fixed the Inverse() and Invert() function in ConcatenatedTransform.cs.

This patch uses the MapBox rather than the MapImage, so if you would like to use this in your code, you will need to change your MapImages to MapBoxes to get all the features.

I also added some new functionality to my MapToolBar, which is also showcased in the demo.

Hopefully I touched on all the major changes. Make sure to check out the readme for a more exhaustive rundown. Good luck and let me know of any problems!


Dec 21, 2007 at 8:26 AM
Thank you for the improvements!

Unfortunately, there seems to be a consistent problem that appears in the sample WinForms application. While shapefiles load just fine, attempting to manually load a raster file produces the following error message:

"There was a problem loading raster. The type initializer for 'OSGeo.GDAL.gdalPINVOKE' threw an exception'

This is exactly the same error message that I was getting in my last attempt to get the GDALRaster layer to work (see So I suspect this is not a coincidence.
Dec 21, 2007 at 12:22 PM

dan3258 wrote:
I made a fix in ProjNET for the reprojection stuff, so I have included ProjNET in the solutions. I forgot to note the change in ProjNET in the readme, so for everyone who is curious, I fixed the Inverse() and Invert() function in ConcatenatedTransform.cs.

Could you send a patch for Proj.Net code that you have fixed?
Maybe you could simply open an issue in the Proj.NEt website and upload the modified code.
Thanks :)
Dec 21, 2007 at 1:57 PM
Dimitris: Make sure you put all the files from the Dependencies folder in the same folder as your .exe. If that doesn't work, try putting the dlls from FWTools that are NOT in the Dependencies folder in with the .exe.

D_Guidi: I created a issue in Proj.Net
Dec 24, 2007 at 9:54 AM
Edited Dec 24, 2007 at 4:24 PM
I put all the Dependencies files in the exe folder and got the same error message. I installed FWTools 2.0.3 (without copying any dll's from the FWTools folder), tried again and this time it worked without any errors. Thanks!

EDIT: It seems that if you run the solution in VS2005 the raster layer loads correctly. If, however, you convert the solution to VS2008 the raster layer again produces the above error. I haven't been able to determine the reason for this.

Could you, if possible, demonstrate how to set the map projection to UTM? (Programatically, that is).
Jan 8, 2008 at 2:43 PM
It seems that I left a Post Build event on ProjNet that will result in a error. The build should still be successful and the program should still run, but this error is probably annoying and misleading.

To get rid of it, go to the properties for ProjNet. Select Build Events and then remove the line in the Post-build Event box.
Jan 23, 2008 at 6:37 AM
Hi Dan
Thank you for these improvements.
I load a Jpeg (1024*512) file using your path in SharpMap V0.9 (and VS2005 ).But raster pixels is not in the correct ground coordinate.
I used following code to place them in correct coordinate, using your patch, But I think I should first define a coordinate system for my Jpeg file,
But I don't Know how to do it.


Feb 5, 2008 at 8:24 AM

Just wondering when a raster gets reprojected and displayed on the map, is there any re sampling algorithm applied on the image? and is it configurable?
I have had a brief look through the code, but cant find anything, does the Gdal readraster method apply default resampling? Also i couldnt find this from the sparse doco on the c# wrapper for gdal.

Feb 6, 2008 at 2:16 PM

First, does your jpeg have geographic information (eg. a jgw file). This is needed for knowing where place the image in ground space.

To set a layer's projection:

BaL.Projection = "GEOGCS\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]";

If a map does not already have a projection, it will set its projection to the first layer with projection information. The map will take care of reprojecting your layer (if needed).

You do not need either of these lines:



I have not implemented any resampling. The ReadRaster function seems to use nearest neighbor, I have not found an option to change this.

Sep 17, 2009 at 1:37 PM


I used your code to load a tif file with a Cassini_Soldner projection. Then I chose Sample/Projection/Geograpic i.e. the map's projection will be set to


But Cassini_Soldner is not provided by ProjNet, in the method CreateCoordinateOperation() only mercator, albers and lambert projections are supported.

 Is there another possibility to use a map with a Cassini_Soldner projection and display points with GPS coordinates?