Example #1
0
        async public Task SetSerialize(Serialized value)
        {
            if (value == null)
            {
                return;
            }

            if (this.FeatureDataset == null)
            {
                IFeatureDataset dataset = PlugInManager.Create(new Guid(value.DatasetGuid)) as IFeatureDataset;
                if (dataset == null)
                {
                    throw new Exception("Unable to crete dataset");
                }
                await dataset.SetConnectionString(value.ConnectionString);

                this.FeatureDataset = dataset;
            }

            _selecteFeturesclasses.Serialize = value.FeatureClasses;
            _tolerance.Serialize             = value.NetworkTolerance;
            await _complexEdges.OnShowWizardPage();

            _complexEdges.Serialize = value.ComplexEdges;
            await _switches.OnShowWizardPage();

            _switches.Serialize    = value.Nodes;
            _edgeWeights.Serialize = value.EdgeWeights;

            var assembly = Assembly.LoadFrom(SystemVariables.ApplicationDirectory + @"\" + value.NetworkCreatorAssembly);

            _networkCreatorType = (assembly.CreateInstance(value.NetworkCreatorType) as INetworkCreator)?.GetType();
        }
Example #2
0
        async public Task <object> GetInstanceAsync()
        {
            if (_dataset == null)
            {
                _dataset = new SdeDataset();
                await _dataset.SetConnectionString(_connectionString);

                await _dataset.Open();
            }
            return(_dataset);
        }
Example #3
0
        async public Task <object> GetInstanceAsync()
        {
            if (_dataset != null)
            {
                _dataset.Dispose();
            }

            _dataset = new GeoJsonServiceDataset();
            await _dataset.SetConnectionString(_connectionString);

            await _dataset.Open();

            return(_dataset);
        }
Example #4
0
        async public Task <object> GetInstanceAsync()
        {
            if (_dataset != null)
            {
                _dataset.Dispose();
            }

            _dataset = new MongoDbDataset();
            await _dataset.SetConnectionString(_connectionString);

            await _dataset.Open();

            ((MongoDbDataset)_dataset).BeforeCreateFeatureClass = BeforeCreateFeatureClass;

            return(_dataset);
        }
Example #5
0
        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...");
        }
Example #6
0
        async static Task <int> Main(string[] args)
        {
            string gmlSource = String.Empty;
            string cacheSource = String.Empty;
            string cacheTarget = String.Empty;
            int    jpegQuality = -1, maxlevel = -1;
            bool   listFilenames = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-gml")
                {
                    gmlSource = args[++i];
                }
                if (args[i] == "-cache")
                {
                    cacheSource = args[++i];
                }
                else if (args[i] == "-target")
                {
                    cacheTarget = args[++i];
                }
                else if (args[i] == "-jpeg-qual")
                {
                    jpegQuality = int.Parse(args[++i]);
                }
                else if (args[i] == "-maxlevel")
                {
                    maxlevel = int.Parse(args[++i]);
                }
                else if (args[i] == "-listfilenames")
                {
                    PlugInManager.InitSilent = true;
                    listFilenames            = true;
                }
            }

            if (String.IsNullOrWhiteSpace(gmlSource) || String.IsNullOrWhiteSpace(cacheSource) || String.IsNullOrWhiteSpace(cacheTarget))
            {
                Console.WriteLine("USAGE:");
                Console.WriteLine("gView.Cmd.ClipCompactTilecache.exe -gml <Filename> -cache <cachedirectory> -target <cachetarget>");
                Console.WriteLine("                      [-jpeg-qual <quality  0..100>] -maxlevel <level>");
                Console.WriteLine("                      [-listfilenames]");
                return(1);
            }

            PlugInManager   compMan    = new PlugInManager();
            IFeatureDataset gmlDataset = compMan.CreateInstance(new Guid("dbabe7f1-fe46-4731-ab2b-8a324c60554e")) as IFeatureDataset;

            if (gmlDataset == null)
            {
                Console.WriteLine("GML Dataset-plugin is not supported");
                return(1);
            }

            await gmlDataset.SetConnectionString(gmlSource);

            await gmlDataset.Open();

            List <IPolygon> sourcePolygons = new List <IPolygon>();

            foreach (var element in await gmlDataset.Elements())
            {
                if (element.Class is IFeatureClass)
                {
                    var fc = (IFeatureClass)element.Class;

                    using (var cursor = await fc.GetFeatures(null))
                    {
                        IFeature feature;
                        while ((feature = await cursor.NextFeature()) != null)
                        {
                            if (feature.Shape is IPolygon)
                            {
                                sourcePolygons.Add((IPolygon)feature.Shape);
                            }
                        }
                    }
                }
            }

            if (!listFilenames)
            {
                Console.WriteLine(sourcePolygons.Count + " polygons found for clipping...");
            }

            FileInfo configFile = new FileInfo(cacheSource + @"\conf.json");

            if (!configFile.Exists)
            {
                throw new ArgumentException("File " + configFile.FullName + " not exists");
            }

            #region Image Encoding Parameters

            System.Drawing.Imaging.ImageCodecInfo jpgEncoder = GetEncoder(System.Drawing.Imaging.ImageFormat.Jpeg);

            // Create an Encoder object based on the GUID
            // for the Quality parameter category.
            System.Drawing.Imaging.Encoder myEncoder =
                System.Drawing.Imaging.Encoder.Quality;

            // Create an EncoderParameters object.
            // An EncoderParameters object has an array of EncoderParameter
            // objects. In this case, there is only one
            // EncoderParameter object in the array.
            System.Drawing.Imaging.EncoderParameters myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);

            #endregion

            CompactTileConfig cacheConfig = JsonConvert.DeserializeObject <CompactTileConfig>(File.ReadAllText(configFile.FullName));
            double            dpm         = cacheConfig.Dpi / 0.0254;

            foreach (var level in cacheConfig.Levels)
            {
                if (!listFilenames)
                {
                    Console.WriteLine("Level: " + level.Level + " Scale=" + level.Scale);
                }

                double resolution      = (level.Scale / dpm);
                double tileWorldWidth  = cacheConfig.TileSize[0] * resolution;
                double tileWorldHeight = cacheConfig.TileSize[1] * resolution;

                var scaleDirectory = new DirectoryInfo(cacheSource + @"\" + ((int)level.Scale).ToString());
                if (!scaleDirectory.Exists)
                {
                    continue;
                }

                foreach (var bundleFile in scaleDirectory.GetFiles("*.tilebundle"))
                {
                    var bundle = new Bundle(bundleFile.FullName);
                    if (!bundle.Index.Exists)
                    {
                        continue;
                    }

                    int    startRow = bundle.StartRow, startCol = bundle.StartCol;
                    double bundleWorldWidth = tileWorldWidth * 128D, bundleWorldHeight = tileWorldHeight * 128D;

                    IPoint bundleLowerLeft = new Point(cacheConfig.Origin[0] + startCol * tileWorldWidth,
                                                       cacheConfig.Origin[1] - startRow * tileWorldHeight - bundleWorldHeight);
                    IEnvelope bundleEnvelope = new Envelope(bundleLowerLeft, new Point(bundleLowerLeft.X + bundleWorldWidth, bundleLowerLeft.Y + bundleWorldHeight));

                    if (!Intersect(bundleEnvelope, sourcePolygons))
                    {
                        continue;
                    }

                    if (listFilenames)
                    {
                        Console.WriteLine(bundleFile.FullName);
                        continue;
                    }

                    Console.WriteLine("Clip bundle: " + bundleFile.FullName);

                    var clippedBundleFile = new FileInfo(cacheTarget + "/" + (int)level.Scale + "/" + bundleFile.Name);
                    if (!clippedBundleFile.Directory.Exists)
                    {
                        clippedBundleFile.Directory.Create();
                    }

                    if (clippedBundleFile.Exists)
                    {
                        clippedBundleFile.Delete();
                    }

                    var indexBuilder   = new CompactTilesIndexBuilder();
                    int clippedTilePos = 0;

                    for (int r = 0; r < 128; r++)
                    {
                        for (int c = 0; c < 128; c++)
                        {
                            int tileLength;
                            int tilePos = bundle.Index.TilePosition(r, c, out tileLength);

                            if (tilePos >= 0 && tileLength >= 0)
                            {
                                IPoint tileLowerLeft = new Point(cacheConfig.Origin[0] + (startCol + c) * tileWorldWidth,
                                                                 cacheConfig.Origin[1] - (startRow + r + 1) * tileWorldHeight);
                                IEnvelope tileEnvelope = new Envelope(tileLowerLeft, new Point(tileLowerLeft.X + tileWorldWidth, tileLowerLeft.Y + tileWorldHeight));

                                if (!Intersect(tileEnvelope, sourcePolygons))
                                {
                                    continue;
                                }

                                Console.WriteLine("Append tile " + level.Level + "/" + (startRow + r) + "/" + (startCol + c));

                                byte[] data = bundle.ImageData(tilePos, tileLength);

                                if (jpegQuality > 0)
                                {
                                    #region New Jpeg Quality

                                    MemoryStream ms = new MemoryStream(data);
                                    using (System.Drawing.Image image = System.Drawing.Image.FromStream(ms))
                                    {
                                        MemoryStream outputMs = new MemoryStream();

                                        System.Drawing.Imaging.EncoderParameter myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(myEncoder, Convert.ToInt64(jpegQuality));
                                        myEncoderParameters.Param[0] = myEncoderParameter;

                                        image.Save(outputMs, jpgEncoder, myEncoderParameters);
                                        data = outputMs.ToArray();
                                    }

                                    #endregion
                                }
                                using (var stream = new FileStream(clippedBundleFile.FullName, FileMode.Append))
                                {
                                    stream.Write(data, 0, data.Length);
                                }

                                indexBuilder.SetValue(r, c, clippedTilePos, data.Length);
                                clippedTilePos += data.Length;
                            }
                        }
                    }

                    if (clippedTilePos > 0)
                    {
                        indexBuilder.Save(clippedBundleFile.Directory.FullName + @"\" + new FileInfo(bundle.Index.Filename).Name);
                    }
                }

                if (maxlevel >= 0 && level.Level >= maxlevel)
                {
                    break;
                }
            }

            return(0);
        }
Example #7
0
        async public Task Execute(ProgressReporterEvent reporter)
        {
            if (_serialized == null)
            {
                throw new Exception("can't execute. No config!");
            }

            #region Instance Creator

            var assembly = Assembly.LoadFrom(SystemVariables.ApplicationDirectory + @"\" + _serialized.NetworkCreatorAssembly);
            if (assembly == null)
            {
                throw new Exception("Assembly not found: " + _serialized.NetworkCreatorAssembly);
            }
            var creator = assembly.CreateInstance(_serialized.NetworkCreatorType) as INetworkCreator;
            if (creator == null)
            {
                throw new Exception("Type " + _serialized.NetworkCreatorType + " is not a network creator");
            }

            #endregion

            #region Open Dataset

            IFeatureDataset dataset = PlugInManager.Create(new Guid(_serialized.DatasetGuid)) as IFeatureDataset;
            if (dataset == null)
            {
                throw new Exception("Unable to crete dataset");
            }

            await dataset.SetConnectionString(_serialized.ConnectionString);

            await dataset.Open();

            this.FeatureDataset  = dataset;
            this.FeatureDatabase = dataset.Database as IFeatureDatabase3;
            if (this.FeatureDatabase == null)
            {
                throw new Exception("Featuredatabase no implements IFeatureDatabase3");
            }

            #endregion

            creator.NetworkName        = _serialized.FeatureClasses.NetworkName;
            creator.FeatureDataset     = this.FeatureDataset;
            creator.EdgeFeatureClasses = await this.EdgeFeatureclasses();

            creator.NodeFeatureClasses = await this.NodeFeatureclasses();

            creator.SnapTolerance    = this.SnapTolerance;
            creator.ComplexEdgeFcIds = await this.ComplexEdgeFcIds();

            creator.GraphWeights = this.GraphWeights;
            creator.SwitchNodeFcIdAndFieldnames = await this.SwitchNodeFcIds();

            creator.NodeTypeFcIds = await this.NetworkNodeTypeFcIds();

            if (reporter != null && creator is IProgressReporter)
            {
                ((IProgressReporter)creator).ReportProgress += (ProgressReport progressEventReport) =>
                {
                    reporter(progressEventReport);
                };
            }

            await creator.Run();
        }