当前位置:网站首页>批量修改Shapefile属性表的一种方法(使用gdal.jar)
批量修改Shapefile属性表的一种方法(使用gdal.jar)
2022-08-09 09:29:00 【我一时想不起】
批量修改Esri Shapefile属性的方法有很多中,比如使用各类GIS软件自带的字段计算器、函数库等等。
也可以使用GDAL有关的函数库来处理矢量文件,这样操作起来应该会更自由一些、个性化可能更强大一些。GDAL是由开源地理信息基金会发布的一个开源函数库,目前有C、C++、Python、Java版本的API。可以用来做地理信息系统的开发工作。下载及介绍链接:GDAL — GDAL documentation
以下是使用gdal.jar来完成对矢量文件的操作。
public void WriteShpDbf() {
// 注册所有的驱动
ogr.RegisterAll();
// 为了支持中文路径,请添加下面这句代码
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
// 为了使属性表字段支持中文,请添加下面这句
gdal.SetConfigOption("SHAPE_ENCODING", "");
String FileName = "F:\\test\\25.shp";
// 注意空格是必须的
String strDriverName = "ESRI Shapefile";
// 加载驱动
org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName);
if (oDriver == null) {
System.out.println("打开驱动失败!");
return;
}
// 打开文件,获取数据源
DataSource oDS = oDriver.Open(FileName, 1);
if (oDS == null) {
System.out.println("打开矢量文件【" + FileName + "】失败!\n");
return;
}
//打开图层,一般ESRI Shapefile只有一个图层
Layer oLayer = oDS.GetLayer(0);
if (oLayer == null) {
System.out.println("打开图层失败!\n");
return;
}
System.out.println("图层:" + oLayer.GetName());
int ct = (int) oLayer.GetFeatureCount();
System.out.println("要素数量" + ct);
//遍历要素,一个要素可以理解为Shapefile属性表中的一行
for (int i = 0; i < ct; i++) {
//根据下标获取要素
Feature feature = oLayer.GetFeature(i);
//给integer型字段bh赋值
feature.SetField("bh", (i + 1));
//将要素应用到图层
oLayer.SetFeature(feature);
}
//将图层数据保存到硬盘
oLayer.SyncToDisk();
}
边栏推荐
- 软件测试分析流程及输出项包括哪些内容?
- Ontology Development Diary 03 - When debugging is in progress
- 8.Properties属性集合
- 6.File类
- 软件测试的流程规范有哪些?具体要怎么做?
- Onnx - environment build 】 【 tensorrt
- 7.Collections工具类
- "The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
- Summary of steps and methods for installing and uninstalling test cases that you must read
- Do you know the principles of test cases and how to write defect reports?
猜你喜欢
随机推荐
性能测试包括哪些方面?分类及测试方法有哪些?
游戏测试的概念是什么?测试方法和流程有哪些?
黑盒测试常见错误类型说明及解决方法有哪些?
Max Flow P
8.Properties属性集合
Do you know the principles of test cases and how to write defect reports?
2048小游戏成品源码
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
4. Character stream
条件和递归
3.编码方式
软件测试流程包括哪些内容?测试方法有哪些?
Ontology development diary 02 - simple sparql query
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
日期操作比较全面得代码
你一定要看的安装及卸载测试用例的步骤及方法总结
关于一次性通过CISSP考试的一点经验分享
RPC服务远程漏洞
7.FileFilter接口
[Personal study summary] CRC verification principle and implementation