[Shapelib] Adding a polygon to a shapefile
Tom Kazimiers
2voodoo at gmx.de
Sun Feb 17 18:43:00 EST 2008
Joaquin,
sorry - I missed that you actually do have first and last elements the
same of your vertex arrays :)
But could you please write down where the error appears and what kind of
error it is?
cheers,
Tom
Tom Kazimiers schrieb:
> Hi Joaquin,
>
> as fas as I understood you can not make this call:
> psObject = SHPCreateObject(nShapeType, ShapeId, nParts, panParts,
> NULL, vertexcount, X, Y, NULL, NULL );
>
> have you checked if osObject is null afterwards? Please try it.
> Could it be that the first and the last vertex of a polygon need to be
> the same? In your code they are different.
> For my writing I use: SHPCreateSimpleObject which implys some NULLs of
> yours - maybe you could try this, too.
>
> bye,
> Tom
>
>
> Joaquin Perez Valera schrieb:
>> Hi
>>
>> 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[7] and Y[7], 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?
>>
>> Thanks.
>>
>>
>> [CODE]
>> #include <iostream>
>> #include <cstdlib>
>> #include "shapefil.h"
>> #include "string.h"
>>
>>
>> using namespace std;
>> int main()
>>
>> {
>> SHPHandle hSHP;
>> DBFHandle hDBF;
>> int nShapeType, nWidth = 3, vertexcount, *panParts,
>> ShapeId, nParts;
>>
>> string shape_name, Col1;
>> Col1= "Column";
>> SHPObject *psObject;
>> cout << "Name of the new Shapefile" << endl;
>> getline(cin,shape_name);
>>
>> cout << "The shapefile is: " << shape_name << endl;
>>
>> //Here I define the type of shapefile, the 5 is for a polygon.
>> nShapeType=5;
>> 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[7], Y[7];
>> X[0] = 220764;
>> Y[0]= 2343777;
>> X[1] = 220610;
>> Y[1]= 2343627;
>> X[2] = 220818;
>> Y[2]= 2343477;
>> X[3] = 221109;
>> Y[3]= 2343777;
>> X[4] = 230504;
>> Y[4]= 2343627;
>> X[5] = 221102;
>> Y[5]= 2343477;
>> X[6] = X[0];
>> Y[6] = Y[0];
>> //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[0] = 1;
>> //What is nParts? For me my polygon has 1 part, an entire part.
>> It is true?
>> nParts = 1;
>> //I give a shape id for this unique object.
>> ShapeId=1;
>> //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 );
>> return 0;
>> }
>>
>> [/CODE]
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Shapelib mailing list
>> Shapelib at lists.maptools.org
>> http://lists.maptools.org/mailman/listinfo/shapelib
>>
>
> _______________________________________________
> Shapelib mailing list
> Shapelib at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/shapelib
>
More information about the Shapelib
mailing list