导航

在 dotNet 调用 MapInfo 应用程序处理特殊问题

Posted on 2006-01-07 21:37  色彩与空间  阅读(2229)  评论(4编辑  收藏  举报

  MapInfo系列产品的一个特点就是便于二次开发和集成,比如MapX,和现在dotNet环境下的MapXtreme2004,还有Java环境下的MapXtreme等
 不过功能最强的还是MapInfo这个平台本身,比如MapInfo的一些方法在MapX中没有提供,这时候就可以用基于MapInfo OLE 的这种方法,把
 MapInfo.exe 嵌入到程序中。下面的这个类可以通过dotNet环境直接调用MapInfo的功能,如果结合相对应的MapBasic的程序,功能更强。
 比如CreateGrid这个方法实现的功能,MapX,MapXtreme2004都无法生成。

    /// <summary>
    
/// MapInfoApplication Class
    
/// </summary>
    public class MapInfoApplication
    {
        
private MapInfoApplication()
        {}

        //静态构造函数创建MapInfo应用程序实例
        
static MapInfoApplication(){
            
if(mapInfo == null){
                mapInfoType 
= Type.GetTypeFromProgID("MapInfo.Application");
                mapInfo 
= System.Activator.CreateInstance(mapInfoType);
                Do(
"Set ProgressBars off");
            }
        }
        
private static Type   mapInfoType;
        
private static object mapInfo;
        
private static object[] parameter= new object[1]; 
        
private static string strCMD = string.Empty;

        
//MapInfo do function
        public static void Do(string command){
            parameter[
0= command;
            mapInfoType.InvokeMember(
"do",BindingFlags.InvokeMethod,null,mapInfo,parameter);
        }
        
        //MapInfo eval function
        
public static string Eval(string command){
            parameter[
0= command;
            
return (string)mapInfoType.InvokeMember("eval",BindingFlags.InvokeMethod,null,mapInfo,parameter);
        }

        
public static bool CreateGrid(string PointTableFile,string GridTableFile/* type is *.mig */){
            
try{
                
string tableName = OpenTable(PointTableFile);
                
if(tableName == string.Empty)
                    
throw new Exception("打开 " + PointTableFile + " 错误!");
                strCMD    
=    " create grid from " + tableName + " with price into \"" + GridTableFile + "\" type \"mig.ghl\" ";
                strCMD 
+= " CoordSys NonEarth Units \"m\" Bounds (-294432301.819, -294234040.53) (294631503.779, 294829765.067)  ";
                strCMD 
+= " inflect 5 by percent at RGB(0, 0, 255) : 0 RGB(0, 255, 255) : 25 RGB(0, 255, 0) : 50 RGB(255, 255, 0) : 75 RGB(255, 0, 0) : 100  ";
                strCMD 
+= " round 10 # use 2 # cell min 601 ";
                strCMD 
+= "interpolate with \"IDW\" version \"100\" using 7 ";
                strCMD 
+= " \"AGGREGATION METHOD\":  \"0\" ";
                strCMD 
+= " \"BORDER\":  \"0\" ";
                strCMD 
+= " \"CELL SIZE\":  \"1\" ";
                strCMD 
+= " \"EXPONENT\":  \"2\" ";
                strCMD 
+= " \"MAX POINTS\":  \"25\" ";
                strCMD 
+= " \"MIN POINTS\":  \"1\" ";
                strCMD 
+= " \"SEARCH RADIUS\":  \"300\" ";
                MapInfoApplication.Do(strCMD);
            }
            
catch(System.Exception){
                
return false;
            }
finally{
                CloseAll();
            }
            
            
return true;
        }

        
public static string OpenTable(string tableFile){
            
try{
                strCMD 
= "Open Table \"" + tableFile +"\"";
                Do(strCMD);
            }
catch(System.Exception){
                
return string.Empty;
            }
            
return System.IO.Path.GetFileNameWithoutExtension(tableFile);
        }

        
public static void CloseAll(){
            Do(
"Close All");
        }
        


    }