一、读shapefile

1、首先,用Arcgis创建所要读的shp文件。打开ArcCatalog,右键NEW->Shapefile,名称Name:point ,要素类型(Feature Type):Point。点击Edit,选择投影类型。

2、打开ArcMap. 单击工具栏里的Add data按钮,打开刚才创建的point.shp文件。

3、添加5个点要素,并添加字段(添加字段要在非编辑模式下,修改字段的值要在编辑模式下,记得退出编辑时要保存),如图所示:

编辑如下C++/GDAL代码:

#include "ogrsf_frmts.h"

int main()

{
OGRRegisterAll(); OGRDataSource *poDS; poDS = OGRSFDriverRegistrar::Open( "G:\\LJF\\point.shp", FALSE );//shape文件存放的路径(point.shp即为自己创建的文件)
if( poDS == NULL )
{
printf( "Open failed.\n%s" );
exit( 1 );
} OGRLayer *poLayer; poLayer = poDS->GetLayerByName( "point" ); OGRFeature *poFeature; poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )//获得要素,本实例指的是五个点,所以会循环5次
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField;
int i=poFDefn->GetFieldCount(); //获得字段的数目,本实例返回5,不包括前两个字段(FID,Shape),这两个字段在arcgis里也不能被修改;
for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
//根据字段值得类型,选择对应的输出
if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
} OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry\n" );
}
OGRFeature::DestroyFeature( poFeature );
} OGRDataSource::DestroyDataSource( poDS );
system("pause");
return 0;
}

运行结果如下图:

用GDAL/OGR去读shapefile的更多相关文章

  1. GEOS库学习之五:与GDAL/OGR结合使用

    要学习GEOS库,肯定绕不开地理方面的东西.如果需要判断的两个多边形或几何图形,不是自己创建的,而是来自shapefile文件,那就得将GEOS库和GDAL/OGR库结合使用了.实际上只需要OGR就行 ...

  2. 使用GDAL/OGR读写矢量文件

    感觉GIS中矢量相关内容还是挺庞杂的,并且由于版本迭代的关系,使用GDAL/OGR读写矢量的资料也有点不太一样.这里总结了一个读写矢量的示例,实现代码如下: #include <iostream ...

  3. C#在使用串口读一长段数据时,前面加延时,等串口缓冲全部收到再去读

    Thread.Sleep(3);//延时一会,等接受完成再去读

  4. QStandardItemModel的data线程安全(在插入数据时,临时禁止sizeHint去读model中的data)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/MatchYang/article/details/52988257 在直接使用QStandardI ...

  5. GDAL OGR Tools

    OGR2OGR ogr2ogr 使用文档 1. GeoJSON 转换 ShapeFile ogr2ogr -nlt POLYGON -skipfailures ground.shp ground.js ...

  6. 尝试去读SQLMAP源码(一)

    本人python 小菜比 一枚.拜读业界典范~~ 阅读sqlmap 的版本是1.1.6,目前应该是最新版. sqlmap.py 脚本中 72~83 def modulePath(): "&q ...

  7. java后端导入excel模板和导入excel文件去读数据

    模板转载地址:https://www.cnblogs.com/zhangyangtao/p/9802948.html 直接上代码(我是基于ssm写的demo,导入文件目前只能读取.xls后缀的exce ...

  8. python gdal ogr osgeo

  9. 浏览器证书问题,chorm,ie,edge,safari都会去读系统证书,firefox例外

    坑爹 没想过浏览器兼容的问题. 为系统安装用户证书后, firefox一直无法连接 提示 连接 www.httpsserver.com:8985 时发生错误. SSL 对等端无法协商出一个可接受的安全 ...

随机推荐

  1. libtcmalloc 简单使用

    下载地址: https://github.com/gperftools/gperftools这个编译很简单,直接进入vsprojects\libtcmalloc_minimal,用vc编译即可,整个过 ...

  2. [转载]Git常用命令

    转载自: Git常用命令 Git配置 git config --global user.name "robbin" git config --global user.email & ...

  3. Kendo Web UI Grid数据绑定,删除,编辑,并把默认英文改成中文

    Kendo Web UI 是个不错的Jquery框.可惜老外写的,很多都是默认的英文,当然我们也可以设置成中文,接下来,我们就看看Grid是如何实现的数据绑定(Kendo Grid数据绑定实现有很多方 ...

  4. Python中range的用法

    函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但 ...

  5. uva201 Squares

     Squares  A children's board game consists of a square array of dots that contains lines connecting ...

  6. c# post方式发送请求

    public static bool CheckNew(string serverIP) { bool passed = false; try { string url = string.Format ...

  7. Shell脚本调试技术

    http://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ 一. 前言 shell编程在unix/linux世界中使用得非常广泛,熟练掌握 ...

  8. UI设计(流程/界面)设计规范

    1.UI设计基本概念与流程 1.1 目的 规范公司UI设计流程,使UI设计师参与到产品设计整个环节中来,对产品的易用性进行全流程负责,使UI设计的流程规范化,保证UI设计流程的可操作性. 1.2范围  ...

  9. What’s the difference between an interface and an abstract class in Java?

    原文 What’s the difference between an interface and an abstract class in Java? It’s best to start answ ...

  10. Majority Element 解答

    Solution 1 Naive way First, sort the array using Arrays.sort in Java. Than, scan once to find the ma ...