首页 技术 正文
技术 2022年11月14日
0 收藏 724 点赞 4,737 浏览 1600 个字

一、读shapefile

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

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

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

编辑如下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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903