Practical Implementation of Generating GDB Files with JAVA Based on GDAL

Introduction

In previous blogs, I have introduced the reading of gdb files, the GIS tool GDAL (Part III) reading gdb data, and an article on exploring GDAL’s support for FileGDB drivers on Windows. These articles mainly discuss the reading of gdb files and simple read/write operations. In practical work, gdb is a rich type of database that can record a lot of data; a gdb file can contain multiple layers and multiple attribute lists. This article will deeply explain how to use the Java language to write layers based on GDAL, set Chinese aliases for attribute table fields, and explain through examples, finally using ArcMap to verify spatial and attribute information.

1. Specific Development in JAVA

1. Defining Pom.xml

The sample project uses Maven for jar management, so it is necessary to define the relevant dependencies in Pom.xml. The reference code is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.yelang</groupId><artifactId>gdal_demo1</artifactId><version>0.0.1-SNAPSHOT</version><name>gdal_demo1</name><description>First experiment with gdal</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies><dependency><groupId>org.gdal</groupId><artifactId>gdal</artifactId><version>3.4.3</version><scope>system</scope><systemPath>${project.basedir}/lib/gdal.jar</systemPath></dependency><dependency><groupId>net.sf.ucanaccess</groupId><artifactId>ucanaccess</artifactId><version>4.0.4</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency></dependencies></project>

2. Defining gdb Driver Support

// Register all drivers
gdal.AllRegister();
// To support Chinese paths, please add the following line of code
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
// To enable attribute table fields to support Chinese, please add the following line
gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
/*******************Define Coordinate System********************/
SpatialReference sr = new SpatialReference();
sr.ImportFromEPSG(4326);
/*******************Output gdb Layer, Table********************/
String saveGdbPath = "D:/creategdb/测试生成gdb1210.gdb";
org.gdal.ogr.Driver oDriver = ogr.GetDriverByName("FileGDB");
if (oDriver == null) {
	System.out.println("FileGDB" + " driver is not available!\n");
	return;
}
DataSource dataSourceGDB = oDriver.CreateDataSource(saveGdbPath);

3. Creating gdb Layer

DataSource dataSourceGDB = oDriver.CreateDataSource(saveGdbPath);// gdb Layer
gdbLayer = dataSourceGDB.CreateLayer("ZDXX", sr, ogr.wkbPolygon, null);

4. Creating Attribute Columns and Setting Aliases

// Create a character attribute called FeatureName with a length of 50
FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString);
oFieldName.SetAlternativeName("Feature Type");
oFieldName.SetWidth(100);
gdbLayer.CreateField(oFieldName, 1);

5. Creating Spatial Elements

FeatureDefn oDefn = gdbLayer.GetLayerDefn();// Create triangle feature
Feature oFeatureTriangle = new Feature(oDefn);
oFeatureTriangle.SetField(0, 0);
oFeatureTriangle.SetField(1, "Triangle");
Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");
oFeatureTriangle.SetGeometry(geomTriangle);
gdbLayer.CreateFeature(oFeatureTriangle);

2. Generating gdb Files

After the above series of steps, the generation test of the gdb file is basically completed. For the complete test code, you can click on the Java-based GDAL gdb generation example code, download it locally, and run it normally after ensuring the environment configuration is correct.

1. gdb File Directory

In the generated directory, open the folder ending with the gdb suffix, and you can see the following files (there are many, scroll down to see more)

Practical Implementation of Generating GDB Files with JAVA Based on GDAL

2. ArcMap Verification

Use ArcMap to open the above gdb file to verify whether the file can be opened normally, and check whether the spatial reference settings, attribute settings, spatial information, and attribute aliases have been written correctly, etc.

Practical Implementation of Generating GDB Files with JAVA Based on GDAL

Practical Implementation of Generating GDB Files with JAVA Based on GDAL

Practical Implementation of Generating GDB Files with JAVA Based on GDAL

Practical Implementation of Generating GDB Files with JAVA Based on GDAL

3. Conclusion

The above is the main content of this article. This article will deeply explain how to use the Java language to write layers based on GDAL, set Chinese aliases for attribute table fields, and explain through examples, finally using ArcMap to verify spatial and attribute information. Experiments show that the gdb file’s spatial reference is set correctly, and the attribute information and spatial information are set up, which can be opened normally through ArcMap.

Leave a Comment