使用gdal读取shp及filegdb文件

一、使用qgis开源工具构建两个文件,分别是filegdb和shp,每个文件包含一个图层,图层内容只包含一个字段:id,有两个数据行,图层几何为多边形,图层都是如下的效果。

二、使用rust读取上述文件

  • rust依赖
rust 复制代码
[dependencies]
gdal = "0.17.1"
  • 读取shp
rust 复制代码
let source = "E:/a.shp";
let data = gdal::Dataset::open(source).unwrap();
let layer = data.layer(0).unwrap();
println!("layer_count:{}",layer.feature_count());
let start = 0;
let end = layer.feature_count();
for i in start..end{
    match layer.feature(i){
        Some(feature) => {
            let filed_id= &feature.field_as_integer_by_name("id").unwrap();
            println!("++数据索引:{}++字段内容:{:?}++几何:{:?}",i,filed_id,feature.geometry());
        },
        None => {
            println!("++{}++None",i);
        },
    };
}

读取结果:

layer_count:2

++数据索引:0++字段内容:Some(1)++几何:Some(POLYGON ((-0.518043358996392 0.521232057117657,4.73046834618934 0.672511281341224,5.00976830389323 -2.57346955999557,-0.808980814937951 -2.73621961134104,-0.518043358996392 0.521232057117657),(0.925006422473741 -0.235197478165903,0.622431468294519 -1.95716715064606,3.67145600656206 -1.66637545113517,3.63654351184907 -0.188647804914247,0.925006422473741 -0.235197478165903),(-0.180555910104184 -0.561039123478221,0.215119029976337 0.044101814636796,-0.192193408341846 -0.060635653828846,-0.180555910104184 -0.561039123478221),(-0.285293394243145 -1.67800799341694,-0.366755881906781 -2.29442185095562,0.57588147534387 -2.34093376312268,-0.285293394243145 -1.67800799341694),(4.34643090434648 -1.49187934036916,3.69473100303738 -2.35256150119173,4.43953089024778 -2.35256150119173,4.61409336381271 -1.86411893486817,4.742105844427 -1.44534463965808,4.47444338496076 -1.22429231197673,4.48608088319843 -0.700680602759594,4.71883084795167 -0.700680602759594,4.1835059290192 -0.654133864500525,4.34643090434648 -1.49187934036916)))

++数据索引:1++字段内容:Some(2)++几何:Some(POLYGON ((0.749541894170621 -3.51691666514472,3.61879402562659 -5.06406242230235,0.81986670131415 -5.02186753801624,0.749541894170621 -3.51691666514472)))

从结果看到,读取shp文件的数据行索引是从 0 开始的

  • 读取filegdb
rust 复制代码
let source = "E:/a_gdb.gdb";
let data = Dataset::open(source).unwrap();
let mut layer = data.layer_by_name("a").unwrap();
println!("layer_count:{}",layer.feature_count());

let start = 1;
let end = layer.feature_count()+1;
for i in start..end{
    match layer.feature(i){
        Some(feature) => {
            let filed_id= &feature.field_as_integer_by_name("id").unwrap();
            println!("++数据索引:{}++字段内容:{:?}++几何:{:?}",i,filed_id,feature.geometry());
        },
        None => {
            println!("++{}++None",i);
        },
    };
}

读取结果:

layer_count:2

++数据索引:1++字段内容:Some(1)++几何:Some(MULTIPOLYGON (((-0.518043358999989 0.52123205700002,4.73046834600001 0.672511281000027,5.00976830399998 -2.57346955999998,-0.808980814999984 -2.73621961100002,-0.518043358999989 0.52123205700002),(0.925006422000024 -0.235197477999975,0.622431468 -1.95716715100002,3.67145600700002 -1.66637545100002,3.636543512 -0.188647805000016,0.925006422000024 -0.235197477999975),(-0.18055591000001 -0.561039123,0.215119029999983 0.044101814999976,-0.19219340799998 -0.060635654,-0.18055591000001 -0.561039123),(-0.285293394 -1.67800799299999,-0.366755882 -2.29442185099998,0.575881475000017

-2.34093376300001,-0.285293394 -1.67800799299999),(4.34643090399999 -1.49187934000003,3.69473100300002 -2.35256150100003,4.43953089000001 -2.35256150100003,4.61409336399998 -1.86411893500002,4.74210584399998 -1.44534463999997,4.47444338499997 -1.22429231199999,4.486080883 -0.700680603000023,4.71883084799998 -0.700680603000023,4.18350592899998 -0.654133865,4.34643090399999

-1.49187934000003))))

++数据索引:2++字段内容:Some(2)++几何:Some(MULTIPOLYGON (((0.749541894 -3.516916665,3.61879402599999 -5.06406242200001,0.819866701000024 -5.02186753799998,0.749541894 -3.516916665))))

从读取结果中看出,filegdb的数据行索引是从 1 开始的

三、总结

读取这两种不同的数据源,方法总体上是差不多的,需要在遍历数据的时候,注意数据行起始索引的区别。

相关推荐
mldong13 小时前
同一份 15 个流程 JSON,第六种语言也跑通了:工作流引擎 Rust 移植实录
架构·rust
Naylor2 天前
只借不占:Rust 的引用与借用
后端·rust
对象存储与RustFS2 天前
RustFS 后台扫描器与自愈调参:五档速度、位腐检测周期与并发上限
后端·rust·开源
q平面人3 天前
【总工笔记】mindoc平板、桌面端笔记软件,发布到mindoc服务端
rust·tauri·华为平板·mindoc·总工笔记
MC皮蛋侠客3 天前
Tauri 2.x 系列(九):安全模型——Capabilities、Permissions、Scope 与 CSP
rust·tauri
Source.Liu3 天前
【Dioxus】Windows 环境下 Rust + Dioxus 安装配置笔记
windows·rust·dioxus
zLLM_Lab3 天前
不装 Python/PyTorch:8.1 MB Rust 程序在 MacBook Air M5 跑多模态大模型
rust
梦醒沉醉3 天前
std1.97.1——result模块细览
rust
Thneonl3 天前
同一资源、不同 ID:多源拓扑的 Identity Resolution
架构·rust
绍磊leo3 天前
【保姆级】dora-rs 一键安装脚本:类ros2的fishros 风格交互菜单 + 国内镜像加速,把坑都替你踩完了
rust·dora-rs