Combining C + + and gdal for creating and Writing Shapefiles (shp) files

Source: Internet
Author: User

Tool: Vs2012+gdal 2.0

Include Header files:

#include "Ogrsf_frmts.h"

int Main ()
{
Const Char*pszdrivername ="ESRI shapefiles"; Gdaldriver*Podriver; Gdalallregister (); Podriver= Getgdaldrivermanager ()Getdriverbyname (pszdrivername); if(Podriver = =NULL) {printf ("%s driver not available.\n", Pszdrivername); return 0; } Gdaldataset*PoDS; PoDS= Podriver->create ("D:/newshp.shp",0,0,0, Gdt_unknown, NULL);//Create shp file if(PoDS = =NULL) {printf ("Creation of output file failed.\n" ); return 0; } Ogrlayer*Polayer; Polayer= Pods->createlayer ("Point_out", NULL, wkbpoint, NULL); if(Polayer = =NULL) {printf ("Layer Creation failed.\n" ); return 0; } ogrfielddefn IDfield ("ID", Oftreal); Ogrfielddefn Firstfield ("NAME", Oftinteger); Ogrfielddefn Secondfield ("X", Oftreal); Ogrfielddefn Thirdfield ("Y", Oftreal); Idfield.setwidth ( +); Firstfield.setwidth ( +); Secondfield.setwidth ( +); Thirdfield.setwidth ( +); Polayer->createfield (&IDfield); Polayer->createfield (&Firstfield); Polayer->createfield (&Secondfield); Polayer->createfield (&Thirdfield); intx, y; intA=Ten, b= -; for(intI=1; i<=Ten; i++) {ogrfeature*pofeature; Pofeature= Ogrfeature::createfeature (polayer->Getlayerdefn ()); Pofeature->setfield ("ID", i); Pofeature->setfield ("NAME", i); X= (rand ()% (b-a)) +A; Y= (rand ()% (b-a)) +A; 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) {printf ("Failed to create feature in shapefile.\n" ); return 0; } ogrfeature::D estroyfeature (pofeature); } gdalclose (PoDS); return 1;
}

In this way, a newshp.shp file is generated in the D-packing directory, opened with ArcGIS and displayed as follows

Combining C + + and gdal for creating and Writing Shapefiles (shp) files

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.