GDAl C + + Create shp

Source: Internet
Author: User

For gdal,c++ development environment testing.

#include <iostream>#include"gdal_priv.h"#include"Ogrsf_frmts.h"#include"Ogr_geometry.h"#pragmaComment (lib, "C:\\warmerda\\bld\\lib\\gdal_i.lib")using namespacestd;voidCreateshapefile () {Const Char*pszdrivername ="ESRI shapefiles"; Ogrsfdriver*Podriver;    Ogrregisterall (); Podriver= Ogrsfdriverregistrar::getregistrar ()Getdriverbyname (pszdrivername); if(Podriver = =NULL) {printf ("%s driver not available.\n", Pszdrivername); Exit (1 ); } Ogrdatasource*PoDS; PoDS= Podriver->createdatasource ("point_out.shp", NULL); if(PoDS = =NULL) {printf ("Creation of output file failed.\n" ); Exit (1 ); } Ogrlayer*Polayer; Polayer= Pods->createlayer ("Point_out", NULL, wkbpoint, NULL); if(Polayer = =NULL) {printf ("Layer Creation failed.\n" ); Exit (1 ); } ogrfielddefn Ofield ("Name", oftstring); Ofield.setwidth ( +); if(Polayer->createfield (&ofield)! =Ogrerr_none) {printf ("Creating Name Field failed.\n" ); Exit (1 ); }    Doublex, y; Charszname[ -];  while( !feof (stdin)&& fscanf (stdin,"%lf,%lf,%32s", &x, &y, szName) = =3) {ogrfeature*pofeature; Pofeature=NewOgrfeature (polayer->Getlayerdefn ()); Pofeature->setfield ("Name", SzName);        Ogrpoint pt;        Pt.setx (x);        Pt.sety (y); Pofeature->setgeometry (&PT); if(Polayer->createfeature (pofeature)! =Ogrerr_none) {printf ("Failed to create feature in shapefile.\n" ); Exit (1 );    } ogrfeature::D estroyfeature (pofeature); } Ogrdatasource::D estroydatasource (PoDS);}intMain () {//Createshapefile (); //cout<< "Create success" <<endl;    Const Char*pszdrivername="ESRI shapefiles"; Ogrsfdriver*Podriver;    Ogrregisterall (); Podriver=ogrsfdriverregistrar::getregistrar ()Getdriverbyname (pszdrivername); if(podriver==NULL) {cout<<"Registration Failed"<<Endl; } Ogrdatasource*PoDS; PoDS=podriver->createdatasource ("point_out.shp", NULL); if(pods==NULL) {cout<<"Create drive failed! "<<Endl; } Ogrlayer*Polayer; Polayer=pods->createlayer ("Point_out", Null,wkbpoint,null); if(polayer==NULL) {cout<<"failed to create layer"<<Endl; } ogrfielddefn Ofield ("Name", oftstring); Ofield.setwidth ( A); if(Polayer->createfield (&ofield)! =Ogrerr_none) {cout<<"failed to add field! "<<Endl; }    //The following loop reads the point information in the "X,y,name" format from stdin.     Doublex=0, y= -; Charszname[ -];  for(intA=0; a!=Ten;++a) {x+=A; Y+=A; Ogrfeature*pofeature; Pofeature=NewOgrfeature (polayer->Getlayerdefn ()); Pofeature->setfield ("Name", SzName);        Ogrpoint pt;        Pt.setx (x);        Pt.sety (y); Pofeature->setgeometry (&PT); if(Polayer->createfeature (pofeature)! =Ogrerr_none) {cout<<"failed to create feature"<<Endl; }        //ogrfeature::D estroyfeature (*pofeature);//Create a feature. Ogrlayer::createfeature () only re-replicates a feature, so the feature object needs to be cleared after the operation is completed    }    //finally releases the Datasoure resource and refreshes all the write operationsOgrdatasource::D estroydatasource (PoDS); cout<<"The file was created successfully! "<<Endl; return 0;}

GDAl C + + Create shp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.