读取
public static void readVectorFile() {
String strVectorFile = "D:\\Java\\Projects\\dx\\poly.shp";
//注册所有的驱动
ogr.RegisterAll();
//支持中文路径名
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//属性表字段支持中文
gdal.SetConfigOption("SHAPE_ENCODING", "");
//打开数据
DataSource ds = ogr.Open(strVectorFile, 0);
if (null == ds) {
System.out.println("Open [" + strVectorFile + "] failed!");
return;
}
System.out.println("Open [" + strVectorFile + "] success!");
//获取该数据源中的图层数(一般shp数据的图层只有一个,mdb、dxf等图层会有多个)
int layerCount = ds.GetLayerCount();
//获取第一个图层
Layer layer = ds.GetLayerByIndex(0);
if (null == layer) {
System.out.println("获取第0个图层失败!");
return;
}
//对图层进行初始化(如果对图层进行了过滤操作,执行此句后,之前过滤全部清空)
layer.ResetReading();
//通过属性表的SQL语句对图层中的要素进行筛选
// layer.SetAttributeFilter("")
//通过指定的几何对象对图层中的要素进行筛选
// layer.SetSpatialFilter();
//通过指定的四至范围对图层中的要素进行筛选
// layer.SetSpatialFilterRect();
//获取图层中的属性表表头并输出
System.out.println("属性表结构信息:");
FeatureDefn defn = layer.GetLayerDefn();
//获取字段数量
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() + ")");
}
//输出图层中的要素个数
System.out.println("要素个数:" + layer.GetFeatureCount(0));
Feature feature = null;
//获取要素中的属性表内容
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("数据集关闭!");
}
输出:
Open [D:\Java\Projects\dx\poly.shp] success!
属性表结构信息:
AREA: Real(12.3)
EAS_ID: Integer64(11.0)
PRFEDEA: String(16.0)
要素个数:10
215229.266
168
35043411
数据集关闭!
写入
示例(创建ESRI的SHP文件)
捋了下大致流程:
- 设置文件类型 > 创建数据源 > 创建图层 > 创建要素 > 在图层上创建属性表(各个字段) > 获取添加属性表的图层的要素定义 > 依照该要素定义生成新的要素 > 设置要素对象的参数值 > 在图层上创建该要素
/**
* 写入矢量
*
* @param strVectorFile
*/
public static void writeVectorFile(String strVectorFile) {
//注册所有的驱动
ogr.RegisterAll();
//支持中文路径
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
//属性表支持中文
gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
String strDriverName = "ESRI Shapefile";
//指定驱动
Driver driver = ogr.GetDriverByName(strDriverName);
if (null == driver) {
System.out.println(strDriverName + " 驱动不可用!");
return;
}
//创建数据源
DataSource dataSource = driver.CreateDataSource(strVectorFile, null);
if (null == dataSource) {
System.out.println("创建矢量文件【" + strVectorFile + "】失败!");
return;
}
//创建图层,创建一个多边形图层(此处未指定空间参考,如需要需在这里进行指定)
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 + "】创建失败!");
return;
}
//创建属性表
//创建第一个字段(整型)
FieldDefn fieldIDDefn = new FieldDefn("FieldID", ogr.OFTInteger);
layer.CreateField(fieldIDDefn, 1);
//创建第二个字段(字符,长度50)
FieldDefn fieldNameDefn = new FieldDefn("FieldName", ogr.OFTString);
fieldNameDefn.SetWidth(100);
layer.CreateField(fieldNameDefn, 1);
//创建第三个字段(字符)
FieldDefn fieldDefnArea = new FieldDefn("FieldArea", ogr.OFTString);
fieldDefnArea.SetWidth(50);
layer.CreateField(fieldDefnArea);
FeatureDefn featureDefn = layer.GetLayerDefn();
//创建要素:三角形
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);
//创建要素:矩形
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);
//创建要素:相交部分
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
文件
删除
/**
* 矢量数据管理:删除
*
* @param strVerctorFile
*/
public static void vectorDelete(String strVerctorFile) {
// TODO: 2021/8/2 生成的三个文件 shp shx dbf只能删除 shx
try {
//注册所有的驱动
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());
}
}
重命名
额,貌似就是先复置旧的给新的,然后再删除旧的。。。
/**
* 矢量数据管理:重命名
*
* @param strOldName
* @param strNewName
*/
static public void vectorRename(String strOldName, String strNewName) {
//注册所有驱动
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));
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135474.html