1 #ifndef SHAPEFILE_H_INCLUDED
2 #define SHAPEFILE_H_INCLUDED
4 /******************************************************************************
5 * $Id: shapefil.h,v 1.52 2011-12-11 22:26:46 fwarmerdam Exp $
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.52 2011-12-11 22:26:46 fwarmerdam
41 * upgrade .qix access code to use SAHooks (gdal #3365)
43 * Revision 1.51 2011-07-24 05:59:25 fwarmerdam
44 * minimize use of CPLError in favor of SAHooks.Error()
46 * Revision 1.50 2011-05-13 17:35:17 fwarmerdam
47 * added DBFReorderFields() and DBFAlterFields() functions (from Even)
49 * Revision 1.49 2011-04-16 14:38:21 fwarmerdam
50 * avoid warnings with gcc on SHP_CVSID
52 * Revision 1.48 2010-08-27 23:42:52 fwarmerdam
53 * add SHPAPI_CALL attribute in code
55 * Revision 1.47 2010-01-28 11:34:34 fwarmerdam
56 * handle the shape file length limits more gracefully (#3236)
58 * Revision 1.46 2008-11-12 14:28:15 fwarmerdam
59 * DBFCreateField() now works on files with records
61 * Revision 1.45 2008/11/11 17:47:10 fwarmerdam
62 * added DBFDeleteField() function
64 * Revision 1.44 2008/01/16 20:05:19 bram
65 * Add file hooks that accept UTF-8 encoded filenames on some platforms. Use SASetupUtf8Hooks
66 * tosetup the hooks and check SHPAPI_UTF8_HOOKS for its availability. Currently, this
67 * is only available on the Windows platform that decodes the UTF-8 filenames to wide
68 * character strings and feeds them to _wfopen and _wremove.
70 * Revision 1.43 2008/01/10 16:35:30 fwarmerdam
71 * avoid _ prefix on #defined symbols (bug 1840)
73 * Revision 1.42 2007/12/18 18:28:14 bram
74 * - create hook for client specific atof (bugzilla ticket 1615)
75 * - check for NULL handle before closing cpCPG file, and close after reading.
77 * Revision 1.41 2007/12/15 20:25:32 bram
78 * dbfopen.c now reads the Code Page information from the DBF file, and exports
79 * this information as a string through the DBFGetCodePage function. This is
80 * either the number from the LDID header field ("LDID/<number>") or as the
81 * content of an accompanying .CPG file. When creating a DBF file, the code can
82 * be set using DBFCreateEx.
84 * Revision 1.40 2007/12/06 07:00:25 fwarmerdam
85 * dbfopen now using SAHooks for fileio
87 * Revision 1.39 2007/12/04 20:37:56 fwarmerdam
88 * preliminary implementation of hooks api for io and errors
90 * Revision 1.38 2007/11/21 22:39:56 fwarmerdam
91 * close shx file in readonly mode (GDAL #1956)
93 * Revision 1.37 2007/10/27 03:31:14 fwarmerdam
94 * limit default depth of tree to 12 levels (gdal ticket #1594)
96 * Revision 1.36 2007/09/10 23:33:15 fwarmerdam
97 * Upstreamed support for visibility flag in SHPAPI_CALL for the needs
98 * of GDAL (gdal ticket #1810).
100 * Revision 1.35 2007/09/03 19:48:10 fwarmerdam
101 * move DBFReadAttribute() static dDoubleField into dbfinfo
103 * Revision 1.34 2006/06/17 15:33:32 fwarmerdam
104 * added pszWorkField - bug 1202 (rso)
106 * Revision 1.33 2006/02/15 01:14:30 fwarmerdam
107 * added DBFAddNativeFieldType
109 * Revision 1.32 2006/01/26 15:07:32 fwarmerdam
110 * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
112 * Revision 1.31 2006/01/05 01:27:27 fwarmerdam
113 * added dbf deletion mark/fetch
115 * Revision 1.30 2005/01/03 22:30:13 fwarmerdam
116 * added support for saved quadtrees
118 * Revision 1.29 2004/09/26 20:09:35 fwarmerdam
119 * avoid rcsid warnings
121 * Revision 1.28 2003/12/29 06:02:18 fwarmerdam
122 * added cpl_error.h option
124 * Revision 1.27 2003/04/21 18:30:37 warmerda
125 * added header write/update public methods
127 * Revision 1.26 2002/09/29 00:00:08 warmerda
128 * added FTLogical and logical attribute read/write calls
130 * Revision 1.25 2002/05/07 13:46:30 warmerda
131 * added DBFWriteAttributeDirectly().
133 * Revision 1.24 2002/04/10 16:59:54 warmerda
134 * added SHPRewindObject
136 * Revision 1.23 2002/01/15 14:36:07 warmerda
137 * updated email address
139 * Revision 1.22 2002/01/15 14:32:00 warmerda
140 * try to improve SHPAPI_CALL docs
146 #include <dbmalloc.h>
153 /************************************************************************/
154 /* Configuration options. */
155 /************************************************************************/
157 /* -------------------------------------------------------------------- */
158 /* Should the DBFReadStringAttribute() strip leading and */
159 /* trailing white space? */
160 /* -------------------------------------------------------------------- */
161 #define TRIM_DBF_WHITESPACE
163 /* -------------------------------------------------------------------- */
164 /* Should we write measure values to the Multipatch object? */
165 /* Reportedly ArcView crashes if we do write it, so for now it */
167 /* -------------------------------------------------------------------- */
168 #define DISABLE_MULTIPATCH_MEASURE
170 /* -------------------------------------------------------------------- */
173 /* The following two macros are present to allow forcing */
174 /* various calling conventions on the Shapelib API. */
176 /* To force __stdcall conventions (needed to call Shapelib */
177 /* from Visual Basic and/or Dephi I believe) the makefile could */
178 /* be modified to define: */
180 /* /DSHPAPI_CALL=__stdcall */
182 /* If it is desired to force export of the Shapelib API without */
183 /* using the shapelib.def file, use the following definition. */
185 /* /DSHAPELIB_DLLEXPORT */
187 /* To get both at once it will be necessary to hack this */
188 /* include file to define: */
190 /* #define SHPAPI_CALL __declspec(dllexport) __stdcall */
191 /* #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall */
193 /* The complexity of the situtation is partly caused by the */
194 /* peculiar requirement of Visual C++ that __stdcall appear */
195 /* after any "*"'s in the return value of a function while the */
196 /* __declspec(dllexport) must appear before them. */
197 /* -------------------------------------------------------------------- */
200 #define SHAPELIB_DLLEXPORT 1
204 #ifdef SHAPELIB_DLLEXPORT
205 # define SHPAPI_CALL __declspec(dllexport)
206 # define SHPAPI_CALL1(x) __declspec(dllexport) x
208 # define SHPAPI_CALL __declspec(dllimport)
209 # define SHPAPI_CALL1(x) __declspec(dllimport) x
214 # if defined(USE_GCC_VISIBILITY_FLAG)
215 # define SHPAPI_CALL __attribute__ ((visibility("default")))
216 # define SHPAPI_CALL1(x) __attribute__ ((visibility("default"))) x
223 # define SHPAPI_CALL1(x) x SHPAPI_CALL
226 /* -------------------------------------------------------------------- */
227 /* Macros for controlling CVSID and ensuring they don't appear */
228 /* as unreferenced variables resulting in lots of warnings. */
229 /* -------------------------------------------------------------------- */
230 #ifndef DISABLE_CVSID
231 # if defined(__GNUC__) && __GNUC__ >= 4
232 # define SHP_CVSID(string) static char cpl_cvsid[] __attribute__((used)) = string;
234 # define SHP_CVSID(string) static char cpl_cvsid[] = string; \
235 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
238 # define SHP_CVSID(string)
241 /* -------------------------------------------------------------------- */
242 /* On some platforms, additional file IO hooks are defined that */
243 /* UTF-8 encoded filenames Unicode filenames */
244 /* -------------------------------------------------------------------- */
245 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
246 # define SHPAPI_WINDOWS
247 # define SHPAPI_UTF8_HOOKS
250 /* -------------------------------------------------------------------- */
251 /* IO/Error hook functions. */
252 /* -------------------------------------------------------------------- */
256 typedef unsigned long SAOffset;
260 SAFile (*FOpen) ( const char *filename, const char *access);
261 SAOffset (*FRead) ( void *p, SAOffset size, SAOffset nmemb, SAFile file);
262 SAOffset (*FWrite)( void *p, SAOffset size, SAOffset nmemb, SAFile file);
263 SAOffset (*FSeek) ( SAFile file, SAOffset offset, int whence );
264 SAOffset (*FTell) ( SAFile file );
265 int (*FFlush)( SAFile file );
266 int (*FClose)( SAFile file );
267 int (*Remove) ( const char *filename );
269 void (*Error) ( const char *message );
270 double (*Atof) ( const char *str );
273 void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks );
274 #ifdef SHPAPI_UTF8_HOOKS
275 void SHPAPI_CALL SASetupUtf8Hooks( SAHooks *psHooks );
278 /************************************************************************/
280 /************************************************************************/
288 int nShapeType; /* SHPT_* */
290 unsigned int nFileSize; /* SHP file */
294 unsigned int *panRecOffset;
295 unsigned int *panRecSize;
297 double adBoundsMin[4];
298 double adBoundsMax[4];
302 unsigned char *pabyRec;
306 typedef SHPInfo * SHPHandle;
308 /* -------------------------------------------------------------------- */
309 /* Shape types (nSHPType) */
310 /* -------------------------------------------------------------------- */
314 #define SHPT_POLYGON 5
315 #define SHPT_MULTIPOINT 8
316 #define SHPT_POINTZ 11
318 #define SHPT_POLYGONZ 15
319 #define SHPT_MULTIPOINTZ 18
320 #define SHPT_POINTM 21
322 #define SHPT_POLYGONM 25
323 #define SHPT_MULTIPOINTM 28
324 #define SHPT_MULTIPATCH 31
327 /* -------------------------------------------------------------------- */
328 /* Part types - everything but SHPT_MULTIPATCH just uses */
330 /* -------------------------------------------------------------------- */
332 #define SHPP_TRISTRIP 0
333 #define SHPP_TRIFAN 1
334 #define SHPP_OUTERRING 2
335 #define SHPP_INNERRING 3
336 #define SHPP_FIRSTRING 4
339 /* -------------------------------------------------------------------- */
340 /* SHPObject - represents on shape (without attributes) read */
341 /* from the .shp file. */
342 /* -------------------------------------------------------------------- */
347 int nShapeId; /* -1 is unknown/unassigned */
372 /* -------------------------------------------------------------------- */
373 /* SHP API Prototypes */
374 /* -------------------------------------------------------------------- */
376 /* If pszAccess is read-only, the fpSHX field of the returned structure */
377 /* will be NULL as it is not necessary to keep the SHX file open */
378 SHPHandle SHPAPI_CALL
379 SHPOpen( const char * pszShapeFile, const char * pszAccess );
380 SHPHandle SHPAPI_CALL
381 SHPOpenLL( const char *pszShapeFile, const char *pszAccess,
383 SHPHandle SHPAPI_CALL
384 SHPCreate( const char * pszShapeFile, int nShapeType );
385 SHPHandle SHPAPI_CALL
386 SHPCreateLL( const char * pszShapeFile, int nShapeType,
389 SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
390 double * padfMinBound, double * padfMaxBound );
392 SHPObject SHPAPI_CALL1(*)
393 SHPReadObject( SHPHandle hSHP, int iShape );
395 SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
398 SHPDestroyObject( SHPObject * psObject );
400 SHPComputeExtents( SHPObject * psObject );
401 SHPObject SHPAPI_CALL1(*)
402 SHPCreateObject( int nSHPType, int nShapeId, int nParts,
403 const int * panPartStart, const int * panPartType,
405 const double * padfX, const double * padfY,
406 const double * padfZ, const double * padfM );
407 SHPObject SHPAPI_CALL1(*)
408 SHPCreateSimpleObject( int nSHPType, int nVertices,
409 const double * padfX,
410 const double * padfY,
411 const double * padfZ );
414 SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
416 void SHPAPI_CALL SHPClose( SHPHandle hSHP );
417 void SHPAPI_CALL SHPWriteHeader( SHPHandle hSHP );
419 const char SHPAPI_CALL1(*)
420 SHPTypeName( int nSHPType );
421 const char SHPAPI_CALL1(*)
422 SHPPartTypeName( int nPartType );
424 /* -------------------------------------------------------------------- */
425 /* Shape quadtree indexing API. */
426 /* -------------------------------------------------------------------- */
428 /* this can be two or four for binary or quad tree */
429 #define MAX_SUBNODE 4
431 /* upper limit of tree levels for automatic estimation */
432 #define MAX_DEFAULT_TREE_DEPTH 12
434 typedef struct shape_tree_node
436 /* region covered by this node */
437 double adfBoundsMin[4];
438 double adfBoundsMax[4];
440 /* list of shapes stored at this node. The papsShapeObj pointers
441 or the whole list can be NULL */
444 SHPObject **papsShapeObj;
447 struct shape_tree_node *apsSubNode[MAX_SUBNODE];
462 SHPTree SHPAPI_CALL1(*)
463 SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
464 double *padfBoundsMin, double *padfBoundsMax );
466 SHPDestroyTree( SHPTree * hTree );
469 SHPWriteTree( SHPTree *hTree, const char * pszFilename );
472 SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
474 SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
477 SHPTreeTrimExtraNodes( SHPTree * hTree );
480 SHPTreeFindLikelyShapes( SHPTree * hTree,
481 double * padfBoundsMin,
482 double * padfBoundsMax,
485 SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
488 SHPSearchDiskTree( FILE *fp,
489 double *padfBoundsMin, double *padfBoundsMax,
493 typedef struct SHPDiskTreeInfo* SHPTreeDiskHandle;
495 SHPTreeDiskHandle SHPAPI_CALL
496 SHPOpenDiskTree( const char* pszQIXFilename,
500 SHPCloseDiskTree( SHPTreeDiskHandle hDiskTree );
503 SHPSearchDiskTreeEx( SHPTreeDiskHandle hDiskTree,
504 double *padfBoundsMin, double *padfBoundsMax,
508 SHPWriteTreeLL(SHPTree *hTree, const char *pszFilename, SAHooks *psHooks );
510 /************************************************************************/
512 /************************************************************************/
526 int *panFieldDecimals;
532 int bCurrentRecordModified;
533 char *pszCurrentRecord;
535 int nWorkFieldLength;
541 double dfDoubleField;
547 typedef DBFInfo * DBFHandle;
557 #define XBASE_FLDHDR_SZ 32
560 DBFHandle SHPAPI_CALL
561 DBFOpen( const char * pszDBFFile, const char * pszAccess );
562 DBFHandle SHPAPI_CALL
563 DBFOpenLL( const char * pszDBFFile, const char * pszAccess,
565 DBFHandle SHPAPI_CALL
566 DBFCreate( const char * pszDBFFile );
567 DBFHandle SHPAPI_CALL
568 DBFCreateEx( const char * pszDBFFile, const char * pszCodePage );
569 DBFHandle SHPAPI_CALL
570 DBFCreateLL( const char * pszDBFFile, const char * pszCodePage, SAHooks *psHooks );
573 DBFGetFieldCount( DBFHandle psDBF );
575 DBFGetRecordCount( DBFHandle psDBF );
577 DBFAddField( DBFHandle hDBF, const char * pszFieldName,
578 DBFFieldType eType, int nWidth, int nDecimals );
581 DBFAddNativeFieldType( DBFHandle hDBF, const char * pszFieldName,
582 char chType, int nWidth, int nDecimals );
585 DBFDeleteField( DBFHandle hDBF, int iField );
588 DBFReorderFields( DBFHandle psDBF, int* panMap );
591 DBFAlterFieldDefn( DBFHandle psDBF, int iField, const char * pszFieldName,
592 char chType, int nWidth, int nDecimals );
594 DBFFieldType SHPAPI_CALL
595 DBFGetFieldInfo( DBFHandle psDBF, int iField,
596 char * pszFieldName, int * pnWidth, int * pnDecimals );
599 DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
602 DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
604 DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
605 const char SHPAPI_CALL1(*)
606 DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
607 const char SHPAPI_CALL1(*)
608 DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
610 DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
613 DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
616 DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
617 double dFieldValue );
619 DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
620 const char * pszFieldValue );
622 DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
625 DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
626 const char lFieldValue);
628 DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
630 const char SHPAPI_CALL1(*)
631 DBFReadTuple(DBFHandle psDBF, int hEntity );
633 DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
635 int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape );
636 int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
639 DBFHandle SHPAPI_CALL
640 DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
643 DBFClose( DBFHandle hDBF );
645 DBFUpdateHeader( DBFHandle hDBF );
647 DBFGetNativeFieldType( DBFHandle hDBF, int iField );
649 const char SHPAPI_CALL1(*)
650 DBFGetCodePage(DBFHandle psDBF );
656 #endif /* ndef SHAPEFILE_H_INCLUDED */