• C#+GDAL读写文件


     读取shp文件:

     1  private void btnBrower_Click(object sender, EventArgs e)
     2         {
     3             OpenFileDialog dlg = new OpenFileDialog();
     4             dlg.Title = "打开ShapeFile数据";
     5             dlg.Filter = "ShapeFile数据(*.shp)|*.shp";
     6             if (dlg.ShowDialog() == DialogResult.OK)
     7             {
     8                 Ogr.RegisterAll();
     9 
    10                 string strVectorFile = dlg.FileName;
    11                 textBox1.Text = strVectorFile;
    12                 //打开数据  
    13                 DataSource ds = Ogr.Open(strVectorFile, 0);
    14                 if (ds == null)
    15                 {
    16                     listBox1.Items.Add(string.Format("打开文件【{0}】失败!", strVectorFile));
    17                     return;
    18                 }
    19                 listBox1.Items.Add(string.Format("打开文件【{0}】成功!", strVectorFile));
    20 
    21                 // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个  
    22                 int iLayerCount = ds.GetLayerCount();
    23 
    24                 // 获取第一个图层  
    25                 Layer oLayer = ds.GetLayerByIndex(0);
    26                 if (oLayer == null)
    27                 {
    28                     listBox1.Items.Add(string.Format("获取第{0}个图层失败!
    ", 0));
    29                     return;
    30                 }
    31 
    32                 // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空  
    33                 oLayer.ResetReading();
    34 
    35                 // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容  
    36                 //oLayer.SetAttributeFilter(""NAME99"LIKE "北京市市辖区"");
    37 
    38                 // 通过指定的几何对象对图层中的要素进行筛选  
    39                 //oLayer.SetSpatialFilter();  
    40 
    41                 // 通过指定的四至范围对图层中的要素进行筛选  
    42                 //oLayer.SetSpatialFilterRect();  
    43 
    44                 // 获取图层中的属性表表头并输出  
    45                 listBox1.Items.Add("属性表结构信息:");
    46                 FeatureDefn oDefn = oLayer.GetLayerDefn();
    47                 int iFieldCount = oDefn.GetFieldCount();
    48                 for (int iAttr = 0; iAttr < iFieldCount; iAttr++)
    49                 {
    50                     FieldDefn oField = oDefn.GetFieldDefn(iAttr);
    51 
    52                     listBox1.Items.Add(string.Format("{0}:{1} ({2}.{3})", oField.GetNameRef(),
    53                              oField.GetFieldTypeName(oField.GetFieldType()),
    54                              oField.GetWidth(), oField.GetPrecision()));
    55                 }
    56                 // 输出图层中的要素个数  
    57                 listBox1.Items.Add(string.Format("要素个数 = {0}", oLayer.GetFeatureCount(0)));
    58                 Feature oFeature = null;
    59                 // 下面开始遍历图层中的要素  
    60                 while ((oFeature = oLayer.GetNextFeature()) != null)
    61                 {
    62                     Geometry geo = oFeature.GetGeometryRef();
    63                     wkbGeometryType wkb = geo.GetGeometryType();
    64                     listBox1.Items.Add(string.Format("当前处理第要素值:{0}", wkb.ToString()));
    65                     string strGml=geo.ExportToGML();
    66                     listBox1.Items.Add(strGml);
    67                     listBox1.Items.Add(string.Format("当前处理第{0}个: 
    属性值:", oFeature.GetFID()));
    68 
    69                     // 获取要素中的属性表内容  
    70                     for (int iField = 0; iField < iFieldCount; iField++)
    71                     {
    72                         FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField);
    73                         FieldType type = oFieldDefn.GetFieldType();
    74                         switch (type)
    75                         {
    76                             case FieldType.OFTString:
    77                                 listBox1.Items.Add(string.Format("{0}	", oFeature.GetFieldAsString(iField)));
    78                                 break;
    79                             case FieldType.OFTReal:
    80                                 listBox1.Items.Add(string.Format("{0}	", oFeature.GetFieldAsDouble(iField)));
    81                                 break;
    82                             case FieldType.OFTInteger:
    83                                 listBox1.Items.Add(string.Format("{0}	", oFeature.GetFieldAsInteger(iField)));
    84                                 break;
    85                             default:
    86                                 listBox1.Items.Add(string.Format("{0}	", oFeature.GetFieldAsString(iField)));
    87                                 break;
    88                         }
    89                     }
    90                     // 获取要素中的几何体  
    91                     Geometry oGeometry = oFeature.GetGeometryRef();
    92                     // 为了演示,只输出一个要素信息  
    93                     break;
    94                 }
    95                 listBox1.Items.Add("数据集关闭!");
    96             }
    97         }
    View Code

     读取个人地理数据库mdb:

      1  private void btnBrower_Click(object sender, EventArgs e)
      2         {
      3             OpenFileDialog dlg = new OpenFileDialog();
      4             dlg.Title = "打开Personal Geodatabase";
      5             dlg.Filter = "Personal Geodatabase数据(*.mdb)|*.mdb";
      6             if (dlg.ShowDialog() == DialogResult.OK)
      7             {
      8                 Ogr.RegisterAll();
      9 
     10                 string strVectorFile = dlg.FileName;
     11                 textBox1.Text = strVectorFile;
     12                 string strInfo = string.Empty;
     13                 //打开数据  
     14                 DataSource ds = Ogr.Open(strVectorFile, 0);
     15                 if (ds == null)
     16                 {
     17                     strInfo += string.Format("打开文件【{0}】失败!
    ", strVectorFile);
     18                     return;
     19                 }
     20                 strInfo += string.Format("打开文件【{0}】成功!
    ", strVectorFile);
     21 
     22                 // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个  
     23                 int iLayerCount = ds.GetLayerCount();
     24                 strInfo += string.Format("个人地理数据库共包含{0}个图层!
    ", iLayerCount);
     25                 // 获取第一个图层  
     26                 Layer oLayer = ds.GetLayerByIndex(1);
     27                 if (oLayer == null)
     28                 {
     29                     strInfo += string.Format("获取第{0}个图层失败!
    ", 1);
     30                     return;
     31                 }
     32                 strInfo += string.Format("第0个图层名称:{0}
    ", oLayer.GetName());
     33                 // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空  
     34                 oLayer.ResetReading();
     35 
     36                 // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容  
     37                 //oLayer.SetAttributeFilter(""NAME99"LIKE "北京市市辖区"");
     38 
     39                 // 通过指定的几何对象对图层中的要素进行筛选  
     40                 //oLayer.SetSpatialFilter();  
     41 
     42                 // 通过指定的四至范围对图层中的要素进行筛选  
     43                 //oLayer.SetSpatialFilterRect();  
     44 
     45                 // 获取图层中的属性表表头并输出  
     46                 strInfo += "属性表结构信息:
    ";
     47                 FeatureDefn oDefn = oLayer.GetLayerDefn();
     48                 int iFieldCount = oDefn.GetFieldCount();
     49                 for (int iAttr = 0; iAttr < iFieldCount; iAttr++)
     50                 {
     51                     FieldDefn oField = oDefn.GetFieldDefn(iAttr);
     52 
     53                     strInfo += string.Format("{0}:{1} ({2}.{3})
    ", oField.GetNameRef(),
     54                              oField.GetFieldTypeName(oField.GetFieldType()),
     55                              oField.GetWidth(), oField.GetPrecision());
     56                 }
     57                 // 输出图层中的要素个数  
     58                 strInfo += string.Format("要素个数 = {0}
    ", oLayer.GetFeatureCount(0));
     59                 Feature oFeature = null;
     60                 // 下面开始遍历图层中的要素  
     61                 while ((oFeature = oLayer.GetNextFeature()) != null)
     62                 {
     63                     Geometry geo = oFeature.GetGeometryRef();
     64                     if (geo==null)
     65                     {
     66                         break;
     67                     }
     68                     wkbGeometryType wkb = geo.GetGeometryType();
     69                     strInfo += string.Format("当前处理第{0}要素值:{1}
    ", oFeature.GetFID(), wkb.ToString());
     70                     string strGml = geo.ExportToGML();
     71                     strInfo += strGml;
     72                     strInfo += string.Format("
    当前处理第{0}个: 
    属性值:", oFeature.GetFID());
     73 
     74                     // 获取要素中的属性表内容  
     75                     for (int iField = 0; iField < iFieldCount; iField++)
     76                     {
     77                         FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField);
     78                         FieldType type = oFieldDefn.GetFieldType();
     79                         switch (type)
     80                         {
     81                             case FieldType.OFTString:
     82                                 strInfo += string.Format("{0}	", oFeature.GetFieldAsString(iField));
     83                                 break;
     84                             case FieldType.OFTReal:
     85                                 strInfo += string.Format("{0}	", oFeature.GetFieldAsDouble(iField));
     86                                 break;
     87                             case FieldType.OFTInteger:
     88                                 strInfo += string.Format("{0}	", oFeature.GetFieldAsInteger(iField));
     89                                 break;
     90                             default:
     91                                 strInfo += string.Format("{0}	", oFeature.GetFieldAsString(iField));
     92                                 break;
     93                         }
     94                     }
     95                     // 获取要素中的几何体  
     96                     Geometry oGeometry = oFeature.GetGeometryRef();
     97                     // 为了演示,只输出一个要素信息  
     98                     break;
     99                 }
    100                 strInfo += "
    数据集关闭!";
    101                 richTextBox1.Text = strInfo;
    102             }
    103         }
    View Code
  • 相关阅读:
    IIS网站应用偶尔出现"服务不可用"或者显示乱码字体
    mac os下切换pip3国内源并安装requests库
    mysql5.6运行一段时间之后网站页面出现乱码解决办法
    mac pro下安装安装 SymPy 和 matplotlib报错解决方案
    python3汉诺塔简单实现代码
    用python提取xml里面的链接源码
    Mac环境下 jieba 配置记录
    AngularJS 整理学习
    Java有关List的stream基本操作
    Callable的简单使用
  • 原文地址:https://www.cnblogs.com/yhlx125/p/3418310.html
Copyright © 2020-2023  润新知