讀取
public static void readVectorFile() {
String strVectorFile = "D:\\Java\\Projects\\dx\\poly.shp";
//注冊所有的驅(qū)動
ogr.RegisterAll();
//支持中文路徑名
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//屬性表字段支持中文
gdal.SetConfigOption("SHAPE_ENCODING", "");
//打開數(shù)據(jù)
DataSource ds = ogr.Open(strVectorFile, 0);
if (null == ds) {
System.out.println("Open [" + strVectorFile + "] failed!");
return;
}
System.out.println("Open [" + strVectorFile + "] success!");
//獲取該數(shù)據(jù)源中的圖層數(shù)(一般shp數(shù)據(jù)的圖層只有一個赃泡,mdb粟害、dxf等圖層會有多個)
int layerCount = ds.GetLayerCount();
//獲取第一個圖層
Layer layer = ds.GetLayerByIndex(0);
if (null == layer) {
System.out.println("獲取第0個圖層失敗潦俺!");
return;
}
//對圖層進行初始化(如果對圖層進行了過濾操作曼玩,執(zhí)行此句后僧凰,之前過濾全部清空)
layer.ResetReading();
//通過屬性表的SQL語句對圖層中的要素進行篩選
// layer.SetAttributeFilter("")
//通過指定的幾何對象對圖層中的要素進行篩選
// layer.SetSpatialFilter();
//通過指定的四至范圍對圖層中的要素進行篩選
// layer.SetSpatialFilterRect();
//獲取圖層中的屬性表表頭并輸出
System.out.println("屬性表結(jié)構(gòu)信息:");
FeatureDefn defn = layer.GetLayerDefn();
//獲取字段數(shù)量
int field = defn.GetFieldCount();
for (int attr = 0; attr < field; attr++) {
FieldDefn fieldDefn = defn.GetFieldDefn(attr);
System.out.println(fieldDefn.GetNameRef() + ": " +
fieldDefn.GetFieldTypeName(fieldDefn.GetFieldType()) + "(" + fieldDefn.GetWidth() + "." + fieldDefn.GetPrecision() + ")");
}
//輸出圖層中的要素個數(shù)
System.out.println("要素個數(shù):" + layer.GetFeatureCount(0));
Feature feature = null;
//獲取要素中的屬性表內(nèi)容
while ((feature = layer.GetNextFeature()) != null) {
for (int i = 0; i < field; i++) {
FieldDefn fieldDefn = defn.GetFieldDefn(i);
int type = fieldDefn.GetFieldType();
switch (type) {
case ogr.OFTString:
System.out.println(feature.GetFieldAsString(i) + "\t");
break;
case ogr.OFTReal:
System.out.println(feature.GetFieldAsDouble(i) + "\t");
break;
case ogr.OFTInteger:
System.out.println(feature.GetFieldAsInteger(i) + "\t");
break;
default:
System.out.println(feature.GetFieldAsString(i) + "\t");
break;
}
}
//獲取要素中的幾何體
Geometry geometry = feature.GetGeometryRef();
break;
}
System.out.println("數(shù)據(jù)集關(guān)閉!");
}
輸出:
Open [D:\Java\Projects\dx\poly.shp] success!
屬性表結(jié)構(gòu)信息:
AREA: Real(12.3)
EAS_ID: Integer64(11.0)
PRFEDEA: String(16.0)
要素個數(shù):10
215229.266
168
35043411
數(shù)據(jù)集關(guān)閉继效!
寫入
示例(創(chuàng)建ESRI的SHP文件)
捋了下大致流程:
- 設(shè)置文件類型 > 創(chuàng)建數(shù)據(jù)源 > 創(chuàng)建圖層 > 創(chuàng)建要素 > 在圖層上創(chuàng)建屬性表(各個字段) > 獲取添加屬性表的圖層的要素定義 > 依照該要素定義生成新的要素 > 設(shè)置要素對象的參數(shù)值 > 在圖層上創(chuàng)建該要素
/**
* 寫入矢量
*
* @param strVectorFile
*/
public static void writeVectorFile(String strVectorFile) {
//注冊所有的驅(qū)動
ogr.RegisterAll();
//支持中文路徑
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//屬性表支持中文
gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
String strDriverName = "ESRI Shapefile";
//指定驅(qū)動
Driver driver = ogr.GetDriverByName(strDriverName);
if (null == driver) {
System.out.println(strDriverName + " 驅(qū)動不可用症杏!");
return;
}
//創(chuàng)建數(shù)據(jù)源
DataSource dataSource = driver.CreateDataSource(strVectorFile, null);
if (null == dataSource) {
System.out.println("創(chuàng)建矢量文件【" + strVectorFile + "】失敗瑞信!");
return;
}
//創(chuàng)建圖層厉颤,創(chuàng)建一個多邊形圖層(此處未指定空間參考,如需要需在這里進行指定)
Layer layer = dataSource.CreateLayer("TestPolygon", null, ogr.wkbPolygon, null);
//指定空間參考的話長這個樣子(以WGS84示例):
// SpatialReference spatialReference=new SpatialReference("");
// spatialReference.SetWellKnownGeogCS("WGS84");
// Layer layer = dataSource.CreateLayer("TestPolygon", spatialReference, ogr.wkbPolygon, null);
if (null == layer) {
System.out.println("圖層【" + layer + "】創(chuàng)建失敺布颉逼友!");
return;
}
//創(chuàng)建屬性表
//創(chuàng)建第一個字段(整型)
FieldDefn fieldIDDefn = new FieldDefn("FieldID", ogr.OFTInteger);
layer.CreateField(fieldIDDefn, 1);
//創(chuàng)建第二個字段(字符,長度50)
FieldDefn fieldNameDefn = new FieldDefn("FieldName", ogr.OFTString);
fieldNameDefn.SetWidth(100);
layer.CreateField(fieldNameDefn, 1);
//創(chuàng)建第三個字段(字符)
FieldDefn fieldDefnArea = new FieldDefn("FieldArea", ogr.OFTString);
fieldDefnArea.SetWidth(50);
layer.CreateField(fieldDefnArea);
FeatureDefn featureDefn = layer.GetLayerDefn();
//創(chuàng)建要素:三角形
Feature featureTriangle = new Feature(featureDefn);//個人覺得有點Bean工廠跟BeanDefinition的感覺
featureTriangle.SetField(0, 0);
featureTriangle.SetField(1, "三角形");
Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,35 0,10 15,0 0))");
featureTriangle.SetGeometry(geomTriangle);
featureTriangle.SetField(2, geomTriangle.Area());
layer.CreateFeature(featureTriangle);
//創(chuàng)建要素:矩形
Feature featureRectangle = new Feature(featureDefn);
featureRectangle.SetField(0, 1);
featureRectangle.SetField(1, "矩形");
Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");
featureRectangle.SetGeometry(geomRectangle);
featureRectangle.SetField(2, geomRectangle.Area());
layer.CreateFeature(featureRectangle);
//創(chuàng)建要素:相交部分
Feature featureIntersection = new Feature(featureDefn);
featureIntersection.SetField(0, 2);
featureIntersection.SetField(1, "intersection");
Geometry intersection = geomTriangle.Intersection(geomRectangle);
featureIntersection.SetGeometry(intersection);
featureIntersection.SetField(2, intersection.GetArea());
layer.CreateFeature(featureIntersection);
System.out.println("Finish!");
}
生成的.shp
文件
image.png
刪除
/**
* 矢量數(shù)據(jù)管理:刪除
*
* @param strVerctorFile
*/
public static void vectorDelete(String strVerctorFile) {
// TODO: 2021/8/2 生成的三個文件 shp shx dbf只能刪除 shx
try {
//注冊所有的驅(qū)動
ogr.RegisterAll();
File file = new File(strVerctorFile);
Driver driver = null;
{
//打開矢量
DataSource dataSource = ogr.Open(strVerctorFile, 0);
if (null == dataSource) {
file.delete();
System.out.println("null == dataSource");
return;
}
driver = dataSource.GetDriver();
if (null == driver) {
file.delete();
System.out.println("null == driver");
return;
}
}
if (driver.DeleteDataSource(strVerctorFile) == ogr.OGRERR_NONE) {
System.out.println("driver.DeleteDataSource(strVerctorFile) == ogr.OGRERR_NONE");
file.delete();
return;
} else {
file.delete();
}
System.out.println("Finish!");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
重命名
額秤涩,貌似就是先復(fù)置舊的給新的帜乞,然后再刪除舊的。筐眷。黎烈。
/**
* 矢量數(shù)據(jù)管理:重命名
*
* @param strOldName
* @param strNewName
*/
static public void vectorRename(String strOldName, String strNewName) {
//注冊所有驅(qū)動
ogr.RegisterAll();
File file = new File(strOldName);
Driver driver = null;
{
//打開矢量
DataSource dataSource = ogr.Open(strOldName, 0);
if (null == dataSource) {
file.renameTo(new File(strNewName));
return;
}
driver = dataSource.GetDriver();
if (null == driver) {
file.renameTo(new File(strNewName));
return;
}
DataSource dataSourceNew = driver.CopyDataSource(dataSource, strNewName, null);
if (null == dataSourceNew) {
file.renameTo(new File(strNewName));
return;
}
}
if(driver.DeleteDataSource(strOldName) == ogr.OGRERR_NONE){
return;
}else{
file.renameTo(new File(strNewName));
}
}