GDAL's java environment configuration and Analysis of converting shp to json

Source: Internet
Author: User

GDAL's java environment configuration and Analysis of converting shp to json

Overview:

Two days ago, a colleague asked me another question. The question was probably like packing the shp data bit zip format and uploading it. After the upload is complete, it will be displayed on the web. This requirement was previously met on Arcgis Online, so I am still familiar with it, so I told him: First decompress the zip file, then read the shp data and convert it to Geojson and return it to the foreground, which is displayed on the web. Fortunately, I quickly converted shp to Geojson for my colleagues, and asked me how to display Geojson in Arcgis for js, I intercepted an Arcgis for js API and gave it to him to implement the above functions. At the same time, I asked him how to implement the conversion. He told me that it was implemented by GDAL and gave me the key code. Because the code is C, after a weekend of hard work, I implemented it in JAVA and shared it with you.

 

Implementation:

It is difficult to use GDAL in java. The attempt process in the middle is skipped here and the result is directly described.

The first is jdk. The number of digits of the downloaded GDAL must be consistent with that of the JDK. The number of digits of my JDK is 32 bits, So I downloaded the 32-bit GDAL for your convenience, and upload this to the Baidu network disk, for: Link: http://pan.baidu.com/s/1mhrywn2 password: lsch. After the GDAL is downloaded, copy all the dll Files to the jdk installation path "C: \ Program Files (x86) \ Java \ jdk1.7.0 _ 17 \ jre \ bin.

Add the JDAL. jar file to your Java lib reference.

Finally, the Code is implemented. The key code for implementation is as follows:

 

Package com. lzugis. gdal; 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, add the following code gdal. setConfigOption ("GDAL_FILENAME_IS_UTF8", "YES"); // Add the following gdal to make the Attribute Table field support Chinese characters. setConfigOption ("SHAPE_ENCODING", ""); String strVectorFile = "D: \ data \ china \ capital. shp "; // open the data DataS Ource ds = ogr. Open (strVectorFile, 0); if (ds = null) {System. out. println ("failed to Open the file! "); Return;} System. out. println (" file opened successfully! "); Driver dv = ogr. GetDriverByName (" GeoJSON "); if (dv = null) {System. out. println (" failed to open the Driver! "); Return;} System. out. println (" drive enabled successfully! "); Dv. CopyDataSource (ds," D :\\ data \ china \ capital. geojson "); System. out. println (" conversion successful! ");}}
The printed information is as follows:

 


 

 

 

 

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.