当前位置:网站首页>批量修改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();
}
边栏推荐
- 1. The concept of flow
- Onnx - environment build 】 【 tensorrt
- JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
- 手机APP测试流程规范和方法你知道多少?
- 接口测试主要测试哪方面?需要哪些技能?要怎么学习?
- Go-控制语句那些事
- Another implementation of lateral view explode
- 4. Character stream
- 2048小游戏成品源码
- Cisco common basic configuration of common commands
猜你喜欢

本体开发日记05-努力理解SWRL(下)

Another implementation of lateral view explode
测试用例的原则、缺陷报告怎么写你都知道吗?

Cisco common basic configuration of common commands

可以写进简历的软件测试项目实战经验(包含电商、银行、app等)

STM32F103实现IAP在线升级应用程序

A Practical Guide to Building OWL Ontologies using Protege4 and CO-ODE Tools - Version 1.3 (7.4 Annotation Properties - Annotation Properties)

QT sets the icon of the exe executable

手机APP测试流程规范和方法你知道多少?

有返回值的函数
随机推荐
常用功能测试的检查点与用例设计思路
安装torch_sparse失败解决方法
软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
.ts 音频文件转换成 .mp3 文件
Lecture 4 SVN
Global 19 Google Satellite Map Free View Download
Cisco common basic configuration of common commands
黑盒测试常见错误类型说明及解决方法有哪些?
关于一次性通过CISSP考试的一点经验分享
class object property method class member
2.字节流
银联最新测试工程师笔试题目,你能得多少分?
RPC服务远程漏洞
字符串
常用命令之思科常用基础配置
MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression
[Personal study summary] CRC verification principle and implementation
1. Introduction to threads
Tigase插件编写——注册用户批量查询
unittest测试框架原理及测试流程解析,看完绝对有提升