GDAL实现读写ESRI ArcGIS的shapfile文件
废话少说,直接上代码吧。
#include "stdafx.h"
#include <ogrsf_frmts/ogrsf_frmts.h>
#include <Windows.h>
#define DEFAULT_STRING_LENGTH 256
int _tmain(int argc, _TCHAR* argv[])
{
OGRSFDriverRegistrar *p_ogr_regist=NULL;
OGRDataSource *p_ogr_ds=NULL;
OGRSFDriver *p_ogr_drv=NULL;
OGRLayer *p_ogr_layer=NULL;
OGRFeatureDefn *p_feature_defn=NULL;
OGRFeature *p_feature=NULL;
OGRPoint *p_point=NULL;
OGRErr o_code=0;
OGRFieldDefn *p_field_name=NULL;
OGRFieldDefn *p_field_age=NULL;
LPCSTR s_field_name="Name";
LPCSTR s_field_age="Age";
LPCSTR s_driver_name="ESRI Shapfile";
LPCSTR s_shp_path="F:\\point.shp";
LPCSTR s_layer_name="person_location_layer";
LPSTR s_temp=NULL;
int i_drv_count=0;
int i=0;
int i_field_count=0;
int i_age=0;
int i_scanf_count=0;
double f_longtitude=0;
double f_latitude=0;
char s_longtitude[DEFAULT_STRING_LENGTH]={0};
char s_latitude[DEFAULT_STRING_LENGTH]={0};
char s_age[DEFAULT_STRING_LENGTH]={0};
char s_name[DEFAULT_STRING_LENGTH+1]={0};
OGRRegisterAll();
p_ogr_regist=OGRSFDriverRegistrar::GetRegistrar();
i_drv_count=p_ogr_regist->GetDriverCount();
printf("有效的驱动列表:\n");
for (;i<i_drv_count;i++)
{
p_ogr_drv=p_ogr_regist->GetDriver(i);
printf(p_ogr_drv->GetName());
printf("\t");
}
printf("\n");
p_ogr_drv=p_ogr_regist->GetDriver(0);//GDAL 1.X版本
printf("已选择驱动:");
printf(p_ogr_drv->GetName());
printf("\n");
if(p_ogr_drv==NULL)
{
printf("创建ESRI Shapfile驱动失败!\n");
exit(1);
}
p_ogr_ds=p_ogr_drv->Open(s_shp_path,TRUE);
if(p_ogr_ds==NULL)
{
p_ogr_ds=p_ogr_drv->CreateDataSource(s_shp_path,NULL);
if (p_ogr_ds==NULL)
{
printf("创建或者打开Shapfile格式文件失败!\n");
exit(1);
}
}
p_ogr_layer=p_ogr_ds->GetLayer(0);
if (p_ogr_layer==NULL)
{
p_ogr_layer=p_ogr_ds->CreateLayer(s_layer_name,NULL,wkbPoint,NULL);
}
if (p_ogr_layer->GetGeomType()!=wkbPoint)
{
p_ogr_ds->DeleteLayer(0);
p_ogr_ds->CreateLayer(s_layer_name,NULL,wkbPoint,NULL);
}
if(p_ogr_layer==NULL)
{
printf("打开或者创建图层失败!\n");
exit(1);
}
p_feature_defn=p_ogr_layer->GetLayerDefn();
i_field_count=p_feature_defn->GetFieldCount();
p_field_name=p_feature_defn->GetFieldDefn(0);
if(p_field_name==NULL)
{
p_field_name=new OGRFieldDefn(s_field_name,OFTString);
p_field_name->SetWidth(32);
o_code=p_ogr_layer->CreateField(p_field_name);
delete p_field_name;
}
else
{
if(p_field_name->GetWidth()<32)
{
p_field_name->SetWidth(32);
}
s_temp=const_cast<LPSTR>(p_field_name->GetNameRef());
if(strstr(s_field_name,s_temp)!=s_field_name)
{
p_field_name->SetName(s_field_name);
}
}
p_field_age=p_feature_defn->GetFieldDefn(1);
if(p_field_age==NULL)
{
p_field_age=new OGRFieldDefn(s_field_age,OFTInteger);
p_field_age->SetWidth(4);
o_code=p_ogr_layer->CreateField(p_field_age);
delete p_field_age;
}
else
{
if(p_field_age->GetWidth()<4)
{
p_field_age->SetWidth(4);
}
s_temp=const_cast<LPSTR>(p_field_age->GetNameRef());
if(strstr(s_field_age,s_temp)!=s_field_age)
{
p_field_age->SetName(s_field_age);
}
}
printf("输入示例:2.0 3.0 zhangsan 18。\n");
printf("任意位置输入exit可停止输入并查看输入结果。\n");
while (!feof(stdin)&&fscanf(stdin,"%s%s%s%s",s_longtitude,s_latitude,s_name,s_age)==4)
{
if(strstr(s_longtitude,"exit")==s_longtitude||strstr(s_latitude,"exit")==s_latitude||strstr(s_name,"exit")==s_name||strstr(s_age,"exit")==s_age)
break;
f_longtitude=atof(s_longtitude);
f_latitude=atof(s_latitude);
i_age=atoi(s_age);
p_feature=OGRFeature::CreateFeature(p_ogr_layer->GetLayerDefn());
p_feature->SetField(s_field_name,s_name);
p_feature->SetField(s_field_age,i_age);
p_point=new OGRPoint;
p_point->setX(f_longtitude);
p_point->setY(f_latitude);
o_code=p_feature->SetGeometry(p_point);
if(o_code!=OGRERR_NONE)
{
printf("设置空间要素的几何图形数据失败!\n");
exit(1);
}
o_code=p_ogr_layer->CreateFeature(p_feature);
if(o_code!=OGRERR_NONE)
{
printf("设置图层的空间要素数据失败!\n");
exit(1);
}
delete p_point;
OGRFeature::DestroyFeature(p_feature);
ZeroMemory(s_longtitude,DEFAULT_STRING_LENGTH);
ZeroMemory(s_latitude,DEFAULT_STRING_LENGTH);
ZeroMemory(s_name,DEFAULT_STRING_LENGTH);
ZeroMemory(s_age,DEFAULT_STRING_LENGTH);
printf("已记录一个空间要素数据!\n");
}
OGRDataSource::DestroyDataSource(p_ogr_ds);
printf("创建ESRI Shapefile成功,可使用ArcGIS相关工具软件打开查看。按回车键退出。\n");
gets(s_name);
return 0;
}
转载请注明出处:http://blog.csdn.net/caoshiying/
转载自:https://blog.csdn.net/caoshiying/article/details/50973887