/// <summary> /// 通过路径获取工作空间 /// </summary> /// <param name="eType">要打开的工作空间类别</param> /// <param name="path">文件或文件夹路径</param> /// <returns></returns> public static IWorkspace GetWorkspaceFromFile(string path, EWorkspaceType eType) { try { IWorkspaceName workspaceName = new WorkspaceNameClass(); workspaceName.WorkspaceFactoryProgID = eType.GetDescription(1); workspaceName.PathName = path; IName iName = (IName)workspaceName; IWorkspace workspace = iName.Open() as IWorkspace; Marshal.ReleaseComObject(iName); Marshal.ReleaseComObject(workspaceName); return(workspace); } catch (Exception ex) { var msg = ex.Message; //判断路径长度是否符合要求(Windows默认的字符的长度限制为260,一个中文字符长度为2)(路径超长不一定出错,mdb数据会出错,shp数据不一定出错) if (!FileOpt.PathLengthValidate(path, out var length)) { msg += $"\r\n 可能原因为:路径长度超出限制,无法识别“{path}”的数据,请修改数据存放路径\r\n(允许路径最大长度为260,该路径长度为{length})\r\n"; } throw new Exception($"打开{eType.GetDescription(2)}工作空间“{path}”出错:{msg}"); } }
/// <summary> /// 校验并返回与数据源一致的条件查询语句 /// <para>例如eType == <see cref="EWorkspaceType.Access"/>,条件查询语句为 BH like '440101%',则返回结果为 BH like '440101*'</para> /// </summary> /// <param name="eType">数据源类型</param> /// <param name="whereClause">需要校验的条件查询语句</param> public static string ValidateWhereClause(EWorkspaceType eType, string whereClause) { if (eType == EWorkspaceType.Access && whereClause.Contains("like")) { return(whereClause.Replace('_', '?').Replace('%', '*')); } return(whereClause); }
/// <summary> /// 打开工作空间 /// </summary> /// <param name="connStrOrPath"> /// 工作空间路径或连接字符串,可以是以下情况: /// 1、shp、txt、dwg、栅格文件(GRID、TIFF、ERDAS IMAGE等)所在目录; /// 2、gdb文件夹自身路径; /// 3、mdb、xls、xlsx文件路径; /// 4、sde连接字符串(SERVER=ditu.test.com;INSTANCE=5151;DATABASE=sde_test;USER=sa;PASSWORD=sa;VERSION=dbo.DEFAULT); /// 5、oledb连接字符串,包括连接Excel、Access、Oracle、SQLServer等(Provider=Microsoft.Jet.OLEDB.4.0;Data Source=x:\xxx.mdb;User Id=admin;Password=xxx;); /// 6、直连sql连接字符串(server=localhost;uid=sa;pwd=sa;database=myDatabase) /// </param> /// <param name="eType"> /// 标识优先将strConnOrPath作为打开哪种工作空间的参数,值为Default时, /// 根据strConnOrPath参数自动识别为shp/gdb/mdb/sde/oledb的其中一种工作空间</param> /// <returns></returns> public static IWorkspace GetWorkSpace(string connStrOrPath, EWorkspaceType eType = EWorkspaceType.Default) { IWorkspace workspace = null; if (IsConnectionString(connStrOrPath))//当参数是数据库连接字符串时 { var tmpConnStr = connStrOrPath.ToLower(); if (tmpConnStr.Contains("provider") && tmpConnStr.Contains("oledb")) { eType = EWorkspaceType.OleDb; } else if (eType == EWorkspaceType.Default) { eType = EWorkspaceType.Sde; } workspace = GetWorksapceFromConnStr(connStrOrPath, eType); } else if (IsWorkspacePath(connStrOrPath)) { if (!System.IO.Path.IsPathRooted(connStrOrPath)) { connStrOrPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, connStrOrPath); } if (System.IO.Directory.Exists(connStrOrPath)) //当参数是文件夹路径时 { if (eType == EWorkspaceType.Default) { eType = connStrOrPath.ToLower().EndsWith(".gdb") ? EWorkspaceType.FileGDB : EWorkspaceType.ShapeFile; } workspace = GetWorkspaceFromFile(connStrOrPath, eType); } else if (System.IO.File.Exists(connStrOrPath)) //当参数是文件路径时 { var extension = System.IO.Path.GetExtension(connStrOrPath)?.ToLower(); if (extension == ".mdb") { eType = EWorkspaceType.Access; } else if (extension == ".xls" || extension == ".xlsx") { eType = EWorkspaceType.Excel; } workspace = GetWorkspaceFromFile(connStrOrPath, eType); } } return(workspace); }
/// <summary> /// 设置图层数据源 /// </summary> /// <param name="layer">设置数据源的图层</param> /// <param name="workspacePath">工作空间路径</param> /// <param name="dataSetName">数据集名称,数据源不在数据集中则应为null</param> /// <param name="objectName">图层关联对象的名称,即要素类名称或栅格数据集名称</param> public static void SetSourcePath(this ILayer layer, string workspacePath, string dataSetName, string objectName) { if (!WorkspaceEx.IsWorkspacePath(workspacePath) && !WorkspaceEx.IsConnectionString(workspacePath)) { throw new ArgumentException($"找不到数据源({workspacePath}),请指定正确的数据源!"); } switch (layer) { case IFeatureLayer featureLayer: if (featureLayer is IDataLayer dataLayer) { IDatasetName datasetName = (IDatasetName)dataLayer.DataSourceName; EWorkspaceType eWorkspaceType = WorkspaceEx.GetDefaultWorkspaceType(workspacePath); datasetName.WorkspaceName.WorkspaceFactoryProgID = eWorkspaceType.GetDescription(1); datasetName.WorkspaceName.PathName = workspacePath; //TODO: if (!string.IsNullOrWhiteSpace(dataSetName)) { datasetName.Category = dataSetName; } if (!string.IsNullOrWhiteSpace(objectName)) { datasetName.Name = objectName; } } break; case IRasterLayer rasterLayer: objectName = string.IsNullOrWhiteSpace(objectName) ? ((rasterLayer as IDataLayer)?.DataSourceName as IDatasetName)?.Name : objectName; if (objectName == null) { throw new Exception($"指定数据源名称(参数{nameof(objectName)})不能为空!"); } rasterLayer.CreateFromFilePath(!string.IsNullOrWhiteSpace(dataSetName) ? Path.Combine(workspacePath, dataSetName, objectName) : Path.Combine(workspacePath, objectName)); break; case IGroupLayer groupLayer: throw new Exception("该图层对象为图层组(IGroupLayer),无法设置图层组的数据源,请指定到具体的图层"); default: throw new NotImplementedException( "图层不是要素图层(IFeatureLayer),也不是栅格图层(IRasterLayer),未实现其他类型图层的数据源设置"); } }
/// <summary> /// 通过路径获取工作空间 /// </summary> /// <param name="eType">要打开的工作空间类别</param> /// <param name="path">文件或文件夹路径</param> /// <returns></returns> public static IWorkspace GetWorkspaceFromFile(string path, EWorkspaceType eType) { try { IWorkspaceName workspaceName = new WorkspaceNameClass(); workspaceName.WorkspaceFactoryProgID = eType.GetDescription(1); workspaceName.PathName = path; IName iName = (IName)workspaceName; return(iName.Open() as IWorkspace); } catch (Exception ex) { throw new Exception($"打开{eType.GetDescription(2)}工作空间“{path}”出错:{ex.Message}"); } }
/// <summary> /// 通过连接字符串获取工作空间,连接字符串参考: /// <para>①sde: SERVER=ditu.test.com;INSTANCE=5151;DATABASE=sde_test;USER=sa;PASSWORD=sa;VERSION=dbo.DEFAULT</para> /// <para>②sql: server=localhost;uid=sa;pwd=sa;database=myDatabase</para> /// <para>③oleDb: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=x:\xxx.mdb;User Id=admin;Password=xxx;</para> /// </summary> /// <param name="eType">要打开的工作空间类别</param> /// <param name="connnectString">连接字符串</param> /// <returns></returns> public static IWorkspace GetWorksapceFromConnStr(string connnectString, EWorkspaceType eType) { try { IWorkspaceName workspaceName = new WorkspaceNameClass(); workspaceName.WorkspaceFactoryProgID = eType.GetDescription(1); workspaceName.ConnectionProperties = ConnectStringToPropetySet(connnectString); IName iName = (IName)workspaceName; return(iName.Open() as IWorkspace); } catch (Exception ex) { throw new Exception($"打开{eType.GetDescription(2)}工作空间“{connnectString}”出错:{ex.Message}"); } }
/// <summary> /// 创建工作空间(地理数据库) /// </summary> /// <param name="eType">工作空间类别</param> /// <param name="parentDirectory"> /// 工作空间所在的父目录,具体为: /// <para>①shp文件所在文件夹再上一层目录,e.g. c:\gis;</para> /// <para>②mdb文件所在文件夹,e.g. c:\gis;</para> /// <para>③dwg文件所在文件夹,e.g. c:\gis;</para> /// <para>④gdb文件夹的上一层目录,e.g. c:\gis;</para> /// </param> /// <param name="strWorkspaceName"> /// 工作空间名称,具体为: /// <para>①shp文件所在文件夹,e.g. testShpFolder;</para> /// <para>②mdb文件名,e.g. testMdbFile(.mdb);</para> /// <para>③dwg文件所在文件夹,e.g. testCadFolder;</para> /// <para>④gdb文件夹名,e.g. testGdb(.gdb);</para> /// </param> /// <returns></returns> public static IWorkspace NewWorkspace(EWorkspaceType eType, string parentDirectory, string strWorkspaceName) { if (eType == EWorkspaceType.Default) { eType = GetDefaultWorkspaceType(strWorkspaceName); } var extension = System.IO.Path.GetExtension(strWorkspaceName); if (!string.IsNullOrEmpty(extension)) { strWorkspaceName = strWorkspaceName.Replace(extension, ""); } switch (eType) { case EWorkspaceType.ShapeFile: return(NewWorkspace(new ShapefileWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.FileGDB: return(NewWorkspace(new FileGDBWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.Access: return(NewWorkspace(new AccessWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.CAD: return(NewWorkspace(new CadWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.InMemory: return(NewWorkspace(new InMemoryWorkspaceFactoryClass(), "", strWorkspaceName)); case EWorkspaceType.Sde: return(NewWorkspace(new SdeWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.Excel: return(NewWorkspace(new ExcelWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.TextFile: return(NewWorkspace(new TextFileWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.OleDb: return(NewWorkspace(new OLEDBWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.Raster: return(NewWorkspace(new RasterWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.Sql: return(NewWorkspace(new SqlWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); default: throw new ArgumentException($"未实现创建{eType.GetDescription()}的操作,请联系系统管理员"); } }
/// <summary> /// 创建工作空间(地理数据库) /// </summary> /// <param name="eType">工作空间类别</param> /// <param name="parentDirectory"> /// 工作空间所在的父目录,具体为: /// ①shp文件所在文件夹再上一层目录,e.g. c:\gis; /// ②mdb文件所在文件夹,e.g. c:\gis; /// ③dwg文件所在文件夹,e.g. c:\gis; /// ④gdb文件夹的上一层目录,e.g. c:\gis; /// </param> /// <param name="strWorkspaceName"> /// 工作空间名称,具体为: /// ①shp文件所在文件夹,e.g. testShpFolder; /// ②mdb文件名,e.g. testMdbFile(.mdb);//去掉扩展名 /// ③dwg文件所在文件夹,e.g. testCadFolder; /// ④gdb文件夹名,e.g. testGdb.gdb; /// </param> /// <returns></returns> public static IWorkspace NewWorkspace(EWorkspaceType eType, string parentDirectory, string strWorkspaceName) { if (eType == EWorkspaceType.Default) { eType = GetDefaultWorkspaceType(strWorkspaceName); } switch (eType) { case EWorkspaceType.ShapeFile: return(NewWorkspace(new ShapefileWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.FileGDB: return(NewWorkspace(new FileGDBWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.Access: return(NewWorkspace(new AccessWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); case EWorkspaceType.CAD: return(NewWorkspace(new CadWorkspaceFactoryClass(), parentDirectory, strWorkspaceName)); default: throw new ArgumentException($"未实现创建{eType.GetDescription()}的操作,请联系系统管理员"); } }
/// <summary> /// 校验并返回与数据源一致的条件查询语句 /// <para>例如eType == <see cref="EWorkspaceType.Access"/>,条件查询语句为 BH like '440101%',则返回结果为 BH like '440101*'</para> /// </summary> /// <param name="eType">数据源类型</param> /// <param name="whereClause">需要校验的条件查询语句</param> public static string ValidateWhereClause(EWorkspaceType eType, string whereClause) => TableEx.ValidateWhereClause(eType, whereClause);
/// <summary> /// 校验并返回与数据源一致的条件查询语句 /// <para>例如eType == <see cref="EWorkspaceType.Access"/>,条件查询语句为 BH like '440101%',则返回结果为 BH like '440101*'</para> /// </summary> /// <param name="eType">数据源类型</param> /// <param name="whereClause">需要校验的条件查询语句</param> public static string ValidateWhereClause(EWorkspaceType eType, string whereClause) { return(TableEx.ValidateWhereClause(eType, whereClause)); }