将shape文件转成geojson格式

本文介绍如何使用ogr2ogr工具将Shape文件转换为GeoJSON格式。通过具体命令展示,帮助读者快速掌握转换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将shape文件转成geojson格式

使用ogr2ogr.exe工具


E:\Program Files\PostgreSQL\9.5\bin>ogr2ogr.exe -f GEOJSON G:\fl\data\shp1\gridPoly.json G:\fl\data\shp1\gridPoly.shp

### C# 控制台应用程序实现 GeoJSON Shapefile 以下是基于 C# 的控制台应用程序代码,该程序可以遍历指定路径下所有的 GeoJSON 文件并将其换为 Shapefile。此功能可以通过使用第三方库 `NetTopologySuite` 和 `GeoAPI` 来完成。 #### 安装必要的 NuGet 包 为了支持 GeoJSON 解析和 Shapefile 输出,需要安装以下 NuGet 包: - NetTopologySuite.IO.GeoJSON[^1] - NetTopologySuite.IO.ShapeFile 这些包提供了处理地理空间数据所需的工具集。 #### 实现代码 下面是一个完整的控制台应用程序示例: ```csharp using System; using System.Collections.Generic; using System.IO; using NetTopologySuite.Features; using NetTopologySuite.Geometries; using NetTopologySuite.IO; namespace GeoJsonToShapefileConverter { class Program { static void Main(string[] args) { string inputPath = @"C:\path\to\geojson"; // 替换为目标目录 string outputPath = @"C:\path\to\shapefiles"; // 替换为输出目录 if (!Directory.Exists(inputPath)) { Console.WriteLine("输入路径不存在!"); return; } if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } List<string> geoJsonFiles = GetGeoJsonFiles(inputPath); foreach (string file in geoJsonFiles) { ConvertGeoJsonToShapefile(file, outputPath); } } private static List<string> GetGeoJsonFiles(string directoryPath) { return new List<string>(Directory.GetFiles(directoryPath, "*.geojson", SearchOption.AllDirectories)); } private static void ConvertGeoJsonToShapefile(string geoJsonFilePath, string outputDir) { try { using (var reader = new GeoJsonReader()) { var features = ReadFeaturesFromGeoJson(reader, geoJsonFilePath); string shapefileName = Path.GetFileNameWithoutExtension(geoJsonFilePath) + ".shp"; string fullOutputPath = Path.Combine(outputDir, shapefileName); WriteFeaturesToShapefile(features, fullOutputPath); Console.WriteLine($"成功将 {geoJsonFilePath} 换为 {fullOutputPath}"); } } catch (Exception ex) { Console.WriteLine($"换失败: {ex.Message}"); } } private static IEnumerable<Feature> ReadFeaturesFromGeoJson(GeoJsonReader reader, string filePath) { Geometry geometry = null; ISet<AttributeDescriptor> attributes = new HashSet<AttributeDescriptor>(); FeatureCollection featureCollection = reader.Read<FeatureCollection>(File.ReadAllText(filePath)); foreach (var feature in featureCollection.Features) { yield return feature; } } private static void WriteFeaturesToShapefile(IEnumerable<Feature> features, string outputPath) { using (var writer = new ShapefileWriter(outputPath, true, Encoding.UTF8)) { foreach (var feature in features) { writer.Write(feature.Geometry, feature.Attributes); } } } } } ``` #### 关键说明 上述代码实现了以下几个主要功能: 1. **读取 GeoJSON 文件**:通过 `GeoJsonReader` 类解析 GeoJSON 数据,并提取其中的几何对象和属性。 2. **写入 Shapefile 文件**:利用 `ShapefileWriter` 将几何对象及其关联属性保存到 Shapefile 中。 3. **批量处理文件夹中的所有 GeoJSON 文件**:递归扫描目标路径下的 `.geojson` 文件,并逐一执行换操作。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值