www.pudn.com > mitab-1.5.1.zip > MiApi.cs


// $Id: MiApi.cs,v 1.2 2005/03/24 17:02:06 dmorissette Exp $ 
// 
 
using System; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Collections; 
 
namespace EBop.MapObjects.MapInfo { 
 
	// feature type values 
	public enum FeatureType { 
		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}; 
 
	public enum FieldType {  
		TABFT_Char = 1, 
		TABFT_Integer = 2, 
		TABFT_SmallInt = 3, 
		TABFT_Decimal = 4, 
		TABFT_Float = 5, 
		TABFT_Date = 6, 
		TABFT_Logical = 7}; 
 
	public enum Justification { 
		TABTJ_Left = 0, 
		TABTJ_Center = 1, 
		TABTJ_Right = 2}; 
 
	public enum TextSpacing { 
		TABTS_Single = 0, 
		TABTS_1_5 = 1, 
		TABTS_Double = 2}; 
 
	// test linetype 
	public enum LineSpacing { 
		TABTL_NoLine = 0, 
		TABTL_Simple = 1, 
		TABTL_Arrow = 2}; 
 
	///  
	/// Wrapper functions for the version 1.3.0 of the MapInfo Tab API. 
	///  
	///  
	/// Requires mitab.dll (www.maptools.org) 
	/// See http://mitab.maptools.org/ 
	///  
	/// Graham Sims 
	/// Environment Bay of Plenty, Whakatane, New Zealand 
	/// http://www.envbop.govt.nz 
	///  
	public class MiApi { 
 
		private MiApi() { 
		} 
		 
		///  
		/// Returns the version of the library.  
		///  
		/// An integer representing the current version of the MITAB library in the  
		/// format xxxyyyzzz, e.g. returns 1002004 for v1.2.4.   
		///  
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_getlibversion(); 
 
		///  
		/// Get the last error message.  
		///  
		///  
		/// Fetches the last error message posted with CPLError(), that hasn't been cleared by CPLErrorReset().  
		/// The returned pointer is to an internal string that should not be altered or freed.  
		///  
		/// A pointer to the last error message, or an empty string if there is no posted error message 
		[DllImport("mitab.dll", EntryPoint="mitab_c_getlasterrormsg")] 
		private static extern IntPtr _mitab_c_getlasterrormsg(); 
		public static string mitab_c_getlasterrormsg() { 
			return Marshal.PtrToStringAnsi(_mitab_c_getlasterrormsg()); 
		} 
		 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_getlasterrormsg_vb ( 
//			[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)] string errormsg, int l); 
 
		///  
		/// Fetch the last error number.  
		///  
		/// This is the error number, not the error class.  
		/// The error number of the last error to occur, or CPLE_None (0) if there are no posted errors 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_getlasterrorno(); 
 
		///  
		/// Open an existing .TAB or .MIF dataset for read access.  
		///  
		/// The function automatically detects the format (.MIF or .TAB) of the specified file. 
		/// Note that it is not possible to open a file for update (i.e. read+write) with the current  
		/// version of the library. 
		/// The complete filename (including extension .TAB or .MIF) of the file to open. 
		/// A valid mitab_handle, or NULL if the file could not be opened. 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_open([MarshalAs(UnmanagedType.LPStr)] string filename); 
 
		///  
		/// Close a dataset previously opened using mitab_c_open() or created using mitab_c_create().  
		///  
		/// The mitab_handle of the dataset to close. 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_close(IntPtr mitab_handle); 
 
		///  
		/// 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. 
		///  
		/// The complete filename (including extension .TAB or .MIF) of the file to create. 
		/// 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. 
		/// 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(). 
		/// 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. 
		/// The lower dataset bound. 
		/// The right dataset bound.  
		/// The left dataset bound. 
		/// A valid mitab_handle, or NULL if the file could not be created. 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_create(  
			[MarshalAs(UnmanagedType.LPStr)] string filename, 
			[MarshalAs(UnmanagedType.LPStr)] string mif_or_tab, 
			[MarshalAs(UnmanagedType.LPStr)] string mif_projectiondef, 
			double north, double south, 
			double east, double west ); 
												    
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_add_field( IntPtr handle,  
			[MarshalAs(UnmanagedType.LPStr)] string field_name, 
			int field_type, int width, int precision,  
			int indexed, int unique ); 
 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_write_feature(IntPtr handle, IntPtr feature ); 
 
 
		///  
		/// Iterator to get the next valid feature id when reading a dataset opened with mitab_c_open().  
		///  
		/// the mitab_handle of the file opened for read access. 
		/// The id of the last feature that was read.  
		/// Use -1 to get first feature id in the dataset. 
		/// The next valid feature id in the dataset, or -1 when there are no more feature ids.  
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_next_feature_id(IntPtr handle, int last_feature_id ); 
 
		///  
		/// Read a mitab_feature object from the file.  
		///  
		/// Works only with datasets opened with mitab_c_open(). 
		/// The mitab_handle of the file opened for read access. 
		/// The id of the feature to read, obtained by calling mitab_c_next_feature_id() 
		/// 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. 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_read_feature( IntPtr handle, int feature_id ); 
 
		///  
		/// Destroy a mitab_feature object and release all memory associated with it.  
		///  
		/// The mitab_feature to destroy. 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_destroy_feature( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_feature_id( IntPtr feature ); 
 
		///  
		/// Create a new mitab_feature object to be written to a dataset created using mitab_c_create().  
		///  
		/// The handle of the dataset opened for write access. 
		/// A member of the feature type enumeration. 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) and TABFC_MultiPoint (10) 
		/// 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(). 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_create_feature( IntPtr handle, int feature_type ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_field( IntPtr feature, int field_index,  
			[MarshalAs(UnmanagedType.LPStr)] string value ); 
     
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_text( IntPtr feature,  
			[MarshalAs(UnmanagedType.LPStr)] string text ); 
 
 
		///  
		/// Get the text string on a TABFC_Text object.  
		///  
		/// The mitab_feature object. 
		/// A pointer to the text string in the object. 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_text")] 
		public static extern IntPtr _mitab_c_get_text( IntPtr feature ); 
		public static string mitab_c_get_text( IntPtr feature ) { 
			return Marshal.PtrToStringAnsi(_mitab_c_get_text(feature)); 
		} 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_text_vb( IntPtr  feature,  
//			[MarshalAs(UnmanagedType.LPStr)] string text, int l ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_text_display( IntPtr feature, 
			double angle, double height, double width, 
			int fg_color, int bg_color, 
			int justification, int spacing, int linetype ); 
 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_text_angle( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_text_height( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_text_width( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_text_fgcolor( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_text_bgcolor( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_text_justification( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_text_spacing( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_text_linetype( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_pen( IntPtr feature, int width, int pattern, int color ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_pen_color( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_pen_width( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_pen_pattern( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_brush( IntPtr feature, 
			int fg_color, int bg_color, int pattern, 
			int transparent ); 
		 
		///  
		/// Get an object's brush foreground color property. Applies to region, ellipse and rectangle objects. 
		///  
		/// The mitab_feature object. 
		/// The brush foreground color (24 bits RGB value). 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_brush_fgcolor( IntPtr feature ); 
 
		///  
		/// Get an object's brush background color property. Applies to region, ellipse and rectangle objects. 
		///  
		/// The mitab_feature object. 
		/// The brush foreground color (24 bits RGB value). 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_brush_bgcolor( IntPtr feature ); 
 
		///  
		/// Get an object's brush pattern property. Applies to region, ellipse and rectangle objects. 
		///  
		/// The mitab_feature object. 
		/// The brush pattern number (1 is none, 2 is solid fill, etc.). 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_brush_pattern( IntPtr feature ); 
 
		///  
		/// Get an object's brush transparency property. Applies to region, ellipse and rectangle objects. 
		///  
		/// The mitab_feature object. 
		/// The brush transparency value, either 0 for an opaque brush (using bg color) or 1 for  
		/// transparent (ignore bg color). 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_brush_transparent( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_font( IntPtr feature,  
			[MarshalAs(UnmanagedType.LPStr)] string font_name ); 
 
		///  
		/// Get the font name from a TABFC_Text or TABFC_FontPoint object, or the symbol name from a  
		/// TABFC_CustomPoint.  
		///  
		/// The mitab_feature object. 
		/// The text font name. 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_font")] 
		private static extern IntPtr _mitab_c_get_font( IntPtr feature ); 
		public static string mitab_c_get_font( IntPtr feature ) { 
			return Marshal.PtrToStringAnsi(_mitab_c_get_font( feature )); 
		} 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_font_vb( IntPtr feature,  
//			[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)] string font, int l ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_symbol( IntPtr feature, int symbol_no, 
		   int symbol_size, int symbol_color ); 
 
		///  
		/// Get an object's symbol color property. Applies only to point and multipoint objects. 
		///  
		/// The mitab_feature object. 
		/// The symbol color (24 bits RGB value). 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_symbol_color( IntPtr feature ); 
 
		///  
		/// Get an object's symbol number property. Applies only to point and multipoint objects. 
		///  
		/// The mitab_feature object. 
		/// The symbol number (valid range: 32 to 67) 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_symbol_no( IntPtr feature ); 
 
		///  
		/// Get an object's symbol size property. Applies only to point and multipoint objects. 
		///  
		/// The mitab_feature object. 
		/// The symbol size in pixels (valid range 1 to 48) 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_symbol_size( IntPtr feature ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_points( IntPtr feature, int part, 
			int vertex_count, ref double x, ref double y ); 
 
		[DllImport("mitab.dll")] 
		public static extern void mitab_c_set_arc( IntPtr feature,  
			double center_x, double center_y, 
			double x_radius, double y_radius, 
			double start_angle, double end_angle); 
 
		///  
		/// Return a mitab_feature's object type.  
		///  
		/// The mitab_feature object. 
		/// A member of the FeatureType enumeration, one of 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) or TABFC_MultiPoint (10). 
		[DllImport("mitab.dll")] 
		public static extern FeatureType mitab_c_get_type( IntPtr feature ); 
 
		///  
		/// Return the number of parts (rings or polyline segments) in an object.  
		///  
		/// The mitab_feature object. 
		/// The number of parts (in a region or polyline) or 0 if the object's  
		/// geometry was not set. For all object types other than polyline region, returns 1  
		/// if object geometry is set. 
		///  
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_parts( IntPtr feature ); 
 
		///  
		/// Return the number of points in a part of a mitab_feature object.  
		///  
		/// The mitab_feature object. 
		/// The part number we are interested in.  
		/// Use 0 for objects which cannot have multiple parts. 
		/// The number of points in that feature's part or 0 if the object has no  
		/// geometry or the part number is invalid. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_vertex_count( IntPtr feature, int part ); 
 
		///  
		/// Return the X coordinate of a point in a part of a mitab_feature object. 
		///  
		/// The mitab_feature object. 
		/// The part number we are interested in. Use 0 for objects which cannot have multiple parts. 
		/// The point number, with 0 being the first point. 
		/// The X coordinate of the specified point or null if the object has no geometry or the part or the point number is invalid. 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_vertex_x( IntPtr feature, int part, int vertex ); 
 
		///  
		/// Return the Y coordinate of a point in a part of a mitab_feature object. 
		///  
		/// The mitab_feature object. 
		/// The part number we are interested in. Use 0 for objects which cannot have multiple parts. 
		/// The point number, with 0 being the first point. 
		/// The Y coordinate of the specified point or null if the object has no geometry or the part or the point number is invalid. 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_vertex_y( IntPtr feature, int part, int vertex ); 
 
		///  
		/// Return the number of features in a dataset. 
		///  
		/// The dataset's handle. 
		/// The number of features in the dataset. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_feature_count( IntPtr handle ); 
 
		///  
		/// Return the number of attribute fields in a dataset's schema. 
		///  
		/// The dataset's handle. 
		/// The number of attribute fields defined in the dataset. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_field_count( IntPtr handle ); 
		 
		///  
		/// Return the type of an attribute field in a dataset's schema.  
		///  
		/// The dataset's handle. 
		/// The index of the field to look at, with 0 being the first field. 
		/// The 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) 
		[DllImport("mitab.dll")] 
		public static extern FieldType mitab_c_get_field_type( IntPtr handle, int field ); 
		 
		///  
		/// Return the name of an attribute field in a dataset's schema.  
		///  
		/// The dataset's handle. 
		/// The index of the field to look at, with 0 being the first field. 
		/// A pointer to the field name. The returned string pointer is a reference to an  
		/// internal buffer and should not be modified or freed by the caller. 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_field_name")] 
		private static extern IntPtr _mitab_c_get_field_name(IntPtr handle, int field); 
		public static string mitab_c_get_field_name(IntPtr handle, int field) { 
			return Marshal.PtrToStringAnsi( _mitab_c_get_field_name(handle, field) ); 
		} 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_field_name_vb( IntPtr handle, int field,  
//			[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)] string name, int l ); 
		 
		///  
		/// Return the width of an attribute field in a dataset's schema.  
		///  
		/// The dataset's handle. 
		/// The index of the field to look at, with 0 being the first field. 
		/// The field width. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_field_width( IntPtr handle, int field ); 
 
		///  
		/// Return the precision of an attribute field in a dataset's schema.  
		///  
		/// The dataset's handle. 
		/// The index of the field to look at, with 0 being the first field. 
		/// The field precision. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_get_field_precision( IntPtr handle, int field ); 
 
		///  
		/// Fetch an attribute field value in a mitab_feature as a string. 
		///  
		/// The function returns a reference to an internal string buffer that contains the string  
		/// representation of the attribute field's value (integer and floating point values are converted  
		/// to string using sprintf()). 
		/// The mitab_feature object 
		/// The index of the field to look at, with 0 being the first field. 
		/// A pointer to a string containing the value of the field. The returned string pointer  
		/// is a reference to an internal buffer and should not be modified or freed by the caller.  
		/// Its value will be valid only until the next call to mitab_c_get_field(). 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_field_as_string")] 
		private static extern IntPtr _mitab_c_get_field_as_string( IntPtr feature, int field ); 
		public static string mitab_c_get_field_as_string( IntPtr feature, int field ) { 
			return Marshal.PtrToStringAnsi( _mitab_c_get_field_as_string( feature, field )); 
		} 
 
 
		///  
		/// Fetch an attribute field value in a mitab_feature as a double.  
		///  
		/// The mitab_feature object. 
		/// The index of the field to look at, with 0 being the first field. 
		/// The value of the field converted to double. 
		[DllImport("mitab.dll")] 
		public static extern double mitab_c_get_field_as_double( IntPtr feature, int field); 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_field_as_string_vb( IntPtr feature, int field,  
//			[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]  string value, int l ); 
 
		[DllImport("mitab.dll")] 
		public static extern IntPtr mitab_c_get_projinfo( IntPtr dataset ); 
		 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_set_projinfo( IntPtr dataset, IntPtr projinfo ); 
		 
		/// A pointer to a string 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_mif_coordsys")] 
		private static extern IntPtr _mitab_c_get_mif_coordsys(IntPtr dataset); 
		public static string mitab_c_get_mif_coordsys(IntPtr dataset) { 
			return Marshal.PtrToStringAnsi(_mitab_c_get_mif_coordsys(dataset)); 
		} 
		 
 
		///A pointer to a string 
		[DllImport("mitab.dll", EntryPoint="mitab_c_get_extended_mif_coordsys")] 
		private static extern IntPtr _mitab_c_get_extended_mif_coordsys(IntPtr dataset); 
		public static string mitab_c_get_extended_mif_coordsys(IntPtr dataset) { 
			return Marshal.PtrToStringAnsi(_mitab_c_get_extended_mif_coordsys(dataset)); 
		} 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_mif_coordsys_vb( IntPtr dataset,  
//			[MarshalAs(UnmanagedType.LPStr)] string coordsys, int l); 
 
//		[DllImport("mitab.dll")] 
//		public static extern int mitab_c_get_extended_mif_coordsys_vb( IntPtr dataset,  
//			[MarshalAs(UnmanagedType.LPStr)] string coordsys, int l); 
 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_load_coordsys_table( 
			[MarshalAs(UnmanagedType.LPStr)] string filename); 
 
		///  
		/// Return a false if the ring is the first of a polygon.  
		///  
		/// The mitab_feature object. 
		/// The requested ring index 
		/// True or false depends on the part number of the ring in the polygon, true if it's not the  
		/// first part of a polygon. If the feature is not a region the return value will be false. 
		[DllImport("mitab.dll")] 
		public static extern int mitab_c_is_interior_ring( IntPtr feature, int requestedringindex ); 
	} 
}