/// <summary> /// 返回最后错误代码的描述 /// </summary> public string GetErrorMessage() { string s = ""; switch (m_error) { case LayerSourceError.DbfIsMissing: s = "Dbf table丢失"; break; case LayerSourceError.DbfRecordCountMismatch: s = "DBF表的行数是不同的形状"; break; case LayerSourceError.OcxBased: s = m_ErrorString; break; } m_ErrorString = ""; m_error = LayerSourceError.None; return(s); }
/// <summary> /// 关闭图层. 设置未定义的图层类型 /// </summary> public void Close() { m_error = LayerSourceError.None; switch (this.Type) { case LayerSourceType.Shapefile: m_shapefile.Close(); m_shapefile = null; break; case LayerSourceType.Image: m_image.Close(); m_image = null; break; case LayerSourceType.Grid: m_grid.Close(); m_grid = null; break; default: break; } }
/// <summary> /// 打开指定数据源的图层 /// </summary> /// <param name="filename">文件名</param> /// <param name="callback"></param> /// <returns></returns> public bool Open(string filename, MapWinGIS.ICallback callback) { this.Close(); if (filename.ToLower().EndsWith(".shp")) { MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile(); if (sf.Open(filename, callback)) { // 检查dbf是否存在 bool error = false; if (!File.Exists(Path.ChangeExtension(sf.Filename, ".dbf"))) { m_error = LayerSourceError.DbfIsMissing; error = true; } // 检查DBF记录数相匹配的形状的数量。 MapWinGIS.Table table = new MapWinGIS.Table(); table.Open(Path.ChangeExtension(sf.Filename, ".dbf"), null); if (sf.NumShapes != table.NumRows) { m_error = LayerSourceError.DbfRecordCountMismatch; error = true; } table.Close(); if (error) { sf.Close(); } else { m_shapefile = sf; } return(!error); } else { m_error = LayerSourceError.OcxBased; m_ErrorString = sf.get_ErrorMsg(sf.LastErrorCode); } } else { bool asGrid = true; if (filename.ToLower().EndsWith(".tif")) { asGrid = false; } // TODO: 可能更聪明的选择是在grid/image中使用应用程序设置 if (asGrid) { MapWinGIS.Grid grid = new MapWinGIS.Grid(); if (grid.Open(filename, MapWinGIS.GridDataType.UnknownDataType, false, MapWinGIS.GridFileType.UseExtension, callback)) { m_grid = grid; return(true); } } // 尝试image MapWinGIS.Image image = new MapWinGIS.Image(); if (image.Open(filename, MapWinGIS.ImageType.USE_FILE_EXTENSION, false, callback)) { m_image = image; return(true); } else { m_error = LayerSourceError.OcxBased; m_ErrorString = image.get_ErrorMsg(image.LastErrorCode); } } return(false); }