Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am working on an application that makes use of the c# Mapscript bindings. When I manually set and/or append to the GDAL_DATA, GDAL_DRIVER_PATH, PROJ_LIB, and PATH environment variables - the application code works beautifully.

For a number of reasons, I am trying to avoid permanently setting/changing these environment variables. I am under the impression that by using the .NET frameworks Environment.SetEnvironmentVariable method, that I should be able to set these environment variables at runtime for the current process.

This approach seems to work in some capacity - as the mapscript objects initialize properly (i.e., I dont get PInvoke errors...etc). In my code snippet below, I get an error at map.draw() complaining about GDAL_DATA and gcs.csv.

// add some new environment vars
// TODO check if they may already exist - in which case append to

// append to existing PATH variable
string pathVariable = Environment.GetEnvironmentVariable("PATH")
pathVariable += MY_PATH_VARS

mapObj map = new mapObj(transmitterMapFile);
map.setSize(request.Width, request.Height);
map.setExtent(extent[0], extent[1], extent[2], extent[3]);

imageObj img = map.draw();

Does anyone see something wrong with this approach? Or is there a better way to set these variables at runtime?

share|improve this question
up vote 2 down vote accepted

I use a similar approach in Python with MapScript on Windows and it works fine.

Careful with the following code:

pathVariable += MY_PATH_VARS
Environment.SetEnvironmentVariable("PATH", MY_PATH_VARS)

I believe it should be:

pathVariable += MY_PATH_VARS
Environment.SetEnvironmentVariable("PATH", pathVariable)

I'd look at the C# sourcecode for GdalGoneWild - "Using GDAL in .NET without a big headache"

The environment variable setup code should be of particular interest. The author also mentions in an associated blog post:

One little strange thing I noticed was that I was not able to set up the GDAL_DATA environment variable programmatically. So instead I used Gdal.SetConfigOption which overrides the environment variable.

A couple of final things to check. Are you registering the GDAL drivers elsewhere in your code?


Finally the PATH must contain semi-colons between each specified path.

share|improve this answer
Thanks for the response. I had a typo in my code snippet - I am correctly referencing the appended 'pathVariable'. However, my code still bombs at imageObj img = map.draw(). – user890 Nov 15 '11 at 20:20
Links attached - looks like there may well be an issue with GDAL_DATA – geographika Nov 15 '11 at 21:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.