1 #ifndef SHAPEFILE_H_INCLUDED
2 #define SHAPEFILE_H_INCLUDED
4 /******************************************************************************
5 * $Id: shapefil.h 15715 2008-11-12 15:15:21Z warmerdam $
8 * Purpose: Primary include file for Shapelib.
9 * Author: Frank Warmerdam, warmerdam@pobox.com
11 ******************************************************************************
12 * Copyright (c) 1999, Frank Warmerdam
14 * This software is available under the following "MIT Style" license,
15 * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
16 * option is discussed in more detail in shapelib.html.
20 * Permission is hereby granted, free of charge, to any person obtaining a
21 * copy of this software and associated documentation files (the "Software"),
22 * to deal in the Software without restriction, including without limitation
23 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 * and/or sell copies of the Software, and to permit persons to whom the
25 * Software is furnished to do so, subject to the following conditions:
27 * The above copyright notice and this permission notice shall be included
28 * in all copies or substantial portions of the Software.
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
36 * DEALINGS IN THE SOFTWARE.
37 ******************************************************************************
39 * $Log: shapefil.h,v $
40 * Revision 1.46 2008/11/12 14:28:15 fwarmerdam
41 * DBFCreateField() now works on files with records
43 * Revision 1.45 2008/11/11 17:47:10 fwarmerdam
44 * added DBFDeleteField() function
46 * Revision 1.44 2008/01/16 20:05:19 bram
47 * Add file hooks that accept UTF-8 encoded filenames on some platforms. Use SASetupUtf8Hooks
48 * tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability. Currently, this
49 * is only available on the Windows platform that decodes the UTF-8 filenames to wide
50 * character strings and feeds them to _wfopen and _wremove.
52 * Revision 1.43 2008/01/10 16:35:30 fwarmerdam
53 * avoid _ prefix on #defined symbols (bug 1840)
55 * Revision 1.42 2007/12/18 18:28:14 bram
56 * - create hook for client specific atof (bugzilla ticket 1615)
57 * - check for NULL handle before closing cpCPG file, and close after reading.
59 * Revision 1.41 2007/12/15 20:25:32 bram
60 * dbfopen.c now reads the Code Page information from the DBF file, and exports
61 * this information as a string through the DBFGetCodePage function. This is
62 * either the number from the LDID header field ("LDID/<number>") or as the
63 * content of an accompanying .CPG file. When creating a DBF file, the code can
64 * be set using DBFCreateEx.
66 * Revision 1.40 2007/12/06 07:00:25 fwarmerdam
67 * dbfopen now using SAHooks for fileio
69 * Revision 1.39 2007/12/04 20:37:56 fwarmerdam
70 * preliminary implementation of hooks api for io and errors
72 * Revision 1.38 2007/11/21 22:39:56 fwarmerdam
73 * close shx file in readonly mode (GDAL #1956)
75 * Revision 1.37 2007/10/27 03:31:14 fwarmerdam
76 * limit default depth of tree to 12 levels (gdal ticket #1594)
78 * Revision 1.36 2007/09/10 23:33:15 fwarmerdam
79 * Upstreamed support for visibility flag in SHPAPI_CALL for the needs
80 * of GDAL (gdal ticket #1810).
82 * Revision 1.35 2007/09/03 19:48:10 fwarmerdam
83 * move DBFReadAttribute() static dDoubleField into dbfinfo
85 * Revision 1.34 2006/06/17 15:33:32 fwarmerdam
86 * added pszWorkField - bug 1202 (rso)
88 * Revision 1.33 2006/02/15 01:14:30 fwarmerdam
89 * added DBFAddNativeFieldType
91 * Revision 1.32 2006/01/26 15:07:32 fwarmerdam
92 * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
94 * Revision 1.31 2006/01/05 01:27:27 fwarmerdam
95 * added dbf deletion mark/fetch
97 * Revision 1.30 2005/01/03 22:30:13 fwarmerdam
98 * added support for saved quadtrees
100 * Revision 1.29 2004/09/26 20:09:35 fwarmerdam
101 * avoid rcsid warnings
103 * Revision 1.28 2003/12/29 06:02:18 fwarmerdam
104 * added cpl_error.h option
106 * Revision 1.27 2003/04/21 18:30:37 warmerda
107 * added header write/update public methods
109 * Revision 1.26 2002/09/29 00:00:08 warmerda
110 * added FTLogical and logical attribute read/write calls
112 * Revision 1.25 2002/05/07 13:46:30 warmerda
113 * added DBFWriteAttributeDirectly().
115 * Revision 1.24 2002/04/10 16:59:54 warmerda
116 * added SHPRewindObject
118 * Revision 1.23 2002/01/15 14:36:07 warmerda
119 * updated email address
121 * Revision 1.22 2002/01/15 14:32:00 warmerda
122 * try to improve SHPAPI_CALL docs
128 #include <dbmalloc.h>
132 #include "cpl_error.h"
140 /************************************************************************/
141 /* Configuration options. */
142 /************************************************************************/
144 /* -------------------------------------------------------------------- */
145 /* Should the DBFReadStringAttribute() strip leading and */
146 /* trailing white space? */
147 /* -------------------------------------------------------------------- */
148 #define TRIM_DBF_WHITESPACE
150 /* -------------------------------------------------------------------- */
151 /* Should we write measure values to the Multipatch object? */
152 /* Reportedly ArcView crashes if we do write it, so for now it */
154 /* -------------------------------------------------------------------- */
155 #define DISABLE_MULTIPATCH_MEASURE
157 /* -------------------------------------------------------------------- */
160 /* The following two macros are present to allow forcing */
161 /* various calling conventions on the Shapelib API. */
163 /* To force __stdcall conventions (needed to call Shapelib */
164 /* from Visual Basic and/or Dephi I believe) the makefile could */
165 /* be modified to define: */
167 /* /DSHPAPI_CALL=__stdcall */
169 /* If it is desired to force export of the Shapelib API without */
170 /* using the shapelib.def file, use the following definition. */
172 /* /DSHAPELIB_DLLEXPORT */
174 /* To get both at once it will be necessary to hack this */
175 /* include file to define: */
177 /* #define SHPAPI_CALL __declspec(dllexport) __stdcall */
178 /* #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall */
180 /* The complexity of the situtation is partly caused by the */
181 /* peculiar requirement of Visual C++ that __stdcall appear */
182 /* after any "*"'s in the return value of a function while the */
183 /* __declspec(dllexport) must appear before them. */
184 /* -------------------------------------------------------------------- */
186 #ifdef SHAPELIB_DLLEXPORT
187 # define SHPAPI_CALL __declspec(dllexport)
188 # define SHPAPI_CALL1(x) __declspec(dllexport) x
192 # if defined(USE_GCC_VISIBILITY_FLAG)
193 # define SHPAPI_CALL __attribute__ ((visibility("default")))
194 # define SHPAPI_CALL1(x) __attribute__ ((visibility("default"))) x
201 # define SHPAPI_CALL1(x) x SHPAPI_CALL
204 /* -------------------------------------------------------------------- */
205 /* Macros for controlling CVSID and ensuring they don't appear */
206 /* as unreferenced variables resulting in lots of warnings. */
207 /* -------------------------------------------------------------------- */
208 #ifndef DISABLE_CVSID
209 # define SHP_CVSID(string) static char cpl_cvsid[] = string; \
210 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
212 # define SHP_CVSID(string)
215 /* -------------------------------------------------------------------- */
216 /* On some platforms, additional file IO hooks are defined that */
217 /* UTF-8 encoded filenames Unicode filenames */
218 /* -------------------------------------------------------------------- */
219 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
220 # define SHPAPI_WINDOWS
221 # define SHPAPI_UTF8_HOOKS
224 /* -------------------------------------------------------------------- */
225 /* IO/Error hook functions. */
226 /* -------------------------------------------------------------------- */
230 typedef unsigned long SAOffset;
234 SAFile (*FOpen) ( const char *filename, const char *access);
235 SAOffset (*FRead) ( void *p, SAOffset size, SAOffset nmemb, SAFile file);
236 SAOffset (*FWrite)( void *p, SAOffset size, SAOffset nmemb, SAFile file);
237 SAOffset (*FSeek) ( SAFile file, SAOffset offset, int whence );
238 SAOffset (*FTell) ( SAFile file );
239 int (*FFlush)( SAFile file );
240 int (*FClose)( SAFile file );
241 int (*Remove) ( const char *filename );
243 void (*Error) ( const char *message );
244 double (*Atof) ( const char *str );
247 void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks );
248 #ifdef SHPAPI_UTF8_HOOKS
249 void SHPAPI_CALL SASetupUtf8Hooks( SAHooks *psHooks );
252 /************************************************************************/
254 /************************************************************************/
262 int nShapeType; /* SHPT_* */
264 int nFileSize; /* SHP file */
271 double adBoundsMin[4];
272 double adBoundsMax[4];
276 unsigned char *pabyRec;
280 typedef SHPInfo * SHPHandle;
282 /* -------------------------------------------------------------------- */
283 /* Shape types (nSHPType) */
284 /* -------------------------------------------------------------------- */
288 #define SHPT_POLYGON 5
289 #define SHPT_MULTIPOINT 8
290 #define SHPT_POINTZ 11
292 #define SHPT_POLYGONZ 15
293 #define SHPT_MULTIPOINTZ 18
294 #define SHPT_POINTM 21
296 #define SHPT_POLYGONM 25
297 #define SHPT_MULTIPOINTM 28
298 #define SHPT_MULTIPATCH 31
301 /* -------------------------------------------------------------------- */
302 /* Part types - everything but SHPT_MULTIPATCH just uses */
304 /* -------------------------------------------------------------------- */
306 #define SHPP_TRISTRIP 0
307 #define SHPP_TRIFAN 1
308 #define SHPP_OUTERRING 2
309 #define SHPP_INNERRING 3
310 #define SHPP_FIRSTRING 4
313 /* -------------------------------------------------------------------- */
314 /* SHPObject - represents on shape (without attributes) read */
315 /* from the .shp file. */
316 /* -------------------------------------------------------------------- */
321 int nShapeId; /* -1 is unknown/unassigned */
346 /* -------------------------------------------------------------------- */
347 /* SHP API Prototypes */
348 /* -------------------------------------------------------------------- */
350 /* If pszAccess is read-only, the fpSHX field of the returned structure */
351 /* will be NULL as it is not necessary to keep the SHX file open */
352 SHPHandle SHPAPI_CALL
353 SHPOpen( const char * pszShapeFile, const char * pszAccess );
354 SHPHandle SHPAPI_CALL
355 SHPOpenLL( const char *pszShapeFile, const char *pszAccess,
357 SHPHandle SHPAPI_CALL
358 SHPCreate( const char * pszShapeFile, int nShapeType );
359 SHPHandle SHPAPI_CALL
360 SHPCreateLL( const char * pszShapeFile, int nShapeType,
363 SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
364 double * padfMinBound, double * padfMaxBound );
366 SHPObject SHPAPI_CALL1(*)
367 SHPReadObject( SHPHandle hSHP, int iShape );
369 SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
372 SHPDestroyObject( SHPObject * psObject );
374 SHPComputeExtents( SHPObject * psObject );
375 SHPObject SHPAPI_CALL1(*)
376 SHPCreateObject( int nSHPType, int nShapeId, int nParts,
377 const int * panPartStart, const int * panPartType,
379 const double * padfX, const double * padfY,
380 const double * padfZ, const double * padfM );
381 SHPObject SHPAPI_CALL1(*)
382 SHPCreateSimpleObject( int nSHPType, int nVertices,
383 const double * padfX,
384 const double * padfY,
385 const double * padfZ );
388 SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
390 void SHPAPI_CALL SHPClose( SHPHandle hSHP );
391 void SHPAPI_CALL SHPWriteHeader( SHPHandle hSHP );
393 const char SHPAPI_CALL1(*)
394 SHPTypeName( int nSHPType );
395 const char SHPAPI_CALL1(*)
396 SHPPartTypeName( int nPartType );
398 /* -------------------------------------------------------------------- */
399 /* Shape quadtree indexing API. */
400 /* -------------------------------------------------------------------- */
402 /* this can be two or four for binary or quad tree */
403 #define MAX_SUBNODE 4
405 /* upper limit of tree levels for automatic estimation */
406 #define MAX_DEFAULT_TREE_DEPTH 12
408 typedef struct shape_tree_node
410 /* region covered by this node */
411 double adfBoundsMin[4];
412 double adfBoundsMax[4];
414 /* list of shapes stored at this node. The papsShapeObj pointers
415 or the whole list can be NULL */
418 SHPObject **papsShapeObj;
421 struct shape_tree_node *apsSubNode[MAX_SUBNODE];
436 SHPTree SHPAPI_CALL1(*)
437 SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
438 double *padfBoundsMin, double *padfBoundsMax );
440 SHPDestroyTree( SHPTree * hTree );
443 SHPWriteTree( SHPTree *hTree, const char * pszFilename );
445 SHPReadTree( const char * pszFilename );
448 SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
450 SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
452 SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
455 SHPTreeTrimExtraNodes( SHPTree * hTree );
458 SHPTreeFindLikelyShapes( SHPTree * hTree,
459 double * padfBoundsMin,
460 double * padfBoundsMax,
463 SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
466 SHPSearchDiskTree( FILE *fp,
467 double *padfBoundsMin, double *padfBoundsMax,
470 /************************************************************************/
472 /************************************************************************/
486 int *panFieldDecimals;
492 int bCurrentRecordModified;
493 char *pszCurrentRecord;
495 int nWorkFieldLength;
501 double dfDoubleField;
507 typedef DBFInfo * DBFHandle;
517 #define XBASE_FLDHDR_SZ 32
520 DBFHandle SHPAPI_CALL
521 DBFOpen( const char * pszDBFFile, const char * pszAccess );
522 DBFHandle SHPAPI_CALL
523 DBFOpenLL( const char * pszDBFFile, const char * pszAccess,
525 DBFHandle SHPAPI_CALL
526 DBFCreate( const char * pszDBFFile );
527 DBFHandle SHPAPI_CALL
528 DBFCreateEx( const char * pszDBFFile, const char * pszCodePage );
529 DBFHandle SHPAPI_CALL
530 DBFCreateLL( const char * pszDBFFile, const char * pszCodePage, SAHooks *psHooks );
533 DBFGetFieldCount( DBFHandle psDBF );
535 DBFGetRecordCount( DBFHandle psDBF );
537 DBFAddField( DBFHandle hDBF, const char * pszFieldName,
538 DBFFieldType eType, int nWidth, int nDecimals );
541 DBFAddNativeFieldType( DBFHandle hDBF, const char * pszFieldName,
542 char chType, int nWidth, int nDecimals );
545 DBFDeleteField( DBFHandle hDBF, int iField );
547 DBFFieldType SHPAPI_CALL
548 DBFGetFieldInfo( DBFHandle psDBF, int iField,
549 char * pszFieldName, int * pnWidth, int * pnDecimals );
552 DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
555 DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
557 DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
558 const char SHPAPI_CALL1(*)
559 DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
560 const char SHPAPI_CALL1(*)
561 DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
563 DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
566 DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
569 DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
570 double dFieldValue );
572 DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
573 const char * pszFieldValue );
575 DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
578 DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
579 const char lFieldValue);
581 DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
583 const char SHPAPI_CALL1(*)
584 DBFReadTuple(DBFHandle psDBF, int hEntity );
586 DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
588 int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape );
589 int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
592 DBFHandle SHPAPI_CALL
593 DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
596 DBFClose( DBFHandle hDBF );
598 DBFUpdateHeader( DBFHandle hDBF );
600 DBFGetNativeFieldType( DBFHandle hDBF, int iField );
602 const char SHPAPI_CALL1(*)
603 DBFGetCodePage(DBFHandle psDBF );
609 #endif /* ndef SHAPEFILE_H_INCLUDED */