📄 mitab_capi.cpp
字号:
/************************************************************************/
/**
* Close a dataset previously opened using mitab_c_open() or created using
* mitab_c_create().
*
* @param handle the mitab_handle of the dataset to close.
*/
void MITAB_STDCALL
mitab_c_close( mitab_handle handle )
{
IMapInfoFile *poFile = (IMapInfoFile *) handle;
poFile->Close();
delete poFile;
}
/************************************************************************/
/* mitab_c_create() */
/************************************************************************/
/**
* Create a new .TAB or .MIF dataset.
*
* Note that it is not possible to open a file for update (i.e. read+write)
* with the current version of the library.
*
* @param filename the complete filename (including extension .TAB or .MIF)
* of the file to create.
* @param mif_or_tab one of "mif" to create a .MIF dataset or "tab" to create
* a .TAB dataset. The default is to create a TAB dataset if this
* parameter's value is NULL or an empty string.
* @param mif_projection the projection to use for the dataset, in the same
* format that is used in the "CoordSys" line of a MIF file header.
* If this parameter's value is NULL or empty then a LAT/LON coordsys
* is assumed. See also mitab_c_get_mif_coordsys().
* @param north the upper dataset bound.
* Note that valid bounds are required for a .TAB dataset otherwise
* data may not be stored properly in the file.
* MITAB knows the default bounds only for the most common MapInfo
* coordinate systems, passing north,south,east,west as 0,0,0,0 will
* instruct MITAB to attempt to use the default bounds for that
* projection. If no default bounds are found for this projection then
* your data may not be stored properly in the file unless you provide
* valid bounds via the north,south,east,west parameters.
* @param south the lower dataset bound.
* @param east the right dataset bound.
* @param west the left dataset bound.
* @return a valid mitab_handle, or NULL if the file could not be created.
*/
mitab_handle MITAB_STDCALL
mitab_c_create( const char * filename,
const char * mif_or_tab,
const char * mif_projection,
double north, double south,
double east, double west )
{
IMapInfoFile *poFile;
CPLSetErrorHandler( CPLQuietErrorHandler );
if( mif_or_tab == NULL || !EQUAL(mif_or_tab,"mif") )
{
poFile = new TABFile;
}
else
{
poFile = new MIFFile;
}
if( poFile->Open( filename, "wb" ) != 0 )
{
delete poFile;
return NULL;
}
if( mif_projection != NULL && strlen(mif_projection) > 0 )
{
poFile->SetMIFCoordSys( mif_projection );
}
if (north != 0 || south != 0 || east != 0 || west != 0)
poFile->SetBounds( west, south, east, north );
return (mitab_handle) poFile;
}
/************************************************************************/
/* mitab_c_set_quick_spatial_index_mode() */
/************************************************************************/
/**
* Select "quick spatial index mode".
*
* The default behavior of MITAB is to generate an optimized spatial index,
* but this results in slower write speed.
*
* Applications that want faster write speed and do not care
* about the performance of spatial queries on the resulting file can
* use this function to require the creation of a non-optimal
* spatial index (actually emulating the type of spatial index produced
* by MITAB before version 1.6.0). In this mode writing files can be
* about 5 times faster, but spatial queries can be up to 30 times slower.
*
* mitab_c_set_quick_spatial_index_mode() must be called immediately after
* mitab_c_create() and before starting to write any data to the file.
* This function applies only to newly created TAB files and does not
* do anything useful for MIF files.
*
* @param dataset the mitab_handle of the newly created dataset.
* @return 0 on success, -1 on error.
*/
int MITAB_STDCALL
mitab_c_set_quick_spatial_index_mode( mitab_handle dataset )
{
IMapInfoFile *poFile = (IMapInfoFile *) dataset;
return poFile->SetQuickSpatialIndexMode();
}
/************************************************************************/
/* mitab_c_add_field() */
/* */
/* Add a new field to the schema. Return the field id. */
/************************************************************************/
/**
* Add a new field to the dataset's schema.
*
* Adding fields MUST be done immediately after creating a new dataset and
* BEFORE creating the first feature.
*
* @param dataset the mitab_handle of the newly created dataset.
* @param field_name the name of the new field to create.
* @param field_type the new field type, one of TABFT_Char (1),
* TABFT_Integer (2), TABFT_SmallInt (3), TABFT_Decimal (4),
* TABFT_Float (5), TABFT_Date (6), or TABFT_Logical (7)
* @param width the width of the new field, applies only to char and decimal
* types.
* @param precision the number of digits after the decimal point, applies only
* to the decimal field type.
* @param indexed TRUE (1) to create an indexed field (or FALSE (0) for no
* index)
* @param unique for indexed fields, set this to TRUE (1) if the field values
* are guaranteed to be unique, or FALSE (0) otherwise.
* @return the 0-based index of the new field, or -1 if the field could not
* be created.
*/
int MITAB_STDCALL
mitab_c_add_field( mitab_handle dataset, const char *field_name,
int field_type, int width, int precision,
int indexed, int unique )
{
IMapInfoFile *poFile = (IMapInfoFile *) dataset;
if( poFile->AddFieldNative( field_name, (TABFieldType) field_type,
width, precision, indexed, unique ) != -1 )
{
return poFile->GetLayerDefn()->GetFieldCount() - 1;
}
else
return -1;
}
/************************************************************************/
/* mitab_c_destroy_feature() */
/************************************************************************/
/**
* Destroy a mitab_feature object and release all memory associated with it.
*
* @param feature the mitab_feature to destroy.
*/
void MITAB_STDCALL
mitab_c_destroy_feature( mitab_feature feature )
{
TABFeature *poFeature = (TABFeature *) feature;
if (poFeature)
delete poFeature;
}
/************************************************************************/
/* mitab_c_next_feature_id() */
/* */
/* Iterator to get the next feature id. Use -1 as previous to */
/* get first feature id. Returns -1 when there are no more */
/* feature ids. */
/************************************************************************/
/**
* Iterator to get the next valid feature id when reading a dataset opened
* with mitab_c_open().
*
* @param handle the mitab_handle of the file opened for read access.
* @param last_feature_id the id of the last feature that was read.
* Use -1 to get first feature id in the dataset.
* @return the next valid feature id in the dataset, or -1 when there are
* no more feature ids.
*/
int MITAB_STDCALL
mitab_c_next_feature_id( mitab_handle handle, int last_feature_id )
{
IMapInfoFile *poFile = (IMapInfoFile *) handle;
return poFile->GetNextFeatureId( last_feature_id );
}
/************************************************************************/
/* mitab_c_read_feature() */
/************************************************************************/
/**
* Read a mitab_feature object from the file.
*
* Works only with datasets opened with mitab_c_open().
*
* @param handle the mitab_handle of the file opened for read access.
* @param feature_id the id of the feature to read, obtained by calling
* mitab_c_next_feature_id().
* @return the mitab_feature object that was read. The object will have to
* be destroyed using mitab_c_destroy_feature() once you are done
* with it.
*/
mitab_feature MITAB_STDCALL
mitab_c_read_feature( mitab_handle handle, int feature_id )
{
IMapInfoFile *poFile = (IMapInfoFile *) handle;
TABFeature *poFeature;
poFeature = poFile->GetFeatureRef( feature_id );
if( poFeature != NULL )
poFeature = poFeature->CloneTABFeature();
return (mitab_feature) poFeature;
}
/************************************************************************/
/* mitab_c_write_feature() */
/************************************************************************/
/**
* Write a new feature to a dataset.
*
* Works only with datasets created using mitab_c_create().
*
* The feature to write should have been created using
* mitab_c_create_feature() and its attributes and coordinate information
* set prior to calling this function.
*
* Also note that the mitab_feature object will NOT be owned by the dataset
* after this call and it is still the responsibility of the caller
* to destroy it.
*
* @param handle the mitab_handle of the dataset opened for write access.
* @param feature the mitab_feature to write to the dataset.
* @return TRUE (1) on success, FALSE (0) on error.
*/
int MITAB_STDCALL
mitab_c_write_feature( mitab_handle handle, mitab_feature feature )
{
IMapInfoFile *poFile = (IMapInfoFile *) handle;
TABFeature *poFeature = (TABFeature *) feature;
return poFile->SetFeature( poFeature ) != -1;
}
/************************************************************************/
/* mitab_c_create_feature() */
/************************************************************************/
/**
* Create a new mitab_feature object to be written to a dataset created
* using mitab_c_create().
*
* @param handle the handle of the dataset opened for write access.
* @param feature_type the type of feature object to create. At this point,
* only the following types can be created by this C API function:
* TABFC_NoGeom (0), TABFC_Point (1), TABFC_FontPoint (2),
* TABFC_CustomPoint (3), TABFC_Text (4), TABFC_Polyline (5),
* TABFC_Arc (6), TABFC_Region (7), TABFC_Rectangle (8),
* TABFC_Ellipse (9), TABFC_MultiPoint (10) and TABFC_Collection (11)
* @return the new mitab_feature object, or NULL if creation failed. Note that
* the new object will have to be released using
* mitab_c_destroy_feature().
*/
mitab_feature MITAB_STDCALL
mitab_c_create_feature( mitab_handle handle,
int feature_type )
{
IMapInfoFile *poFile = (IMapInfoFile *) handle;
TABFeature *poFeature = NULL;
if( poFile->GetLayerDefn() == NULL )
{
poFile->AddFieldNative( "NDX", TABFInteger, 10, 0 );
}
if( feature_type == TABFC_NoGeom )
poFeature = new TABFeature(poFile->GetLayerDefn());
else if( feature_type == TABFC_Point )
poFeature = new TABPoint(poFile->GetLayerDefn());
else if( feature_type == TABFC_FontPoint )
poFeature = new TABFontPoint(poFile->GetLayerDefn());
else if( feature_type == TABFC_CustomPoint )
poFeature = new TABCustomPoint(poFile->GetLayerDefn());
else if( feature_type == TABFC_Text )
{
TABText *poText = new TABText(poFile->GetLayerDefn());
poText->SetTextString( "Default Text" );
poFeature = poText;
}
else if( feature_type == TABFC_Polyline )
poFeature = new TABPolyline(poFile->GetLayerDefn());
else if( feature_type == TABFC_Arc )
poFeature = new TABArc(poFile->GetLayerDefn());
else if( feature_type == TABFC_Region )
poFeature = new TABRegion(poFile->GetLayerDefn());
else if( feature_type == TABFC_Rectangle )
poFeature = new TABRectangle(poFile->GetLayerDefn());
else if( feature_type == TABFC_Ellipse )
poFeature = new TABEllipse(poFile->GetLayerDefn());
else if( feature_type == TABFC_MultiPoint )
poFeature = new TABMultiPoint(poFile->GetLayerDefn());
else if( feature_type == TABFC_Collection )
poFeature = new TABCollection(poFile->GetLayerDefn());
return poFeature;
}
/************************************************************************/
/* mitab_c_set_field() */
/************************************************************************/
/**
* Set the value of specified field in a feature object.
*
* @param feature the mitab_feature object.
* @param field_index the 0-based index of the field to set.
* @param field_value a string containing the value to set the field to.
* For any field type, the value should always be passed using its
* character string representation.
*/
void MITAB_STDCALL
mitab_c_set_field( mitab_feature feature, int field_index,
const char *field_value )
{
TABFeature *poFeature = (TABFeature *) feature;
poFeature->SetField( field_index, field_value );
}
/************************************************************************/
/* mitab_c_set_points() */
/************************************************************************/
/**
* Set the feature's coordinates.
*
* @param feature the mitab_feature object.
* @param part the part number. Applies only to a region or a polyline, it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -