当前位置:网站首页>批量修改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();
}
边栏推荐
猜你喜欢
.ts 音频文件转换成 .mp3 文件
接口设计
What does the test plan include?What is the purpose and meaning?
【机器学习】数据科学基础——机器学习基础实践(二)
自动化测试简历编写应该注意哪方面?有哪些技巧?
Do you know the principles of test cases and how to write defect reports?
WAVE SUMMIT 2022深度学习开发者峰会
Summary of steps and methods for installing and uninstalling test cases that you must read
字典
银联最新测试工程师笔试题目,你能得多少分?
随机推荐
有返回值的函数
软件测试的流程规范有哪些?具体要怎么做?
【个人学习总结】CRC校验原理及实现
接口开发规范及测试工具的使用
8.Properties property collection
Sweet alert
1. The concept of flow
8.Properties属性集合
Domestic with Google earth software, see the download 19th level high-resolution satellite images so easy!
STM32F103实现IAP在线升级应用程序
7.Collections tool class
Ontology Development Diary 05-Strive to Understand SWRL (Part 2)
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
6. The File types
[Machine Learning] Basics of Data Science - Basic Practice of Machine Learning (2)
2048小游戏成品源码
Redis high availability
你一定要看的安装及卸载测试用例的步骤及方法总结
1.线程简介
[Environmental Construction] tensorrt