using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.DataSourcesGDB; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; using Kingo.Plugin.ShapeToKOApp.KoDataBase; using Kingo.Plugin.ShapeToKOApp.XSDClass; using NetTopologySuite.IO; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Management; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Threading; namespace Kingo.Plugin.ShapeToKOApp { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); GdalConfiguration.ConfigureGdal(); GdalConfiguration.ConfigureOgr(); if (!KGIS.Framework.AE.AELicense.AoInit()) { return; } // args = new string[] { @"G:\余杭无人机任务制作\余杭区(330110)\工程目录\举证任务\BGDB.gdb\变更数据\JCTB", "JCTB", "null", @"G:\", "null" }; //args = new string[] { @"F:\研发产品文档\02年度变更外业调绘助手\余杭区全部数据shape\测试数据\监测图斑.shp", "监测图斑",@"G:\余杭无人机任务制作\余杭区(330110)\外业调绘成果\20221212101704\Kingoit\phonemap_wydh\projects\县级数据\Source","(SFWY = '1' and(RWDCZT is null or RWDCZT = ''))", "null", "null", "null", "县级数据" }; //args = new string[] { @"F:\研发产品文档\02年度变更外业调绘助手\余杭区全部数据shape\测试数据\监测图斑.shp", "监测图斑", @"G:\余杭无人机任务制作\余杭区(330110)\外业调绘成果\20221212134757\Kingoit\phonemap_wydh\projects\县级数据\Source", "(SFWY='1' and (RWDCZT is null or RWDCZT=''))", "" ,"" ,"" ,"县级数据" }; if (args.Length != 8) { Console.WriteLine("生成KO文件失败,参数错误"); return; } string strLayerPath = args[0].ToString(); string strLayerName = args[1].ToString(); string strOutputPath = args[2].ToString(); string strWhere = args[3].ToString(); string customLayerPath = args[4].ToString(); string customLayerName = args[5].ToString(); string customstrWhere = args[6].ToString(); string customKeyValue = args[7].ToString(); IGeometry pGeometry = null; if (strWhere == "null") strWhere = string.Empty; if (!string.IsNullOrWhiteSpace(customLayerPath) && customLayerPath != "null" && !string.IsNullOrWhiteSpace(customLayerName) && customLayerName != "null") { IFeatureLayer pCuatomLayer = GetFeatureLayer(customLayerPath, customLayerName); if (pCuatomLayer != null && pCuatomLayer.FeatureClass != null) { pGeometry = CustomGeometry(pCuatomLayer.FeatureClass, customstrWhere, customKeyValue); } } IFeatureLayer pFeatureLayer = GetFeatureLayer(strLayerPath, strLayerName); if (pFeatureLayer == null) { Console.WriteLine(string.Format("生成KO文件失败,获取图层信息异常")); return; } KODBFeatureLayer koFeatureLayer = new KODBFeatureLayer(); ISpatialDataSource pSpatialDataSource = new ArcFeatureDataSource(pFeatureLayer as IFeatureLayer, pGeometry, strWhere); //shp字段 koFeatureLayer.FieldInfos = pSpatialDataSource.Fields; //设置样式 koFeatureLayer.Render = SymbolConvert.Instance().GetRenderer((pFeatureLayer as IGeoFeatureLayer).Renderer); koFeatureLayer.ItemInfoRender = SymbolConvert.Instance().GetItemInfoRenderer((pFeatureLayer as IGeoFeatureLayer).Renderer); ////最小比例尺 //koFeatureLayer.MinScale = (int)pFeatureLayer.MinimumScale; ////最大比例尺 //koFeatureLayer.MaxScale = (int)pFeatureLayer.MaximumScale; //图层标注 IGeoFeatureLayer geolayer = pFeatureLayer as IGeoFeatureLayer; if (geolayer != null) { IElementCollection ec; IAnnotateLayerProperties pAnnoLayerProps; geolayer.AnnotationProperties.QueryItem(0, out pAnnoLayerProps, out ec, out ec); ILabelEngineLayerProperties pLabelEngineLayerProps = (ILabelEngineLayerProperties)pAnnoLayerProps; if (pLabelEngineLayerProps != null) { if (!string.IsNullOrWhiteSpace(pLabelEngineLayerProps.Expression) && geolayer.DisplayAnnotation) { koFeatureLayer.LayerLabelName = pLabelEngineLayerProps.Expression.Substring(1, pLabelEngineLayerProps.Expression.Length - 2); } else { koFeatureLayer.LayerLabelName = ""; } } } //设置显示字段 koFeatureLayer.DisplayField = (pFeatureLayer as IFeatureLayer).DisplayField; LabelStyleConvert styleConvert = new LabelStyleConvert(); List labelInfo = styleConvert.EsriLabelToCustomLabels((pFeatureLayer as IGeoFeatureLayer).AnnotationProperties); if (labelInfo != null && labelInfo.Count > 0) koFeatureLayer.LabelingInfo = labelInfo[0]; //设置labelinginfo koFeatureLayer.EncryptData = true; koFeatureLayer.WKID = (pFeatureLayer as IGeoDataset).SpatialReference.FactoryCode; string fileName = (pFeatureLayer as IDataset).BrowseName; koFeatureLayer.OutputFileName = strOutputPath + "\\" + fileName + ".ko"; koFeatureLayer.SpatialDataSource = pSpatialDataSource; LayerComm layerComm = new LayerComm(); koFeatureLayer.ItemInfo = layerComm.FeatureLayerToEntity(pFeatureLayer as IFeatureLayer); if (koFeatureLayer.Render is XSDClass.SimpleRenderer) koFeatureLayer.FeatureSymbol = (koFeatureLayer.Render as XSDClass.SimpleRenderer).Symbol; else if (koFeatureLayer.Render is XSDClass.UniqueValueRenderer) { XSDClass.UniqueValueRenderer uniqueValueRander = koFeatureLayer.Render as XSDClass.UniqueValueRenderer; if (uniqueValueRander != null) koFeatureLayer.FeatureSymbol = uniqueValueRander.DefaultSymbol; } DateTime dt1 = DateTime.Now; koFeatureLayer.Report += (s, c) => { if (koFeatureLayer == null) return; if (s == "插入数据完成!" || c == -1) { DateTime dt3 = DateTime.Now; if (c == -1) { Console.WriteLine(s); } else { Console.WriteLine("完成,共用时" + dt3.Subtract(dt1).TotalSeconds.ToString("0.0") + "秒"); } if (koFeatureLayer.SpatialDataSource.RecordCount == 0) { string msg = string.Format("{0};{1}", 100, 100); Console.WriteLine(msg); } else { string msg = string.Format("{0};{1}", koFeatureLayer.SpatialDataSource.RecordCount, koFeatureLayer.SpatialDataSource.RecordCount); Console.WriteLine(msg); } } else { string msg = string.Format("{0};{1};{2}", s,c, koFeatureLayer.SpatialDataSource.RecordCount); Console.WriteLine(msg); } }; koFeatureLayer.CreateLayerUdp(); } static IFeatureLayer GetFeatureLayer(string layerPath, string layerName) { IWorkspaceFactory pWSFactory = null; IFeatureLayer pFeatureLayer = null; try { if (!string.IsNullOrWhiteSpace(layerPath)) { string filename = System.IO.Path.GetDirectoryName(layerPath); if (layerPath.ToLower().Contains(".shp")) { pWSFactory = new ShapefileWorkspaceFactoryClass(); } else if (layerPath.ToLower().Contains(".gdb")) { pWSFactory = new FileGDBWorkspaceFactory(); string fileExtension = System.IO.Path.GetExtension(filename); if (string.IsNullOrWhiteSpace(fileExtension)) { filename = System.IO.Path.GetDirectoryName(filename); } } else if (layerPath.ToLower().Contains(".mdb")) { pWSFactory = new AccessWorkspaceFactory(); string fileExtension = System.IO.Path.GetExtension(filename); if (string.IsNullOrWhiteSpace(fileExtension)) { filename = System.IO.Path.GetDirectoryName(filename); } } IWorkspace pWorkspace = pWSFactory.OpenFromFile(filename, 0); System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSFactory); pWSFactory = null; IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFeatureClass pSourceFeatureClass = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(layerPath)); if (pSourceFeatureClass != null) { IFeatureLayer layer = new FeatureLayerClass(); layer.FeatureClass = pSourceFeatureClass; pFeatureLayer = layer; } } } catch (Exception ex) { Console.WriteLine("获取图层失败:" + ex.Message); return null; } return pFeatureLayer; } static ESRI.ArcGIS.Geometry.IGeometry CustomGeometry(IFeatureClass pCustomFC, string strWhere, string keyValue) { IGeometry xzqGeo = null; try { ESRI.ArcGIS.Geodatabase.IFeatureCursor featureCursor = pCustomFC.Search(new ESRI.ArcGIS.Geodatabase.QueryFilterClass() { WhereClause = string.Format(strWhere, keyValue) }, false); if (featureCursor == null) return null; ESRI.ArcGIS.Geodatabase.IFeature feature = featureCursor.NextFeature(); if (feature == null) return null; xzqGeo= feature.ShapeCopy; } catch (Exception ex) { Console.WriteLine("获取自定义范围失败:" + ex.Message); return null; } return xzqGeo; } } }