[Shapelib] Adding new shape to shapefile

Bram de Greve bram.degreve at bramz.net
Tue Jun 3 11:29:08 PDT 2008


Hi Romain,

- First, creating-closing-opening probably should not be necessary.  I 
believe you're just fine if you only do the create step.
- I see you don't close the shapefile after writing.  Not sure if this 
is relevant in this case as I see this isn't plain C, so there might be 
an automatic close I'm not aware of.
- Before writing data to a DBF, you first must create the fields (see 
DBFAddField)
- you should not use -1 as record number for a DBF file.  This is just 
fine with adding objects to the shapefile to indicate you want to append 
it to the end, but the DBF API does not understand this.  It just 
returns false as error code (check your error codes!).  Always make sure 
the record number is between 0 and DBFGetRecordCount, 0 and 
DBFGetRecordCount included.  When index is DBFGetRecordCount, it will 
append a new record (but only while writing of course, don't do that for 
reading).

Hope this helps,
Bram

thecrashteam wrote:
> hi,
>
> I post my new code. It doesn't function but there is no error. My shapeFile
> and its dbf file are not created. Someone could say to me why it doesn't
> work please ?
>
> Thank you for your help
>
> Romain
>
>
>
>
> IntPtr ptrCreateSHP = shpFile.SHPCreate(@"\SD-MMCard\TestParcelle\test.shp",
> ShapeFile.ShapeFile.ShapeType.Point);
> shpFile.SHPClose(ptrCreateSHP);
>
> IntPtr ptrCreateDBF =
> shpFile.DBFCreate(@"\SD-MMCard\TestParcelle\test.dbf");
> shpFile.DBFClose(ptrCreateDBF);
>
> IntPtr ptrOpenDBF = shpFile.DBFOpen(@"\SD-MMCard\TestParcelle\test.dbf",
> "rb+");
>
> IntPtr ptrOpenSHP = shpFile.SHPOpen(@"\SD-MMCard\TestParcelle\test.shp",
> "rb+");
>
> double[] x = new double[1];
> double[] y = new double[1];
> double[] z = new double[1];
> x[0] = 803606;
> y[0] = 803606;
> z[0] = 0;
>
> IntPtr psObject =
> shpFile.SHPCreateSimpleObject(ShapeFile.ShapeFile.ShapeType.Point, 1, x, y,
> z);
>
> shpFile.SHPWriteObject(ptrOpenSHP, -1, psObject);
> shpFile.SHPDestroyObject(psObject);
>
> shpFile.DBFWriteDoubleAttribute(ptrOpenDBF, -1, 0, 0d);
> shpFile.DBFWriteStringAttribute(ptrOpenDBF, -1, 1, "toto");
> shpFile.DBFWriteStringAttribute(ptrOpenDBF, -1, 2, "aime");
> shpFile.DBFWriteStringAttribute(ptrOpenDBF, -1, 3, "les");
> shpFile.DBFWriteStringAttribute(ptrOpenDBF, -1, 4, "fraises");
>
> shpFile.DBFClose(ptrOpenDBF);
>
>   



More information about the Shapelib mailing list