Java uses gdal

Source: Internet
Author: User

Before using Java to call gdal, describe the compilation. During compilation, JRE, ant, and swig are required. If no, download these two items first. After the download, install ant and swig and decompress them directly, no installation required. Next, use NotePad to open nmake. Opt and find about 86th rows, as shown below:

# SWIG Java settings!IFNDEF JAVA_HOMEJAVA_HOME = "C:\Program Files\Java\jdk1.6.0_26"!ENDIF!IFNDEF ANT_HOMEANT_HOME=F:\Work\GDAL\gdal_need\apache-ant-1.8.4!ENDIFJAVADOC=$(JAVA_HOME)\bin\javadocJAVAC=$(JAVA_HOME)\bin\javacJAVA=$(JAVA_HOME)\bin\javaJAR=$(JAVA_HOME)\bin\jarJAVA_INCLUDE=-I$(JAVA_HOME)\include -I$(JAVA_HOME)\include\win32

Modify the paths after java_home and ant_home to the local Java and ant paths, as long as they are modified to the main folder (the folder with the bin folder ), if the folder path contains spaces, enclose the path in double quotation marks. Then, replace the swig directory with the local directory. Next we can compile it. The steps are as follows:

1. Compile the Java version gdal:

First, use the command line to compile the C ++ version of gdal. For detailed steps, refer to the blog: http://blog.csdn.net/liminlu0314/article/details/6937194 and use the command line for compiling.

After the compilation is complete, use the CD command to switch to the swig directory, and then execute the compilation. The commands are as follows:

# The following three sentences are used to compile the C ++ version. In fact, only the first sentence is required. The next two sentences are the c: \ warmerda \ bldnmake/F makefile for installation. vcnmake/F makefile. VC installnmake/F makefile. VC devinstall # The next step is to compile the gdalcd swignmake/F makefile of the Java version. VC Java

Under normal circumstances, after the above execution, the Java version of gdal should be compiled. You will see some compiled files in the swig \ Java directory, mainly including the following files:

gdalconstjni.dllgdaljni.dllogrjni.dllosrjni.dllgdal.jar

If the preceding file is used, the compilation is successful. If you do not see the file, view the output information during command line compilation and re-generate the file based on the output troubleshooting error. Copy the preceding five files and the gdal19.dll generated during gdal compilation to a new folder for subsequent calls.

2. Use eclipse to create a new project named gdaltest, and then create a new class named gdaltestapp in this project. Open gdaltestapp. Java, modify the content below, and save the content, as shown in 1.


Figure 1 eclipse Interface

Import Org. gdal. gdal. dataset; import Org. gdal. gdal. driver; import Org. gdal. gdal. gdal; import Org. gdal. gdalconst. gdalconstconstants; public class gdaltestapp {/*** @ Param ARGs */public static void main (string [] ARGs) {string filename_tif = "F: \ data \ Users "; gdal. allregister (); dataset hdataset = gdal. open (filename_tif, gdalconstconstants. ga_readonly); If (hdataset = NULL) {system. err. println ("gdalopen failed-" + gdal. getlasterrorno (); system. err. println (gdal. getlasterrormsg (); system. exit (1);} driver hdriver = hdataset. getdriver (); system. out. println ("Driver:" + hdriver. getshortname () + "/" + hdriver. getlongname (); system. out. println ("size is" + hdataset. getrasterxsize () + "," + hdataset. getrasterysize (); hdataset. delete (); // optional gdal. gdaldestroydrivermanager ();}}

After saving, copy the generated DLL and jar files to the folder where the project is located, right-click the project, and choose build path> Configure build path. The Properties dialog box is displayed, switch to the libraries tab, click Add jars, and select gdal. jar and DLL files with INI names, add them to Lib, as shown in 2, and then confirm.

Figure 2 Java configuration page

Next, click "run". Normally, text similar to the following will be output on the console. The first line is the image drive format, and the second line is the image size.

Driver: GTiff/GeoTIFFSize is 8499, 3403

............................................................ Gorgeous split line .........................................................
.......................................... The following content was updated on August 1, January 30, 2013 ................................................

The previous example does not show how to read data. Add the following code today:

Import Org. gdal. gdal. band; import Org. gdal. gdal. dataset; import Org. gdal. gdal. driver; import Org. gdal. gdal. gdal; import Org. gdal. gdalconst. gdalconstconstants; public class gdaltestapp {/*** @ Param ARGs */public static void main (string [] ARGs) {string filename_tif = "E: \ Austra \ DEM \ srtm_65_18.tif "; gdal. allregister (); dataset hdataset = gdal. open (filename_tif, gdalconstconstants. ga_readonly); If (hdataset = NULL) {system. err. println ("gdalopen failed-" + gdal. getlasterrorno (); system. err. println (gdal. getlasterrormsg (); system. exit (1);} driver hdriver = hdataset. getdriver (); system. out. println ("Driver:" + hdriver. getshortname () + "/" + hdriver. getlongname (); int ixsize = hdataset. getrasterxsize (); int iysize = hdataset. getrasterysize (); system. out. println ("size is" + ixsize + "," + iysize); band = hdataset. getrasterband (1); // here is the DEM data, so declare an int array for storage. If it is another data type, declare the corresponding type to int Buf [] = new int [ixsize]; for (INT I = 0; I <10/* iysize */; I ++) {band. readraster (0, I, ixsize, 1, Buf); // read a row of Data // The output image element value is below, for convenience, I only output data in the 10x10 range in the upper left corner of the For (Int J = 0; j <10/* ixsize */; j ++) system. out. print (BUF [J] + ","); system. out. println ("\ n");} hdataset. delete (); // optional gdal. gdaldestroydrivermanager ();}}

In Java, the interface used to read data is readraster. This function has been overloaded for all data types. Therefore, to read data of a certain type, you only need to declare the corresponding type. Running the above program will output the following content:

Driver: GTiff/GeoTIFFSize is 6001, 600164, 64, 64, 63, 62, 62, 64, 63, 61, 61, 61, 62, 63, 66, 64, 64, 63, 63, 62, 61, 62, 61, 63, 64, 65, 62, 63, 61, 63, 64, 64, 63, 63, 62, 63, 65, 64, 64, 62, 63, 64, 61, 63, 63, 63, 64, 65, 63, 62, 64, 64, 63, 62, 63, 62, 62, 63, 64, 64, 64, 64, 63, 63, 64, 65, 62, 61, 63, 62, 63, 62, 64, 63, 64, 63, 60, 62, 61, 62, 63, 62, 65, 63, 63, 62, 60, 64, 63, 61, 62, 63, 65, 64, 63, 62, 61, 64, 63, 60, 61, 

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.