zoukankan      html  css  js  c++  java
  • GIS开源库shapeLib的使用方法

    http://www.cnblogs.com/cjingzm/archive/2012/03/04/2378900.html

        近期研究了一下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.

    前面介绍了shape API,接下来介绍 dBF API

    DBFOpen()

    DBFHandle DBFOpen( const char * pszDBFFile, const char * pszAccess );
    
      pszDBFFile:		The name of the xBase (.dbf) file to access.
    
      pszAccess:		The fopen() style access string.  At this time only
    			"rb" (read-only binary) and "rb+" (read/write binary) 
    		        should be used.

    DBFCreate()

    DBFHandle DBFCreate( const char * pszDBFFile );
    
      pszDBFFile:		The name of the xBase (.dbf) file to create.
    //如果要创建一个完整的shape文件,首先要创建.shap文件,还要创建.dbf文件,两者缺一不可。

    DBFGetFieldCount()  //得到字段的个数

    int DBFGetFieldCount( DBFHandle hDBF );
    
      hDBF:		The access handle for the file to be queried, as returned
                    by DBFOpen(), or DBFCreate().

    DBFGetRecordCount()

    //得到记录的个数,应该和shape文件中的object的个数对应

    int DBFGetRecordCount( DBFHandle hDBF );
    
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
    

    DBFGetFieldIndex()  //通过字段名称得到字段的编号

    
    
    int DBFGetFieldIndex( DBFHandle hDBF, const char *pszFieldName );
    
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
    

    DBFGetFieldInfo()  //得到字段的信息,包括类型、长度等

    
    
    DBFFieldType DBFGetFieldInfo( DBFHandle hDBF, int iField, char * pszFieldName,
                                  int * pnWidth, int * pnDecimals );
    
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
      iField:	The field to be queried.  This should be a number between 
                    0 and n-1, where n is the number fields on the file, as
                    returned by DBFGetFieldCount().
    
      pszFieldName:	If this pointer is not NULL the name of the requested field
    		will be written to this location.  The pszFieldName buffer 
                    should be at least 12 character is size in order to hold
    		the longest possible field name of 11 characters plus a 
                    terminating zero character.
    
      pnWidth:	If this pointer is not NULL, the width of the requested field
    		will be returned in the int pointed to by pnWidth.  This is
                    the width in characters.  
    
      pnDecimals:	If this pointer is not NULL, the number of decimal places
                    precision defined for the field will be returned.  This is
                    zero for integer fields, or non-numeric fields.
    
    

    DBFAddField()  //向DBF表中添加一个新的属性字段

    
    
    int DBFAddField( DBFHandle hDBF, const char * pszFieldName, 
                     DBFFieldType eType, int nWidth, int nDecimals );
    
      hDBF:		The access handle for the file to be updated, as returned by
    		DBFOpen(), or DBFCreate().
    
      pszFieldName:	The name of the new field.  At most 11 character will be used.
                    In order to use the xBase file in some packages it may be
                    necessary to avoid some special characters in the field names
                    such as spaces, or arithmetic operators.
    
      eType:	One of FTString, FTInteger or FTDouble in order to establish
                    the type of the new field.  Note that some valid xBase field
                    types cannot be created such as date fields.
    
      nWidth:	The width of the field to be created.  For FTString fields this
                    establishes the maximum length of string that can be stored.
                    For FTInteger this establishes the number of digits of the
                    largest number that can
                    be represented.  For FTDouble fields this in combination
                    with the nDecimals value establish the size, and precision
                    of the created field.
    
      nDecimals:    The number of decimal places to reserve for FTDouble fields.
                    For all other field types this should be zero.  For instance
                    with nWidth=7, and nDecimals=3 numbers would be formatted
                    similarly to `123.456'.
    
    

    DBFReadIntegerAttribute()

    
    
    int DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
      
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) from which the field value
                    should be read.
    
      iField:	The field within the selected record that should be read.

    DBFReadDoubleAttribute()

    double DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
      
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) from which the field value
                    should be read.
    
      iField:	The field within the selected record that should be read.

    DBFReadStringAttribute()

    const char *DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
      
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) from which the field value
                    should be read.
    
      iField:	The field within the selected record that should be read.

    DBFIsAttributeNULL()

    int DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
      
      hDBF:		The access handle for the file to be queried, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) from which the field value
                    should be read.
    
      iField:	The field within the selected record that should be read.

    DBFWriteIntegerAttribute

    int DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField,
                                  int nFieldValue );
    
      hDBF:		The access handle for the file to be written, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) to which the field value
                    should be written.
    
      iField:	The field within the selected record that should be written.
    
      nFieldValue:	The integer value that should be written.

    DBFWriteDoubleAttribute()

    int DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
                                 double dFieldValue );
    
      hDBF:		The access handle for the file to be written, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) to which the field value
                    should be written.
    
      iField:	The field within the selected record that should be written.
    
      dFieldValue:	The floating point value that should be written.
    
    The DBFWriteDoubleAttribute() function is used to write a value to a numeric field (FTInteger, or FTDouble). If the write succeeds the value TRUE will be returned, otherwise FALSE will be returned. If the value is too large to fit in the field, it will be truncated and FALSE returned.

    DBFWriteStringAttribute()

    int DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
                                 const char * pszFieldValue );
    
      hDBF:		The access handle for the file to be written, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) to which the field value
                    should be written.
    
      iField:	The field within the selected record that should be written.
    
      pszFieldValue: The string to be written to the field.
    
    The DBFWriteStringAttribute() function is used to write a value to a string field (FString). If the write succeeds the value TRUE willbe returned, otherwise FALSE will be returned. If the value is too large to fit in the field, it will be truncated and FALSE returned.

    DBFWriteNULLAttribute()

    int DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
    
      hDBF:		The access handle for the file to be written, as returned by
    		DBFOpen(), or DBFCreate().
    
      iShape:	The record number (shape number) to which the field value
                    should be written.
    
      iField:	The field within the selected record that should be written.
    
    The DBFWriteNULLAttribute() function is used to clear the indicated field to a NULL value. In the .dbf file this is represented by setting the entire field to spaces. If the write succeeds the value TRUE willbe returned, otherwise FALSE will be returned.

    DBFClose()

    void DBFClose( DBFHandle hDBF );
    
      hDBF:		The access handle for the file to be closed.
    
    The DBFClose() function will close the indicated xBase file (opened with DBFOpen(), or DBFCreate()), flushing out all information to the file on disk, and recovering any resources associated with having the file open. The file handle (hDBF) should not be used again with the DBF API after calling DBFClose().

    DBFGetNativeFieldType()

    char DBFGetNativeFieldType( DBFHandle hDBF, int iField );
    
      hDBF:		The access handle for the file.
      iField:       The field index to query.
      
    
    This function returns the DBF type code of the indicated field. It will be one of:
    • 'C' (String)
    • 'D' (Date)
    • 'F' (Float)
    • 'N' (Numeric, with or without decimal)
    • 'L' (Logical)
    • 'M' (Memo: 10 digits .DBT block ptr)
    • ' ' (field out of range)
  • 相关阅读:
    Unity3d获取APK签名及公钥的方法
    NGUI屏幕自适应
    转:Firefox中firebug和xpath checker工具的使用
    判断输入的手机号和价格是否合法
    IOS 四种保存数据的方式
    IOS开发控制器之间传值的几种小方法
    AlertView的三种弹窗模式
    多线程之NSThread、NSOperation及GCD
    IOS Key-Value Observing (KVO)
    UILabel的相关属性设置
  • 原文地址:https://www.cnblogs.com/liongis/p/2736015.html
Copyright © 2011-2022 走看看