[Shapelib] Re: Adding a polygon to a shapefile

Tom Kazimiers 2voodoo at gmx.de
Wed Feb 20 05:03:23 EST 2008


Joaquin,

if I make the following to changes to your code it works for me:
1. include the C++ version of string (insteat of "string.h" include 
<string>)
Without this getline could not be found for me.
2. Vertices is not initialized - like said before, make sure everything 
is set up correctly - If you put "Vertices = 7;" in everything is ok.

In particular my version of you code is:

[CODE]
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include "shapefil.h"
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    SHPHandle    hSHP;
    DBFHandle    hDBF;
    int        nShapeType, Vertices;
    string  shape_name, Col1;
    SHPObject    *psObject;

    double X[7], Y[7], X1, Y1;
  
    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];
       
    X1 = 220764;
    Y1 = 2343777;
       
     cout << "Name of the new Shapefile" << endl;
    getline(cin,shape_name);

    cout << "The shapefile is: " << shape_name << endl;
 
    nShapeType = 5;
    Vertices = 7;
 
  
    hSHP = SHPCreate( shape_name.c_str(),nShapeType);
  
    hDBF = DBFCreate( shape_name.c_str());
  
    psObject = SHPCreateSimpleObject( nShapeType, Vertices, X, Y, NULL);
    SHPWriteObject( hSHP, -1, psObject );
  
  
    DBFClose( hDBF );
    SHPClose( hSHP );
 
  
      
    return 0;
}
[/CODE]


Cheers,
Tom

Joaquin Perez Valera schrieb:
> Hi
>
> I've been trying with CreateSimpleObject but i have not good results.
>
> I wrote the simple code:
>
> [CODE]
> #include <iostream>
> #include <cstdlib>
> #include "shapefil.h"
> #include "string.h"
>
>
> using namespace std;
> int main()
>
> {
>     SHPHandle    hSHP;
>     DBFHandle    hDBF;
>     int        nShapeType, Vertices;
>     string  shape_name, Col1;
>     SHPObject    *psObject;
>
>     double X[7], Y[7], X1, Y1;
>    
>     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];
>         
>     X1 = 220764;
>     Y1 = 2343777;
>         
>      cout << "Name of the new Shapefile" << endl;
>     getline(cin,shape_name);
>
>     cout << "The shapefile is: " << shape_name << endl;
>  
>     nShapeType = 5;
>    
>   
>    
>     hSHP = SHPCreate( shape_name.c_str(),nShapeType);
>    
>     hDBF = DBFCreate( shape_name.c_str());
>    
>     psObject = SHPCreateSimpleObject( nShapeType, Vertices, X, Y, NULL);
>     SHPWriteObject( hSHP, -1, psObject );
>    
>    
>     DBFClose( hDBF );
>     SHPClose( hSHP );
>   
>    
>        
>     return 0;
> }
> [/CODE]
>
> And when I runs it I have the same message error and the shapefile is 
> corrupt when I want to open it with ArcCatalog.
>
> Well
>
> See you and thanks.
>
>
>
> 2008/2/18, shapelib-request at lists.maptools.org 
> <mailto:shapelib-request at lists.maptools.org> 
> <shapelib-request at lists.maptools.org 
> <mailto:shapelib-request at lists.maptools.org>>:
>
>     Send Shapelib mailing list submissions to
>             shapelib at lists.maptools.org
>     <mailto:shapelib at lists.maptools.org>
>
>     To subscribe or unsubscribe via the World Wide Web, visit
>             http://lists.maptools.org/mailman/listinfo/shapelib
>     or, via email, send a message with subject or body 'help' to
>             shapelib-request at lists.maptools.org
>     <mailto:shapelib-request at lists.maptools.org>
>
>     You can reach the person managing the list at
>             shapelib-owner at lists.maptools.org
>     <mailto:shapelib-owner at lists.maptools.org>
>
>     When replying, please edit your Subject line so it is more specific
>     than "Re: Contents of Shapelib digest..."
>
>
>     Today's Topics:
>
>        1. Adding a polygon to a shapefile (Joaquin Perez Valera)
>        2. Re: Adding a polygon to a shapefile (Tom Kazimiers)
>        3. Re: Adding a polygon to a shapefile (Tom Kazimiers)
>
>
>     ----------------------------------------------------------------------
>
>     Message: 1
>     Date: Sun, 17 Feb 2008 16:30:21 -0600
>     From: "Joaquin Perez Valera" <joaquinperezvalera at gmail.com
>     <mailto:joaquinperezvalera at gmail.com>>
>     Subject: [Shapelib] Adding a polygon to a shapefile
>     To: shapelib at lists.maptools.org <mailto:shapelib at lists.maptools.org>
>     Message-ID:
>             <79a4e0710802171430y7dbad9c0p34780e668e3546ff at mail.gmail.com
>     <mailto:79a4e0710802171430y7dbad9c0p34780e668e3546ff at mail.gmail.com>>
>     Content-Type: text/plain; charset="iso-8859-1"
>
>     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]
>     -------------- next part --------------
>     An HTML attachment was scrubbed...
>     URL:
>     http://lists.maptools.org/pipermail/shapelib/attachments/20080217/88536964/attachment-0001.html
>
>     ------------------------------
>
>     Message: 2
>     Date: Mon, 18 Feb 2008 00:34:51 +0100
>     From: Tom Kazimiers <2voodoo at gmx.de <mailto:2voodoo at gmx.de>>
>     Subject: Re: [Shapelib] Adding a polygon to a shapefile
>     To: Shapelib Development <shapelib at lists.maptools.org
>     <mailto:shapelib at lists.maptools.org>>
>     Message-ID: <47B8C49B.3080801 at gmx.de <mailto:47B8C49B.3080801 at gmx.de>>
>     Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>     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 <mailto:Shapelib at lists.maptools.org>
>     > http://lists.maptools.org/mailman/listinfo/shapelib
>     >
>
>
>
>     ------------------------------
>
>     Message: 3
>     Date: Mon, 18 Feb 2008 00:43:00 +0100
>     From: Tom Kazimiers <2voodoo at gmx.de <mailto:2voodoo at gmx.de>>
>     Subject: Re: [Shapelib] Adding a polygon to a shapefile
>     To: Shapelib Development <shapelib at lists.maptools.org
>     <mailto:shapelib at lists.maptools.org>>
>     Message-ID: <47B8C684.5040701 at gmx.de <mailto:47B8C684.5040701 at gmx.de>>
>     Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>     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 <mailto:Shapelib at lists.maptools.org>
>     >> http://lists.maptools.org/mailman/listinfo/shapelib
>     >>
>     >
>     > _______________________________________________
>     > Shapelib mailing list
>     > Shapelib at lists.maptools.org <mailto:Shapelib at lists.maptools.org>
>     > http://lists.maptools.org/mailman/listinfo/shapelib
>     >
>
>
>
>     ------------------------------
>
>     _______________________________________________
>     Shapelib mailing list
>     Shapelib at lists.maptools.org <mailto:Shapelib at lists.maptools.org>
>     http://lists.maptools.org/mailman/listinfo/shapelib
>
>
>     End of Shapelib Digest, Vol 46, Issue 3
>     ***************************************
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Shapelib mailing list
> Shapelib at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/shapelib
>   



More information about the Shapelib mailing list