1. 安装GDAL包
点击管理NuGet程序包下载GDAL、GDAL.Native和Newtonsoft.Json。之后项目文件目录下会添加一个GdalConfiguration.cs类。
2. 修改GdalConfiguration.cs类
将GdalConfiguration.cs中的内容全部修改为以下代码:
using System;
using System.IO;
using System.Reflection;
using Gdal = OSGeo.GDAL.Gdal;
using Ogr = OSGeo.OGR.Ogr;
namespace ShpTool
{
public static partial class GdalConfiguration
{
private static bool _configuredOgr;
private static bool _configuredGdal;
/// <summary>
/// Function to determine which platform we're on
/// </summary>
private static string GetPlatform()
{
return IntPtr.Size == 4 ? "x86" : "x64";
}
/// <summary>
/// Construction of Gdal/Ogr
/// </summary>
static GdalConfiguration()
{
var executingAssemblyFile = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var executingDirectory = Path.GetDirectoryName(executingAssemblyFile);
if (string.IsNullOrEmpty(executingDirectory))
throw new InvalidOperationException("cannot get executing directory");
var gdalPath = Path.Combine(executingDirectory, "gdal");
var nativePath = Path.Combine(gdalPath, GetPlatform());
// Prepend native path to environment path, to ensure the
// right libs are being used.
var path = Environment.GetEnvironmentVariable("PATH");
path = nativePath + ";" + Path.Combine(nativePath, "plugins") + ";" + path;
Environment.SetEnvironmentVariable("PATH", path);
// Set the additional GDAL environment variables.
var gdalData = Path.Combine(gdalPath, "data");
Environment.SetEnvironmentVariable("GDAL_DATA", gdalData);
Gdal.SetConfigOption("GDAL_DATA", gdalData);
var driverPath = Path.Combine(nativePath, "plugins");
Environment.SetEnvironmentVariable("GDAL_DRIVER_PATH", driverPath);
Gdal.SetConfigOption("GDAL_DRIVER_PATH", driverPath);
Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData);
Gdal.SetConfigOption("GEOTIFF_CSV", gdalData);
var projSharePath = Path.Combine(gdalPath, "share");
Environment.SetEnvironmentVariable("PROJ_LIB", projSharePath);
Gdal.SetConfigOption("PROJ_LIB", projSharePath);
}
/// <summary>
/// Method to ensure the static constructor is being called.
/// </summary>
/// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
public static void ConfigureOgr()
{
if (_configuredOgr) return;
// Register drivers
Ogr.RegisterAll();
_configuredOgr = true;
}
/// <summary>
/// Method to ensure the static constructor is being called.
/// </summary>
/// <remarks>Be sure to call this function before using Gdal/Ogr/Osr</remarks>
public static void ConfigureGdal()
{
if (_configuredGdal) return;
// Register drivers
Gdal.AllRegister();
_configuredGdal = true;
}
}
}
3. shp转geojson文件的代码
引用ShpTool中的GdalConfiguration类,然后执行如下代码:
/// <summary>
/// shp转Geojson
/// </summary>
/// <param name="shpPath"></param>
/// <param name="geojsonPath"></param>
public void ShpToGeojson(string shpPath, string geojsonPath)
{
// 下载gdal和gdal.native,将下面两条语句放在Ogr.RegisterAll()之前
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
// 注册所有的驱动
Ogr.RegisterAll();
// 为了支持中文路径,请添加下面这句代码
Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
// 为了使属性表字段支持中文,请添加下面这句
Gdal.SetConfigOption("SHAPE_ENCODING", "");
//打开数据
DataSource ds = Ogr.Open(shpPath, 0);
OSGeo.OGR.Driver dv = Ogr.GetDriverByName("GeoJSON"); //设置geojson驱动
dv.CopyDataSource(ds, geojsonPath, null); //复制数据源并生成geojson
}
4. 测试
添加测试按钮的点击事件进行测试
private void button1_Click_2(object sender, EventArgs e)
{
ShpProcess sp = new ShpProcess();
string shpPath = @"C:\Users\deng\Desktop\aa\aa.shp";
string geojsonPath = @"C:\Users\deng\Desktop\bb\aa.geojson";
sp.ShpToGeojson(shpPath, geojsonPath);
}
测试结果如下:
5. c# Newtonsoft.Json解析geojson
/// <summary>
/// 解析Geojson
/// </summary>
/// <param name="geojsonPath"></param>
public void AnalyticalGeojson(string geojsonPath)
{
StreamReader sr = new StreamReader(geojsonPath, Encoding.UTF8); // 读取geojson
JObject jo = JObject.Parse(sr.ReadToEnd());
JToken jt = jo["features"]; // 获取geojson中features键的内容
List<JToken> jlst = jt.ToList<JToken>();
for (int i = 0; i < jlst.Count; i++)
{
//string jt1 = (string)jlst[i]["geometry"]["type"];
var poiArr = jlst[i]["geometry"]["coordinates"].ToArray(); // 解析shp要素的折点坐标信息
foreach (JToken item in poiArr)
{
foreach (var item1 in item.Children())
{
Console.WriteLine(item1.First); // x
Console.WriteLine(item1.Last); // y
}
}
}
}
6. 参考链接
1.https://blog.csdn.net/weixin_43857611/article/details/120556351
2.https://blog.51cto.com/u_15061948/3837959
3.https://blog.csdn.net/mygisforum/article/details/22478491
4.https://blog.csdn.net/qq_29413829/article/details/84555455
5.https://www.jianshu.com/p/71c017d62c4c