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