我是靠谱客的博主 悲凉蜗牛,这篇文章主要介绍GDAL Shape转Geojson 文件释放方法问题描述,现在分享给大家,希望可以做个参考。

GDAL Shape转Geojson 文件释放方法

  • 问题描述
    • 问题代码
    • 成功代码

问题描述

在项目中遇到一个shape文件转json的需求,于是选用了GDal方案,使用方法网上一大堆,但在执行转换过后生成的文本文件一直释放不了,每次生成的结果文件删除不掉就很恶心,于是各种调试终于找到解决方案

问题代码

生成的文件删除不掉,执行CopyDataSource重名的话, 会报错

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public void convertShapefile2Geojson(string shpPath, string geojsonPath) { using (DataSource ds = Ogr.Open(shpPath, 0)) { if (ds == null) { return; } using (Driver dv = Ogr.GetDriverByName("GeoJSON")) { if (dv == null) { return; } try { if (File.Exists(geojsonPath)) { dv.DeleteDataSource(geojsonPath); } dv.CopyDataSource(ds, geojsonPath, null); dv.Dispose(); } catch (Exception exx) { } } ds.Dispose(); } }

成功代码

加上一句 Gdal.GDALDestroyDriverManager(); 即可

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public void convertShapefile2Geojson(string shpPath, string geojsonPath) { using (DataSource ds = Ogr.Open(shpPath, 0)) { if (ds == null) { return; } using (Driver dv = Ogr.GetDriverByName("GeoJSON")) { if (dv == null) { return; } try { if (File.Exists(geojsonPath)) { dv.DeleteDataSource(geojsonPath); } dv.CopyDataSource(ds, geojsonPath, null); dv.Deregister(); dv.Dispose(); } catch (Exception exx) { } } ds.Dispose(); Gdal.GDALDestroyDriverManager(); } }

最后

以上就是悲凉蜗牛最近收集整理的关于GDAL Shape转Geojson 文件释放方法问题描述的全部内容,更多相关GDAL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(83)

评论列表共有 0 条评论

立即
投稿
返回
顶部