|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
Adding a polygon to a shapefileHi
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@... http://lists.maptools.org/mailman/listinfo/shapelib |
|
|
Re: Adding a polygon to a shapefileHi 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@... > http://lists.maptools.org/mailman/listinfo/shapelib > _______________________________________________ Shapelib mailing list Shapelib@... http://lists.maptools.org/mailman/listinfo/shapelib |
|
|
Re: Adding a polygon to a shapefileJoaquin,
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@... >> http://lists.maptools.org/mailman/listinfo/shapelib >> > > _______________________________________________ > Shapelib mailing list > Shapelib@... > http://lists.maptools.org/mailman/listinfo/shapelib > _______________________________________________ Shapelib mailing list Shapelib@... http://lists.maptools.org/mailman/listinfo/shapelib |
|
|
|
|
|
Re: Re: Adding a polygon to a shapefileJoaquin,
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@... > <mailto:shapelib-request@...> > <shapelib-request@... > <mailto:shapelib-request@...>>: > > Send Shapelib mailing list submissions to > shapelib@... > <mailto:shapelib@...> > > 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@... > <mailto:shapelib-request@...> > > You can reach the person managing the list at > shapelib-owner@... > <mailto:shapelib-owner@...> > > 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@... > <mailto:joaquinperezvalera@...>> > Subject: [Shapelib] Adding a polygon to a shapefile > To: shapelib@... <mailto:shapelib@...> > Message-ID: > <79a4e0710802171430y7dbad9c0p34780e668e3546ff@... > <mailto:79a4e0710802171430y7dbad9c0p34780e668e3546ff@...>> > 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@... <mailto:2voodoo@...>> > Subject: Re: [Shapelib] Adding a polygon to a shapefile > To: Shapelib Development <shapelib@... > <mailto:shapelib@...>> > Message-ID: <47B8C49B.3080801@... <mailto:47B8C49B.3080801@...>> > 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@... <mailto:Shapelib@...> > > http://lists.maptools.org/mailman/listinfo/shapelib > > > > > > ------------------------------ > > Message: 3 > Date: Mon, 18 Feb 2008 00:43:00 +0100 > From: Tom Kazimiers <2voodoo@... <mailto:2voodoo@...>> > Subject: Re: [Shapelib] Adding a polygon to a shapefile > To: Shapelib Development <shapelib@... > <mailto:shapelib@...>> > Message-ID: <47B8C684.5040701@... <mailto:47B8C684.5040701@...>> > 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@... <mailto:Shapelib@...> > >> http://lists.maptools.org/mailman/listinfo/shapelib > >> > > > > _______________________________________________ > > Shapelib mailing list > > Shapelib@... <mailto:Shapelib@...> > > http://lists.maptools.org/mailman/listinfo/shapelib > > > > > > ------------------------------ > > _______________________________________________ > Shapelib mailing list > Shapelib@... <mailto:Shapelib@...> > http://lists.maptools.org/mailman/listinfo/shapelib > > > End of Shapelib Digest, Vol 46, Issue 3 > *************************************** > > > ------------------------------------------------------------------------ > > _______________________________________________ > Shapelib mailing list > Shapelib@... > http://lists.maptools.org/mailman/listinfo/shapelib > _______________________________________________ Shapelib mailing list Shapelib@... http://lists.maptools.org/mailman/listinfo/shapelib |
|
|
|
|
|
Re: Re: Adding a polygon to a shapefileHi Joaquin,
sorry for the confusion :) For testing your code I created a default project in Visual Studio and this came up with the line you do not understand. If you do not want any parameters, etc. you can just write "int main()" instead of "int _tmain(int argc, _TCHAR* argv[])" In this line argc stands vor argcount which gives you the total number of parameters plus exe-name. The Array is used for the parameters. In did not make use of this since I do not make any paremeter handling. Another thing you can omit is: #include "stdafx.h" This is also an auto generated header file with no use in this example. Godd luck, Tom _______________________________________________ Shapelib mailing list Shapelib@... http://lists.maptools.org/mailman/listinfo/shapelib |
| Free embeddable forum powered by Nabble | Forum Help |