GIS开源库shapeLib的使用方法(二)

时间:2022-11-26 00:52:49

前面介绍了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)