[Shapelib] Adding a polygon to a shapefile
Joaquin Perez Valera
joaquinperezvalera at gmail.com
Sun Feb 17 14:30:21 PST 2008
I can create shapefiles and they are valid when I work with ArcCatalog. But
the shapefiles are empty.
Now I'm trying to add objects to my shapefile. I want to add polygons.
First I create a shapefile and a dbf file with an a simple column. Then it
becomes valid for ArcCatalog.
Then I create 2 arrays of 7 elements X and Y, and give a valor to each
element. Seven elements because
I want to draw a polygon of six vertices.
After it I use psObject = SHPCreateObject to create my polygon and after
SHPWriteObject( hSHP, -1, psObject ); to write it
in my shapefile.
It's obviously that I'm doing something wrong.
But I don't know what.
Can somebody help me or say me what I'm doing wrong?
using namespace std;
int nShapeType, nWidth = 3, vertexcount, *panParts, ShapeId,
string shape_name, Col1;
cout << "Name of the new Shapefile" << endl;
cout << "The shapefile is: " << shape_name << endl;
//Here I define the type of shapefile, the 5 is for a polygon.
hSHP = SHPCreate( shape_name.c_str(), nShapeType );
hDBF = DBFCreate( shape_name.c_str() );
cout <<"The shape has "<< DBFGetFieldCount( hDBF ) <<" columns" << endl;
DBFAddField( hDBF, Col1.c_str(), FTInteger, nWidth, 0 );
cout <<"Now the shape has " << DBFGetFieldCount( hDBF ) << " columns";
//At this point the shape is valid for ArcView
//Here I define an array of seven elements and I'll give a coordinate
for each element
double X, Y;
X = 220764;
X = 220610;
X = 220818;
X = 221109;
X = 230504;
X = 221102;
X = X;
Y = Y;
//I know the number of vertex is 7, six of my polygon and an extra
//vertex to close the polygon.
vertexcount = 7;
//I can't understand what is exactly the panParts variable. :(
panParts = 1;
//What is nParts? For me my polygon has 1 part, an entire part. It is
nParts = 1;
//I give a shape id for this unique object.
//From the shp_api I took this line of code to create a feature/object
//SHPCreateObject( nSHPType, iShape, nParts, panPartStart, panPartType,int
nVertices, *padfX, * padfY, *padfZ, *padfM );
//I have the nShapeType;
//I give the ShapeId
//I give the nParts
//I don't know what is panParts
//panPartType is NULL because it isn't a multipatch file
//I know the numer of vertex
//I now the number of vertex I have two arrays of coordinates.
//padfZ and padfM are NULL, zero.
psObject = SHPCreateObject(nShapeType, ShapeId, nParts, panParts, NULL,
vertexcount, X, Y, NULL, NULL );
SHPWriteObject( hSHP, -1, psObject );
SHPDestroyObject( psObject );
DBFClose( hDBF );
SHPClose( hSHP );
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Shapelib