Example of a method for Java to read and write Shapefile with Gdal _java

Source: Internet
Author: User
Tags json

Gdal Introduction

GDAL (Geospatial Data Abstraction Library) is an open source raster space Data Transformation database under the X/mit license agreement. It uses an abstract data model to express the various file formats that are supported. It also has a series of command-line tools for data conversion and processing.

Gdal official website: http://www.gdal.org/, it can support the current popular map data format, including grid and vector map, specific reference to the official website. The library uses C/s + + development, in Java to use the need to compile their own, the specific compilation process here will not say, the following to see the main content of this article.

Examples of Java methods for using Gdal to read and write Shapefile

Read the SHP file and convert it to 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 drive
  ogr. RegisterAll ();
  To support the Chinese path, add the following code,
  gdal. Setconfigoption ("Gdal_filename_is_utf8", "yes");
  To enable the Property table field to support Chinese, 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 succeeded!") " );
  Driver dv = ogr. Getdriverbyname ("Geojson");
  if (dv = null)
  {
   System.out.println ("Open drive failed!") " );
   return;
  }
  SYSTEM.OUT.PRINTLN ("Open drive success!") " );
  Dv. Copydatasource (ds, "D:\\test\\node.json");
  SYSTEM.OUT.PRINTLN ("Conversion successful!") " );
 }
}

Write shp file

Import org.gdal.ogr.*;
Import org.gdal.gdal.*;
  Class WriteShp2 {public static void main (string[] args) {WriteShp2 readshpobj = new WriteShp2 ();
 Readshpobj.writevectorfile ();

  The 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 a property sheet//Below create an integer attribute called FieldID fielddefn ofieldid = new Fielddefn ("FieldID",Ogr.
  Oftinteger);

  Olayer.createfield (Ofieldid); Then create a character-type attribute called FeatureName, with a character length of fielddefn ofieldname = new Fielddefn ("FieldName", ogr.
  oftstring);
  Ofieldname.setwidth (100);

  Olayer.createfield (Ofieldname);

  Featuredefn odefn = Olayer.getlayerdefn ();
  Create triangle elements 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 a rectangular element Feature Ofeaturerectangle = new Feature (ODEFN);
  Ofeaturerectangle.setfield (0, 1);
  Ofeaturerectangle.setfield (1, "rectangular");
  Geometry Geomrectangle = geometry.createfromwkt ("POLYGON ((30 0,60 0,60 30,30 30,30 0)");
  Ofeaturerectangle.setgeometry (Geomrectangle);

  Olayer.createfeature (Ofeaturerectangle);
  Create five angular elements Feature Ofeaturepentagon = new Feature (ODEFN);
  Ofeaturepentagon.setfield (0, 2); OfeatuRepentagon.setfield (1, "V-shaped");
  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 data set creation complete!
 \ 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 for everyone's study or work can bring certain help, if you have questions you can message exchange.

Related Article

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.