1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
|
#ifndef SHAPEFILE_H_INCLUDED
#define SHAPEFILE_H_INCLUDED
/******************************************************************************
* $Id$
*
* Project: Shapelib
* Purpose: Primary include file for Shapelib.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2012-2016, Even Rouault <even dot rouault at spatialys.com>
*
* This software is available under the following "MIT Style" license,
* or at the option of the licensee under the LGPL (see COPYING). This
* option is discussed in more detail in shapelib.html.
*
* --
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*
*/
#include <stdio.h>
#ifdef USE_CPL
#include "cpl_conv.h"
#endif
#ifdef __cplusplus
extern "C"
{
#endif
/************************************************************************/
/* Configuration options. */
/************************************************************************/
/* -------------------------------------------------------------------- */
/* Should the DBFReadStringAttribute() strip leading and */
/* trailing white space? */
/* -------------------------------------------------------------------- */
#define TRIM_DBF_WHITESPACE
/* -------------------------------------------------------------------- */
/* Should we write measure values to the Multipatch object? */
/* Reportedly ArcView crashes if we do write it, so for now it */
/* is disabled. */
/* -------------------------------------------------------------------- */
#define DISABLE_MULTIPATCH_MEASURE
/* -------------------------------------------------------------------- */
/* SHPAPI_CALL */
/* */
/* The following two macros are present to allow forcing */
/* various calling conventions on the Shapelib API. */
/* */
/* To force __stdcall conventions (needed to call Shapelib */
/* from Visual Basic and/or Dephi I believe) the makefile could */
/* be modified to define: */
/* */
/* /DSHPAPI_CALL=__stdcall */
/* */
/* If it is desired to force export of the Shapelib API without */
/* using the shapelib.def file, use the following definition. */
/* */
/* /DSHAPELIB_DLLEXPORT */
/* */
/* To get both at once it will be necessary to hack this */
/* include file to define: */
/* */
/* #define SHPAPI_CALL __declspec(dllexport) __stdcall */
/* #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall */
/* */
/* The complexity of the situation is partly caused by the */
/* peculiar requirement of Visual C++ that __stdcall appear */
/* after any "*"'s in the return value of a function while the */
/* __declspec(dllexport) must appear before them. */
/* -------------------------------------------------------------------- */
#ifdef SHAPELIB_DLLEXPORT
#define SHPAPI_CALL __declspec(dllexport)
#define SHPAPI_CALL1(x) __declspec(dllexport) x
#endif
#ifndef SHPAPI_CALL
#if defined(USE_GCC_VISIBILITY_FLAG)
#define SHPAPI_CALL __attribute__((visibility("default")))
#define SHPAPI_CALL1(x) __attribute__((visibility("default"))) x
#else
#define SHPAPI_CALL
#endif
#endif
#ifndef SHPAPI_CALL1
#define SHPAPI_CALL1(x) x SHPAPI_CALL
#endif
/* -------------------------------------------------------------------- */
/* Macros for controlling CVSID and ensuring they don't appear */
/* as unreferenced variables resulting in lots of warnings. */
/* -------------------------------------------------------------------- */
#ifndef DISABLE_CVSID
#if defined(__GNUC__) && __GNUC__ >= 4
#define SHP_CVSID(string) \
static const char cpl_cvsid[] __attribute__((used)) = string;
#else
#define SHP_CVSID(string) \
static const char cpl_cvsid[] = string; \
static const char *cvsid_aw() \
{ \
return (cvsid_aw() ? NULL : cpl_cvsid); \
}
#endif
#else
#define SHP_CVSID(string)
#endif
/* -------------------------------------------------------------------- */
/* On some platforms, additional file IO hooks are defined that */
/* UTF-8 encoded filenames Unicode filenames */
/* -------------------------------------------------------------------- */
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#define SHPAPI_WINDOWS
#define SHPAPI_UTF8_HOOKS
#endif
/* -------------------------------------------------------------------- */
/* IO/Error hook functions. */
/* -------------------------------------------------------------------- */
typedef int *SAFile;
#ifndef SAOffset
typedef unsigned long SAOffset;
#endif
typedef struct
{
SAFile (*FOpen)(const char *filename, const char *access);
SAOffset (*FRead)(void *p, SAOffset size, SAOffset nmemb, SAFile file);
SAOffset (*FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file);
SAOffset (*FSeek)(SAFile file, SAOffset offset, int whence);
SAOffset (*FTell)(SAFile file);
int (*FFlush)(SAFile file);
int (*FClose)(SAFile file);
int (*Remove)(const char *filename);
void (*Error)(const char *message);
double (*Atof)(const char *str);
} SAHooks;
void SHPAPI_CALL SASetupDefaultHooks(SAHooks *psHooks);
#ifdef SHPAPI_UTF8_HOOKS
void SHPAPI_CALL SASetupUtf8Hooks(SAHooks *psHooks);
#endif
/************************************************************************/
/* SHP Support. */
/************************************************************************/
typedef struct tagSHPObject SHPObject;
typedef struct
{
SAHooks sHooks;
SAFile fpSHP;
SAFile fpSHX;
int nShapeType; /* SHPT_* */
unsigned int nFileSize; /* SHP file */
int nRecords;
int nMaxRecords;
unsigned int *panRecOffset;
unsigned int *panRecSize;
double adBoundsMin[4];
double adBoundsMax[4];
int bUpdated;
unsigned char *pabyRec;
int nBufSize;
int bFastModeReadObject;
unsigned char *pabyObjectBuf;
int nObjectBufSize;
SHPObject *psCachedObject;
} SHPInfo;
typedef SHPInfo *SHPHandle;
/* -------------------------------------------------------------------- */
/* Shape types (nSHPType) */
/* -------------------------------------------------------------------- */
#define SHPT_NULL 0
#define SHPT_POINT 1
#define SHPT_ARC 3
#define SHPT_POLYGON 5
#define SHPT_MULTIPOINT 8
#define SHPT_POINTZ 11
#define SHPT_ARCZ 13
#define SHPT_POLYGONZ 15
#define SHPT_MULTIPOINTZ 18
#define SHPT_POINTM 21
#define SHPT_ARCM 23
#define SHPT_POLYGONM 25
#define SHPT_MULTIPOINTM 28
#define SHPT_MULTIPATCH 31
/* -------------------------------------------------------------------- */
/* Part types - everything but SHPT_MULTIPATCH just uses */
/* SHPP_RING. */
/* -------------------------------------------------------------------- */
#define SHPP_TRISTRIP 0
#define SHPP_TRIFAN 1
#define SHPP_OUTERRING 2
#define SHPP_INNERRING 3
#define SHPP_FIRSTRING 4
#define SHPP_RING 5
/* -------------------------------------------------------------------- */
/* SHPObject - represents on shape (without attributes) read */
/* from the .shp file. */
/* -------------------------------------------------------------------- */
struct tagSHPObject
{
int nSHPType;
int nShapeId; /* -1 is unknown/unassigned */
int nParts;
int *panPartStart;
int *panPartType;
int nVertices;
double *padfX;
double *padfY;
double *padfZ;
double *padfM;
double dfXMin;
double dfYMin;
double dfZMin;
double dfMMin;
double dfXMax;
double dfYMax;
double dfZMax;
double dfMMax;
int bMeasureIsUsed;
int bFastModeReadObject;
};
/* -------------------------------------------------------------------- */
/* SHP API Prototypes */
/* -------------------------------------------------------------------- */
/* If pszAccess is read-only, the fpSHX field of the returned structure */
/* will be NULL as it is not necessary to keep the SHX file open */
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile,
const char *pszAccess);
SHPHandle SHPAPI_CALL SHPOpenLL(const char *pszShapeFile,
const char *pszAccess, SAHooks *psHooks);
SHPHandle SHPAPI_CALL SHPOpenLLEx(const char *pszShapeFile,
const char *pszAccess, SAHooks *psHooks,
int bRestoreSHX);
int SHPAPI_CALL SHPRestoreSHX(const char *pszShapeFile,
const char *pszAccess, SAHooks *psHooks);
/* If setting bFastMode = TRUE, the content of SHPReadObject() is owned by
* the SHPHandle. */
/* So you cannot have 2 valid instances of SHPReadObject() simultaneously.
*/
/* The SHPObject padfZ and padfM members may be NULL depending on the
* geometry */
/* type. It is illegal to free at hand any of the pointer members of the
* SHPObject structure */
void SHPAPI_CALL SHPSetFastModeReadObject(SHPHandle hSHP, int bFastMode);
SHPHandle SHPAPI_CALL SHPCreate(const char *pszShapeFile, int nShapeType);
SHPHandle SHPAPI_CALL SHPCreateLL(const char *pszShapeFile, int nShapeType,
SAHooks *psHooks);
void SHPAPI_CALL SHPGetInfo(SHPHandle hSHP, int *pnEntities,
int *pnShapeType, double *padfMinBound,
double *padfMaxBound);
SHPObject SHPAPI_CALL1(*) SHPReadObject(SHPHandle hSHP, int iShape);
int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape,
SHPObject *psObject);
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject);
void SHPAPI_CALL SHPComputeExtents(SHPObject *psObject);
SHPObject SHPAPI_CALL1(*)
SHPCreateObject(int nSHPType, int nShapeId, int nParts,
const int *panPartStart, const int *panPartType,
int nVertices, const double *padfX, const double *padfY,
const double *padfZ, const double *padfM);
SHPObject SHPAPI_CALL1(*)
SHPCreateSimpleObject(int nSHPType, int nVertices, const double *padfX,
const double *padfY, const double *padfZ);
int SHPAPI_CALL SHPRewindObject(SHPHandle hSHP, SHPObject *psObject);
void SHPAPI_CALL SHPClose(SHPHandle hSHP);
void SHPAPI_CALL SHPWriteHeader(SHPHandle hSHP);
const char SHPAPI_CALL1(*) SHPTypeName(int nSHPType);
const char SHPAPI_CALL1(*) SHPPartTypeName(int nPartType);
/* -------------------------------------------------------------------- */
/* Shape quadtree indexing API. */
/* -------------------------------------------------------------------- */
/* this can be two or four for binary or quad tree */
#define MAX_SUBNODE 4
/* upper limit of tree levels for automatic estimation */
#define MAX_DEFAULT_TREE_DEPTH 12
typedef struct shape_tree_node
{
/* region covered by this node */
double adfBoundsMin[4];
double adfBoundsMax[4];
/* list of shapes stored at this node. The papsShapeObj pointers
or the whole list can be NULL */
int nShapeCount;
int *panShapeIds;
SHPObject **papsShapeObj;
int nSubNodes;
struct shape_tree_node *apsSubNode[MAX_SUBNODE];
} SHPTreeNode;
typedef struct
{
SHPHandle hSHP;
int nMaxDepth;
int nDimension;
int nTotalCount;
SHPTreeNode *psRoot;
} SHPTree;
SHPTree SHPAPI_CALL1(*)
SHPCreateTree(SHPHandle hSHP, int nDimension, int nMaxDepth,
double *padfBoundsMin, double *padfBoundsMax);
void SHPAPI_CALL SHPDestroyTree(SHPTree *hTree);
int SHPAPI_CALL SHPWriteTree(SHPTree *hTree, const char *pszFilename);
int SHPAPI_CALL SHPTreeAddShapeId(SHPTree *hTree, SHPObject *psObject);
int SHPAPI_CALL SHPTreeRemoveShapeId(SHPTree *hTree, int nShapeId);
void SHPAPI_CALL SHPTreeTrimExtraNodes(SHPTree *hTree);
int SHPAPI_CALL1(*)
SHPTreeFindLikelyShapes(SHPTree *hTree, double *padfBoundsMin,
double *padfBoundsMax, int *);
int SHPAPI_CALL SHPCheckBoundsOverlap(double *, double *, double *,
double *, int);
int SHPAPI_CALL1(*)
SHPSearchDiskTree(FILE *fp, double *padfBoundsMin,
double *padfBoundsMax, int *pnShapeCount);
typedef struct SHPDiskTreeInfo *SHPTreeDiskHandle;
SHPTreeDiskHandle SHPAPI_CALL SHPOpenDiskTree(const char *pszQIXFilename,
SAHooks *psHooks);
void SHPAPI_CALL SHPCloseDiskTree(SHPTreeDiskHandle hDiskTree);
int SHPAPI_CALL1(*)
SHPSearchDiskTreeEx(SHPTreeDiskHandle hDiskTree, double *padfBoundsMin,
double *padfBoundsMax, int *pnShapeCount);
int SHPAPI_CALL SHPWriteTreeLL(SHPTree *hTree, const char *pszFilename,
SAHooks *psHooks);
/* -------------------------------------------------------------------- */
/* SBN Search API */
/* -------------------------------------------------------------------- */
typedef struct SBNSearchInfo *SBNSearchHandle;
SBNSearchHandle SHPAPI_CALL SBNOpenDiskTree(const char *pszSBNFilename,
SAHooks *psHooks);
void SHPAPI_CALL SBNCloseDiskTree(SBNSearchHandle hSBN);
int SHPAPI_CALL1(*)
SBNSearchDiskTree(SBNSearchHandle hSBN, double *padfBoundsMin,
double *padfBoundsMax, int *pnShapeCount);
int SHPAPI_CALL1(*)
SBNSearchDiskTreeInteger(SBNSearchHandle hSBN, int bMinX, int bMinY,
int bMaxX, int bMaxY, int *pnShapeCount);
void SHPAPI_CALL SBNSearchFreeIds(int *panShapeId);
/************************************************************************/
/* DBF Support. */
/************************************************************************/
typedef struct
{
SAHooks sHooks;
SAFile fp;
int nRecords;
int nRecordLength; /* Must fit on uint16 */
int nHeaderLength; /* File header length (32) + field
descriptor length + spare space.
Must fit on uint16 */
int nFields;
int *panFieldOffset;
int *panFieldSize;
int *panFieldDecimals;
char *pachFieldType;
char *pszHeader; /* Field descriptors */
int nCurrentRecord;
int bCurrentRecordModified;
char *pszCurrentRecord;
int nWorkFieldLength;
char *pszWorkField;
int bNoHeader;
int bUpdated;
union
{
double dfDoubleField;
int nIntField;
} fieldValue;
int iLanguageDriver;
char *pszCodePage;
int nUpdateYearSince1900; /* 0-255 */
int nUpdateMonth; /* 1-12 */
int nUpdateDay; /* 1-31 */
int bWriteEndOfFileChar; /* defaults to TRUE */
int bRequireNextWriteSeek;
} DBFInfo;
typedef DBFInfo *DBFHandle;
typedef enum
{
FTString,
FTInteger,
FTDouble,
FTLogical,
FTDate,
FTInvalid
} DBFFieldType;
/* Field descriptor/header size */
#define XBASE_FLDHDR_SZ 32
/* Shapelib read up to 11 characters, even if only 10 should normally be used */
#define XBASE_FLDNAME_LEN_READ 11
/* On writing, we limit to 10 characters */
#define XBASE_FLDNAME_LEN_WRITE 10
/* Normally only 254 characters should be used. We tolerate 255 historically */
#define XBASE_FLD_MAX_WIDTH 255
DBFHandle SHPAPI_CALL DBFOpen(const char *pszDBFFile,
const char *pszAccess);
DBFHandle SHPAPI_CALL DBFOpenLL(const char *pszDBFFile,
const char *pszAccess, SAHooks *psHooks);
DBFHandle SHPAPI_CALL DBFCreate(const char *pszDBFFile);
DBFHandle SHPAPI_CALL DBFCreateEx(const char *pszDBFFile,
const char *pszCodePage);
DBFHandle SHPAPI_CALL DBFCreateLL(const char *pszDBFFile,
const char *pszCodePage,
SAHooks *psHooks);
int SHPAPI_CALL DBFGetFieldCount(DBFHandle psDBF);
int SHPAPI_CALL DBFGetRecordCount(DBFHandle psDBF);
int SHPAPI_CALL DBFAddField(DBFHandle hDBF, const char *pszFieldName,
DBFFieldType eType, int nWidth, int nDecimals);
int SHPAPI_CALL DBFAddNativeFieldType(DBFHandle hDBF,
const char *pszFieldName, char chType,
int nWidth, int nDecimals);
int SHPAPI_CALL DBFDeleteField(DBFHandle hDBF, int iField);
int SHPAPI_CALL DBFReorderFields(DBFHandle psDBF, int *panMap);
int SHPAPI_CALL DBFAlterFieldDefn(DBFHandle psDBF, int iField,
const char *pszFieldName, char chType,
int nWidth, int nDecimals);
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField,
char *pszFieldName, int *pnWidth,
int *pnDecimals);
int SHPAPI_CALL DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
int SHPAPI_CALL DBFReadIntegerAttribute(DBFHandle hDBF, int iShape,
int iField);
double SHPAPI_CALL DBFReadDoubleAttribute(DBFHandle hDBF, int iShape,
int iField);
const char SHPAPI_CALL1(*)
DBFReadStringAttribute(DBFHandle hDBF, int iShape, int iField);
const char SHPAPI_CALL1(*)
DBFReadLogicalAttribute(DBFHandle hDBF, int iShape, int iField);
int SHPAPI_CALL DBFIsAttributeNULL(DBFHandle hDBF, int iShape, int iField);
int SHPAPI_CALL DBFWriteIntegerAttribute(DBFHandle hDBF, int iShape,
int iField, int nFieldValue);
int SHPAPI_CALL DBFWriteDoubleAttribute(DBFHandle hDBF, int iShape,
int iField, double dFieldValue);
int SHPAPI_CALL DBFWriteStringAttribute(DBFHandle hDBF, int iShape,
int iField,
const char *pszFieldValue);
int SHPAPI_CALL DBFWriteNULLAttribute(DBFHandle hDBF, int iShape,
int iField);
int SHPAPI_CALL DBFWriteLogicalAttribute(DBFHandle hDBF, int iShape,
int iField,
const char lFieldValue);
int SHPAPI_CALL DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity,
int iField, void *pValue);
const char SHPAPI_CALL1(*) DBFReadTuple(DBFHandle psDBF, int hEntity);
int SHPAPI_CALL DBFWriteTuple(DBFHandle psDBF, int hEntity,
void *pRawTuple);
int SHPAPI_CALL DBFIsRecordDeleted(DBFHandle psDBF, int iShape);
int SHPAPI_CALL DBFMarkRecordDeleted(DBFHandle psDBF, int iShape,
int bIsDeleted);
DBFHandle SHPAPI_CALL DBFCloneEmpty(DBFHandle psDBF,
const char *pszFilename);
void SHPAPI_CALL DBFClose(DBFHandle hDBF);
void SHPAPI_CALL DBFUpdateHeader(DBFHandle hDBF);
char SHPAPI_CALL DBFGetNativeFieldType(DBFHandle hDBF, int iField);
const char SHPAPI_CALL1(*) DBFGetCodePage(DBFHandle psDBF);
void SHPAPI_CALL DBFSetLastModifiedDate(DBFHandle psDBF, int nYYSince1900,
int nMM, int nDD);
void SHPAPI_CALL DBFSetWriteEndOfFileChar(DBFHandle psDBF, int bWriteFlag);
#ifdef __cplusplus
}
#endif
#endif /* ndef SHAPEFILE_H_INCLUDED */
|