shapefil.h

Go to the documentation of this file.
00001 #ifndef _SHAPEFILE_H_INCLUDED
00002 #define _SHAPEFILE_H_INCLUDED
00003 
00004 /******************************************************************************
00005  * $Id: shapefil.h 9954 2011-10-07 19:49:02Z mario $
00006  *
00007  * Project:  Shapelib
00008  * Purpose:  Primary include file for Shapelib.
00009  * Author:   Frank Warmerdam, warmerdam@pobox.com
00010  *
00011  ******************************************************************************
00012  * Copyright (c) 1999, Frank Warmerdam
00013  *
00014  * This software is available under the following "MIT Style" license,
00015  * or at the option of the licensee under the LGPL (see LICENSE.LGPL).  This
00016  * option is discussed in more detail in shapelib.html.
00017  *
00018  * --
00019  * 
00020  * Permission is hereby granted, free of charge, to any person obtaining a
00021  * copy of this software and associated documentation files (the "Software"),
00022  * to deal in the Software without restriction, including without limitation
00023  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00024  * and/or sell copies of the Software, and to permit persons to whom the
00025  * Software is furnished to do so, subject to the following conditions:
00026  *
00027  * The above copyright notice and this permission notice shall be included
00028  * in all copies or substantial portions of the Software.
00029  *
00030  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00031  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00032  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00033  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00034  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00035  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00036  * DEALINGS IN THE SOFTWARE.
00037  ******************************************************************************
00038  *
00039  * $Log$
00040  * Revision 1.2  2004/02/20 14:10:42  lubia
00041  * Migrando para ultima versao da shapelib: 1.2.10
00042  *
00043  * Revision 1.26  2002/09/29 00:00:08  warmerda
00044  * added FTLogical and logical attribute read/write calls
00045  *
00046  * Revision 1.25  2002/05/07 13:46:30  warmerda
00047  * added DBFWriteAttributeDirectly().
00048  *
00049  * Revision 1.24  2002/04/10 16:59:54  warmerda
00050  * added SHPRewindObject
00051  *
00052  * Revision 1.23  2002/01/15 14:36:07  warmerda
00053  * updated email address
00054  *
00055  * Revision 1.22  2002/01/15 14:32:00  warmerda
00056  * try to improve SHPAPI_CALL docs
00057  *
00058  * Revision 1.21  2001/11/01 16:29:55  warmerda
00059  * move pabyRec into SHPInfo for thread safety
00060  *
00061  * Revision 1.20  2001/07/20 13:06:02  warmerda
00062  * fixed SHPAPI attribute for SHPTreeFindLikelyShapes
00063  *
00064  * Revision 1.19  2001/05/31 19:20:13  warmerda
00065  * added DBFGetFieldIndex()
00066  *
00067  * Revision 1.18  2001/05/31 18:15:40  warmerda
00068  * Added support for NULL fields in DBF files
00069  *
00070  * Revision 1.17  2001/05/23 13:36:52  warmerda
00071  * added use of SHPAPI_CALL
00072  *
00073  * Revision 1.16  2000/09/25 14:15:59  warmerda
00074  * added DBFGetNativeFieldType()
00075  *
00076  * Revision 1.15  2000/02/16 16:03:51  warmerda
00077  * added null shape support
00078  *
00079  * Revision 1.14  1999/11/05 14:12:05  warmerda
00080  * updated license terms
00081  *
00082  * Revision 1.13  1999/06/02 18:24:21  warmerda
00083  * added trimming code
00084  *
00085  * Revision 1.12  1999/06/02 17:56:12  warmerda
00086  * added quad'' subnode support for trees
00087  *
00088  * Revision 1.11  1999/05/18 19:11:11  warmerda
00089  * Added example searching capability
00090  *
00091  * Revision 1.10  1999/05/18 17:49:38  warmerda
00092  * added initial quadtree support
00093  *
00094  * Revision 1.9  1999/05/11 03:19:28  warmerda
00095  * added new Tuple api, and improved extension handling - add from candrsn
00096  *
00097  * Revision 1.8  1999/03/23 17:22:27  warmerda
00098  * Added extern "C" protection for C++ users of shapefil.h.
00099  *
00100  * Revision 1.7  1998/12/31 15:31:07  warmerda
00101  * Added the TRIM_DBF_WHITESPACE and DISABLE_MULTIPATCH_MEASURE options.
00102  *
00103  * Revision 1.6  1998/12/03 15:48:15  warmerda
00104  * Added SHPCalculateExtents().
00105  *
00106  * Revision 1.5  1998/11/09 20:57:16  warmerda
00107  * Altered SHPGetInfo() call.
00108  *
00109  * Revision 1.4  1998/11/09 20:19:33  warmerda
00110  * Added 3D support, and use of SHPObject.
00111  *
00112  * Revision 1.3  1995/08/23 02:24:05  warmerda
00113  * Added support for reading bounds.
00114  *
00115  * Revision 1.2  1995/08/04  03:17:39  warmerda
00116  * Added header.
00117  *
00118  */
00119 
00120 #include <stdio.h>
00121 
00122 #ifdef USE_DBMALLOC
00123 #include <dbmalloc.h>
00124 #endif
00125 
00126 #ifdef __cplusplus
00127 extern "C" {
00128 #endif
00129 
00130 /************************************************************************/
00131 /*                        Configuration options.                        */
00132 /************************************************************************/
00133 
00134 /* -------------------------------------------------------------------- */
00135 /*      Should the DBFReadStringAttribute() strip leading and           */
00136 /*      trailing white space?                                           */
00137 /* -------------------------------------------------------------------- */
00138 #define TRIM_DBF_WHITESPACE
00139 
00140 /* -------------------------------------------------------------------- */
00141 /*      Should we write measure values to the Multipatch object?        */
00142 /*      Reportedly ArcView crashes if we do write it, so for now it     */
00143 /*      is disabled.                                                    */
00144 /* -------------------------------------------------------------------- */
00145 #define DISABLE_MULTIPATCH_MEASURE
00146 
00147 /* -------------------------------------------------------------------- */
00148 /*      SHPAPI_CALL                                                     */
00149 /*                                                                      */
00150 /*      The following two macros are present to allow forcing           */
00151 /*      various calling conventions on the Shapelib API.                */
00152 /*                                                                      */
00153 /*      To force __stdcall conventions (needed to call Shapelib         */
00154 /*      from Visual Basic and/or Dephi I believe) the makefile could    */
00155 /*      be modified to define:                                          */
00156 /*                                                                      */
00157 /*        /DSHPAPI_CALL=__stdcall                                       */
00158 /*                                                                      */
00159 /*      If it is desired to force export of the Shapelib API without    */
00160 /*      using the shapelib.def file, use the following definition.      */
00161 /*                                                                      */
00162 /*        /DSHAPELIB_DLLEXPORT                                          */
00163 /*                                                                      */
00164 /*      To get both at once it will be necessary to hack this           */
00165 /*      include file to define:                                         */
00166 /*                                                                      */
00167 /*        #define SHPAPI_CALL __declspec(dllexport) __stdcall           */
00168 /*        #define SHPAPI_CALL1 __declspec(dllexport) * __stdcall        */
00169 /*                                                                      */
00170 /*      The complexity of the situtation is partly caused by the        */
00171 /*      peculiar requirement of Visual C++ that __stdcall appear        */
00172 /*      after any "*"'s in the return value of a function while the     */
00173 /*      __declspec(dllexport) must appear before them.                  */
00174 /* -------------------------------------------------------------------- */
00175 
00176 #if defined( WIN32 ) || defined( __WIN32__ ) || defined ( _WIN32 ) || defined( WIN64 ) || defined( _WIN32_WCE )
00177 
00178 #ifdef SHAPELIB_DLLEXPORT
00179 #  define SHPAPI_CALL __declspec(dllexport)
00180 #  define SHPAPI_CALL1(x)  __declspec(dllexport) x
00181 #endif
00182 
00183 #endif
00184 
00185 #ifndef SHPAPI_CALL
00186 #  define SHPAPI_CALL
00187 #endif
00188 
00189 #ifndef SHPAPI_CALL1
00190 #  define SHPAPI_CALL1(x)      x SHPAPI_CALL
00191 #endif
00192     
00193 /************************************************************************/
00194 /*                             SHP Support.                             */
00195 /************************************************************************/
00196 typedef struct
00197 {
00198     FILE        *fpSHP;
00199     FILE        *fpSHX;
00200 
00201     int         nShapeType;                             /* SHPT_* */
00202     
00203     int         nFileSize;                              /* SHP file */
00204 
00205     int         nRecords;
00206     int         nMaxRecords;
00207     int         *panRecOffset;
00208     int         *panRecSize;
00209 
00210     double      adBoundsMin[4];
00211     double      adBoundsMax[4];
00212 
00213     int         bUpdated;
00214 
00215     unsigned char *pabyRec;
00216     int         nBufSize;
00217 } SHPInfo;
00218 
00219 typedef SHPInfo * SHPHandle;
00220 
00221 /* -------------------------------------------------------------------- */
00222 /*      Shape types (nSHPType)                                          */
00223 /* -------------------------------------------------------------------- */
00224 #define SHPT_NULL       0
00225 #define SHPT_POINT      1
00226 #define SHPT_ARC        3
00227 #define SHPT_POLYGON    5
00228 #define SHPT_MULTIPOINT 8
00229 #define SHPT_POINTZ     11
00230 #define SHPT_ARCZ       13
00231 #define SHPT_POLYGONZ   15
00232 #define SHPT_MULTIPOINTZ 18
00233 #define SHPT_POINTM     21
00234 #define SHPT_ARCM       23
00235 #define SHPT_POLYGONM   25
00236 #define SHPT_MULTIPOINTM 28
00237 #define SHPT_MULTIPATCH 31
00238 
00239 
00240 /* -------------------------------------------------------------------- */
00241 /*      Part types - everything but SHPT_MULTIPATCH just uses           */
00242 /*      SHPP_RING.                                                      */
00243 /* -------------------------------------------------------------------- */
00244 
00245 #define SHPP_TRISTRIP   0
00246 #define SHPP_TRIFAN     1
00247 #define SHPP_OUTERRING  2
00248 #define SHPP_INNERRING  3
00249 #define SHPP_FIRSTRING  4
00250 #define SHPP_RING       5
00251 
00252 /* -------------------------------------------------------------------- */
00253 /*      SHPObject - represents on shape (without attributes) read       */
00254 /*      from the .shp file.                                             */
00255 /* -------------------------------------------------------------------- */
00256 typedef struct
00257 {
00258     int         nSHPType;
00259 
00260     int         nShapeId; /* -1 is unknown/unassigned */
00261 
00262     int         nParts;
00263     int         *panPartStart;
00264     int         *panPartType;
00265     
00266     int         nVertices;
00267     double      *padfX;
00268     double      *padfY;
00269     double      *padfZ;
00270     double      *padfM;
00271 
00272     double      dfXMin;
00273     double      dfYMin;
00274     double      dfZMin;
00275     double      dfMMin;
00276 
00277     double      dfXMax;
00278     double      dfYMax;
00279     double      dfZMax;
00280     double      dfMMax;
00281 } SHPObject;
00282 
00283 /* -------------------------------------------------------------------- */
00284 /*      SHP API Prototypes                                              */
00285 /* -------------------------------------------------------------------- */
00286 SHPHandle SHPAPI_CALL
00287       SHPOpen( const char * pszShapeFile, const char * pszAccess );
00288 SHPHandle SHPAPI_CALL
00289       SHPCreate( const char * pszShapeFile, int nShapeType );
00290 void SHPAPI_CALL
00291       SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType,
00292                   double * padfMinBound, double * padfMaxBound );
00293 
00294 SHPObject SHPAPI_CALL1(*)
00295       SHPReadObject( SHPHandle hSHP, int iShape );
00296 int SHPAPI_CALL
00297       SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
00298 
00299 void SHPAPI_CALL
00300       SHPDestroyObject( SHPObject * psObject );
00301 void SHPAPI_CALL
00302       SHPComputeExtents( SHPObject * psObject );
00303 SHPObject SHPAPI_CALL1(*)
00304       SHPCreateObject( int nSHPType, int nShapeId,
00305                        int nParts, int * panPartStart, int * panPartType,
00306                        int nVertices, double * padfX, double * padfY,
00307                        double * padfZ, double * padfM );
00308 SHPObject SHPAPI_CALL1(*)
00309       SHPCreateSimpleObject( int nSHPType, int nVertices,
00310                              double * padfX, double * padfY, double * padfZ );
00311 
00312 int SHPAPI_CALL
00313       SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
00314 
00315 void SHPAPI_CALL
00316       SHPClose( SHPHandle hSHP );
00317 
00318 const char SHPAPI_CALL1(*)
00319       SHPTypeName( int nSHPType );
00320 const char SHPAPI_CALL1(*)
00321       SHPPartTypeName( int nPartType );
00322 
00323 /* -------------------------------------------------------------------- */
00324 /*      Shape quadtree indexing API.                                    */
00325 /* -------------------------------------------------------------------- */
00326 
00327 /* this can be two or four for binary or quad tree */
00328 #define MAX_SUBNODE     4
00329 
00330 typedef struct shape_tree_node
00331 {
00332     /* region covered by this node */
00333     double      adfBoundsMin[4];
00334     double      adfBoundsMax[4];
00335 
00336     /* list of shapes stored at this node.  The papsShapeObj pointers
00337        or the whole list can be NULL */
00338     int         nShapeCount;
00339     int         *panShapeIds;
00340     SHPObject   **papsShapeObj;
00341 
00342     int         nSubNodes;
00343     struct shape_tree_node *apsSubNode[MAX_SUBNODE];
00344     
00345 } SHPTreeNode;
00346 
00347 typedef struct
00348 {
00349     SHPHandle   hSHP;
00350     
00351     int         nMaxDepth;
00352     int         nDimension;
00353     
00354     SHPTreeNode *psRoot;
00355 } SHPTree;
00356 
00357 SHPTree SHPAPI_CALL1(*)
00358       SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
00359                      double *padfBoundsMin, double *padfBoundsMax );
00360 void    SHPAPI_CALL
00361       SHPDestroyTree( SHPTree * hTree );
00362 
00363 int     SHPAPI_CALL
00364       SHPWriteTree( SHPTree *hTree, const char * pszFilename );
00365 SHPTree SHPAPI_CALL
00366       SHPReadTree( const char * pszFilename );
00367 
00368 int     SHPAPI_CALL
00369       SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
00370 int     SHPAPI_CALL
00371       SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
00372 int     SHPAPI_CALL
00373       SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
00374 
00375 void    SHPAPI_CALL
00376       SHPTreeTrimExtraNodes( SHPTree * hTree );
00377 
00378 int    SHPAPI_CALL1(*)
00379       SHPTreeFindLikelyShapes( SHPTree * hTree,
00380                                double * padfBoundsMin,
00381                                double * padfBoundsMax,
00382                                int * );
00383 int     SHPAPI_CALL
00384       SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
00385 
00386 /************************************************************************/
00387 /*                             DBF Support.                             */
00388 /************************************************************************/
00389 typedef struct
00390 {
00391     FILE        *fp;
00392 
00393     int         nRecords;
00394 
00395     int         nRecordLength;
00396     int         nHeaderLength;
00397     int         nFields;
00398     int         *panFieldOffset;
00399     int         *panFieldSize;
00400     int         *panFieldDecimals;
00401     char        *pachFieldType;
00402 
00403     char        *pszHeader;
00404 
00405     int         nCurrentRecord;
00406     int         bCurrentRecordModified;
00407     char        *pszCurrentRecord;
00408     
00409     int         bNoHeader;
00410     int         bUpdated;
00411 } DBFInfo;
00412 
00413 typedef DBFInfo * DBFHandle;
00414 
00415 typedef enum {
00416   FTString,
00417   FTInteger,
00418   FTDouble,
00419   FTLogical,
00420   FTInvalid,
00421   FTDate,
00422 } DBFFieldType;
00423 
00424 #define XBASE_FLDHDR_SZ       32
00425 
00426 DBFHandle SHPAPI_CALL
00427       DBFOpen( const char * pszDBFFile, const char * pszAccess );
00428 DBFHandle SHPAPI_CALL
00429       DBFCreate( const char * pszDBFFile );
00430 
00431 int     SHPAPI_CALL
00432       DBFGetFieldCount( DBFHandle psDBF );
00433 int     SHPAPI_CALL
00434       DBFGetRecordCount( DBFHandle psDBF );
00435 int     SHPAPI_CALL
00436       DBFAddField( DBFHandle hDBF, const char * pszFieldName,
00437                    DBFFieldType eType, int nWidth, int nDecimals );
00438 
00439 DBFFieldType SHPAPI_CALL
00440       DBFGetFieldInfo( DBFHandle psDBF, int iField, 
00441                        char * pszFieldName, int * pnWidth, int * pnDecimals );
00442 
00443 int SHPAPI_CALL
00444       DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
00445 
00446 int     SHPAPI_CALL
00447       DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
00448 double  SHPAPI_CALL
00449       DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
00450 const char SHPAPI_CALL1(*)
00451       DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
00452 const char SHPAPI_CALL1(*)
00453       DBFReadDatettribute( DBFHandle hDBF, int iShape, int iField );
00454 const char SHPAPI_CALL1(*)
00455       DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
00456 int     SHPAPI_CALL
00457       DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
00458 
00459 int SHPAPI_CALL
00460       DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField, 
00461                                 int nFieldValue );
00462 int SHPAPI_CALL
00463       DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
00464                                double dFieldValue );
00465 int SHPAPI_CALL
00466       DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
00467                                const char * pszFieldValue );
00468 int SHPAPI_CALL
00469       DBFWriteDateAttribute( DBFHandle hDBF, int iShape, int iField,
00470                                const char * pszFieldValue );
00471 int SHPAPI_CALL
00472      DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
00473 
00474 int SHPAPI_CALL
00475      DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
00476                                const char lFieldValue);
00477 int SHPAPI_CALL
00478      DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
00479                                void * pValue );
00480 const char SHPAPI_CALL1(*)
00481       DBFReadTuple(DBFHandle psDBF, int hEntity );
00482 int SHPAPI_CALL
00483       DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
00484 
00485 DBFHandle SHPAPI_CALL
00486       DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
00487  
00488 void    SHPAPI_CALL
00489       DBFClose( DBFHandle hDBF );
00490 char    SHPAPI_CALL
00491       DBFGetNativeFieldType( DBFHandle hDBF, int iField );
00492 
00493 #ifdef __cplusplus
00494 }
00495 #endif
00496 
00497 #endif /* ndef _SHAPEFILE_H_INCLUDED */

Generated on Sun Jul 29 04:01:07 2012 for TerraLib - Development Source by  doxygen 1.5.3