# Include "gdal_include/gdal. H"
# Include "gdal_include/ogr_api.h"
# Include "gdal_include/ogrsf_frmts.h"
# Pragma comment (Lib, "lib/gdal_ I .lib ")
-----------------------------------------
Gdal reads SHP files
(1) register all file format drivers
1 gdalallregister ();
2 ogrregisterall ();
(2) Get the SHP file processor
1 ogrsfdriver * podriver = ogrsfdriverregistrar: getregistrar ()-> getdriverbyname ("ESRI shapefile ");
(3) Open the SHP File
1 ogrdatasource * pods = podriver-> open ("D: \ lakes. SHP", null );
(4) obtain the SHP Layer
1 ogrlayer * polayer = pods-> getlayer (0 );
(5) read geometric and attribute values
1 ogrfeature * pfeature;
2 while (pfeature = polayer-> getnextfeature ())! = NULL)
3 {
4 ogrgeometry * pgeometry = pfeature-> getgeometryref ();
5 If (pgeometry = NULL)
6 {
7 afxmessagebox ("Geometry get failed .");
8 return false;
9}
10
11 ogrwkbgeometrytype geotype = pgeometry-> getgeometrytype ();
12 if (wkbpoint = geotype)
13 cstring strnodeid = pfeature-> getfieldasstring ("nodeid ");
14 else if (wkblinestring = geotype)
15 {
16 ogrlinestring * plinegeo = (ogrlinestring *) pgeometry;
17 double Stax = plinegeo-> getx (0 );
18 double stay = plinegeo-> Gety (0 );
19}
20}
(6) Resource cleanup
1 ogrdatasource: destroydatasource (pods );
2 ogrcleanupall ();
Gdal write SHP File
(1) register all file format drivers
1 gdalallregister ();
2 ogrregisterall ();
(2) Get the SHP file processor
1 ogrsfdriver * podriver = ogrsfdriverregistrar: getregistrar ()-> getdriverbyname ("ESRI shapefile ");
(3) create a SHP File
1 ogrdatasource * pods = podriver-> createdatasource ("D: \ lakes. SHP", null );
(4) create a Layer
Ogrlayer * polayer = pods-> createlayer ("tbline", null, wkblinestring, null );
(5) create a field
1 // string
2 ogrfielddefn ofield1 ("geoobjnum", oftstring );
3 ofield1.setwidth (8 );
4 If (polayer-> createfield (& ofield1 )! = Ogrerr_none ){
5 afxmessagebox ("creating Name field failed. \ n"); Return false ;}
6
7 // floating point number
8 ogrfielddefn ofield2 ("lbtg", oftreal );
9 ofield2.setprecision (3 );
10 if (polayer-> createfield (& ofield2 )! = Ogrerr_none ){
11 afxmessagebox ("creating Name field failed. \ n"); Return false ;}
12
13 // integer
14 ogrfielddefn ofield3 ("Number", oftinteger );
15 if (polayer-> createfield (& ofield3 )! = Ogrerr_none ){
16 afxmessagebox ("creating Name field failed. \ n"); Return false ;}
(6) Create ry and Feature
1 ogrfeature * pofeature;
2 pofeature = new ogrfeature (polayer-> getlayerdefn ());
3
4 pofeature-> setfield ("geoobjnum", strgeoobjnum );
5 pofeature-> setfield ("lbtg", flbtg );
6 pofeature-> setfield ("Number", number );
7
8 ogrlinestring * poline = new ogrlinestring ();
9
10 poline-> setnumpoints (2 );
11 poline-> setpoint (0, startx, starty, 0.0 );
12 poline-> setpoint (1, endx, Endy, 0.0 );
13
14 pofeature-> setgeometrydirectly (poline );
15 if (polayer-> createfeature (pofeature )! = Ogrerr_none)
16 {
17 afxmessagebox ("failed to create feature in shapefile .");
18 return false;
19}
(7) Resource cleanup
1 ogrdatasource: destroydatasource (pods );
2 ogrcleanupall ();