Good information for beginners of GDAL

Source: Internet
Author: User

1, compilation and use: http://blog.csdn.net/gisblog/archive/2008/10/06/3021933.aspx
How to Use GDAL in CSharp? Favorites
The concise and efficient GDAL library is favored by developers. Many open-source GIS software and even commercial GIS software use this library. GDAL uses C ++ and is developed in the Visual studio environment. The C and C ++ support certainly does not have any problems. But for C #, Python, and Java, the process is relatively cumbersome, causing a lot of trouble for beginners. This document uses GDAL1.50 as an example to describe in detail the environment configuration of GDAL in CSharpe, we will discuss how to use GDAL in CSharpe in another article. We hope to help people who use GDAL at first.
GDAL has added swig from 1.4.1 to support C # In. NET environment. The swig folder contains the C # source code. The main steps are as follows:

1. Prepare a C ++ compiler. Visual studio2005 is recommended.

2. Download the source code.

Address: http://download.osgeo.org/gdal/gdal150.zip.

3rd extract gdal150.zip to "C: \ gdal-1.5.0", of course, in other folders can also.

4. Modify errors in source code. The contents of line 171st of the source file "C: \ gdal-1.5.0 \ frmts \ leveller \ levellerdataset. cpp" are as follows:

{"?, KPI/180.0, UNITLABEL_DEGREE },

In? Add the double quotation mark (") to the end of the ID, and change it to the following format:

{"? ", KPI/180.0, UNITLABEL_DEGREE },

You can use any text file editing tool to modify it. The author tried to download the GDAL1.52 version, which has the same problem. unexpectedly, the GDAL library will make such a low-level error.

5. Create the target folder "C: \ gdal-runtime" for installation. In later compilation configuration, We will configure GDAL and install it in this folder.

6. Modify the compilation configuration folder C: \ gdal-1.5.0 \ nmake. opt ". The contents of rows 41st to 43rd are as follows:

! IFNDEF GDAL_HOME

GDAL_HOME = "C: \ warmerda \ bld"

! ENDIF

By default, GDAL will be installed in the "C: \ warmerda \ bld" folder. To better understand the installation process, we will modify row 42nd to the following content:

GDAL_HOME = "C: \ gdal-runtime"

7. Compile the source file. Run the "Visual Studio 2005 command prompt" tool (this tool is located in "Start \ Program \ Microsoft Visual Studio 2005 \ Visual Studio Tools ).

Open the Visual Studio 2005 command prompt tool, type "cd C: \ gdal-1.5.0", enter the folder where the source file is located, and then type "nmake/f makefile. vc" to execute the compilation. The compilation process may take some time, depending on the machine performance.

If no modification is made in step 1, a large number of compilation errors will occur. After 4th errors are accumulated, VS2005 will automatically stop compilation.

8. Installation File.

Type "nmake/f makefile. vc install" and add the bin, data, and html folders to the "C: \ gdal-runtime" folder.

After you type "nmake/f makefile. vc devinstall", the lib and include folders are added to the "C: \ gdal-runtime" folder. These two folders are mainly used for C and C ++ development.

The gdal15.dll file has been generated in the "C: \ gdal-runtime \ bin" folder, which is the core of the GDAL library, regardless of the development method, this DLL file is used to perform substantive operations.

9. Compile the C # source file.

Type cd C: \ gdal-1.5.0 \ swig \ csharp to enter the folder where the C # source file is located.

Type "nmake/f makefile. vc" and generate eight DLL files: gdal_csharp.dll, export, ogr_csharp.dll, osr_csharp.dll, gdal_wrap.dll, export, ogr_wrap.dll, and export. The first four file names contain "csharp", which is slightly different from the other four. We will discuss it in detail later.

10. Copy eight DLL files to the installation folder "C: \ gdal-runtime \ bin ".

11. Create environment variables.

Add the Path C: \ gdal-runtime \ bin to the system variable Path. If this variable does not exist, create a new one. If you are using windows2003, you do not need to restart it after adding it. The modification takes effect immediately. If you are not sure whether the modification has taken effect, you can enter "echo % path %" in the doc window ", if the displayed content contains "C: \ gdal-runtime \ bin", the modification takes effect. If you still cannot determine, restart the instance. This is the safest way.

12. add reference to GDAL in VS2005 CSharp project. Create a CShapr application project and add references to four DLL files containing "csharp" in C: \ gdal-runtime \ bin. Now we can use the GDAL function. To test whether GDAL can be run, we add the following code for the Load event of the form:

Private void Form1_Load (object sender, EventArgs e)
{
Try
{
OSGeo. GDAL. Gdal. AllRegister ();
OSGeo. GDAL. Dataset dataSet = OSGeo. GDAL. Gdal. Open (@ "C: \ F-49-32-(10). tif", Access. GA_ReadOnly );
Int w = dataSet. RasterXSize;
Int h = dataSet. RasterYSize;
MessageBox. Show ("image width =" + w + ", height =" + h );
}
Catch (Exception err)
{
Console. WriteLine (err. Message );
}
} To run the program, the first parameter of the Open Method @ "C: \ F-49-32-(10). tif" should be changed to the path where your local image file is located. If the image runs normally, a dialog box is displayed, showing the width and height of the image.

Other questions:

1. About GDAL_DATA system variables. We recommend that you create this system variable for GDAL. From the above discussion, we already know that this system variable is not necessary, but if you want to use examples provided by GDAL, It is necessary, because some methods in the example need to access this system variable.

2. We have mentioned that creating the installation folder is not necessary. If you use the default method for compiling, it will be installed in the C: \ warmerda \ bld folder. No matter how you use GDAL, your C # program must be able to access these DLL files: including the eight DLL files mentioned above, and C: \ gdal-runtime \ bin \ gdal15.dll file. We can summarize it in one sentence: the core issue of using GDAL in the C # environment is how to ensure that the C # program can access these 9 DLL files.

Here, we will review the basic rules for searching DLL files in Windows:

1) the folder where the execution file is located.

2) windows system installation folder, that is, the windows folder is installed. The GetWindowsDirectory () function can provide the path name of this directory.

3) Windows System directory, that is, the System32 subdirectory. You can call the GetSystemDiretory () function to obtain the path name of this directory.

4) the folder configured in the Path of the system variable or the folder specified by the dos path command.

5) all directories in the directory list of images in the network.

When you need to call the DLL file, windows will look for each folder one by one in the above order. If the corresponding file cannot be found, an exception is returned.

According to the above rules, the path of the system variable does not need to contain the GDAL installation path. We only need to copy the nine DLL files to all the locations of the execution file and run them. For example, copy the file to the bin/debug or bin/release folder, so that the running of the program no longer depends on any system variables. This method is very suitable for publishing and packaging final applications. During program development, we still advocate the use of system variables to point to the GDAL installation path, and the development will be more flexible.

 

 

2. GDAL (Geospatial Data into action Library) supports C # language in. NET environment from version 1.4.1. The basic compilation steps refer to "How to Use GDAL in CSharp" (http://blog.csdn.net/gisblog/archive/2008/10/06/3021933.aspx), the bloggers write the steps in great detail, one way reference compilation smoothly, I just summarized several problems I encountered as follows:

A): http://download.osgeo.org/gdal/. the latest version is 1.6.1, Which is 1.6.2,o (Region _ Region) O ~ Every day there are new things.

B) the bottom layer of GDAL is written in C ++. Therefore, the compilation is successful only when the C ++ compiler is used.

C) among the nine dll files generated, gdal16.dll is the core implementation of the gdal library.

D) when using C #, You need to reference four dll with the _ csharp suffix.

E) during the development process, it is best to add the compiled GDAL library bin folder to the system environment variables.

F) In the packaged and released applications, all the nine dll files must be copied to the program directory.

After paying attention to the above details, you can use it smoothly (^ ω ^)

 

3,

Community activities

Share your gains and tell your story! "I am with the Community" topic essay activity is in full swing

[My personal experiences] Gdal In C # interface Compilation
Gdal is an open-source class library for conversion of raster geospatial data formats. It uses a single abstract data model to meet the application requirements of all supported data formats. The latest version of Gdal is GDAL/OGR 1.6.0.
Similar to Gdal, The OGR class library together with Gdal provides operations on vector data.
For more information, see GDAL
The Gdal application of wanderer N is to use C # To operate HDF files.
The following describes how to compile the Gdal library that supports HDF4 and HDF5 for Wanderer N. First, declare that the Gdal compiled by wanderer N still does not support HDF. Here we will write a record, and the second is communication, for help.
Wanderer N tries to compile Gdal versions: gdal-1.5.2, gdal-1.6.0;
Also need swig, the version used here: swigwin-1.3.38; HDF4: 42r4-win-vnet; HDF5: 5-182-win-vs2005
To support HDF5, you also need to download SZip, here with szip21-vs2005;
Okay, now everything is ready. It is not compiled. Although HDF is not supported after compilation, it is still supported For TIFF and so on...
Compile and prepare ......
1. Decompress Gdal, swigwin, HDF, and szip packages;
2. modify.
  • Modify the bug in Gdal, Source Files \ leveller \ levellerdataset. cpp file row 171 {"?, KPI/180.0, UNITLABEL_DEGREE}, "? "To" "? "", Save;
  • Modify nmake in the root directory of Gdal. opt file: MSVC_VER = 1400 (select VS2005); GDAL_HOME = "YourDir", where YourDir creates your own file for storing compiled files, and you define it yourself; SWIG = swig.exe, fill in the swig.exe path after swigwindecompress it here;

3. Modify nmake. opt again to install the Hdf4 and Hdf5 drivers.

  • Locate "Uncomment the following and update to enable ncsa hdf Release 4 support" and remove the three # under this item. Modify HDF4_PLUGIN = YES HDF4_DIR = E: \ JoSn \ sIon \ Gdal0311_10 \ 42r4-win-vnet HDF4_LIB =$ (HDF4_DIR) \ lib \ hd1_.lib
  • Modify the configurations of HDF5 in the same way. Modify as follows: Export = YES HDF5_DIR = E: \ JoSn \ sIon \ Gdal0311_10 \ export SZIP_DIR = E: \ JoSn \ sIon \ Gdal0311_10 \ szip21-vs2005-enc HDF5_LIB = $ (HDF5_DIR) \ hdf5dll. lib \
    $ (SZIP_DIR) \ dll \ szlibdll. lib
  • Modification ended

After the modification is completed, the compilation starts...
1. Open the VS 2005 command line and locate the Gdal folder;
2. nmake/f makefile. vc;
3. nmake/f makefile. vc install;
4. nmake/f makefile. vc devinstall. After this step is completed, a series of files are generated at GDAL_HOME. gdal15.dll or gdal16.dll will be generated in the bin directory, depending on the gdal version.
5. cd swig \ csharp, locate the csharp folder under the gdal directory.
6. nmake/f makefile. vc. After this step is completed, 8 dll files will be generated in the csharp directory. Copy the eight files to the location of gdal15.dll or gdal16.dll (see step 1 ).
Test program ......
1. Add references to four files * _ csharp. dll.
2. OSGeo. GDAL. Gdal. AllRegister ();
3. OSGeo. GDAL. Dataset ds = OSGeo. GDAL. Gdal. Open (@ "d: \ test. tiff", OSGeo. GDAL. Access. GA_ReadOnly );
4. int w = ds. RasterXSize; int h = ds. RasterYSize; MessageBox. Show ("image width =" + w + ", height =" + h );
5. There is no problem with the test of tiff files, but it is useless to change to HDF4 or HDF5 files. The prompt is: this file is not supported currently.
Seek communication and help
1. hope there will be a deep researcher on Gdal to teach wanderer N compilation to support HDF.
2. wanderer N later used FWTools. It is said that the Gdal compiled by FWTools supports HDF4 and HDF5, but it is not clear that wanderer N is dull and will not use FWTools2.2.8, or FWTools2.2.8 has bugs. In short, there is an exception, OSGeo. GDAL. the type of the initial value of GdalPINVOKE triggers an exception. "This is the same as the exception in the previous compilation of the Gdal library by wanderer N. I don't know why. I hope the GIS colleagues can give me some advice.
3. You can help me and look forward to ing

 

 

4 \

 

Compile, install, and use GDAL in Windows
Time: 03:39:20 Source: RealGIS Author: tqx clicks:
1. Introduction to GDAL
GDAL (homepage: http://www.gdal.org/) is a database that operates on Raster geographic data, written by C. It includes reading, writing, converting, and processing various raster data formats. The current version of GDAL also contains the OGR library, which is also written in C and used to operate vector geographic data, the GDAL Library provides the ability to operate on Raster and vector geographic data. What's even more gratifying is that GDAL is open-source and cross-platform. Currently, many software products use GDAL, such as GRASS, QGIS, and Google Earth. GDAL supports C/C ++, VB, Python, Java, C #/. NET, Ruby, and Perl. Currently, there are not many libraries that support such languages. The format supported by GDAL is amazing, and you can see the Support Formats (http://www.gdal.org/formats_list.html) on its home page ).

2. Download GDAL
Binary (Compiled): http://trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries

Source file (not compiled): http://trac.osgeo.org/gdal/wiki/DownloadSource

The above two links list various versions of various platforms. You can download them as needed. At the time of writing this article, the latest Release version of GDAL is 1.6.0.

3. Pre-compilation Configuration
Directly downloading compiled binary files is easy, but if you want to learn more about GDAL by yourself and use GDAL for development, you 'd better download the source file and compile it by yourself. The compilation method is as follows:

1) Prepare a C ++ compiler, such as Visual Studio2005 or 2003.

2) decompress the downloaded source code to C: \ gdal-X.X.X (X. X. X indicates the version number, of course you can also name it yourself), put the file in other directories can also be.

3) An error occurred while modifying the source file. The contents of row 171st of the source file \ frmts \ leveller \ levellerdataset. cpp are as follows:

{"?, KPI/180.0, UNITLABEL_DEGREE}

In? Double quotation marks (") are added after the" # "symbol, which is changed to the following format:

{"? ", KPI/180.0, UNITLABEL_DEGREE },

For some reason, version 1.6.0 still does not modify this BUG.

4) modify the compilation and installation directory.

Open the configuration file nmake. opt in any text editor, such as Notepad, and find the following code:

! IFNDEF GDAL_HOME

GDAL_HOME = "C: \ warmerda \ bld"

! ENDIF

BINDIR = $ (GDAL_HOME) \ bin

PLUGINDIR = $ (BINDIR) \ gdalplugins

PY_INST_DIR = $ (GDAL_HOME) \ pymod

LIBDIR = $ (GDAL_HOME) \ lib

INCDIR = $ (GDAL_HOME) \ include

DATADIR = $ (GDAL_HOME) \ data

HTMLDIR = $ (GDAL_HOME) \ html

GDAL_HOME = "C: \ warmerda \ bld" is the compiled installation directory. Here you can modify the directory you want, but you can also choose not to change it.

4. Added Python support
Modify PY_INST_DIR = $ (GDAL_HOME) \ pymod to change the path to the Lib \ site-packages folder under python. Change PYDIR = "C: \ Software \ Python24" to the python installation path.

5. Add other support
When compiling GDAL, you can add other support as needed, such as ProJ and GeoTiff. find the relevant configuration section in opt and remove "#", that is, cancel the annotation, and then modify the relevant path. For example:

PROJ.4 stuff section in nmake. opt

# PROJ.4 stuff
# Uncomment the following lines to link PROJ.4 library statically. Otherwise
# It will be linked dynamically during runtime.
# PROJ_FLAGS =-DPROJ_STATIC
# PROJ_INCLUDE =-ID: \ GDAL \ proj-4.5.0 \ src
# PROJ_LIBRARY = D: \ GDAL \ proj-4.5.0 \ src \ proj_ I .lib

This section controls the link mode. By default, it is annotated, that is, dynamic links are used. You only need to copy the dynamic library of proj. To use static links, cancel the comments in gdal and set the corresponding path of proj source code. The advantage of static links is that the dependency between libraries will be determined at the beginning of loading, which can avoid the inexplicable phenomenon of Using Dynamic Link Libraries without dependency libraries.

6. Compile GDAL source code
Open the Visual Studio 2005 command prompt tool, which is located in start \ Program \ Microsoft Visual Studio 2005 \ Visual Studio Tools ". You can also open the "cmd" window directly, but you need to register the VC compilation environment and use the following code (related to the VS installation path of your machine ):

E: \ program files \ Microsoft Visual Studio 8 \ VC \ bin \ vcvars32.bat

At a command prompt, use the "cd" command to navigate to the GDAL folder, such as C: \ gdal-1.6.0, and enter the following command:

Nmake/f makefile. vc

Compilation takes some time. Please wait.

7. Install GDAL
After compilation, enter the following command:

Nmake/f makefile. vc install

After the command is executed, the bin, data, and html folders are created in the installation directory. The bin contains the GDAL Core File gdalXX. dll (XX is the version number, for example, gdal16.dll ).

Run the following command:

Nmake/f makefile. vc devinstall

After the command is executed, create the include and lib folders in the installation directory. These two folders are used for development.

8. compile C # source code
Enter:

C: \ gdal-1.5.0 \ swig \ csharp

Enter the folder where the C # source file is located, and then enter:

Nmake/f makefile. vc

After compilation, the following 8 dll files are generated:

Gdal_csharp.dll,

Gdalconst_csharp.dll,

Ogr_csharp.dll,

Osr_csharp.dll

Gdal_wrap.dll,

Gdalconst_wrap.dll,

Ogr_wrap.dll,

Osr_wrap.dll.

Copy the above eight dll files to the bin folder in the installation directory.

9. Use GDAL
In VS development, add the lib and include paths of GDAL to Tools> Options> Projects (Directories is vc6.0, then, place the generated dll file in the executable file folder or directly in the system32 folder.

If you want to use the exe program in the bin directory, you need to create an environment variable, that is, add the bin Path of the GDAL installation directory (such as C: \ gdal-1.6.0 \ bin) to the Path in the environment variable. Add method: Right-click "my computer"-> "properties"-> "advanced", click "environment variables", and find the Path in the system variables, if no Path variable can be added, add the Path. Multiple paths are separated by semicolons.

 
This article from 3SDN reprinted please in the form of links to indicate the source URL: http://www.3sdn.net/giszt/osgis/2008-12-11/148.html

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.