using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using KGIS.Framework.AE; using KGIS.Framework.AE.Enum; using KGIS.Framework.Maps; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Model; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; namespace kingo.Plugin.BGResultManager.Utility.Metadata { public class ExportMetadata { //这个写死了 白明雅 //public string Scale { get; set; } public ScaleCodeTable Scale { get; set; } public string OutPath { get; set; } public bool IsBG { get; set; } = false; private IHookHelper m_hookHelper = null; private double XMin = double.MaxValue; private double YMin = double.MaxValue; private double XMax = double.MinValue; private double YMax = double.MinValue; private string CoordinateSystemName; public ExportMetadata() { m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = MapsManager.Instance.MapService.Hook; } /// /// 王冠杰2020-03-04 增加参数string BGYear /// /// public void Export(string BGYear) { try { if (string.IsNullOrWhiteSpace((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE) || (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE.Length != 6) { //MessageBox.Show("工程属性中的行政区代码不正确,请检查修改!", "系统提示:", MessageBoxButton.OK); return; } if (string.IsNullOrWhiteSpace((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode)) { //MessageBox.Show("工程属性中的未指定比例尺,请检查修改!", "系统提示:", MessageBoxButton.OK); return; } string MetadataTemplateName = null; if (IsBG) { MetadataTemplateName = "第三次全国国土调查统一时点更新变更元数据模板.xml"; //MetadataTemplateName = "三调变更元数据模板.xml"; } else { MetadataTemplateName = "第三次全国国土调查统一时点更新成果元数据模板.xml"; //MetadataTemplateName = "三调元数据模板.xml"; } string templatePath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath(), "工作空间", "模板", "元数据", MetadataTemplateName); //string savePath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ProjDir; //白明雅 2019-03-05 xml名称前6位行政区编号不补0 //string metaDataFileName = "2001" + Scale + DateTime.Now.Year + (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "000000.xml"; string metaDataFileName = "2001" + Scale.ScaleCode + BGYear + (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + ".xml"; if (!File.Exists(templatePath)) { //MessageBox.Show("缺少模板:" + templatePath, "系统提示:", MessageBoxButton.OK); return; } if (!Directory.Exists(OutPath)) { Directory.CreateDirectory(OutPath); } string filePath = System.IO.Path.Combine(OutPath, metaDataFileName); if (File.Exists(filePath)) { ////白明雅 2018-10-29 test //if (MessageBox.Show("文件已经存在,是否覆盖?", "系统提示:", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel) //{ // return; //} } File.Copy(templatePath, filePath, true); GetSpatialReference(); InitData(filePath); //CustomLogAPI.WriteSystemOperationLog("数据新增", "生成元数据"); //白明雅 2018-10-29 test //MessageBox.Show("元数据生成成功!\r\n" + filePath, "系统提示:", MessageBoxButton.OK); } catch (Exception ex) { LogAPI.Debug("生成元数据失败,可能的原因是:" + ex.Message); //MessageBox.Show("生成元数据失败:" + ex.Message, "系统提示:", MessageBoxButton.OK); } } public void Export() { try { if (string.IsNullOrWhiteSpace((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE) || (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE.Length != 6) { //MessageBox.Show("工程属性中的行政区代码不正确,请检查修改!", "系统提示:", MessageBoxButton.OK); return; } if (string.IsNullOrWhiteSpace((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ScaleCode)) { //MessageBox.Show("工程属性中的未指定比例尺,请检查修改!", "系统提示:", MessageBoxButton.OK); return; } string MetadataTemplateName = null; if (IsBG) { MetadataTemplateName = "三调变更元数据模板.xml"; } else { MetadataTemplateName = "三调元数据模板.xml"; } string templatePath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath(), "工作空间", "模板", "元数据", MetadataTemplateName); //string savePath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ProjDir; //白明雅 2019-03-05 xml名称前6位行政区编号不补0 //string metaDataFileName = "2001" + Scale + DateTime.Now.Year + (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + "000000.xml"; string metaDataFileName = "2001" + Scale.ScaleCode + DateTime.Now.Year + (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).CODE + ".xml";//2020.03.30 王冠杰修改 if (!File.Exists(templatePath)) { //MessageBox.Show("缺少模板:" + templatePath, "系统提示:", MessageBoxButton.OK); return; } if (!Directory.Exists(OutPath)) { Directory.CreateDirectory(OutPath); } string filePath = System.IO.Path.Combine(OutPath, metaDataFileName); if (File.Exists(filePath)) { ////白明雅 2018-10-29 test //if (MessageBox.Show("文件已经存在,是否覆盖?", "系统提示:", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel) //{ // return; //} } File.Copy(templatePath, filePath, true); GetSpatialReference(); InitData(filePath); //CustomLogAPI.WriteSystemOperationLog("数据新增", "生成元数据"); //白明雅 2018-10-29 test //MessageBox.Show("元数据生成成功!\r\n" + filePath, "系统提示:", MessageBoxButton.OK); } catch (Exception ex) { LogAPI.Debug("生成元数据失败,可能的原因是:" + ex.Message); //MessageBox.Show("生成元数据失败:" + ex.Message, "系统提示:", MessageBoxButton.OK); } } /// /// 初始化元数据 /// /// private void InitData(string filePath) { try { XDocument xDoc = XDocument.Load(filePath); XElement root = xDoc.Root; foreach (XElement xElement in root.Elements()) { if (xElement.Name.LocalName.Equals("标识信息")) { CreateScale(xElement); } if (xElement.Name.LocalName.Equals("空间参照系统信息")) { CreateGeodeticCoordinate(xElement); } if (xElement.Name.LocalName.Equals("内容信息")) { CreateContentDescription(xElement); } ForEachMetaData(xElement, null); } xDoc.Save(filePath); } catch (Exception ex) { LogAPI.Debug(ex); throw; } } /// /// 递归遍历节点 /// /// private void ForEachMetaData(XElement xElement, XElement parentXElement) { foreach (XElement xElementNode in xElement.Elements()) { int i = xElementNode.Elements().Count(); if (i > 0) { ForEachMetaData(xElementNode, xElement); } else { SetValue(xElementNode, xElement); } } } /// /// 创建描述内容 /// /// private void CreateContentDescription(XElement xElement) { int i = 0; string ConfigFilePath = null; if (IsBG) { ConfigFilePath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath(), "Configs", "BGCheckTableMappingConfig.xml"); if (!File.Exists(ConfigFilePath)) { CreateBGCheckTableMappingConfig(ConfigFilePath); } } else { ConfigFilePath = System.IO.Path.Combine(SysAppPath.GetCurrentAppPath(), "Configs", "CheckTableMappingConfig.xml"); } XDocument xDoc = XDocument.Load(ConfigFilePath, LoadOptions.None); //XElement root = xDoc.Root; foreach (XElement table in xDoc.Descendants("Table")) { XElement contentDescription = new XElement("MD_内容描述"); XElement layerName = new XElement("图层名称"); if (table.Attribute("Name") != null && !string.IsNullOrWhiteSpace(table.Attribute("Name").Value)) { if (!table.Attribute("Name").Value.EndsWith("注记")) { layerName.Value = table.Attribute("Name").Value; } else { if (i == 0) { layerName.Value = "注记"; i++; } else { continue; } } } XElement ysdm = new XElement("要素类型名称"); ysdm.Value = layerName.Value; XElement propertyList = new XElement("属性列表"); foreach (XElement property in table.Elements()) { if (property.Attribute("ExtendField") == null || !property.Attribute("ExtendField").Value.Equals("True")) { propertyList.Add(new XElement("字段名称", property.Attribute("AliasName").Value)); } } contentDescription.Add(layerName); contentDescription.Add(ysdm); contentDescription.Add(propertyList); xElement.Add(contentDescription); } } /// /// 设置比例尺 /// /// private void CreateScale(XElement xElement) { foreach (XElement item in xElement.Descendants("西边经度")) { item.SetValue(XMin); } foreach (XElement item in xElement.Descendants("东边经度")) { item.SetValue(XMax); } foreach (XElement item in xElement.Descendants("南边纬度")) { item.SetValue(YMin); } foreach (XElement item in xElement.Descendants("北边纬度")) { item.SetValue(YMax); } foreach (XElement item in xElement.Descendants("调查比例尺")) { item.SetValue((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).Scale);//.Replace(":", "/")); } foreach (XElement item in xElement.Descendants("空间分辨率")) { item.SetValue((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).Scale.Split(':')[1]); } } /// /// 设置空间参照系统信息 /// /// private void CreateGeodeticCoordinate(XElement xElement) { foreach (XElement item in xElement.Descendants("大地坐标参照系统名称")) { item.SetValue("CGCS2000"); //item.SetValue((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).GetPRJName()); } foreach (XElement item in xElement.Descendants("坐标系统类型")) { item.SetValue("投影平面直角坐标系"); } foreach (XElement item in xElement.Descendants("坐标系统名称")) { item.SetValue("高斯-克吕格投影(" + (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).FDD + "度带)"); //item.SetValue(CoordinateSystemName); } foreach (XElement item in xElement.Descendants("投影坐标系统参数")) { item.SetValue((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).FDD); } foreach (XElement item in xElement.Descendants("垂向坐标参照系统名称")) { item.SetValue("1985国家高程基准"); } } /// /// 写入日期,版本信息 /// /// /// private void SetValue(XElement xElement, XElement parentXElement) { try { switch (xElement.Name.LocalName) { case "日期": xElement.SetValue(DateTime.Now.ToShortDateString()); break; case "版本": if (parentXElement.Name.LocalName.Equals("CI_引用")) { xElement.SetValue(DateTime.Now.Year.ToString() + "年版本"); } break; case "": break; } } catch (Exception) { throw; } } /// /// 获取坐标系 /// /// //public void GetSpatialReference(IFeatureWorkspace pFeatureWorkSpace) public void GetSpatialReference() { IFeatureClass pFeatureClass = null; ISpatialReference sr = null; IEnvelope env = null; IProjectedCoordinateSystem project = null; try { List allLayers = MapsManager.Instance.MapService.GetAllLayerInMap();// LayerHelper.GetAllLayerInMap(m_hookHelper.FocusMap); foreach (ILayer pLayer in allLayers) { if ((pLayer as IFeatureLayer) == null || (pLayer as IFeatureLayer).FeatureClass == null) { continue; } pFeatureClass = (pLayer as IFeatureLayer).FeatureClass; if (pFeatureClass.FeatureCount(null) <= 0) { continue; } IGeoDataset gd = pFeatureClass as IGeoDataset; if (gd == null) { continue; } env = gd.Extent; project = gd.SpatialReference as IProjectedCoordinateSystem; if (env == null || env.IsEmpty || project == null) { continue; } if (project.GeographicCoordinateSystem == null) { continue; } env.Project(project.GeographicCoordinateSystem); if (XMin > env.XMin) { XMin = env.XMin; } if (YMin > env.YMin) { YMin = env.YMin; } if (XMax < env.XMax) { XMax = env.XMax; } if (YMax < env.YMax) { YMax = env.YMax; } if (sr == null) { sr = gd.SpatialReference; } } IESRISpatialReferenceGEN2 prjsr = sr as IESRISpatialReferenceGEN2; string spatialrefstr = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).PrjFileStr; //if (!File.Exists((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).PrjFilePath)) //{ // List listPrjPath = (MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).PrjFilePath.Split(new string[] { "\\Coordinate Systems\\" }, StringSplitOptions.RemoveEmptyEntries).ToList(); // if (listPrjPath.Count > 0) // { // string prjPath = System.IO.Path.Combine(KGIS.Config.SysAppPath.GetCurrentAppPath(), "Coordinate Systems", listPrjPath[1]); // if (File.Exists(prjPath)) // { // spatialrefstr = File.ReadAllText(prjPath); // } // else // { // throw new Exception("获取工程坐标参考信息异常!"); // } // } // else // { // throw new Exception("获取工程坐标参考信息异常!"); // } //} //else //{ // spatialrefstr = File.ReadAllText((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).PrjFilePath); //} string beginstr = "PROJECTION["; int leftindex = spatialrefstr.IndexOf(beginstr) + beginstr.Length; string spatialrefrightstr = spatialrefstr.Substring(leftindex); int length = spatialrefrightstr.IndexOf("]"); CoordinateSystemName = spatialrefrightstr.Substring(0, length).Replace("\"", ""); } catch (Exception ex) { throw ex; } } private void CreateBGCheckTableMappingConfig(string configFilePath) { IWorkspaceAPI s_WsAPI = null; try { //创建XmlDocument对象 XmlDocument xmlDoc = new XmlDocument(); //XML的声明 XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); //追加xmldecl位置 xmlDoc.AppendChild(xmlSM); //创建CheckTableMapping节点 XmlElement MainElement = xmlDoc.CreateElement("", "CheckTableMapping", ""); //追加CheckTableMapping的根节点位置 xmlDoc.AppendChild(MainElement); string currentGDBPath = System.IO.Path.Combine((MapsManager.Instance.MapService.GetProjectInfo() as ProjectInfo).ProjDir, "工程数据.gdb"); s_WsAPI = new WorkspaceAPI(currentGDBPath,WorkspaceTypeEnum.GDBFile); Dictionary s_FcList = s_WsAPI.GetFeatureClassName(esriDatasetType.esriDTFeatureDataset); if (s_FcList.Count > 0) { foreach (KeyValuePair kvp in s_FcList) { IFeatureClassAPI fcSourceAPI = s_WsAPI.OpenFeatureClass(kvp.Value); IFeatureClass fcSource = fcSourceAPI.FeatureClass; FeatureClass fc = fcSource as FeatureClass; if (fcSource.FeatureDataset != null && fcSource.FeatureDataset.Name == "变更成果") { XmlElement TableElement = xmlDoc.CreateElement("", "Table", ""); TableElement.SetAttribute("TableName", fc.Name); TableElement.SetAttribute("Name", fcSource.AliasName); TableElement.SetAttribute("Type", GetShapeTypeName(fcSource)); TableElement.SetAttribute("IsNecessary", "True"); TableElement.SetAttribute("YSDM", GetYSDMByLayerName(fc.Name)); IFields fields = fcSource.Fields; for (int i = 0; i < fields.FieldCount; i++) { IField field = fields.Field[i]; if (field.Name == "OBJECTID" || field.Name == "SHAPE" || field.Name == "SHAPE_Length" || field.Name == "SHAPE_Area") { continue; } XmlElement FieldElement = xmlDoc.CreateElement("", "Column", ""); FieldElement.SetAttribute("Name", field.Name); FieldElement.SetAttribute("AliasName", field.AliasName); FieldElement.SetAttribute("AllowDBNull", field.IsNullable ? "True" : "False"); FieldElement.SetAttribute("DataType", GetFieldTypeName(field.Type)); if (field.Type == esriFieldType.esriFieldTypeString) { FieldElement.SetAttribute("MaxLength", field.Length.ToString()); } if (field.Type == esriFieldType.esriFieldTypeSingle || field.Type == esriFieldType.esriFieldTypeDouble) { FieldElement.SetAttribute("Precision", field.Precision.ToString()); } TableElement.AppendChild(FieldElement); } MainElement.AppendChild(TableElement); } } } xmlDoc.Save(configFilePath); } catch (Exception ex) { LogAPI.Debug(ex); } finally { if (s_WsAPI != null) { s_WsAPI.CloseWorkspace(); } } } private Dictionary ysdm = null; public Dictionary YSDM { get { if (ysdm == null) { ysdm = new Dictionary(); ysdm.Add("1000110000", "CLKZD"); ysdm.Add("1000110408", "JZKZD"); ysdm.Add("1000119000", "CLKZDZJ"); ysdm.Add("1000600100", "XZQ"); ysdm.Add("1000600200", "XZQJX"); ysdm.Add("1000609000", "XZQZJ"); ysdm.Add("1000600400", "CJDCQ"); ysdm.Add("1000600500", "CJDCQJX"); ysdm.Add("1000608000", "CJDCQZJ"); ysdm.Add("1000710000", "DGX"); ysdm.Add("1000720000", "GCZJD"); ysdm.Add("1000780000", "PDT"); ysdm.Add("2001010100", "DLTB"); ysdm.Add("2001010200", "DLTBZJ"); ysdm.Add("2006010100", "TDQSQ"); ysdm.Add("2006010200", "TDQSQZJ"); ysdm.Add("2006020100", "TDQSQJX"); ysdm.Add("2006020200", "TDQSQJXZJ"); ysdm.Add("2005010300", "YJJBNTTB"); ysdm.Add("2005010900", "YJJBNTTBZJ"); ysdm.Add("2099010100", "LSYD"); ysdm.Add("2099010200", "LSYDZJ"); ysdm.Add("2099020100", "PZWJSTD"); ysdm.Add("2099020200", "PZWJSTBZJ"); ysdm.Add("2099030100", "CZCDYD"); ysdm.Add("2099030200", "CZCDYDZJ"); ysdm.Add("2099040100", "GDDB"); ysdm.Add("2099040200", "GDDBZJ"); ysdm.Add("2099050100", "ZYXMYD"); ysdm.Add("2099050200", "ZYXMYDZJ"); ysdm.Add("2099060100", "KFYQ"); ysdm.Add("2099060200", "KFYQZJ"); ysdm.Add("3001010000", "GJGY"); ysdm.Add("3001020000", "ZRBHQ"); ysdm.Add("3001030000", "SLGY"); ysdm.Add("3001040000", "FJMSQ"); ysdm.Add("3001050000", "DZGY"); ysdm.Add("3001060000", "ZRYCBHQ"); ysdm.Add("3001070000", "SDGY"); ysdm.Add("3001080000", "YYSSYD"); ysdm.Add("3001090000", "SCZZBHQ"); ysdm.Add("3001990000", "QTJZKFQ"); ysdm.Add("3001200000", "ZRBHQZJ"); ysdm.Add("3002010000", "CSKFBJ"); ysdm.Add("3002020000", "CSKFBJZJ"); ysdm.Add("2099110100", "WJMHD"); ysdm.Add("2099100100", "LMFW"); } return ysdm; } } public string GetShapeTypeName(IFeatureClass fc) { string strType = ""; switch (fc.ShapeType) { case esriGeometryType.esriGeometryPoint: strType = "点"; break; case esriGeometryType.esriGeometryLine: strType = "线"; break; case esriGeometryType.esriGeometryPolygon: strType = "面"; break; default: if (fc.FeatureType == esriFeatureType.esriFTAnnotation) { strType = "注记"; } else { strType = "其他"; } break; } return strType; } private string GetYSDMByLayerName(string LayerName) { if (LayerName.EndsWith("GX")) { LayerName = LayerName.Replace("GX", ""); } else if (LayerName.EndsWith("GXGC")) { LayerName = LayerName.Replace("GXGC", ""); } KeyValuePair kvp = YSDM.FirstOrDefault(x => x.Value == LayerName); return kvp.Key; } private string GetFieldTypeName(esriFieldType fieldType) { string strTypeName = ""; switch (fieldType) { case esriFieldType.esriFieldTypeSmallInteger: strTypeName = "Int"; break; case esriFieldType.esriFieldTypeInteger: strTypeName = "Int"; break; case esriFieldType.esriFieldTypeSingle: strTypeName = "Float"; break; case esriFieldType.esriFieldTypeDouble: strTypeName = "Double"; break; case esriFieldType.esriFieldTypeString: strTypeName = "String"; break; case esriFieldType.esriFieldTypeDate: strTypeName = "Date"; break; case esriFieldType.esriFieldTypeOID: strTypeName = "Int"; break; case esriFieldType.esriFieldTypeGeometry: break; case esriFieldType.esriFieldTypeBlob: break; case esriFieldType.esriFieldTypeRaster: break; case esriFieldType.esriFieldTypeGUID: break; case esriFieldType.esriFieldTypeGlobalID: break; case esriFieldType.esriFieldTypeXML: break; default: break; } return strTypeName; } } }