GDAL:shp文件创建
程序员文章站
2022-03-20 13:31:08
...
创建一个只有点要素的shp文件
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
OGRRegisterAll();
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver;
poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(pszDriverName);//注册OGRSFDriver驱动器
if (poDriver == NULL)
{
cout << pszDriverName << " driver not available." << endl;
return 0;
}
OGRDataSource *poDS;
poDS = poDriver->CreateDataSource("D:/TEST/point_out2.shp", NULL);//创建shp数据集,如果已存在文件,会报错
if (poDS == NULL)
{
cout << "Creation of point_out.shp file failed." << endl;
return 0;
}
OGRLayer *poLayer;
poLayer = poDS->CreateLayer("point_out", NULL, wkbPoint, NULL);//创建图层wkbPoint为点图层
if (poLayer == NULL)
{
cout << "Layer creation failed." << endl;
return 0;
}
OGRFieldDefn firstField("faci_code", OFTInteger);//创建字段
OGRFieldDefn secondField("X", OFTReal);
OGRFieldDefn thirdField("Y", OFTReal);
firstField.SetWidth(32);//设置字段长度
secondField.SetWidth(32);
thirdField.SetWidth(32);
poLayer->CreateField(&firstField);//给图层新增字段列表
poLayer->CreateField(&secondField);
poLayer->CreateField(&thirdField);
double x, y;
int code;
for (int i = 0; i != 100; ++i)
{
code = i + 1;
x = i;
y = 100 + i;
OGRFeature *poFeature;//创建要素指针
poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//构造函数中加入字段信息
poFeature->SetField("faci_code", code);//将字段和对应数据进行绑定
poFeature->SetField("X", x);
poFeature->SetField("Y", y);
OGRPoint pt;//创建点几何类型
pt.setX(x);//给点几何类型赋值
pt.setY(y);
poFeature->SetGeometry(&pt);//将当前点的几何类型和对应的要素绑定
if (poLayer->CreateFeature(poFeature) != OGRERR_NONE)//将要素和图层进行绑定
{
cout << "Failed to create feature in shapefile." << endl;
return 0;
}
OGRFeature::DestroyFeature(poFeature);//销毁要素指针
}
OGRDataSource::DestroyDataSource(poDS);//最后关闭数据源指针
system("pause");
return 0;
}
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
GDALAllRegister();
const char * pFileName = "shp.tif";
const char * pszDriverName = "ESRI Shapefile";
GDALDriver * poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName);
if(poDriver == NULL)
return ;
GDALDataset * poDs = poDriver->Create(pFileName, 0,0,0, GDT_Unknown, NULL);
if(poDs == NULL)
return ;
//wkbPolygon表示绘制多边形;wkbLinearRing表示绘制线环
OGRLayer * poLayer = poDs->CreateLayer("ring", NULL, wkbLineString, NULL);//不删
if(poLayer == NULL)
{
return;
}
OGRFeature * poFeature = OGRFeature::CreateFeature(poLayer->GetLayerDefn());//放到DestroyFeature删
//加入几何图形
//例1,绘制线环,内部不标记颜色.线的绘制原则是,从第一个点开始,逐次绘制直到最后一个点。
//对于闭合的线环,由于首点和尾点是一个点。所以4边形绘制需要5个点,首点被重复一次
OGRLineString line;
line.setNumPoints(5);
line.setPoint(0, 0, 1000, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
line.setPoint(1, 0, 0, 0);
line.setPoint(2, 1000, 0, 0);
line.setPoint(3, 1000, 1000, 0);
line.setPoint(4, 0, 1000, 0);
poFeature->SetGeometry(&line);
//例2,绘制多边形。多边形内部亦被颜色标记
/*OGRPolygon poly;
std::vector<QPoint> vec;
vec.push_back(QPoint(0,1));
vec.push_back(QPoint(1,1));
vec.push_back(QPoint(1,0));
vec.push_back(QPoint(0,0));
OGRLinearRing ring;
for(int i = 0; i < 4; i++)
{
ring.addPoint(vec[i].x(), vec[i].y());
}
ring.closeRings();
poly.addRing(&ring);
poFeature->SetGeometry(&poly);*/
//例3 多个线
/*OGRMultiLineString multiLine;
OGRLineString line1, line2;
line1.setNumPoints(5);
line1.setPoint(0, 0, 1000, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
line1.setPoint(1, 0, 0, 0);
line1.setPoint(2, 1000, 0, 0);
line1.setPoint(3, 1000, 1000, 0);
line1.setPoint(4, 0, 1000, 0);
line2.setNumPoints(5);
line2.setPoint(0, 500, 1500, 0);//第一个输入变量是序号,第二、三、四分别是线XYZ坐标
line2.setPoint(1, 500, 500, 0);
line2.setPoint(2, 1500, 500, 0);
line2.setPoint(3, 1500, 1500, 0);
line2.setPoint(4, 500, 1500, 0);
multiLine.addGeometry((const OGRGeometry *)&line1);
multiLine.addGeometry((const OGRGeometry *)&line2);
poFeature->SetGeometry(&multiLine);*/
if(poLayer->CreateFeature(poFeature) != OGRERR_NONE)
{
return;
}
OGRFeature::DestroyFeature(poFeature);
GDALClose(poDs);
OGRCleanupAll();