1 #ifndef _SHAPEFILE_H_INCLUDED
2 #define _SHAPEFILE_H_INCLUDED
3 
4 /******************************************************************************
5  * $Id: shapefil.h 290 2015-02-03 13:57:30Z rsbivand $
6  *
7  * Project:  Shapelib
8  * Purpose:  Primary include file for Shapelib.
9  * Author:   Frank Warmerdam, warmerdam@pobox.com
10  *
11  ******************************************************************************
12  * Copyright (c) 1999, Frank Warmerdam
13  *
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.
17  *
18  * --
19  *
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:
26  *
27  * The above copyright notice and this permission notice shall be included
28  * in all copies or substantial portions of the Software.
29  *
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  ******************************************************************************
38  *
39  * $Log: shapefil.h,v $
40  * Revision 1.1.1.1  2005/09/01 18:21:44  rsbivand
41  * Initial import.
42  *
43  * Revision 1.14  2005/03/14 15:21:29  fwarmerdam
44  * updated
45  *
46  * Revision 1.30  2005/01/03 22:30:13  fwarmerdam
47  * added support for saved quadtrees
48  *
49  * Revision 1.29  2004/09/26 20:09:35  fwarmerdam
50  * avoid rcsid warnings
51  *
52  * Revision 1.28  2003/12/29 06:02:18  fwarmerdam
53  * added cpl_error.h option
54  *
55  * Revision 1.27  2003/04/21 18:30:37  warmerda
56  * added header write/update public methods
57  *
58  * Revision 1.26  2002/09/29 00:00:08  warmerda
59  * added FTLogical and logical attribute read/write calls
60  *
61  * Revision 1.25  2002/05/07 13:46:30  warmerda
62  * added DBFWriteAttributeDirectly().
63  *
64  * Revision 1.24  2002/04/10 16:59:54  warmerda
65  * added SHPRewindObject
66  *
67  * Revision 1.23  2002/01/15 14:36:07  warmerda
68  * updated email address
69  *
70  * Revision 1.22  2002/01/15 14:32:00  warmerda
71  * try to improve SHPAPI_CALL docs
72  *
73  * Revision 1.21  2001/11/01 16:29:55  warmerda
74  * move pabyRec into SHPInfo for thread safety
75  *
76  * Revision 1.20  2001/07/20 13:06:02  warmerda
77  * fixed SHPAPI attribute for SHPTreeFindLikelyShapes
78  *
79  * Revision 1.19  2001/05/31 19:20:13  warmerda
80  * added DBFGetFieldIndex()
81  *
82  * Revision 1.18  2001/05/31 18:15:40  warmerda
83  * Added support for NULL fields in DBF files
84  *
85  * Revision 1.17  2001/05/23 13:36:52  warmerda
86  * added use of SHPAPI_CALL
87  *
88  * Revision 1.16  2000/09/25 14:15:59  warmerda
89  * added DBFGetNativeFieldType()
90  *
91  * Revision 1.15  2000/02/16 16:03:51  warmerda
92  * added null shape support
93  *
94  * Revision 1.14  1999/11/05 14:12:05  warmerda
95  * updated license terms
96  *
97  * Revision 1.13  1999/06/02 18:24:21  warmerda
98  * added trimming code
99  *
100  * Revision 1.12  1999/06/02 17:56:12  warmerda
101  * added quad'' subnode support for trees
102  *
103  * Revision 1.11  1999/05/18 19:11:11  warmerda
104  * Added example searching capability
105  *
106  * Revision 1.10  1999/05/18 17:49:38  warmerda
107  * added initial quadtree support
108  *
109  * Revision 1.9  1999/05/11 03:19:28  warmerda
110  * added new Tuple api, and improved extension handling - add from candrsn
111  *
112  * Revision 1.8  1999/03/23 17:22:27  warmerda
113  * Added extern "C" protection for C++ users of shapefil.h.
114  *
115  * Revision 1.7  1998/12/31 15:31:07  warmerda
116  * Added the TRIM_DBF_WHITESPACE and DISABLE_MULTIPATCH_MEASURE options.
117  *
118  * Revision 1.6  1998/12/03 15:48:15  warmerda
119  * Added SHPCalculateExtents().
120  *
121  * Revision 1.5  1998/11/09 20:57:16  warmerda
122  * Altered SHPGetInfo() call.
123  *
124  * Revision 1.4  1998/11/09 20:19:33  warmerda
125  * Added 3D support, and use of SHPObject.
126  *
127  * Revision 1.3  1995/08/23 02:24:05  warmerda
128  * Added support for reading bounds.
129  *
130  * Revision 1.2  1995/08/04  03:17:39  warmerda
131  * Added header.
132  *
133  */
134 
135 #include <stdio.h>
136 
137 #ifdef USE_DBMALLOC
138 #include <dbmalloc.h>
139 #endif
140 
141 #ifdef USE_CPL
142 #include "cpl_error.h"
143 #endif
144 
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148 
149 /************************************************************************/
150 /*                        Configuration options.                        */
151 /************************************************************************/
152 
153 /* -------------------------------------------------------------------- */
154 /*      Should the DBFReadStringAttribute() strip leading and           */
155 /*      trailing white space?                                           */
156 /* -------------------------------------------------------------------- */
157 #define TRIM_DBF_WHITESPACE
158 
159 /* -------------------------------------------------------------------- */
160 /*      Should we write measure values to the Multipatch object?        */
161 /*      Reportedly ArcView crashes if we do write it, so for now it     */
162 /*      is disabled.                                                    */
163 /* -------------------------------------------------------------------- */
164 #define DISABLE_MULTIPATCH_MEASURE
165 
166 /* -------------------------------------------------------------------- */
167 /*      SHPAPI_CALL                                                     */
168 /*                                                                      */
169 /*      The following two macros are present to allow forcing           */
170 /*      various calling conventions on the Shapelib API.                */
171 /*                                                                      */
172 /*      To force __stdcall conventions (needed to call Shapelib         */
173 /*      from Visual Basic and/or Dephi I believe) the makefile could    */
174 /*      be modified to define:                                          */
175 /*                                                                      */
176 /*        /DSHPAPI_CALL=__stdcall                                       */
177 /*                                                                      */
178 /*      If it is desired to force export of the Shapelib API without    */
179 /*      using the shapelib.def file, use the following definition.      */
180 /*                                                                      */
181 /*        /DSHAPELIB_DLLEXPORT                                          */
182 /*                                                                      */
183 /*      To get both at once it will be necessary to hack this           */
184 /*      include file to define:                                         */
185 /*                                                                      */
186 /*        #define SHPAPI_CALL __declspec(dllexport) __stdcall           */
187 /*        #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall        */
188 /*                                                                      */
189 /*      The complexity of the situtation is partly caused by the        */
190 /*      peculiar requirement of Visual C++ that __stdcall appear        */
191 /*      after any "*"'s in the return value of a function while the     */
192 /*      __declspec(dllexport) must appear before them.                  */
193 /* -------------------------------------------------------------------- */
194 
195 #ifdef SHAPELIB_DLLEXPORT
196 #  define SHPAPI_CALL __declspec(dllexport)
197 #  define SHPAPI_CALL1(x)  __declspec(dllexport) x
198 #endif
199 
200 #ifndef SHPAPI_CALL
201 #  define SHPAPI_CALL
202 #endif
203 
204 #ifndef SHPAPI_CALL1
205 #  define SHPAPI_CALL1(x)      x SHPAPI_CALL
206 #endif
207 
208 /* -------------------------------------------------------------------- */
209 /*      Macros for controlling CVSID and ensuring they don't appear     */
210 /*      as unreferenced variables resulting in lots of warnings.        */
211 /* -------------------------------------------------------------------- */
212 /*#ifndef DISABLE_CVSID
213 #  define SHP_CVSID(string)     static char cpl_cvsid[] = string; \
214 static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
215 #else
216 #  define SHP_CVSID(string)
217 #endif*/
218 
219 /************************************************************************/
220 /*                             SHP Support.                             */
221 /************************************************************************/
222 typedef	struct
223 {
224     FILE        *fpSHP;
225     FILE	*fpSHX;
226 
227     int		nShapeType;				/* SHPT_* */
228 
229     int		nFileSize;				/* SHP file */
230 
231     int         nRecords;
232     int		nMaxRecords;
233     int		*panRecOffset;
234     int		*panRecSize;
235 
236     double	adBoundsMin[4];
237     double	adBoundsMax[4];
238 
239     int		bUpdated;
240 
241     unsigned char *pabyRec;
242     int         nBufSize;
243 } SHPInfo;
244 
245 typedef SHPInfo * SHPHandle;
246 
247 /* -------------------------------------------------------------------- */
248 /*      Shape types (nSHPType)                                          */
249 /* -------------------------------------------------------------------- */
250 #define SHPT_NULL	0
251 #define SHPT_POINT	1
252 #define SHPT_ARC	3
253 #define SHPT_POLYGON	5
254 #define SHPT_MULTIPOINT	8
255 #define SHPT_POINTZ	11
256 #define SHPT_ARCZ	13
257 #define SHPT_POLYGONZ	15
258 #define SHPT_MULTIPOINTZ 18
259 #define SHPT_POINTM	21
260 #define SHPT_ARCM	23
261 #define SHPT_POLYGONM	25
262 #define SHPT_MULTIPOINTM 28
263 #define SHPT_MULTIPATCH 31
264 
265 
266 /* -------------------------------------------------------------------- */
267 /*      Part types - everything but SHPT_MULTIPATCH just uses           */
268 /*      SHPP_RING.                                                      */
269 /* -------------------------------------------------------------------- */
270 
271 #define SHPP_TRISTRIP	0
272 #define SHPP_TRIFAN	1
273 #define SHPP_OUTERRING	2
274 #define SHPP_INNERRING	3
275 #define SHPP_FIRSTRING	4
276 #define SHPP_RING	5
277 
278 /* -------------------------------------------------------------------- */
279 /*      SHPObject - represents on shape (without attributes) read       */
280 /*      from the .shp file.                                             */
281 /* -------------------------------------------------------------------- */
282 typedef struct
283 {
284     int		nSHPType;
285 
286     int		nShapeId; /* -1 is unknown/unassigned */
287 
288     int		nParts;
289     int		*panPartStart;
290     int		*panPartType;
291 
292     int		nVertices;
293     double	*padfX;
294     double	*padfY;
295     double	*padfZ;
296     double	*padfM;
297 
298     double	dfXMin;
299     double	dfYMin;
300     double	dfZMin;
301     double	dfMMin;
302 
303     double	dfXMax;
304     double	dfYMax;
305     double	dfZMax;
306     double	dfMMax;
307 } SHPObject;
308 
309 /* -------------------------------------------------------------------- */
310 /*      SHP API Prototypes                                              */
311 /* -------------------------------------------------------------------- */
312 SHPHandle SHPAPI_CALL
313       SHPOpen( const char * pszShapeFile, const char * pszAccess );
314 SHPHandle SHPAPI_CALL
315       SHPCreate( const char * pszShapeFile, int nShapeType );
316 void SHPAPI_CALL
317       SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
318                   double * padfMinBound, double * padfMaxBound );
319 
320 SHPObject SHPAPI_CALL1(*)
321       SHPReadObject( SHPHandle hSHP, int iShape );
322 int SHPAPI_CALL
323       SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
324 
325 void SHPAPI_CALL
326       SHPDestroyObject( SHPObject * psObject );
327 void SHPAPI_CALL
328       SHPComputeExtents( SHPObject * psObject );
329 SHPObject SHPAPI_CALL1(*)
330       SHPCreateObject( int nSHPType, int nShapeId, int nParts,
331                        const int * panPartStart, const int * panPartType,
332                        int nVertices,
333                        const double * padfX, const double * padfY,
334                        const double * padfZ, const double * padfM );
335 SHPObject SHPAPI_CALL1(*)
336       SHPCreateSimpleObject( int nSHPType, int nVertices,
337                              const double * padfX,
338                              const double * padfY,
339                              const double * padfZ );
340 
341 int SHPAPI_CALL
342       SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
343 
344 void SHPAPI_CALL SHPClose( SHPHandle hSHP );
345 void SHPAPI_CALL SHPWriteHeader( SHPHandle hSHP );
346 
347 const char SHPAPI_CALL1(*)
348       SHPTypeName( int nSHPType );
349 const char SHPAPI_CALL1(*)
350       SHPPartTypeName( int nPartType );
351 
352 /* -------------------------------------------------------------------- */
353 /*      Shape quadtree indexing API.                                    */
354 /* -------------------------------------------------------------------- */
355 
356 /* this can be two or four for binary or quad tree */
357 #define MAX_SUBNODE	4
358 
359 typedef struct shape_tree_node
360 {
361     /* region covered by this node */
362     double	adfBoundsMin[4];
363     double	adfBoundsMax[4];
364 
365     /* list of shapes stored at this node.  The papsShapeObj pointers
366        or the whole list can be NULL */
367     int		nShapeCount;
368     int		*panShapeIds;
369     SHPObject   **papsShapeObj;
370 
371     int		nSubNodes;
372     struct shape_tree_node *apsSubNode[MAX_SUBNODE];
373 
374 } SHPTreeNode;
375 
376 typedef struct
377 {
378     SHPHandle   hSHP;
379 
380     int		nMaxDepth;
381     int		nDimension;
382     int         nTotalCount;
383 
384     SHPTreeNode	*psRoot;
385 } SHPTree;
386 
387 SHPTree SHPAPI_CALL1(*)
388       SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
389                      double *padfBoundsMin, double *padfBoundsMax );
390 void    SHPAPI_CALL
391       SHPDestroyTree( SHPTree * hTree );
392 
393 int	SHPAPI_CALL
394       SHPWriteTree( SHPTree *hTree, const char * pszFilename );
395 SHPTree SHPAPI_CALL
396       SHPReadTree( const char * pszFilename );
397 
398 int	SHPAPI_CALL
399       SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
400 int	SHPAPI_CALL
401       SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
402 int	SHPAPI_CALL
403       SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
404 
405 void 	SHPAPI_CALL
406       SHPTreeTrimExtraNodes( SHPTree * hTree );
407 
408 int    SHPAPI_CALL1(*)
409       SHPTreeFindLikelyShapes( SHPTree * hTree,
410                                double * padfBoundsMin,
411                                double * padfBoundsMax,
412                                int * );
413 int     SHPAPI_CALL
414       SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
415 
416 int SHPAPI_CALL1(*)
417 SHPSearchDiskTree( FILE *fp,
418                    double *padfBoundsMin, double *padfBoundsMax,
419                    int *pnShapeCount );
420 
421 /************************************************************************/
422 /*                             DBF Support.                             */
423 /************************************************************************/
424 typedef	struct
425 {
426     FILE	*fp;
427 
428     int         nRecords;
429 
430     int		nRecordLength;
431     int		nHeaderLength;
432     int		nFields;
433     int		*panFieldOffset;
434     int		*panFieldSize;
435     int		*panFieldDecimals;
436     char	*pachFieldType;
437 
438     char	*pszHeader;
439 
440     int		nCurrentRecord;
441     int		bCurrentRecordModified;
442     char	*pszCurrentRecord;
443 
444     int		bNoHeader;
445     int		bUpdated;
446 } DBFInfo;
447 
448 typedef DBFInfo * DBFHandle;
449 
450 typedef enum {
451   FTString,
452   FTInteger,
453   FTDouble,
454   FTLogical,
455   FTInvalid
456 } DBFFieldType;
457 
458 #define XBASE_FLDHDR_SZ       32
459 
460 DBFHandle SHPAPI_CALL
461       DBFOpen( const char * pszDBFFile, const char * pszAccess );
462 DBFHandle SHPAPI_CALL
463       DBFCreate( const char * pszDBFFile );
464 
465 int	SHPAPI_CALL
466       DBFGetFieldCount( DBFHandle psDBF );
467 int	SHPAPI_CALL
468       DBFGetRecordCount( DBFHandle psDBF );
469 int	SHPAPI_CALL
470       DBFAddField( DBFHandle hDBF, const char * pszFieldName,
471                    DBFFieldType eType, int nWidth, int nDecimals );
472 
473 DBFFieldType SHPAPI_CALL
474       DBFGetFieldInfo( DBFHandle psDBF, int iField,
475                        char * pszFieldName, int * pnWidth, int * pnDecimals );
476 
477 int SHPAPI_CALL
478       DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
479 
480 int 	SHPAPI_CALL
481       DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
482 double 	SHPAPI_CALL
483       DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
484 const char SHPAPI_CALL1(*)
485       DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
486 const char SHPAPI_CALL1(*)
487       DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
488 int     SHPAPI_CALL
489       DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
490 
491 int SHPAPI_CALL
492       DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
493                                 int nFieldValue );
494 int SHPAPI_CALL
495       DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
496                                double dFieldValue );
497 int SHPAPI_CALL
498       DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
499                                const char * pszFieldValue );
500 int SHPAPI_CALL
501      DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
502 
503 int SHPAPI_CALL
504      DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
505 			       const char lFieldValue);
506 int SHPAPI_CALL
507      DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
508                                void * pValue );
509 const char SHPAPI_CALL1(*)
510       DBFReadTuple(DBFHandle psDBF, int hEntity );
511 int SHPAPI_CALL
512       DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
513 
514 DBFHandle SHPAPI_CALL
515       DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
516 
517 void	SHPAPI_CALL
518       DBFClose( DBFHandle hDBF );
519 void    SHPAPI_CALL
520       DBFUpdateHeader( DBFHandle hDBF );
521 char    SHPAPI_CALL
522       DBFGetNativeFieldType( DBFHandle hDBF, int iField );
523 
524 #ifdef __cplusplus
525 }
526 #endif
527 
528 #endif /* ndef _SHAPEFILE_H_INCLUDED */
529