[Gdal-dev] GDAL, and C# Access

Frank Warmerdam warmerdam at pobox.com
Thu Nov 4 11:55:45 EST 2004


Folks,

There has been talk from time to time about using GDAL from C# or as a .NET
assemblage.  I can't say I grok all the issues in this area, but I thought
I should pass on something Roger Bedell sent me over a year ago.  He sent
a whole project showing how GDAL could be called from C#, which I am finally
looking at briefly.  I couldn't actually run the project, but I did dig
through the code, and found the following was the core of the GDAL
interaction.

The point is that it is fairly easy to call out to C entry points from C#.
At some point I might well try and maintain an appropriate set of C#
declarations for the GDAL C API though I am not ready to do that just yet.


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Application.Run(new Form1());
		}

		public enum GA_Access
		{
			/*! Read only (no update) access */ GA_ReadOnly = 0,
			/*! Read/write access. */           GA_Update = 1
		}

		[DllImport("gdal11.dll")]
		public static extern IntPtr
			GDALOpen( string pszFilename, GA_Access eAccess );

		[DllImport("gdal11.dll")]
		public static extern IntPtr
			GDALGetDatasetDriver( IntPtr GDALDatasetH );


		[DllImport("gdal11.dll")]
		public static extern void
			GDALAllRegister();

		[DllImport("gdal11.dll",CharSet=CharSet.Ansi)]
		public static extern string
			GDALGetDriverShortName( IntPtr GDALDriverH );

		[DllImport("gdal11.dll",CharSet=CharSet.Ansi)]
		public static extern string
			GDALGetDriverLongName( IntPtr GDALDriverH );

		[DllImport("gdal11.dll")]
		public static extern int GDALGetRasterXSize( IntPtr GDALDatasetH );

		[DllImport("gdal11.dll")]
		public static extern int GDALGetRasterYSize( IntPtr GDALDatasetH );
		
		[DllImport("gdal11.dll")]
		public static extern int GDALGetRasterCount( IntPtr GDALDatasetH );

		[DllImport("gdal11.dll",CharSet=CharSet.Ansi)]
		public static extern string GDALGetProjectionRef( IntPtr GDALDatasetH );


		public enum CPLErr
		{
			CE_None = 0,
			CE_Debug = 1,
			CE_Warning = 2,
			CE_Failure = 3,
			CE_Fatal = 4

		}

		[DllImport("gdal11.dll")]
		public static extern CPLErr GDALGetGeoTransform( IntPtr GDALDatasetH, [In,Out] double [] Transform );

		[DllImport("gdal11.dll")]
		public static extern IntPtr
		GDALGetDriverByName( string s );

		[DllImport("gdal11.dll")]
		public static extern IntPtr
		GDALCreateCopy( IntPtr GDALDriverH, string s, IntPtr GDALDatasetH,
						int i, IntPtr j, IntPtr k, IntPtr l);


		[DllImport("gdal11.dll")]
		public static extern void
			GDALClose( IntPtr GDALDriverH );


		[DllImport("gdal11.dll")]
		public static extern void
			GDALDestroyDriverManager();

		private void button1_Click(object sender, System.EventArgs e)
		{
			GDALAllRegister();
			
			IntPtr hDataset = GDALOpen( Application.StartupPath + "\\..\\..\\SampleData\\SARA_IRS.TIF", GA_Access.GA_ReadOnly);
			if( hDataset == (IntPtr)null )
			{
				MessageBox.Show("Could not open Raster File.");
			}

			IntPtr   hDriver;
	
			hDriver = GDALGetDatasetDriver( hDataset );

			//This seems to wig out .NET...Looks to me GDAL has become unstable due to this call.
//			MessageBox.Show("Driver: " + GDALGetDriverShortName( hDriver ) + " " + GDALGetDriverLongName( hDriver ) );

			MessageBox.Show("Size is " + GDALGetRasterXSize( hDataset ) + " " + GDALGetRasterYSize( hDataset ) + " " + 
GDALGetRasterCount( hDataset ) );

//			string ProjectionRef = null;

			//Also this call destabilized the stack. Looks to me like strings are not handled
			//the way they should be by .NET.
//			ProjectionRef = GDALGetProjectionRef( hDataset );

//			if( ProjectionRef != null )
//				MessageBox.Show("Projection is " + ProjectionRef );

			double [] adfGeoTransform = new double[6];
			if( GDALGetGeoTransform( hDataset, adfGeoTransform ) == CPLErr.CE_None )
			{
				MessageBox.Show( "Origin = " + adfGeoTransform[0] + " " + adfGeoTransform[3] );

				MessageBox.Show( "Pixel Size = " +	adfGeoTransform[1] + " " + adfGeoTransform[5] );
			}


			//Convert to some other format (JPG for example)
			hDriver = GDALGetDriverByName( "JPEG" );

			//Do a straight copy.
			IntPtr hOutDS = GDALCreateCopy( hDriver, Application.StartupPath + "\\..\\..\\SampleData\\SARA_IRS.JPG", hDataset,
				0, (IntPtr)null,
				(IntPtr)null, (IntPtr)null);

			if(hOutDS != (IntPtr)null)
				GDALClose( hOutDS );

			//Shut everything down.
			GDALClose( hDataset );
			GDALDestroyDriverManager();

			MessageBox.Show("Export to JPEG complete.");
		}
	}

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list