GDAL介紹
GDAL(Geospatial Data Abstraction Library)是一個在X/MIT許可協(xié)議下的開源柵格空間數(shù)據(jù)轉(zhuǎn)換庫。它利用抽象數(shù)據(jù)模型來表達所支持的各種文件格式。它還有一系列命令行工具來進行數(shù)據(jù)轉(zhuǎn)換和處理。
GDAL官方網(wǎng)址:http://www.gdal.org/,它能支持當(dāng)前流行的各種地圖數(shù)據(jù)格式,包括柵格和矢量地圖,具體參考官方網(wǎng)站。該庫使用C/C++開發(fā),在Java中使用需要自己編譯,具體編譯過程這里就不說了,下面來看看本文的主要內(nèi)容吧。
Java使用GDAL讀寫shapefile的方法示例
讀取shp文件,并把它轉(zhuǎn)化為json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public class GdalShpTest { public static void main(String[] args) { // 注冊所有的驅(qū)動 ogr.RegisterAll(); // 為了支持中文路徑,請?zhí)砑酉旅孢@句代碼 gdal.SetConfigOption( "GDAL_FILENAME_IS_UTF8" , "YES" ); // 為了使屬性表字段支持中文,請?zhí)砑酉旅孢@句 gdal.SetConfigOption( "SHAPE_ENCODING" , "" ); String strVectorFile = "D:\\test\\NODE.shp" ; //打開文件 DataSource ds = ogr.Open(strVectorFile, 0 ); if (ds == null ) { System.out.println( "打開文件失敗!" ); return ; } System.out.println( "打開文件成功!" ); Driver dv = ogr.GetDriverByName( "GeoJSON" ); if (dv == null ) { System.out.println( "打開驅(qū)動失敗!" ); return ; } System.out.println( "打開驅(qū)動成功!" ); dv.CopyDataSource(ds, "D:\\test\\node.json" ); System.out.println( "轉(zhuǎn)換成功!" ); } } |
寫shp文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
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 + " 驅(qū)動不可用!\n" ); return ; } DataSource oDS = oDriver.CreateDataSource(strVectorFile, null ); if (oDS == null ) { System.out.println( "創(chuàng)建矢量文件【" + strVectorFile + "】失敗!\n" ); return ; } Layer oLayer = oDS.CreateLayer( "TestPolygon" , null , ogr.wkbPolygon, null ); if (oLayer == null ) { System.out.println( "圖層創(chuàng)建失敗!\n" ); return ; } // 下面創(chuàng)建屬性表 // 先創(chuàng)建一個叫FieldID的整型屬性 FieldDefn oFieldID = new FieldDefn( "FieldID" , ogr.OFTInteger); oLayer.CreateField(oFieldID); // 再創(chuàng)建一個叫FeatureName的字符型屬性,字符長度為50 FieldDefn oFieldName = new FieldDefn( "FieldName" , ogr.OFTString); oFieldName.SetWidth( 100 ); oLayer.CreateField(oFieldName); FeatureDefn oDefn = oLayer.GetLayerDefn(); // 創(chuàng)建三角形要素 Feature oFeatureTriangle = new Feature(oDefn); oFeatureTriangle.SetField( 0 , 0 ); oFeatureTriangle.SetField( 1 , "三角形" ); Geometry geomTriangle = Geometry.CreateFromWkt( "POLYGON ((0 0,20 0,10 15,0 0))" ); oFeatureTriangle.SetGeometry(geomTriangle); oLayer.CreateFeature(oFeatureTriangle); // 創(chuàng)建矩形要素 Feature oFeatureRectangle = new Feature(oDefn); oFeatureRectangle.SetField( 0 , 1 ); oFeatureRectangle.SetField( 1 , "矩形" ); Geometry geomRectangle = Geometry.CreateFromWkt( "POLYGON ((30 0,60 0,60 30,30 30,30 0))" ); oFeatureRectangle.SetGeometry(geomRectangle); oLayer.CreateFeature(oFeatureRectangle); // 創(chuàng)建五角形要素 Feature oFeaturePentagon = new Feature(oDefn); oFeaturePentagon.SetField( 0 , 2 ); oFeaturePentagon.SetField( 1 , "五角形" ); 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數(shù)據(jù)集創(chuàng)建完成!\n" ); } } |
得到test.dbf, test.shp, test.shx。
test.dbf如下:
打開形狀如下
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://www.cnblogs.com/kaituorensheng/p/5868010.html#_label1