Home > Back-end >  Gdal call OGRErr Intersection computes/Clip/Union function without any elements after generation
Gdal call OGRErr Intersection computes/Clip/Union function without any elements after generation

Time:12-27

Small white beginners gdal, want to use c + + realize the vector points and the clip operation, find a website that is as follows:

My code block:
# include "PCH. H"
# include "gdal_priv. H"
# include "ogrsf_frmts. H"
#include

Bool VectorIntersection (const char * pszSrcShp, const char * pszMethodShp, const char * pszDstShp, const char * pszFormat);

Int main ()
{
Const char * pszSrcFile="E: \ \ MyThesis \ \ HeNan \ \ HeNan SHP";//the original file
Const char * pszMethodFile="E: \ \ MyThesis \ \ SiChuan \ \ SiChuan SHP";//used to clip file
Const char * pszOutFile="E: \ \ MyThesis \ \ union SHP";//the result file
VectorIntersection (pszSrcFile, pszMethodFile pszOutFile, "the ESRI Shapefile");
return 0;
}

Bool VectorIntersection (const char * pszSrcShp, const char * pszMethodShp, const char * pszDstShp, const char * pszFormat)
{
GDALAllRegister ();
CPLSetConfigOption (" SHAPE_ENCODING ", "");
CPLSetConfigOption (" GDAL_FILENAME_IS_UTF8 ", "NO");

//read the data
GDALDataset * poSrcDS;
PoSrcDS=(GDALDataset *) GDALOpenEx (pszSrcShp GDAL_OF_VECTOR, NULL, NULL, NULL);
If (poSrcDS==NULL)
{
Printf (" Open failed. \ n ");
exit(1);
}
GDALDataset * poMethodDS;
PoMethodDS=(GDALDataset *) GDALOpenEx (pszMethodShp GDAL_OF_VECTOR, NULL, NULL, NULL);
If (poMethodDS==NULL)
{
Printf (" Open failed. \ n ");
exit(1);
}

//use Shapefile driver
GDALDriver * poDriver;
PoDriver=GetGDALDriverManager () - & gt; GetDriverByName (pszFormat);
If (poDriver==NULL)
{
Printf (" % s driver not available. \ n ", pszFormat);
exit(1);
}

//according to the file name to create SHP file
GDALDataset * poDstDS=poDriver - & gt; Create (pszDstShp, 0, 0, 0, GDT_Unknown, NULL);
If (poDstDS==NULL)
{
Printf (" the Creation of the output file failed. \ n ");
exit(1);
}

//access layer
OGRLayer * poSrcLayer=poSrcDS - & gt; GetLayer (0);
If (poSrcLayer==NULL)
{
Printf (" Creation of layer failed. \ n ");
GDALClose (poSrcDS);//close the file
GDALClose (poMethodDS);
GDALClose (poDstDS);
exit(1);
}

OGRLayer * poMethodLayer=poMethodDS - & gt; GetLayer (0);
If (poMethodLayer==NULL)
{
Printf (" Creation of layer failed. \ n ");
GDALClose (poSrcDS);//close the file
GDALClose (poMethodDS);
GDALClose (poDstDS);
exit(1);
}

//define the spatial reference is the same as the original vector data and create the layer
OGRLayer * poDstLayer;
PSRS OGRSpatialReference *=poSrcLayer - & gt; GetSpatialRef ();
PoDstLayer=poDstDS - & gt; CreateLayer (" NewLayer pSRS wkbPolygon, NULL);
If (poDstLayer==NULL)
{
Printf (" Creation of layer failed. \ n ");
GDALClose (poSrcDS);//close the file
GDALClose (poMethodDS);
GDALClose (poDstDS);
exit(1);
}

PoSrcLayer - & gt; The Union (poMethodLayer poDstLayer, NULL, NULL, NULL);

GDALClose (poSrcDS);//close the file
GDALClose (poMethodDS);
GDALClose (poDstDS);

return true;
}

Reference blog: https://blog.csdn.net/secyb/article/details/80246105

CodePudding user response:

I hope you bosses glad!

CodePudding user response:

May be a topology errors or two SHP projection
  • Related