shp与geojson互转
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 34 35 36 37 38 39 |
///
/// 将shp转为geojson /// /// shp文件全路径 /// public static List<</span>object> ConvertShp2Geojson(string shapeFilePath) { try { // 为了支持中文路径 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // 为了使属性表字段支持中文 Gdal.SetConfigOption("SHAPE_ENCODING", ""); Gdal.AllRegister(); Ogr.RegisterAll(); Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); var ds = drv.Open(shapeFilePath, 0); Layer layer = ds.GetLayerByIndex(0); Feature f; layer.ResetReading(); List<</span>object> sb = new List<</span>object>(); while ((f = layer.GetNextFeature()) != null) { var geom = f.GetGeometryRef(); if (geom != null) { var geometryJson = geom.ExportToJson(new string[] { }); var obj = JsonConvert.DeserializeObject(geometryJson); sb.Add(obj); } } return sb; } catch (Exception e) { throw e; } } |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
///
/// 调用ogr2ogr.exe将shp转为geojson /// /// shp文件路径 public static string RunJsonConvertProcess(string shpfile) { try { string dataDir = Directory.GetCurrentDirectory(); string accessPath = dataDir + @"\bin\gdal\apps"; string fileName = Path.GetFileNameWithoutExtension(shpfile); var jsonPath = shpfile.Substring(0, shpfile.LastIndexOf("\") + 1) + fileName + ".json"; if (!File.Exists(accessPath + "\\ogr2ogr.exe")) throw new Exception("不存在转换工具ogr2ogr"); string cmd = $"ogr2ogr -f "GeoJSON\" {jsonPath} {shpfile}"; string str = string.Format(@"C:&cd {0}&{1}&{2}", accessPath, cmd,"exit"); RunCmd(str); if(File.Exists(jsonPath)) return jsonPath; else return ""; } catch (Exception e) { return "error:"+e.Message; } } //向cmd窗口写入命令 //获取cmd窗口的输出信息 |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
///
/// 使用GDAL将shp转为geojson /// /// shp文件全路径 /// public static List<</span>object> Shp2GeoJsonByGDAL(string shapeFilePath) { try { // 为了支持中文路径 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); // 为了使属性表字段支持中文 Gdal.SetConfigOption("SHAPE_ENCODING", ""); Gdal.AllRegister(); Ogr.RegisterAll(); Driver drv = Ogr.GetDriverByName("ESRI Shapefile"); var ds = drv.Open(shapeFilePath, 0); Layer layer = ds.GetLayerByIndex(0); Feature f; layer.ResetReading(); List<</span>object> sb = new List<</span>object>(); while ((f = layer.GetNextFeature()) != null) { var geom = f.GetGeometryRef(); if (geom != null) { var geometryJson = geom.ExportToJson(new string[] { }); var obj = JsonConvert.DeserializeObject(geometryJson); sb.Add(obj); } } return sb; } catch (Exception e) { throw e; } } /// /// 调用ogr2ogr.exe将shp转为geojson /// /// shp文件路径 public static string Shp2GeoJsonByOgr(string shpfile) { try { string dataDir = Directory.GetCurrentDirectory(); string accessPath = dataDir + @"\bin\postgresql"; string fileName = Path.GetFileNameWithoutExtension(shpfile); var jsonPath = shpfile.Substring(0, shpfile.LastIndexOf("\") + 1) + fileName + ".json"; if (!File.Exists(accessPath + "\\ogr2ogr.exe")) throw new Exception("不存在转换工具ogr2ogr"); string cmd = $"ogr2ogr -f "GeoJSON\" {jsonPath} {shpfile}"; string str = string.Format(@"C:&cd {0}&{1}&{2}", accessPath, cmd,"exit"); RunCmd(accessPath,str); if(File.Exists(jsonPath)) } //先设置环境变量,再运行 //.NET Core不支持gbk和gb2312,这里需要处理一下 //向cmd窗口写入命令 //获取cmd窗口的输出信息 |
shp与geojson互转相关推荐
- java 使用gdal_java-gdal实现shp转geojson
安装gdal的话,将shp转geojson会非常简单.本文因项目需要,以java-gdal讲述如何实现.ps:当然只要装有gdal就都可以实现功能. ogr2ogr命令 在控制台执行如下语句: D:\ ...
- shp与json互转(转载)
转自:http://blog.sina.com.cn/s/blog_673c98be0102v78i.html 对于搞GIS开发的,这2种数据格式太重要了. 一.shp转json 这个要容易些,方法也 ...
- Mars3D(含Cesium)数据及服务篇:shp转geojson格式
shp格式介绍 Shapefile文件是ESRI公司ArcGIS平台的常用格式文件,是工业标准的矢量数据文件. Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何 ...
- java使用geotools shp转geojson(wtk转geojson) 精度丢失
java 使用geotools将shp转为geojson时,发现geojson的精度丢失,所以导致geojson展示在地图上如下图:(使用wkt转geojson也存在同样问题) 查看api发现是Geo ...
- arcgis更改图层坐标系_【干货】ArcGIS之shp文件坐标系互转
原标题:[干货]ArcGIS之shp文件坐标系互转 准备arcgis10.4 shp文件,没有定义坐标系,格式是经纬度的 准备的数据如下: 1.先将shp文件定义为EPSG:4326 首先打开arct ...
- 用shp制作geoJson格式地图数据(shp convert to geoJson)
本文紧接前文,简单说明利用shp数据制作Echarts支持的geoJson格式的地图数据.本文以北京市通州区各镇的shp数据为例进行说明. 今天是香港回归20周年之际,在这个特殊的日子,祝愿祖国繁荣昌 ...
- GeoTools——shp转geojson
目录 一.引言 二.代码操作 1.服务端 2.返回数据 三.总结 一.引言 数据库中经常存储的格式是符合OGC标准的WKT或WKB,而在网络中经常传输的格式是json,因此我们会经常把各种数据转为ge ...
- Cesium加载建筑物模型(shp转Geojson\3Dtiles)
本文主要介绍cesium加载Geojson和3dtile格式的建筑物模型文件,除此之外还介绍了Cesium工具栏的屏蔽方法.天地图的加载.地球初始状态设置等几个部分的内容,其中又不乏参照.优化诸如: ...
- [Python] GDAL/OGR操作矢量数据(shp、GeoJSON)
GDAL项目旨于地理数据抽象模型对地理数据文件进行读写管理:而其项目下有两大类模块:GDAL和OGR OGR提供操作矢量数据的API,GDAL模块提供栅格数据的API [相关链接] 1.GDAL/OG ...
最新文章
- 获取系统昨日最高峰时的AWR报告,get_hight_load_awr
- Php 链式执行,PHP实现链式操作的三种方法详解
- kettle、Oozie、camus、gobblin
- 合并K个排序链表—leetcode23
- 2007上半年網絡工程師考題
- JQuery + jquery插件
- java读取与写入excel(.xls .xlsx)
- BeanFactory容器的设计原理
- STL中容器的介绍及分类
- Excel函数公式大全—MATCH函数
- 数据库系统概念(中文版)(第6版)pdf
- 【历史上的今天】3 月 31 日:世界备份日;皮克斯创始人出生;《黑客帝国》上映
- 如何判断一个文件是否是jpeg格式?
- 2011计算机一级a,计算机一级A2011 上海.doc
- 使用Contect.Handler显示提交过来的“编辑成功”的信息
- matlab的shading,matlab colormap,caxis,shading,hsv,pcolor, alpha
- 布尔逻辑析取范式思考
- 罗技g502鼠标宏设置教程分享
- 基于MATLAB的线性规划解决方法——单纯形法
- word转换成excel导致身份证错乱的解决办法
热门文章
- string数据库使用和实践第一部分string数据库介绍
- windows下安装weblogic11g集群 BEA-149509报错
- C++:在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加 “#include “stdafx.h“”?
- 财务管理专业写毕业论文有没有什么好的选题?
- Linux find 实战
- QT 多目录多工程 配置
- 词典软件测试工资,必应词典客户端软件测试报告
- 【单片机基础篇】rc延迟电路
- jsp/servlet过滤器和struts2拦截器的有什么区别
- maven的settings配置文件详解