资源简介
GDAL进行shapefile数据栅格化,将shapefile中第一个多边形读取出来栅格化,根据“ID”字段
代码片段和文件信息
#include
#include “gdal_priv.h“
#include “gdal_alg.h“
#include “ogrsf_frmts.h“
#include “ogr_core.h“
using namespace std;
int main()
{
GDALAllRegister();
OGRRegisterAll();
OGRDataSource *poDS;
char* shpName;
shpName = “..\\testRes\\SW_UTM.shp“;
poDS = OGRSFDriverRegistrar::Open(shpName FALSE );
if( poDS == NULL )
{
cout<<“Open “< return 0;
}
OGRlayer* polayer;
polayer = poDS->Getlayer(0);
OGRFeature * poFeature = polayer->GetFeature(0);
OGRGeometry * poGeometry = poFeature->GetGeometryRef();
const char * pszOutFileName = “.\\src2_out.tif“;
char * pszProjection;
OGRSpatialReference * poSRS = polayer->GetSpatialRef();
OGREnvelope psExtent;
poGeometry->getEnvelope(&psExtent);
int m_nImageWidth = (int)((psExtent.MaxX - psExtent.MinX)/500.0+0.5);
int m_nImageHeight =(int)((psExtent.MaxY - psExtent.MinY)/500.0+0.5);
if ( !poSRS )
{
cout<<“Can‘t open the “< m_nImageWidth = psExtent.MaxX;
m_nImageHeight = psExtent.MaxY;
}
else
{
poSRS->exportToWkt( &pszProjection );
}
const char *pszFormat = “GTiff“;
GDALDriver *poDriver;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
GDALDataset *poNewDS = poDriver->Create( pszOutFileName m_nImageWidthm_nImageHeight1 GDT_Byte NULL );
if ( !poSRS )
{
double adfGeoTransform[6];
adfGeoTransform[0] = psExtent.MinX;
adfGeoTransform[1] = 500.0;
adfGeoTransform[2] = 0;
adfGeoTransform[3] = psExtent.MaxY;
adfGeoTransform[4] = 0;
adfGeoTransform[5] = -500.0;
GDALSetGeoTransform( poNewDS adfGeoTransform );
OGRSpatialReference * poOGRSR = NULL;
poOGRSR->SetUTM(50);
poSRS->exportToWkt(&pszProjection);
poNewDS->SetProjection(pszProjection);
- 上一篇:获取系统安装软件(vc++源码)
- 下一篇:二项堆Binomial Heap
评论
共有 条评论