近期研究了一下GIS開源庫shapeLib讀寫ArcGIS數據的API函數,先整理一下,將各個API的用法介紹一下。
分為兩個模塊,shape API和DBF API,前者的讀取.shp文件的空間幾何信息,后者讀取.dbf文件的屬性信息。
Shape API:
Shape Types (shape類型)
shape文件的類型定義如下:
#define SHPT_NULL 0 2D Shape Types (pre ArcView 3.x): #define SHPT_POINT 1 Points #define SHPT_ARC 3 Arcs (Polylines, possible in parts) #define SHPT_POLYGON 5 Polygons (possible in parts) #define SHPT_MULTIPOINT 8 MultiPoint (related points) 3D Shape Types (may include "measure" values for vertices): #define SHPT_POINTZ 11 #define SHPT_ARCZ 13 #define SHPT_POLYGONZ 15 #define SHPT_MULTIPOINTZ 18 2D + Measure Types: #define SHPT_POINTM 21 #define SHPT_ARCM 23 #define SHPT_POLYGONM 25 #define SHPT_MULTIPOINTM 28 Complex (TIN-like) with Z, and Measure: #define SHPT_MULTIPATCH 31
SHPObject (shape文件中包含的要素對象)
typedef struct { int nSHPType; //Shape Type (SHPT_* - see list above) 要素類型 int nShapeId; //Shape Number (-1 is unknown/unassigned) ID int nParts; //# of Parts (0 implies single part with no info) 要素有幾個部分組成 int *panPartStart; //Start Vertex of part 要素部分的起始點 int *panPartType; //Part Type (SHPP_RING if not SHPT_MULTIPATCH) 各個部分的類型 int nVertices; //Vertex list double *padfX; double *padfY; double *padfZ; //(all zero if not provided) double *padfM; //(all zero if not provided) double dfXMin; //Bounds in X, Y, Z and M dimensions double dfYMin; double dfZMin; double dfMMin; double dfXMax; double dfYMax; double dfZMax; double dfMMax; } SHPObject;
SHPOpen()
SHPHandle SHPOpen( const char * pszShapeFile, const char * pszAccess ); pszShapeFile: //shape文件的全路徑 pszAccess: //打開方式 "rb" (read-only binary) and "rb+" (read/write binary)
//打開shape文件,操作完必須調用后面的關閉。
SHPGetInfo() //得到shape對象的信息
void SHPGetInfo( SHPHandle hSHP, int * pnEntities, int * pnShapeType, double * padfMinBound, double * padfMaxBound ); hSHP: // shape文件的句柄 The handle previously returned by SHPOpen() or SHPCreate(). pnEntities: A pointer to an integer into which the number of entities/structures should be placed. May be NULL. pnShapetype: A pointer to an integer into which the shapetype of this file should be placed. Shapefiles may contain either SHPT_POINT, SHPT_ARC, SHPT_POLYGON or SHPT_MULTIPOINT entities. This may be NULL. padfMinBound: The X, Y, Z and M minimum values will be placed into this four entry array. This may be NULL. padfMaxBound: The X, Y, Z and M maximum values will be placed into this four entry array. This may be NULL.
SHPReadObject() //讀取每條記錄中的信息
SHPObject *SHPReadObject( SHPHandle hSHP, int iShape ); hSHP: The handle previously returned by SHPOpen() or SHPCreate(). iShape: The entity number of the shape to read. Entity numbers are between 0 and nEntities-1 (as returned by SHPGetInfo()).
SHPClose()
void SHPClose( SHPHandle hSHP );
SHPCreate() //創建一個shape文件
SHPHandle SHPCreate( const char * pszShapeFile, int nShapeType ); pszShapeFile: //創建文件的名稱 nShapeType: //類型,參加上面定義的類型
SHPCreateSimpleObject() //創建簡單要素對象,一般調用此方法
SHPObject * SHPCreateSimpleObject( int nSHPType, int nVertices, double *padfX, double * padfY, double *padfZ, ); nSHPType: The SHPT_ type of the object to be created, such as SHPT_POINT, or SHPT_POLYGON. nVertices: The number of vertices being passed in padfX, padfY, and padfZ. padfX: An array of nVertices X coordinates of the vertices for this object. padfY: An array of nVertices Y coordinates of the vertices for this object. padfZ: An array of nVertices Z coordinates of the vertices for this object. This may be NULL in which case they are all assumed to be zero.
SHPCreateObject() //創建復雜對象,參數中包含所有信息
SHPObject * SHPCreateObject( int nSHPType, int iShape, int nParts, int * panPartStart, int * panPartType, int nVertices, double *padfX, double * padfY, double *padfZ, double *padfM ); nSHPType: The SHPT_ type of the object to be created, such as SHPT_POINT, or SHPT_POLYGON. iShape: The shapeid to be recorded with this shape. nParts: The number of parts for this object. If this is zero for ARC, or POLYGON type objects, a single zero valued part will be created internally. panPartStart: The list of zero based start vertices for the rings (parts) in this object. The first should always be zero. This may be NULL if nParts is 0. panPartType: The type of each of the parts. This is only meaningful for MULTIPATCH files. For all other cases this may be NULL, and will be assumed to be SHPP_RING. nVertices: The number of vertices being passed in padfX, padfY, and padfZ. padfX: An array of nVertices X coordinates of the vertices for this object. padfY: An array of nVertices Y coordinates of the vertices for this object. padfZ: An array of nVertices Z coordinates of the vertices for this object. This may be NULL in which case they are all assumed to be zero. padfM: An array of nVertices M (measure values) of the vertices for this object. This may be NULL in which case they are all assumed to be zero.
SHPComputeExtents() //重新計算記錄的取值范圍
void SHPComputeExtents( SHPObject * psObject ); psObject: An existing shape object to be updated in place.
SHPWriteObject() //向shape文件中添加一條記錄,只有先添加了記錄,才能添加屬性信息
int SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject *psObject ); hSHP: The handle previously returned by SHPOpen("r+") or SHPCreate(). iShape: The entity number of the shape to write. A value of -1 should be used for new shapes. psObject: The shape to write to the file. This should have been created with SHPCreateObject(), or SHPCreateSimpleObject().
SHPDestroyObject() //刪除對象
void SHPDestroyObject( SHPObject *psObject ); psObject: The object to deallocate.
SHPRewindObject()
int SHPRewindObject( SHPHandle hSHP, SHPObject *psObject ); hSHP: The shapefile (not used at this time). psObject: The object to deallocate.
This function will reverse any rings necessary in order to enforce the shapefile restrictions on the required order of inner and outer rings in the Shapefile specification. It returns TRUE if a change is made and FALSE if no change is made. Only polygon objects will be affected though any object may be passed.