1 #ifndef _SHAPEFILE_H_INCLUDED
2 #define _SHAPEFILE_H_INCLUDED
3 
4 /******************************************************************************
5  * Project:  Shapelib
6  * Purpose:  Primary include file for Shapelib.
7  * Author:   Frank Warmerdam, warmerdam@pobox.com
8  *
9  ******************************************************************************
10  * Copyright (c) 1999, Frank Warmerdam
11  *
12  * This software is available under the following "MIT Style" license,
13  * or at the option of the licensee under the LGPL (see LICENSE.LGPL).  This
14  * option is discussed in more detail in shapelib.html.
15  *
16  * --
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining a
19  * copy of this software and associated documentation files (the "Software"),
20  * to deal in the Software without restriction, including without limitation
21  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22  * and/or sell copies of the Software, and to permit persons to whom the
23  * Software is furnished to do so, subject to the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be included
26  * in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
29  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
34  * DEALINGS IN THE SOFTWARE.
35  ******************************************************************************
36  */
37 
38 #include <stdio.h>
39 
40 #ifdef USE_DBMALLOC
41 #include <dbmalloc.h>
42 #endif
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 /************************************************************************/
49 /*                        Configuration options.                        */
50 /************************************************************************/
51 
52 /* -------------------------------------------------------------------- */
53 /*      Should the DBFReadStringAttribute() strip leading and           */
54 /*      trailing white space?                                           */
55 /* -------------------------------------------------------------------- */
56 #define TRIM_DBF_WHITESPACE
57 
58 /* -------------------------------------------------------------------- */
59 /*      Should we write measure values to the Multipatch object?        */
60 /*      Reportedly ArcView crashes if we do write it, so for now it     */
61 /*      is disabled.                                                    */
62 /* -------------------------------------------------------------------- */
63 #define DISABLE_MULTIPATCH_MEASURE
64 
65 /* -------------------------------------------------------------------- */
66 /*      SHPAPI_CALL                                                     */
67 /*                                                                      */
68 /*      The following two macros are present to allow forcing           */
69 /*      various calling conventions on the Shapelib API.                */
70 /*                                                                      */
71 /*      To force __stdcall conventions (needed to call Shapelib         */
72 /*      from Visual Basic and/or Dephi I believe) the makefile could    */
73 /*      be modified to define:                                          */
74 /*                                                                      */
75 /*        /DSHPAPI_CALL=__stdcall                                       */
76 /*                                                                      */
77 /*      If it is desired to force export of the Shapelib API without    */
78 /*      using the shapelib.def file, use the following definition.      */
79 /*                                                                      */
80 /*        /DSHAPELIB_DLLEXPORT                                          */
81 /*                                                                      */
82 /*      To get both at once it will be necessary to hack this           */
83 /*      include file to define:                                         */
84 /*                                                                      */
85 /*        #define SHPAPI_CALL __declspec(dllexport) __stdcall           */
86 /*        #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall        */
87 /*                                                                      */
88 /*      The complexity of the situtation is partly caused by the        */
89 /*      peculiar requirement of Visual C++ that __stdcall appear        */
90 /*      after any "*"'s in the return value of a function while the     */
91 /*      __declspec(dllexport) must appear before them.                  */
92 /* -------------------------------------------------------------------- */
93 
94 #ifdef SHAPELIB_DLLEXPORT
95 #  define SHPAPI_CALL __declspec(dllexport)
96 #  define SHPAPI_CALL1(x)  __declspec(dllexport) x
97 #endif
98 
99 #ifndef SHPAPI_CALL
100 #  define SHPAPI_CALL
101 #endif
102 
103 #ifndef SHPAPI_CALL1
104 #  define SHPAPI_CALL1(x)      x SHPAPI_CALL
105 #endif
106 
107 /************************************************************************/
108 /*                             SHP Support.                             */
109 /************************************************************************/
110 typedef        struct
111 {
112     FILE        *fpSHP;
113     FILE        *fpSHX;
114 
115     int                nShapeType;                                /* SHPT_* */
116 
117     int                nFileSize;                                /* SHP file */
118 
119     int         nRecords;
120     int                nMaxRecords;
121     int                *panRecOffset;
122     int                *panRecSize;
123 
124     double        adBoundsMin[4];
125     double        adBoundsMax[4];
126 
127     int                bUpdated;
128 
129     unsigned char *pabyRec;
130     int         nBufSize;
131 } SHPInfo;
132 
133 typedef SHPInfo * SHPHandle;
134 
135 /* -------------------------------------------------------------------- */
136 /*      Shape types (nSHPType)                                          */
137 /* -------------------------------------------------------------------- */
138 #define SHPT_NULL        0
139 #define SHPT_POINT        1
140 #define SHPT_ARC        3
141 #define SHPT_POLYGON        5
142 #define SHPT_MULTIPOINT        8
143 #define SHPT_POINTZ        11
144 #define SHPT_ARCZ        13
145 #define SHPT_POLYGONZ        15
146 #define SHPT_MULTIPOINTZ 18
147 #define SHPT_POINTM        21
148 #define SHPT_ARCM        23
149 #define SHPT_POLYGONM        25
150 #define SHPT_MULTIPOINTM 28
151 #define SHPT_MULTIPATCH 31
152 
153 
154 /* -------------------------------------------------------------------- */
155 /*      Part types - everything but SHPT_MULTIPATCH just uses           */
156 /*      SHPP_RING.                                                      */
157 /* -------------------------------------------------------------------- */
158 
159 #define SHPP_TRISTRIP        0
160 #define SHPP_TRIFAN        1
161 #define SHPP_OUTERRING        2
162 #define SHPP_INNERRING        3
163 #define SHPP_FIRSTRING        4
164 #define SHPP_RING        5
165 
166 /* -------------------------------------------------------------------- */
167 /*      SHPObject - represents on shape (without attributes) read       */
168 /*      from the .shp file.                                             */
169 /* -------------------------------------------------------------------- */
170 typedef struct
171 {
172     int                nSHPType;
173 
174     int                nShapeId; /* -1 is unknown/unassigned */
175 
176     int                nParts;
177     int                *panPartStart;
178     int                *panPartType;
179 
180     int                nVertices;
181     double        *padfX;
182     double        *padfY;
183     double        *padfZ;
184     double        *padfM;
185 
186     double        dfXMin;
187     double        dfYMin;
188     double        dfZMin;
189     double        dfMMin;
190 
191     double        dfXMax;
192     double        dfYMax;
193     double        dfZMax;
194     double        dfMMax;
195 } SHPObject;
196 
197 /* -------------------------------------------------------------------- */
198 /*      SHP API Prototypes                                              */
199 /* -------------------------------------------------------------------- */
200 SHPHandle SHPAPI_CALL
201       SHPOpen( const char * pszShapeFile, const char * pszAccess );
202 SHPHandle SHPAPI_CALL
203       SHPCreate( const char * pszShapeFile, int nShapeType );
204 void SHPAPI_CALL
205       SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
206                   double * padfMinBound, double * padfMaxBound );
207 
208 SHPObject SHPAPI_CALL1(*)
209       SHPReadObject( SHPHandle hSHP, int iShape );
210 int SHPAPI_CALL
211       SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
212 
213 void SHPAPI_CALL
214       SHPDestroyObject( SHPObject * psObject );
215 void SHPAPI_CALL
216       SHPComputeExtents( SHPObject * psObject );
217 SHPObject SHPAPI_CALL1(*)
218       SHPCreateObject( int nSHPType, int nShapeId,
219                        int nParts, const int * panPartStart, const int * panPartType,
220                        int nVertices, const double * padfX, const double * padfY,
221                        const double * padfZ, const double * padfM );
222 SHPObject SHPAPI_CALL1(*)
223       SHPCreateSimpleObject( int nSHPType, int nVertices,
224                              double * padfX, double * padfY, double * padfZ );
225 
226 int SHPAPI_CALL
227       SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
228 
229 void SHPAPI_CALL
230       SHPClose( SHPHandle hSHP );
231 
232 const char SHPAPI_CALL1(*)
233       SHPTypeName( int nSHPType );
234 const char SHPAPI_CALL1(*)
235       SHPPartTypeName( int nPartType );
236 
237 /* -------------------------------------------------------------------- */
238 /*      Shape quadtree indexing API.                                    */
239 /* -------------------------------------------------------------------- */
240 
241 /* this can be two or four for binary or quad tree */
242 #define MAX_SUBNODE        4
243 
244 typedef struct shape_tree_node
245 {
246     /* region covered by this node */
247     double        adfBoundsMin[4];
248     double        adfBoundsMax[4];
249 
250     /* list of shapes stored at this node.  The papsShapeObj pointers
251        or the whole list can be NULL */
252     int                nShapeCount;
253     int                *panShapeIds;
254     SHPObject   **papsShapeObj;
255 
256     int                nSubNodes;
257     struct shape_tree_node *apsSubNode[MAX_SUBNODE];
258 
259 } SHPTreeNode;
260 
261 typedef struct
262 {
263     SHPHandle   hSHP;
264 
265     int                nMaxDepth;
266     int                nDimension;
267 
268     SHPTreeNode        *psRoot;
269 } SHPTree;
270 
271 SHPTree SHPAPI_CALL1(*)
272       SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
273                      double *padfBoundsMin, double *padfBoundsMax );
274 void    SHPAPI_CALL
275       SHPDestroyTree( SHPTree * hTree );
276 
277 int        SHPAPI_CALL
278       SHPWriteTree( SHPTree *hTree, const char * pszFilename );
279 SHPTree SHPAPI_CALL
280       SHPReadTree( const char * pszFilename );
281 
282 int        SHPAPI_CALL
283       SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
284 int        SHPAPI_CALL
285       SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
286 int        SHPAPI_CALL
287       SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
288 
289 void         SHPAPI_CALL
290       SHPTreeTrimExtraNodes( SHPTree * hTree );
291 
292 int    SHPAPI_CALL1(*)
293       SHPTreeFindLikelyShapes( SHPTree * hTree,
294                                double * padfBoundsMin,
295                                double * padfBoundsMax,
296                                int * );
297 int     SHPAPI_CALL
298       SHPCheckBoundsOverlap( const double *, const double *, const double *, const double *, int );
299 
300 /************************************************************************/
301 /*                             DBF Support.                             */
302 /************************************************************************/
303 typedef        struct
304 {
305     FILE        *fp;
306 
307     int         nRecords;
308 
309     int                nRecordLength;
310     int                nHeaderLength;
311     int                nFields;
312     int                *panFieldOffset;
313     int                *panFieldSize;
314     int                *panFieldDecimals;
315     char        *pachFieldType;
316 
317     char        *pszHeader;
318 
319     int                nCurrentRecord;
320     int                bCurrentRecordModified;
321     char        *pszCurrentRecord;
322 
323     int                bNoHeader;
324     int                bUpdated;
325 } DBFInfo;
326 
327 typedef DBFInfo * DBFHandle;
328 
329 typedef enum {
330   FTString,
331   FTInteger,
332   FTDouble,
333   FTLogical,
334   FTInvalid
335 } DBFFieldType;
336 
337 #define XBASE_FLDHDR_SZ       32
338 
339 DBFHandle SHPAPI_CALL
340       DBFOpen( const char * pszDBFFile, const char * pszAccess );
341 DBFHandle SHPAPI_CALL
342       DBFCreate( const char * pszDBFFile );
343 
344 int        SHPAPI_CALL
345       DBFGetFieldCount( DBFHandle psDBF );
346 int        SHPAPI_CALL
347       DBFGetRecordCount( DBFHandle psDBF );
348 int        SHPAPI_CALL
349       DBFAddField( DBFHandle hDBF, const char * pszFieldName,
350                    DBFFieldType eType, int nWidth, int nDecimals );
351 
352 DBFFieldType SHPAPI_CALL
353       DBFGetFieldInfo( DBFHandle psDBF, int iField,
354                        char * pszFieldName, int * pnWidth, int * pnDecimals );
355 
356 int SHPAPI_CALL
357       DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
358 
359 int         SHPAPI_CALL
360       DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
361 double         SHPAPI_CALL
362       DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
363 const char SHPAPI_CALL1(*)
364       DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
365 const char SHPAPI_CALL1(*)
366       DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
367 int     SHPAPI_CALL
368       DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
369 
370 int SHPAPI_CALL
371       DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
372                                 int nFieldValue );
373 int SHPAPI_CALL
374       DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
375                                double dFieldValue );
376 int SHPAPI_CALL
377       DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
378                                const char * pszFieldValue );
379 int SHPAPI_CALL
380      DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
381 
382 int SHPAPI_CALL
383      DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
384                                const char lFieldValue);
385 int SHPAPI_CALL
386      DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
387                                void * pValue );
388 const char SHPAPI_CALL1(*)
389       DBFReadTuple(DBFHandle psDBF, int hEntity );
390 int SHPAPI_CALL
391       DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
392 
393 DBFHandle SHPAPI_CALL
394       DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
395 
396 void        SHPAPI_CALL
397       DBFClose( DBFHandle hDBF );
398 char    SHPAPI_CALL
399       DBFGetNativeFieldType( DBFHandle hDBF, int iField );
400 
401 #ifdef __cplusplus
402 }
403 #endif
404 
405 #endif /* ndef _SHAPEFILE_H_INCLUDED */
406