shapefil.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. #ifndef _SHAPEFILE_H_INCLUDED
  2. #define _SHAPEFILE_H_INCLUDED
  3. /******************************************************************************
  4. * $Id$
  5. *
  6. * Project: Shapelib
  7. * Purpose: Primary include file for Shapelib.
  8. * Author: Frank Warmerdam, warmerdam@pobox.com
  9. *
  10. ******************************************************************************
  11. * Copyright (c) 1999, Frank Warmerdam
  12. *
  13. * This software is available under the following "MIT Style" license,
  14. * or at the option of the licensee under the LGPL (see LICENSE.LGPL). This
  15. * option is discussed in more detail in shapelib.html.
  16. *
  17. * --
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a
  20. * copy of this software and associated documentation files (the "Software"),
  21. * to deal in the Software without restriction, including without limitation
  22. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. * and/or sell copies of the Software, and to permit persons to whom the
  24. * Software is furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included
  27. * in all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  32. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. * DEALINGS IN THE SOFTWARE.
  36. ******************************************************************************
  37. *
  38. * $Log: shapefil.h,v $
  39. * Revision 1.40 2007/12/06 07:00:25 fwarmerdam
  40. * dbfopen now using SAHooks for fileio
  41. *
  42. * Revision 1.39 2007/12/04 20:37:56 fwarmerdam
  43. * preliminary implementation of hooks api for io and errors
  44. *
  45. * Revision 1.38 2007/11/21 22:39:56 fwarmerdam
  46. * close shx file in readonly mode (GDAL #1956)
  47. *
  48. * Revision 1.37 2007/10/27 03:31:14 fwarmerdam
  49. * limit default depth of tree to 12 levels (gdal ticket #1594)
  50. *
  51. * Revision 1.36 2007/09/10 23:33:15 fwarmerdam
  52. * Upstreamed support for visibility flag in SHPAPI_CALL for the needs
  53. * of GDAL (gdal ticket #1810).
  54. *
  55. * Revision 1.35 2007/09/03 19:48:10 fwarmerdam
  56. * move DBFReadAttribute() static dDoubleField into dbfinfo
  57. *
  58. * Revision 1.34 2006/06/17 15:33:32 fwarmerdam
  59. * added pszWorkField - bug 1202 (rso)
  60. *
  61. * Revision 1.33 2006/02/15 01:14:30 fwarmerdam
  62. * added DBFAddNativeFieldType
  63. *
  64. * Revision 1.32 2006/01/26 15:07:32 fwarmerdam
  65. * add bMeasureIsUsed flag from Craig Bruce: Bug 1249
  66. *
  67. * Revision 1.31 2006/01/05 01:27:27 fwarmerdam
  68. * added dbf deletion mark/fetch
  69. *
  70. * Revision 1.30 2005/01/03 22:30:13 fwarmerdam
  71. * added support for saved quadtrees
  72. *
  73. * Revision 1.29 2004/09/26 20:09:35 fwarmerdam
  74. * avoid rcsid warnings
  75. *
  76. * Revision 1.28 2003/12/29 06:02:18 fwarmerdam
  77. * added cpl_error.h option
  78. *
  79. * Revision 1.27 2003/04/21 18:30:37 warmerda
  80. * added header write/update public methods
  81. *
  82. * Revision 1.26 2002/09/29 00:00:08 warmerda
  83. * added FTLogical and logical attribute read/write calls
  84. *
  85. * Revision 1.25 2002/05/07 13:46:30 warmerda
  86. * added DBFWriteAttributeDirectly().
  87. *
  88. * Revision 1.24 2002/04/10 16:59:54 warmerda
  89. * added SHPRewindObject
  90. *
  91. * Revision 1.23 2002/01/15 14:36:07 warmerda
  92. * updated email address
  93. *
  94. * Revision 1.22 2002/01/15 14:32:00 warmerda
  95. * try to improve SHPAPI_CALL docs
  96. */
  97. #include <stdio.h>
  98. #ifdef USE_DBMALLOC
  99. #include <dbmalloc.h>
  100. #endif
  101. #ifdef USE_CPL
  102. #include "cpl_error.h"
  103. #include "cpl_vsi.h"
  104. #endif
  105. #ifdef __cplusplus
  106. extern "C" {
  107. #endif
  108. /************************************************************************/
  109. /* Configuration options. */
  110. /************************************************************************/
  111. /* -------------------------------------------------------------------- */
  112. /* Should the DBFReadStringAttribute() strip leading and */
  113. /* trailing white space? */
  114. /* -------------------------------------------------------------------- */
  115. #define TRIM_DBF_WHITESPACE
  116. /* -------------------------------------------------------------------- */
  117. /* Should we write measure values to the Multipatch object? */
  118. /* Reportedly ArcView crashes if we do write it, so for now it */
  119. /* is disabled. */
  120. /* -------------------------------------------------------------------- */
  121. #define DISABLE_MULTIPATCH_MEASURE
  122. /* -------------------------------------------------------------------- */
  123. /* SHPAPI_CALL */
  124. /* */
  125. /* The following two macros are present to allow forcing */
  126. /* various calling conventions on the Shapelib API. */
  127. /* */
  128. /* To force __stdcall conventions (needed to call Shapelib */
  129. /* from Visual Basic and/or Dephi I believe) the makefile could */
  130. /* be modified to define: */
  131. /* */
  132. /* /DSHPAPI_CALL=__stdcall */
  133. /* */
  134. /* If it is desired to force export of the Shapelib API without */
  135. /* using the shapelib.def file, use the following definition. */
  136. /* */
  137. /* /DSHAPELIB_DLLEXPORT */
  138. /* */
  139. /* To get both at once it will be necessary to hack this */
  140. /* include file to define: */
  141. /* */
  142. /* #define SHPAPI_CALL __declspec(dllexport) __stdcall */
  143. /* #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall */
  144. /* */
  145. /* The complexity of the situtation is partly caused by the */
  146. /* peculiar requirement of Visual C++ that __stdcall appear */
  147. /* after any "*"'s in the return value of a function while the */
  148. /* __declspec(dllexport) must appear before them. */
  149. /* -------------------------------------------------------------------- */
  150. #ifdef SHAPELIB_DLLEXPORT
  151. # define SHPAPI_CALL __declspec(dllexport)
  152. # define SHPAPI_CALL1(x) __declspec(dllexport) x
  153. #endif
  154. #ifndef SHPAPI_CALL
  155. # if defined(USE_GCC_VISIBILITY_FLAG)
  156. # define SHPAPI_CALL __attribute__ ((visibility("default")))
  157. # define SHPAPI_CALL1(x) __attribute__ ((visibility("default"))) x
  158. # else
  159. # define SHPAPI_CALL
  160. # endif
  161. #endif
  162. #ifndef SHPAPI_CALL1
  163. # define SHPAPI_CALL1(x) x SHPAPI_CALL
  164. #endif
  165. /* -------------------------------------------------------------------- */
  166. /* Macros for controlling CVSID and ensuring they don't appear */
  167. /* as unreferenced variables resulting in lots of warnings. */
  168. /* -------------------------------------------------------------------- */
  169. #ifndef DISABLE_CVSID
  170. # define SHP_CVSID(string) static char cpl_cvsid[] = string; \
  171. static char *cvsid_aw() { return( cvsid_aw() ? ((char *) NULL) : cpl_cvsid ); }
  172. #else
  173. # define SHP_CVSID(string)
  174. #endif
  175. /* -------------------------------------------------------------------- */
  176. /* IO/Error hook functions. */
  177. /* -------------------------------------------------------------------- */
  178. typedef int *SAFile;
  179. #ifndef SAOffset
  180. typedef unsigned long SAOffset;
  181. #endif
  182. typedef struct {
  183. SAFile (*FOpen) ( const char *filename, const char *path);
  184. SAOffset (*FRead) ( void *p, SAOffset size, SAOffset nmemb, SAFile file);
  185. SAOffset (*FWrite)( void *p, SAOffset size, SAOffset nmemb, SAFile file);
  186. SAOffset (*FSeek) ( SAFile file, SAOffset offset, int whence );
  187. SAOffset (*FTell) ( SAFile file );
  188. int (*FFlush)( SAFile file );
  189. int (*FClose)( SAFile file );
  190. void (*Error) ( const char *message );
  191. } SAHooks;
  192. void SHPAPI_CALL SASetupDefaultHooks( SAHooks *psHooks );
  193. /************************************************************************/
  194. /* SHP Support. */
  195. /************************************************************************/
  196. typedef struct
  197. {
  198. SAHooks sHooks;
  199. SAFile fpSHP;
  200. SAFile fpSHX;
  201. int nShapeType; /* SHPT_* */
  202. int nFileSize; /* SHP file */
  203. int nRecords;
  204. int nMaxRecords;
  205. int *panRecOffset;
  206. int *panRecSize;
  207. double adBoundsMin[4];
  208. double adBoundsMax[4];
  209. int bUpdated;
  210. unsigned char *pabyRec;
  211. int nBufSize;
  212. } SHPInfo;
  213. typedef SHPInfo * SHPHandle;
  214. /* -------------------------------------------------------------------- */
  215. /* Shape types (nSHPType) */
  216. /* -------------------------------------------------------------------- */
  217. #define SHPT_NULL 0
  218. #define SHPT_POINT 1
  219. #define SHPT_ARC 3
  220. #define SHPT_POLYGON 5
  221. #define SHPT_MULTIPOINT 8
  222. #define SHPT_POINTZ 11
  223. #define SHPT_ARCZ 13
  224. #define SHPT_POLYGONZ 15
  225. #define SHPT_MULTIPOINTZ 18
  226. #define SHPT_POINTM 21
  227. #define SHPT_ARCM 23
  228. #define SHPT_POLYGONM 25
  229. #define SHPT_MULTIPOINTM 28
  230. #define SHPT_MULTIPATCH 31
  231. /* -------------------------------------------------------------------- */
  232. /* Part types - everything but SHPT_MULTIPATCH just uses */
  233. /* SHPP_RING. */
  234. /* -------------------------------------------------------------------- */
  235. #define SHPP_TRISTRIP 0
  236. #define SHPP_TRIFAN 1
  237. #define SHPP_OUTERRING 2
  238. #define SHPP_INNERRING 3
  239. #define SHPP_FIRSTRING 4
  240. #define SHPP_RING 5
  241. /* -------------------------------------------------------------------- */
  242. /* SHPObject - represents on shape (without attributes) read */
  243. /* from the .shp file. */
  244. /* -------------------------------------------------------------------- */
  245. typedef struct
  246. {
  247. int nSHPType;
  248. int nShapeId; /* -1 is unknown/unassigned */
  249. int nParts;
  250. int *panPartStart;
  251. int *panPartType;
  252. int nVertices;
  253. double *padfX;
  254. double *padfY;
  255. double *padfZ;
  256. double *padfM;
  257. double dfXMin;
  258. double dfYMin;
  259. double dfZMin;
  260. double dfMMin;
  261. double dfXMax;
  262. double dfYMax;
  263. double dfZMax;
  264. double dfMMax;
  265. int bMeasureIsUsed;
  266. } SHPObject;
  267. /* -------------------------------------------------------------------- */
  268. /* SHP API Prototypes */
  269. /* -------------------------------------------------------------------- */
  270. /* If pszAccess is read-only, the fpSHX field of the returned structure */
  271. /* will be NULL as it is not necessary to keep the SHX file open */
  272. SHPHandle SHPAPI_CALL
  273. SHPOpen( const char * pszShapeFile, const char * pszAccess );
  274. SHPHandle SHPAPI_CALL
  275. SHPOpenLL( const char *pszShapeFile, const char *pszAccess,
  276. SAHooks *psHooks );
  277. SHPHandle SHPAPI_CALL
  278. SHPCreate( const char * pszShapeFile, int nShapeType );
  279. SHPHandle SHPAPI_CALL
  280. SHPCreateLL( const char * pszShapeFile, int nShapeType,
  281. SAHooks *psHooks );
  282. void SHPAPI_CALL
  283. SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
  284. double * padfMinBound, double * padfMaxBound );
  285. SHPObject SHPAPI_CALL1(*)
  286. SHPReadObject( SHPHandle hSHP, int iShape );
  287. int SHPAPI_CALL
  288. SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
  289. void SHPAPI_CALL
  290. SHPDestroyObject( SHPObject * psObject );
  291. void SHPAPI_CALL
  292. SHPComputeExtents( SHPObject * psObject );
  293. SHPObject SHPAPI_CALL1(*)
  294. SHPCreateObject( int nSHPType, int nShapeId, int nParts,
  295. const int * panPartStart, const int * panPartType,
  296. int nVertices,
  297. const double * padfX, const double * padfY,
  298. const double * padfZ, const double * padfM );
  299. SHPObject SHPAPI_CALL1(*)
  300. SHPCreateSimpleObject( int nSHPType, int nVertices,
  301. const double * padfX,
  302. const double * padfY,
  303. const double * padfZ );
  304. int SHPAPI_CALL
  305. SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
  306. void SHPAPI_CALL SHPClose( SHPHandle hSHP );
  307. void SHPAPI_CALL SHPWriteHeader( SHPHandle hSHP );
  308. const char SHPAPI_CALL1(*)
  309. SHPTypeName( int nSHPType );
  310. const char SHPAPI_CALL1(*)
  311. SHPPartTypeName( int nPartType );
  312. /* -------------------------------------------------------------------- */
  313. /* Shape quadtree indexing API. */
  314. /* -------------------------------------------------------------------- */
  315. /* this can be two or four for binary or quad tree */
  316. #define MAX_SUBNODE 4
  317. /* upper limit of tree levels for automatic estimation */
  318. #define MAX_DEFAULT_TREE_DEPTH 12
  319. typedef struct shape_tree_node
  320. {
  321. /* region covered by this node */
  322. double adfBoundsMin[4];
  323. double adfBoundsMax[4];
  324. /* list of shapes stored at this node. The papsShapeObj pointers
  325. or the whole list can be NULL */
  326. int nShapeCount;
  327. int *panShapeIds;
  328. SHPObject **papsShapeObj;
  329. int nSubNodes;
  330. struct shape_tree_node *apsSubNode[MAX_SUBNODE];
  331. } SHPTreeNode;
  332. typedef struct
  333. {
  334. SHPHandle hSHP;
  335. int nMaxDepth;
  336. int nDimension;
  337. int nTotalCount;
  338. SHPTreeNode *psRoot;
  339. } SHPTree;
  340. SHPTree SHPAPI_CALL1(*)
  341. SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
  342. double *padfBoundsMin, double *padfBoundsMax );
  343. void SHPAPI_CALL
  344. SHPDestroyTree( SHPTree * hTree );
  345. int SHPAPI_CALL
  346. SHPWriteTree( SHPTree *hTree, const char * pszFilename );
  347. SHPTree SHPAPI_CALL
  348. SHPReadTree( const char * pszFilename );
  349. int SHPAPI_CALL
  350. SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
  351. int SHPAPI_CALL
  352. SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
  353. int SHPAPI_CALL
  354. SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
  355. void SHPAPI_CALL
  356. SHPTreeTrimExtraNodes( SHPTree * hTree );
  357. int SHPAPI_CALL1(*)
  358. SHPTreeFindLikelyShapes( SHPTree * hTree,
  359. double * padfBoundsMin,
  360. double * padfBoundsMax,
  361. int * );
  362. int SHPAPI_CALL
  363. SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
  364. int SHPAPI_CALL1(*)
  365. SHPSearchDiskTree( FILE *fp,
  366. double *padfBoundsMin, double *padfBoundsMax,
  367. int *pnShapeCount );
  368. /************************************************************************/
  369. /* DBF Support. */
  370. /************************************************************************/
  371. typedef struct
  372. {
  373. SAHooks sHooks;
  374. SAFile fp;
  375. int nRecords;
  376. int nRecordLength;
  377. int nHeaderLength;
  378. int nFields;
  379. int *panFieldOffset;
  380. int *panFieldSize;
  381. int *panFieldDecimals;
  382. char *pachFieldType;
  383. char *pszHeader;
  384. int nCurrentRecord;
  385. int bCurrentRecordModified;
  386. char *pszCurrentRecord;
  387. int nWorkFieldLength;
  388. char *pszWorkField;
  389. int bNoHeader;
  390. int bUpdated;
  391. double dfDoubleField;
  392. } DBFInfo;
  393. typedef DBFInfo * DBFHandle;
  394. typedef enum {
  395. FTString,
  396. FTInteger,
  397. FTDouble,
  398. FTLogical,
  399. FTInvalid
  400. } DBFFieldType;
  401. #define XBASE_FLDHDR_SZ 32
  402. DBFHandle SHPAPI_CALL
  403. DBFOpen( const char * pszDBFFile, const char * pszAccess );
  404. DBFHandle SHPAPI_CALL
  405. DBFOpenLL( const char * pszDBFFile, const char * pszAccess,
  406. SAHooks *psHooks );
  407. DBFHandle SHPAPI_CALL
  408. DBFCreate( const char * pszDBFFile );
  409. DBFHandle SHPAPI_CALL
  410. DBFCreateLL( const char * pszDBFFile, SAHooks *psHooks );
  411. int SHPAPI_CALL
  412. DBFGetFieldCount( DBFHandle psDBF );
  413. int SHPAPI_CALL
  414. DBFGetRecordCount( DBFHandle psDBF );
  415. int SHPAPI_CALL
  416. DBFAddField( DBFHandle hDBF, const char * pszFieldName,
  417. DBFFieldType eType, int nWidth, int nDecimals );
  418. int SHPAPI_CALL
  419. DBFAddNativeFieldType( DBFHandle hDBF, const char * pszFieldName,
  420. char chType, int nWidth, int nDecimals );
  421. DBFFieldType SHPAPI_CALL
  422. DBFGetFieldInfo( DBFHandle psDBF, int iField,
  423. char * pszFieldName, int * pnWidth, int * pnDecimals );
  424. int SHPAPI_CALL
  425. DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
  426. int SHPAPI_CALL
  427. DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
  428. double SHPAPI_CALL
  429. DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
  430. const char SHPAPI_CALL1(*)
  431. DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
  432. const char SHPAPI_CALL1(*)
  433. DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
  434. int SHPAPI_CALL
  435. DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
  436. int SHPAPI_CALL
  437. DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
  438. int nFieldValue );
  439. int SHPAPI_CALL
  440. DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
  441. double dFieldValue );
  442. int SHPAPI_CALL
  443. DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
  444. const char * pszFieldValue );
  445. int SHPAPI_CALL
  446. DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
  447. int SHPAPI_CALL
  448. DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
  449. const char lFieldValue);
  450. int SHPAPI_CALL
  451. DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
  452. void * pValue );
  453. const char SHPAPI_CALL1(*)
  454. DBFReadTuple(DBFHandle psDBF, int hEntity );
  455. int SHPAPI_CALL
  456. DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
  457. int SHPAPI_CALL DBFIsRecordDeleted( DBFHandle psDBF, int iShape );
  458. int SHPAPI_CALL DBFMarkRecordDeleted( DBFHandle psDBF, int iShape,
  459. int bIsDeleted );
  460. DBFHandle SHPAPI_CALL
  461. DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
  462. void SHPAPI_CALL
  463. DBFClose( DBFHandle hDBF );
  464. void SHPAPI_CALL
  465. DBFUpdateHeader( DBFHandle hDBF );
  466. char SHPAPI_CALL
  467. DBFGetNativeFieldType( DBFHandle hDBF, int iField );
  468. #ifdef __cplusplus
  469. }
  470. #endif
  471. #endif /* ndef _SHAPEFILE_H_INCLUDED */