版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/funkstill/article/details/88199772
问题描述:
需要对一批shp文件读取,提取其中部分字段到数据库
解决方法:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OSGeo.GDAL;
using OSGeo.OGR;
namespace learnGDAL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Gdal.AllRegister();
Ogr.RegisterAll();
OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "CP936");
string shpFilePath = @"E:\GDAL\gdal\learnGDAL\learnGDAL\testdata\110105dp.shp";
OSGeo.OGR.Driver driver = Ogr.GetDriverByName("ESRI Shapefile");
DataSource ds = null;
Layer layer = null;
try
{
ds = Ogr.Open(shpFilePath, 0);
layer = ds.GetLayerByIndex(0);
//当前shp元素数
int count = (int)layer.GetFeatureCount(0);
Feature fe = layer.GetNextFeature();
while (fe != null)
{
string XZQDM = fe.GetFieldAsString("XZQDM");
string TBBH = fe.GetFieldAsString("TBBH");
double BZB = fe.GetFieldAsDouble("BZB");
double LZB = fe.GetFieldAsDouble("LZB");
double TBMJ = fe.GetFieldAsDouble("TBMJ");
string TZ = fe.GetFieldAsString("TZ");
string YSFNH = fe.GetFieldAsString("YSFNH");
label1.Text += TBBH+" "+TZ+"\n";
fe = layer.GetNextFeature();
}
}
catch (Exception ex)
{
}
finally
{
if (layer != null)
{
layer.Dispose();
}
if (ds != null)
{
ds.Dispose();
}
driver.Dispose();
}
}
}
}
其实吧,这个方案也不靠谱。。。
继“CP936”也失效之后重新自定义方法解决GDAL读取SHP乱码问题