Introduction to GDAL
GDAL (Geospatial Data Abstraction Library) is an open source raster space data conversion library under the X/MIT license agreement. It uses abstract data models to express the supported file formats. It also has a series of command-line tools for data conversion and processing.
GDAL official website: http://www.gdal.org/, it can support various currently popular map data formats, including raster and vector maps, for details, refer to the official website. This library is developed using C/C++. It requires self-compilation when used in Java. I won’t talk about the specific compilation process here. Let’s take a look at the main content of this article.
Example of Java method to read and write shapefile using GDAL
Read the shp file and convert it into json
import org.gdal.ogr.*;import org.gdal.ogr.Driver;import org.gdal.gdal.*;public class GdalShpTest { public static void main(String[] args) { // Register all drivers ogr.RegisterAll(); // To support Chinese paths, please add the following code gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8","YES"); // To make the attribute table field support Chinese, please add the following sentence gdal.SetConfigOption("SHAPE_ENCODING",""); String strVectorFile = "D://test//NODE.shp"; //Open file DataSource ds = ogr.Open(strVectorFile,0); if (ds == null) { System.out.println("Open file failed!" ); return; } System.out.println("Open file successfully!" ); Driver dv = ogr.GetDriverByName("GeoJSON"); if (dv == null) { System.out.println("Open driver failed!" ); return; } System.out.println("Open driver successfully!" ); dv.CopyDataSource(ds, "D://test//node.json"); System.out.println("Conversion successful!"); }} Write a shp file
import org.gdal.ogr.*;import org.gdal.gdal.*;class writeShp2 { public static void main(String[] args) { writeShp2 readshpObj = new writeShp2(); readshpObj.WriteVectorFile(); } static void WriteVectorFile() { String strVectorFile = "D://test//test.shp"; ogr.RegisterAll(); gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); String strDriverName = "ESRI Shapefile"; org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName); if (oDriver == null) { System.out.println(strVectorFile + " Driver is not available! /n"); return; } DataSource oDS = oDriver.CreateDataSource(strVectorFile, null); if (oDS == null) { System.out.println("Create vector file [" + strVectorFile + "] failed! /n"); return; } Layer oLayer = oDS.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null); if (oLayer == null) { System.out.println("Layer creation failed! /n"); return; } // Create the attribute table below// First create an integer attribute called FieldID oFieldID = new FieldDefn("FieldID", ogr.OFTInteger); oLayer.CreateField(oFieldID); // Create a character attribute called FeatureName, with a character length of 50 FieldDefn oFieldName = new FieldDefn("FieldName", ogr.OFTString); oFieldName.SetWidth(100); oLayer.CreateField(oFieldName); FeatureDefn oDefn = oLayer.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); oLayer.CreateFeature(oFeatureTriangle); // Create rectangle feature Feature oFeatureRectangle = new Feature(oDefn); oFeatureRectangle.SetField(0, 1); oFeatureRectangle.SetField(1, "Rectangle"); Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); oFeatureRectangle.SetGeometry(geomRectangle); oLayer.CreateFeature(oFeatureRectangle); // Create pentagon feature Feature oFeaturePentagon = new Feature(oDefn); oFeaturePentagon.SetField(0, 2); oFeaturePentagon.SetField(1, "Pentagon"); Geometry geomPentagon = Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))"); oFeaturePentagon.SetGeometry(geomPentagon); oLayer.CreateFeature(oFeaturePentagon); oDS.SyncToDisk(); System.out.println("/n dataset creation is completed! /n"); }}Get test.dbf, test.shp, test.shx.
test.dbf is as follows:
Open the shape as follows
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.