async public override Task <bool> Refresh() { try { await base.Refresh(); _mapDocument = new MapDocument(); XmlStream stream = new XmlStream(""); stream.ReadStream(_filename); //_mapDocument.LoadMapDocument(_filename); await stream.LoadAsync("MapDocument", _mapDocument); foreach (IMap map in _mapDocument.Maps) { base.AddChildObject(new MapExplorerObject(this, _filename, map)); } return(true); } catch (Exception ex) { string errMsg = ex.Message; return(false); } }
async public Task <bool> LoadMapDocumentAsync(string path) { XmlStream stream = new XmlStream(""); if (stream.ReadStream(path)) { while (_maps.Count > 0) { this.RemoveMap((IMap)_maps.First()); } return(await stream.LoadAsync("MapDocument", this) != null); } return(false); }
async public Task Run(string[] args) { string inFile = String.Empty; string outFile = String.Empty; string command = "info"; int datasetIndex = -1; List <string> parameterNames = new List <string>(); List <string> newParameterValues = new List <string>(); for (int i = 1; i < args.Length - 1; i++) { switch (args[i].ToLower()) { case "-cmd": command = args[++i]; break; case "-mxl": inFile = args[++i]; break; case "-out-mxl": outFile = args[++i]; break; case "-dsindex": case "-dataset-index": datasetIndex = int.Parse(args[++i]); break; case "-parameter-name": case "-parameter": parameterNames.Add(args[++i]); break; case "-new-value": case "-new-parameter-value": newParameterValues.Add(args[++i]); break; } } if (String.IsNullOrEmpty(inFile)) { throw new IncompleteArgumentsException(); } XmlStream stream = new XmlStream(""); stream.ReadStream(inFile); MxlDocument doc = new MxlDocument(); await stream.LoadAsync("MapDocument", doc); var map = doc.Maps.FirstOrDefault() as Map; if (map.HasErrorMessages) { throw new Exception($"Can't load source mxl { inFile }:{ Environment.NewLine }{ String.Join('\n', map.ErrorMessages) }"); } bool saveOutput = false; switch (command) { case "info": DatasetInfo(map); break; case "modify-connectionstring": case "modify-cs": await ModifyConnectionString(map, datasetIndex, parameterNames, newParameterValues); saveOutput = true; break; default: throw new Exception($"Unkown command: { command }"); } if (saveOutput) { stream = new XmlStream(""); stream.Save("MapDocument", doc); Console.WriteLine($"Write: { outFile }"); stream.WriteStream(outFile); Console.WriteLine("succeeded..."); } }
async public Task Run(string[] args) { string inFile = String.Empty; string outFile = String.Empty; string targetConnectionString = String.Empty; IEnumerable <string> dontCopyFeatues = null; Guid targetGuid = new Guid(); for (int i = 1; i < args.Length - 1; i++) { switch (args[i].ToLower()) { case "-mxl": inFile = args[++i]; break; case "-target-connectionstring": targetConnectionString = args[++i]; break; case "-target-guid": var guid = args[++i]; switch (guid.ToLower()) { case "sqlserver": targetGuid = new Guid("3B870AB5-8BE0-4a00-911D-ECC6C83DD6B4"); break; case "postgres": targetGuid = new Guid("33254063-133D-4b17-AAE2-46AF7A7DA733"); break; case "sqlite": targetGuid = new Guid("36DEB6AC-EA0C-4B37-91F1-B2E397351555"); break; default: targetGuid = new Guid(guid); break; } break; case "-out-mxl": outFile = args[++i]; break; case "-dont-copy-features-from": dontCopyFeatues = args[++i].Split(',').Select(n => n.Trim().ToLower()); break; } } if (String.IsNullOrEmpty(inFile) || String.IsNullOrEmpty(targetConnectionString) || targetGuid.Equals(new Guid())) { throw new IncompleteArgumentsException(); } if (String.IsNullOrEmpty(outFile)) { outFile = String.IsNullOrEmpty(inFile) ? String.Empty : inFile.Substring(0, inFile.LastIndexOf(".")) + "_fdb.mxl"; } XmlStream stream = new XmlStream(""); stream.ReadStream(inFile); MxlDocument doc = new MxlDocument(); await stream.LoadAsync("MapDocument", doc); var pluginManager = new PlugInManager(); #region Destination Dataset IFeatureDataset targetFeatureDataset = pluginManager.CreateInstance(targetGuid) as IFeatureDataset; if (targetFeatureDataset == null) { throw new Exception("Plugin with GUID '" + targetGuid.ToString() + "' is not a feature dataset..."); } await targetFeatureDataset.SetConnectionString(targetConnectionString); await targetFeatureDataset.Open(); var targetDatabase = (IFDBDatabase)targetFeatureDataset.Database; #endregion Destination Dataset var map = doc.Maps.FirstOrDefault() as Map; var featureLayers = map.TOC.Layers.Where(l => l is IFeatureLayer) .Select(l => (IFeatureLayer)l); if (map.Datasets != null) { int datasetId = 0; foreach (var dataset in map.Datasets.ToArray()) { Console.WriteLine(); Console.WriteLine($"Dataset: { dataset.DatasetName }"); Console.WriteLine($" { dataset.GetType() }"); Console.WriteLine("-------------------------------------------------------"); foreach (var dsElement in map.MapElements.Where(e => e.DatasetID == datasetId)) { if (dsElement?.Class == null) { continue; } var featureLayer = featureLayers.Where(l => l.DatasetID == datasetId && l.Class == dsElement.Class) .FirstOrDefault(); if (featureLayer == null) { continue; } Console.WriteLine(); Console.WriteLine($"FeatureLayer: { featureLayer.Title }"); Console.WriteLine($" Class: { dsElement.Class.Name }"); Console.WriteLine($" { dsElement.Class.GetType() }"); Console.WriteLine(); var sourceFc = dsElement.Class as IFeatureClass; if (sourceFc == null) { Console.WriteLine("Class is not a FeatureClass"); continue; } #region Create Target Featureclass (if not exists) string targetFcName = dsElement.Class.Name; if (targetFcName.Contains(".")) { targetFcName = targetFcName.Substring(targetFcName.LastIndexOf(".") + 1); } var targetFc = (await targetFeatureDataset.Element(targetFcName))?.Class as IFeatureClass; if (targetFc != null) { var count = await targetFc.CountFeatures(); if (count > 0) { Console.Write($"Already exists in target fdb ({ count } features)"); } else { if (!await targetDatabase.DeleteFeatureClass(targetFcName)) { throw new Exception($"Can't delete existing (empty) featureclass { targetFcName }"); } } } else { var fcId = await targetDatabase.CreateFeatureClass( targetFeatureDataset.DatasetName, targetFcName, new GeometryDef() { GeometryType = sourceFc.GeometryType, HasM = sourceFc.HasM, HasZ = sourceFc.HasZ, SpatialReference = sourceFc.SpatialReference }, new Fields(sourceFc.Fields.ToEnumerable().Select(f => { if (f != null && f.type == FieldType.ID && f.name.ToUpper().Equals("FDB_OID") == false) // also include original ID Column { return(new Field(f.name, FieldType.integer)); } return(f); }))); if (fcId <= 0) { throw new Exception($"Can't create featureclass { targetFcName }: { targetDatabase.LastErrorMessage }"); } targetFc = (await targetFeatureDataset.Element(targetFcName)).Class as IFeatureClass; if (targetFc == null) { throw new Exception($"Can't load target FeatureClass { targetFcName }"); } var copyFeatures = dontCopyFeatues == null || (!dontCopyFeatues.Contains(sourceFc.Name.ToLower()) && !dontCopyFeatues.Contains(targetFc.Name.ToLower())); if (copyFeatures) { var sIndexDef = new gViewSpatialIndexDef(null, 62); var tree2 = await SpatialIndex2( targetDatabase, sourceFc, sIndexDef); tree2.Trim(); List <long> nids = new List <long>(); foreach (BinaryTree2BuilderNode node in tree2.Nodes) { nids.Add(node.Number); } await((AccessFDB)targetDatabase).ShrinkSpatialIndex(targetFcName, nids); await((AccessFDB)targetDatabase).SetSpatialIndexBounds(targetFcName, "BinaryTree2", tree2.Bounds, tree2.SplitRatio, tree2.MaxPerNode, tree2.maxLevels); await((AccessFDB)targetDatabase).SetFeatureclassExtent(targetFcName, tree2.Bounds); #endregion Create Target Featureclass (if not exists) var featureBag = new List <IFeature>(); IFeature feature = null; int counter = 0; Console.WriteLine("Copy features:"); if (sourceFc is IFeatureClassPerformanceInfo && ((IFeatureClassPerformanceInfo)sourceFc).SupportsHighperformanceOidQueries == false) { using (var memoryFeatureBag = new FeatureBag()) { #region Read all Features to FeatureBag (Memory) // // eg. SDE Multiversion Views are very slow, queiried win OID Filter!! // Console.WriteLine("Source feature class do not support high performance oid quries!"); QueryFilter filter = new QueryFilter() { WhereClause = "1=1" }; filter.AddField("*"); Console.WriteLine("Read all features to memory feature bag..."); using (var featureCursor = await sourceFc.GetFeatures(filter)) { if (featureCursor == null) { throw new Exception($"Can't query features from soure featureclass: { (sourceFc is IDebugging ? ((IDebugging)sourceFc).LastException?.Message : "") }"); } while ((feature = await featureCursor.NextFeature()) != null) { memoryFeatureBag.AddFeature(feature); counter++; if (counter % 10000 == 0) { Console.Write($"...{ counter }"); } } } #endregion Read all Features to FeatureBag (Memory) #region Write to target featureclass Console.WriteLine($"...{ counter }"); Console.WriteLine("copy feature to target feature class"); counter = 0; foreach (BinaryTree2BuilderNode node in tree2.Nodes) { foreach (var memoryFeature in memoryFeatureBag.GetFeatures(node.OIDs)) { memoryFeature.Fields.Add(new FieldValue("$FDB_NID", node.Number)); featureBag.Add(memoryFeature); counter++; if (counter % 10000 == 0) { await Store(targetDatabase, targetFc, featureBag, counter); } } } #endregion Write to target featureclass } GC.Collect(); } else { #region Query all per Oid and Node foreach (BinaryTree2BuilderNode node in tree2.Nodes) { RowIDFilter filter = new RowIDFilter(sourceFc.IDFieldName); filter.IDs = node.OIDs; filter.SubFields = "*"; using (var featureCursor = await sourceFc.GetFeatures(filter)) { if (featureCursor == null) { throw new Exception($"Can't query features from soure featureclass: { (sourceFc is IDebugging ? ((IDebugging)sourceFc).LastException?.Message : "") }"); } while ((feature = await featureCursor.NextFeature()) != null) { feature.Fields.Add(new FieldValue("$FDB_NID", node.Number)); featureBag.Add(feature); counter++; if (counter % 10000 == 0) { await Store(targetDatabase, targetFc, featureBag, counter); } } } } #endregion Query all per Oid and Node } await Store(targetDatabase, targetFc, featureBag, counter); await((AccessFDB)targetDatabase).CalculateExtent(targetFcName); } } dsElement.Title = targetFc.Name; ((DatasetElement)dsElement).Class = targetFc; } ((MapPersist)map).SetDataset(datasetId, targetFeatureDataset); datasetId++; } } map.Compress(); stream = new XmlStream(""); stream.Save("MapDocument", doc); Console.WriteLine($"Write: { outFile }"); stream.WriteStream(outFile); Console.WriteLine("succeeded..."); }
async public Task LoadMapDocument(string filename) { if (_doc == null || filename == "") { return; } _docFilename = filename; System.IO.FileInfo fi = new System.IO.FileInfo(filename); if (!fi.Exists) { return; } if (fi.Extension.ToLower() != ".axl") { RemoveAllDataViews(); if (_appWindow != null) { _appWindow.RemoveAllToolbars(); } _activeDataView = null; } XmlStream stream = new XmlStream(""); if (fi.Extension.ToLower() == ".rdm") { StreamReader sr = new StreamReader(filename); byte[] bytes = new byte[fi.Length]; BinaryReader br = new BinaryReader(sr.BaseStream); br.Read(bytes, 0, bytes.Length); sr.Close(); bytes = Crypto.Decrypt(bytes, _cryptoKey); MemoryStream ms = new MemoryStream(bytes); stream.ReadStream(ms); ms.Close(); } else { stream.ReadStream(filename); } while (_doc.Maps.Count() > 0) { _doc.RemoveMap(_doc.Maps.First()); } _dataViews.Clear(); await stream.LoadAsync("MapDocument", _doc); // Load DataViews... DataView dv; while ((dv = (DataView)stream.Load("DataView", null, new DataView(_doc.Maps))) != null) { if (!(dv.Map is Map)) { continue; } DataView dataView = _appWindow.AddDataView((Map)dv.Map); if (dataView == null) { continue; } dataView.Envelope = dv.Envelope; dataView.TOC = dv.TOC; dataView.DisplayRotation = dv.DisplayRotation; if (_activeDataView == null) { _activeDataView = dataView; } } if (_dataViews.Count == 0 && _doc.Maps.Count() > 0) { //_appWindow.AddDataView((Map)_doc.Maps[0]); _activeDataView = _dataViews[0]; } if (_activeDataView != null && _activeDataView.MapView != null) { _activeDataView.MapView.Tool = this.ActiveTool; } ValidateUI(); IsDirty = false; if (fi.Extension.ToLower() == ".restore") { IsDirty = true; fi = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - ".restore".Length)); _docFilename = fi.FullName; } _appWindow.Title = "gView.Carto " + fi.Name; _readonly = (fi.Extension.ToLower() == ".rdm"); foreach (var map in _doc.Maps) { if (map.HasErrorMessages) { FormToolException.Show($"Loading Map '{ map.Name }'", String.Join(Environment.NewLine, map.ErrorMessages)); } } if (AfterLoadMapDocument != null) { AfterLoadMapDocument(_doc); } }
private static async Task <int> Main(string[] args) { try { string inFile = args.FirstOrDefault(); for (int i = 1; i < args.Length; i++) { if (args[i] == "-??" && i < args.Length - 1) { } } if (String.IsNullOrWhiteSpace(inFile)) { Console.WriteLine("Usage: gView.Cmd.MxlInfo mxl-file [Options]"); } XmlStream stream = new XmlStream(""); stream.ReadStream(inFile); MapDocument doc = new MapDocument(); await stream.LoadAsync("MapDocument", doc); foreach (var map in doc.Maps) { if (map?.MapElements == null) { continue; } Console.WriteLine($"Map: { map.Name }"); Console.WriteLine("=========================================================="); var featureLayers = map.TOC.Layers.Where(l => l is IFeatureLayer) .Select(l => (IFeatureLayer)l); Console.WriteLine("FeatureLayers:"); Console.WriteLine("-------------------------------------------------------------"); if (map.Datasets != null) { int datasetID = 0; foreach (var dataset in map.Datasets) { Console.WriteLine($"Dataset: { dataset.DatasetName }"); Console.WriteLine($" { dataset.GetType().ToString() }"); Console.WriteLine("-------------------------------------------------------"); foreach (var dsElement in map.MapElements.Where(e => e.DatasetID == datasetID)) { if (dsElement?.Class == null) { continue; } var featureLayer = featureLayers.Where(l => l.DatasetID == datasetID && l.Class == dsElement.Class) .FirstOrDefault(); if (featureLayer == null) { continue; } Console.WriteLine($"FeatureLayer: { featureLayer.Title }"); Console.WriteLine($" Class: { dsElement.Class.Name }"); Console.WriteLine($" { dsElement.Class.GetType().ToString() }"); } } } } return(0); } catch (Exception ex) { Console.WriteLine("Exception:"); Console.WriteLine(ex.Message); return(1); } }