Esempio n. 1
0
        public CreateTileGridClass(
            string name,
            IFeatureDataset targetDataset,
            ISpatialIndexDef spatialIndexDef,
            IRasterDataset sourceDataset,
            double tileSizeX, double tileSizeY,
            double resX, double resY,
            int levels,
            string cacheDirectory,
            TileGridType gridType)
        {
            _name            = name;
            _cacheDirectory  = cacheDirectory;
            _targetDataset   = targetDataset;
            _spatialIndexDef = spatialIndexDef;
            _sourceDataset   = sourceDataset;
            _tileSizeX       = tileSizeX;
            _tileSizeY       = tileSizeY;
            _resX            = resX;
            _resY            = resY;
            _levels          = levels;
            _gridType        = gridType;

            _fdb = (SqlFDB)_targetDataset.Database;

            _createLevels = new List <int>();
            for (int i = 0; i < _levels; i++)
            {
                _createLevels.Add(i);
            }
        }
        async private Task ImportAsync(object element)
        {
            if (_fdb == null || _import == null)
            {
                return;
            }

            ISpatialIndexDef sIndexDef = null;

            if (_fdb is AccessFDB)
            {
                sIndexDef = await((AccessFDB)_fdb).SpatialIndexDef(_dsname);
            }

            if (element is IFeatureClass)
            {
                if (!await _import.ImportToNewFeatureclass(
                        _fdb,
                        _dsname,
                        ((IFeatureClass)element).Name,
                        (IFeatureClass)element,
                        null,
                        true,
                        null,
                        sIndexDef))
                {
                    MessageBox.Show(_import.lastErrorMsg);
                }
            }
            else if (element is FeatureClassListViewItem)
            {
                FeatureClassListViewItem item = element as FeatureClassListViewItem;
                if (item.FeatureClass == null)
                {
                    return;
                }

                MSSpatialIndex msIndex = new MSSpatialIndex();
                msIndex.GeometryType       = GeometryFieldType.MsGeometry;
                msIndex.SpatialIndexBounds = item.FeatureClass.Envelope;
                msIndex.Level1             = msIndex.Level2 = msIndex.Level3 = msIndex.Level4 = MSSpatialIndexLevelSize.LOW;

                if (!await _import.ImportToNewFeatureclass(
                        _fdb,
                        _dsname,
                        item.TargetName,
                        item.FeatureClass,
                        item.ImportFieldTranslation,
                        true,
                        null,
                        sIndexDef))
                {
                    MessageBox.Show(_import.lastErrorMsg);
                }
            }
        }
Esempio n. 3
0
        internal AccessFDBDataset(AccessFDB fdb, string dsname, ISpatialIndexDef sIndexDef)
            : this()
        {
            if (fdb == null)
            {
                return;
            }

            ConnectionString = "mdb=" + fdb.ConnectionString + ";dsname=" + dsname;
            Open();
        }
Esempio n. 4
0
        public bool Open()
        {
            if (_fdb == null) return false;

            _dsID = _fdb.DatasetID(_dsname);
            if (_dsID < 0) return false;

            _sRef = this.SpatialReference;
            _state = DatasetState.opened;
            _sIndexDef = _fdb.SpatialIndexDef(_dsID);

            return true;
        }
Esempio n. 5
0
        async public Task <IExplorerObject> CreateExplorerObject(IExplorerObject parentExObject)
        {
            if (!CanCreate(parentExObject))
            {
                return(null);
            }

            var instance = await parentExObject.GetInstanceAsync();

            if (!(instance is IFeatureDataset) || !(((IDataset)instance).Database is SQLiteFDB))
            {
                return(null);
            }
            SQLiteFDB fdb = ((IDataset)instance).Database as SQLiteFDB;

            FormNewFeatureclass dlg = await FormNewFeatureclass.Create(instance as IFeatureDataset);

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return(null);
            }

            IGeometryDef gDef = dlg.GeometryDef;

            int FCID = await fdb.CreateFeatureClass(
                parentExObject.Name,
                dlg.FeatureclassName,
                gDef,
                dlg.Fields);

            if (FCID < 0)
            {
                MessageBox.Show("ERROR: " + fdb.LastErrorMessage);
                return(null);
            }

            ISpatialIndexDef sIndexDef = await fdb.SpatialIndexDef(parentExObject.Name);

            await fdb.SetSpatialIndexBounds(dlg.FeatureclassName, "BinaryTree2", dlg.SpatialIndexExtents, 0.55, 200, dlg.SpatialIndexLevels);

            IDatasetElement element = await((IFeatureDataset)instance).Element(dlg.FeatureclassName);

            return(new SQLiteFDBFeatureClassExplorerObject(
                       parentExObject as SQLiteFDBDatasetExplorerObject,
                       _filename,
                       parentExObject.Name,
                       element));
        }
        public IExplorerObject CreateExplorerObject(IExplorerObject parentExObject)
        {
            if (!CanCreate(parentExObject))
            {
                return(null);
            }

            if (!(parentExObject.Object is IFeatureDataset) || !(((IDataset)parentExObject.Object).Database is AccessFDB))
            {
                return(null);
            }
            AccessFDB fdb = ((IDataset)parentExObject.Object).Database as AccessFDB;

            FormNewFeatureclass dlg = new FormNewFeatureclass(parentExObject.Object as IFeatureDataset);

            if (dlg.ShowDialog() != DialogResult.OK)
            {
                return(null);
            }

            IGeometryDef gDef = dlg.GeometryDef;

            int FCID = fdb.CreateFeatureClass(
                parentExObject.Name,
                dlg.FeatureclassName,
                gDef,
                dlg.Fields);

            if (FCID < 0)
            {
                MessageBox.Show("ERROR: " + fdb.lastErrorMsg);
                return(null);
            }

            ISpatialIndexDef sIndexDef = fdb.SpatialIndexDef(parentExObject.Name);

            fdb.SetSpatialIndexBounds(dlg.FeatureclassName, "BinaryTree2", dlg.SpatialIndexExtents, 0.55, 200, dlg.SpatialIndexLevels);

            IDatasetElement element = ((IFeatureDataset)parentExObject.Object)[dlg.FeatureclassName];

            return(new AccessFDBFeatureClassExplorerObject(
                       parentExObject as AccessFDBDatasetExplorerObject,
                       _filename,
                       parentExObject.Name,
                       element));
        }
Esempio n. 7
0
        public bool Open()
        {
            if (_connString == null || _connString == "" ||
                _dsname == null || _dsname == "" || _fdb == null)
            {
                return(false);
            }

            _dsID = _fdb.DatasetID(_dsname);
            if (_dsID == -1)
            {
                return(false);
            }

            _sRef      = _fdb.SpatialReference(_dsname);
            _sIndexDef = _fdb.SpatialIndexDef(_dsID);

            _state = DatasetState.opened;
            return(true);
        }
Esempio n. 8
0
        async public Task <bool> Open()
        {
            if (_fdb == null)
            {
                return(false);
            }

            _dsID = await _fdb.DatasetID(_dsname);

            if (_dsID < 0)
            {
                return(false);
            }

            _sRef = await this.GetSpatialReference();

            _state     = DatasetState.opened;
            _sIndexDef = await _fdb.SpatialIndexDef(_dsID);

            return(true);
        }
Esempio n. 9
0
        async public Task <bool> Open()
        {
            if (_fdb == null)
            {
                return(false);
            }

            _dsID = await _fdb.DatasetID(_dsname);

            if (_dsID < 0)
            {
                _errMsg = _fdb.LastErrorMessage ?? _fdb.lastException?.Message;
                return(false);
            }

            _sRef = await this.GetSpatialReference();

            _state     = DatasetState.opened;
            _sIndexDef = await _fdb.SpatialIndexDef(_dsID);

            return(true);
        }
Esempio n. 10
0
        async public Task <bool> ImportToNewFeatureclass(IFeatureDatabase fdb, string dsname, string fcname, IFeatureClass sourceFC, FieldTranslation fieldTranslation, bool project, List <IQueryFilter> filters, ISpatialIndexDef sIndexDef, geometryType?sourceGeometryType = null)
        {
            if (!_cancelTracker.Continue)
            {
                return(true);
            }

            if (fdb is AccessFDB)
            {
                ISpatialIndexDef dsSpatialIndexDef = await((AccessFDB)fdb).SpatialIndexDef(dsname);
                if (sIndexDef == null)
                {
                    sIndexDef = dsSpatialIndexDef;
                }
                else if (sIndexDef.GeometryType != dsSpatialIndexDef.GeometryType)
                {
                    _errMsg = "Spatial-Index-Definition-GeometryTypes are not compatible!";
                    return(false);
                }
            }
            if (sIndexDef == null)
            {
                sIndexDef = new gViewSpatialIndexDef();
            }

            bool msSpatial = false;

            if (fdb is SqlFDB &&
                (sIndexDef.GeometryType == GeometryFieldType.MsGeography ||
                 sIndexDef.GeometryType == GeometryFieldType.MsGeometry))
            {
                msSpatial = true;
            }
            else
            {
                int maxAllowedLevel = ((fdb is SqlFDB || fdb is pgFDB) ? 62 : 30);
                if (sIndexDef.Levels > maxAllowedLevel)
                {
                    ISpatialReference defSRef = sIndexDef.SpatialReference;
                    sIndexDef = new gViewSpatialIndexDef(
                        sIndexDef.SpatialIndexBounds,
                        Math.Min(sIndexDef.Levels, maxAllowedLevel),
                        sIndexDef.MaxPerNode,
                        sIndexDef.SplitRatio);
                    ((gViewSpatialIndexDef)sIndexDef).SpatialReference = defSRef;
                }
            }

            try
            {
                fcname = fcname.Replace(".", "_");

                IFeatureDataset destDS = await fdb.GetDataset(dsname);

                if (destDS == null)
                {
                    _errMsg = fdb.LastErrorMessage;
                    return(false);
                }

                IDatasetElement destLayer = await destDS.Element(fcname);

                if (destLayer != null)
                {
                    if (ReportRequest != null)
                    {
                        RequestArgs args = new RequestArgs(
                            "Featureclass " + fcname + " already exists in " + dsname + "\nDo want to replace it?",
                            MessageBoxButtons.YesNoCancel,
                            DialogResult.Cancel);
                        ReportRequest(this, args);
                        switch (args.Result)
                        {
                        case DialogResult.No:
                            return(true);

                        case DialogResult.Cancel:
                            _errMsg = "Import is canceled by the user...";
                            return(false);
                        }
                    }
                }

                GeometryDef geomDef = new GeometryDef(sourceFC);
                if (geomDef.GeometryType == geometryType.Unknown && sourceGeometryType != null)
                {
                    geomDef.GeometryType = sourceGeometryType.Value;
                }

                int fcID = -1;
                if (destLayer != null)
                {
                    if (fdb is AccessFDB)
                    {
                        fcID = await((AccessFDB)fdb).ReplaceFeatureClass(destDS.DatasetName,
                                                                         fcname,
                                                                         geomDef,
                                                                         (fieldTranslation == null) ?
                                                                         ((sourceFC.Fields != null) ? (IFields)sourceFC.Fields.Clone() : new Fields()) :
                                                                         fieldTranslation.DestinationFields);
                        if (fcID < 0)
                        {
                            _errMsg = "Can't replace featureclass " + fcname + "...\r\n" + fdb.LastErrorMessage;
                            destDS.Dispose();
                            return(false);
                        }
                    }
                    else
                    {
                        await fdb.DeleteFeatureClass(fcname);
                    }
                }
                if (fcID < 0)
                {
                    fcID = await fdb.CreateFeatureClass(destDS.DatasetName,
                                                        fcname,
                                                        geomDef,
                                                        (fieldTranslation == null)?
                                                        ((sourceFC.Fields != null) ? (IFields)sourceFC.Fields.Clone() : new Fields()) :
                                                        fieldTranslation.DestinationFields);
                }
                if (fcID < 0)
                {
                    _errMsg = "Can't create featureclass " + fcname + "...\r\n" + fdb.LastErrorMessage;
                    destDS.Dispose();
                    return(false);
                }

                destLayer = await destDS.Element(fcname);

                if (destLayer == null || !(destLayer.Class is IFeatureClass))
                {
                    _errMsg = "Can't load featureclass " + fcname + "...\r\n" + destDS.LastErrorMessage;
                    destDS.Dispose();
                    return(false);
                }
                IFeatureClass destFC = destLayer.Class as IFeatureClass;

                if (project && destFC.SpatialReference != null && !destFC.SpatialReference.Equals(sourceFC.SpatialReference))
                {
                    _transformer = GeometricTransformerFactory.Create();
                    //_transformer.FromSpatialReference = sourceFC.SpatialReference;
                    //_transformer.ToSpatialReference = destFC.SpatialReference;
                    _transformer.SetSpatialReferences(sourceFC.SpatialReference, destFC.SpatialReference);
                }

                if (!Envelope.IsNull(sIndexDef.SpatialIndexBounds) &&
                    sIndexDef.SpatialReference != null && !sIndexDef.SpatialReference.Equals(destFC.SpatialReference))
                {
                    if (!sIndexDef.ProjectTo(destFC.SpatialReference))
                    {
                        _errMsg = "Can't project SpatialIndex Boundaries...";
                        destDS.Dispose();
                        return(false);
                    }
                }

                DualTree           tree  = null;
                BinaryTree2Builder tree2 = null;

                if (msSpatial)
                {
                    ((SqlFDB)fdb).SetMSSpatialIndex((MSSpatialIndex)sIndexDef, destFC.Name);
                    await((SqlFDB)fdb).SetFeatureclassExtent(destFC.Name, sIndexDef.SpatialIndexBounds);
                }
                else
                {
                    if (_treeVersion == TreeVersion.BinaryTree)
                    {
                        tree = await SpatialIndex(sourceFC, sIndexDef.MaxPerNode, filters);

                        if (tree == null)
                        {
                            return(false);
                        }
                    }
                    else if (_treeVersion == TreeVersion.BinaryTree2)
                    {
                        if (_schemaOnly && sourceFC.Dataset.Database is IImplementsBinarayTreeDef)
                        {
                            BinaryTreeDef tDef = await((IImplementsBinarayTreeDef)sourceFC.Dataset.Database).BinaryTreeDef(sourceFC.Name);
                            tree2 = new BinaryTree2Builder(tDef.Bounds, tDef.MaxLevel, tDef.MaxPerNode, tDef.SplitRatio);
                        }
                        else
                        {
                            tree2 = await SpatialIndex2(fdb, sourceFC, sIndexDef, filters);

                            if (tree2 == null)
                            {
                                return(false);
                            }
                        }
                    }

                    // Vorab einmal alle "Bounds" festlegen, damit auch
                    // ein aufzubauender Layer geviewt werden kann
                    if (_treeVersion == TreeVersion.BinaryTree2 && fdb is AccessFDB)
                    {
                        if (ReportAction != null)
                        {
                            ReportAction(this, "Insert spatial index nodes");
                        }
                        List <long> nids = new List <long>();
                        foreach (BinaryTree2BuilderNode node in tree2.Nodes)
                        {
                            nids.Add(node.Number);
                        }
                        await((AccessFDB)fdb).ShrinkSpatialIndex(fcname, nids);

                        if (ReportAction != null)
                        {
                            ReportAction(this, "Set spatial index bounds");
                        }
                        //((AccessFDB)fdb).SetSpatialIndexBounds(fcname, "BinaryTree2", tree2.Bounds, sIndexDef.SplitRatio, sIndexDef.MaxPerNode, tree2.maxLevels);
                        await((AccessFDB)fdb).SetSpatialIndexBounds(fcname, "BinaryTree2", tree2.Bounds, tree2.SplitRatio, tree2.MaxPerNode, tree2.maxLevels);
                        await((AccessFDB)fdb).SetFeatureclassExtent(fcname, tree2.Bounds);
                    }
                }
                if (_cancelTracker.Continue)
                {
                    bool result = true;
                    if (!_schemaOnly)
                    {
                        if (msSpatial)
                        {
                            result = await CopyFeatures(sourceFC, fdb, destFC, fieldTranslation, filters);
                        }
                        else if (_treeVersion == TreeVersion.BinaryTree)
                        {
                            if (String.IsNullOrEmpty(sourceFC.IDFieldName)) // SDE Views haben keine ID -> Tree enthält keine Features
                            {
                                result = await CopyFeatures(sourceFC, fdb, destFC, fieldTranslation, filters);
                            }
                            else
                            {
                                result = await CopyFeatures(sourceFC, fdb, destFC, fieldTranslation, tree);
                            }
                        }
                        else if (_treeVersion == TreeVersion.BinaryTree2)
                        {
                            if (String.IsNullOrEmpty(sourceFC.IDFieldName)) // SDE Views haben keine ID -> Tree enthält keine Features
                            {
                                result = await CopyFeatures(sourceFC, fdb, destFC, fieldTranslation, filters);
                            }
                            else
                            {
                                result = await CopyFeatures2(sourceFC, fdb, destFC, fieldTranslation, tree2);
                            }
                        }
                        if (!result)
                        {
                            await fdb.DeleteFeatureClass(fcname);

                            destDS.Dispose();
                            return(false);
                        }
                    }
                }

                destDS.Dispose();

                if (_cancelTracker.Continue && fdb is AccessFDB)
                {
                    if (ReportAction != null)
                    {
                        ReportAction(this, "Calculate extent");
                    }
                    await((AccessFDB)fdb).CalculateExtent(destFC);

                    if (msSpatial == false)
                    {
                        if (_treeVersion == TreeVersion.BinaryTree)
                        {
                            if (ReportAction != null)
                            {
                                ReportAction(this, "Set spatial index bounds");
                            }
                            await((AccessFDB)fdb).SetSpatialIndexBounds(fcname, "BinaryTree", tree.Bounds, sIndexDef.SplitRatio, sIndexDef.MaxPerNode, 0);

                            if (ReportAction != null)
                            {
                                ReportAction(this, "Insert spatial index nodes");
                            }
                            await((AccessFDB)fdb).__intInsertSpatialIndexNodes2(fcname, tree.Nodes);
                        }
                    }
                    return(true);
                }
                else
                {
                    await fdb.DeleteFeatureClass(fcname);

                    _errMsg = "Import is canceled by the user...";
                    return(false);
                }
            }
            finally
            {
                if (_transformer != null)
                {
                    _transformer.Release();
                    _transformer = null;
                }
            }
        }
Esempio n. 11
0
        async private Task <BinaryTree2Builder> SpatialIndex2(IFeatureDatabase fdb, IFeatureClass fc, ISpatialIndexDef def, List <IQueryFilter> filters)
        {
            if (fc == null)
            {
                return(null);
            }

            IEnvelope bounds = null;

            if (fc.Envelope != null)
            {
                bounds = fc.Envelope;
            }
            else if (fc.Dataset is IFeatureDataset && await((IFeatureDataset)fc.Dataset).Envelope() != null)
            {
                bounds = await((IFeatureDataset)fc.Dataset).Envelope();
            }
            if (bounds == null)
            {
                return(null);
            }
            if (_transformer != null)
            {
                IGeometry transBounds = _transformer.Transform2D(bounds) as IGeometry;
                if (transBounds != null)
                {
                    bounds = transBounds.Envelope;
                }
            }
            int maxAllowedLevel            = ((fdb is SqlFDB) ? 62 : 30);
            BinaryTree2Builder treeBuilder =
                ((Envelope.IsNull(def.SpatialIndexBounds)) ?
                 new BinaryTree2Builder(bounds,
                                        ((def.Levels != 0) ? def.Levels : maxAllowedLevel),
                                        ((def.MaxPerNode != 0) ? def.MaxPerNode : 500),
                                        ((def.SplitRatio != 0.0) ? def.SplitRatio : 0.55)) :
                 new BinaryTree2Builder2(def.SpatialIndexBounds, def.Levels, def.MaxPerNode, def.SplitRatio));

            if (filters == null)
            {
                QueryFilter filter = new QueryFilter();
                filter.AddField(fc.ShapeFieldName);
                filters = new List <IQueryFilter>();
                filters.Add(filter);
            }
            foreach (IQueryFilter filter in filters)
            {
                using (IFeatureCursor fCursor = await fc.GetFeatures(filter))
                {
                    if (fCursor == null)
                    {
                        _errMsg = "Fatal error: sourcedb query failed...";
                        return(null);
                    }

                    if (ReportAction != null)
                    {
                        ReportAction(this, "Calculate spatial index");
                    }

                    IEnvelope fcEnvelope = bounds;
                    if (_transformer != null)
                    {
                        IGeometry geom = _transformer.Transform2D(fcEnvelope) as IGeometry;
                        if (geom == null)
                        {
                            _errMsg = "SpatialIndex: Can't project featureclass extent!";
                            return(null);
                        }
                        fcEnvelope = geom.Envelope;
                    }
                    //treeBuilder = new BinaryTree2Builder(fcEnvelope, maxLevels, maxPerNode);

                    int      counter = 0;
                    IFeature feat;
                    while ((feat = await fCursor.NextFeature()) != null)
                    {
                        if (!_cancelTracker.Continue)
                        {
                            break;
                        }

                        IGeometry shape = feat.Shape;
                        if (_transformer != null)
                        {
                            shape = _transformer.Transform2D(shape) as IGeometry;
                        }

                        feat.Shape = shape;
                        treeBuilder.AddFeature(feat);

                        if ((counter % 1000) == 0 && ReportProgress != null)
                        {
                            ReportProgress(this, counter);
                        }
                        counter++;
                    }
                    fCursor.Dispose();
                }
            }

            treeBuilder.Trim();
            return(treeBuilder);
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            string source_connstr = "", source_fc = "";
            string dest_connstr = "", dest_fc = "";

            string[]         sourceFields = null, destFields = null;
            Guid             source_guid = Guid.Empty, dest_guid = Guid.Empty;
            bool             checkout            = false;
            string           checkoutDescription = String.Empty;
            string           child_rights        = "iud";
            string           parent_rights       = "iud";
            string           conflict_handling   = "normal";
            ISpatialIndexDef treeDef             = null;

            for (int i = 0; i < args.Length - 1; i++)
            {
                if (args[i] == "-source_connstr")
                {
                    source_connstr = args[++i];
                }

                else if (args[i] == "-source_guid")
                {
                    source_guid = new Guid(args[++i]);
                }

                else if (args[i] == "-source_fc")
                {
                    source_fc = args[++i];
                }

                else if (args[i] == "-dest_connstr")
                {
                    dest_connstr = args[++i];
                }

                else if (args[i] == "-dest_guid")
                {
                    dest_guid = new Guid(args[++i]);
                }

                else if (args[i] == "-dest_fc")
                {
                    dest_fc = args[++i];
                }

                else if (args[i] == "-sourcefields")
                {
                    sourceFields = args[++i].Split(';');
                }

                else if (args[i] == "-destfields")
                {
                    destFields = args[++i].Split(';');
                }

                else if (args[i] == "-checkout")
                {
                    checkout            = true;
                    checkoutDescription = args[++i];
                }

                else if (args[i] == "-pr")
                {
                    parent_rights = args[++i];
                }
                else if (args[i] == "-cr")
                {
                    child_rights = args[++i];
                }
                else if (args[i] == "-ch")
                {
                    conflict_handling = args[++i];
                }

                //else if (args[i] == "-si")
                //{
                //    treeDef = BinaryTreeDef.FromString(args[++i]);
                //    if (treeDef == null)
                //    {
                //        Console.WriteLine("Invalid Spatial Index Def. " + args[i]);
                //    }
                //}
            }

            if (source_connstr == "" || source_fc == "" || source_guid == Guid.Empty ||
                dest_connstr == "" || dest_fc == "" || dest_guid == Guid.Empty)
            {
                Console.WriteLine("USAGE:");
                Console.WriteLine("gView.Cmd.CopyFeatureclass -source_connstr <Source Dataset Connection String>");
                Console.WriteLine("                           -source_guid <GUID of Dataset Extension>");
                Console.WriteLine("                           -source_fc <Featureclass name>");
                Console.WriteLine("                           -dest_connstr <Destination Dataset Connection String>");
                Console.WriteLine("                           -dest_guid <GUID of Dataset Extension>");
                Console.WriteLine("                           -dest_fc <Featureclass name>");
                Console.WriteLine("   when check out featureclass:");
                Console.WriteLine("                -checkout <Description> ... Write checkout information");
                Console.WriteLine("                -pr ... parent rights. <iud|iu|ud|...> (i..INSERT, u..UPDATE, d..DELETE)");
                Console.WriteLine("                -cr ... child rights.  <iud|iu|ud|...> (i..INSERT, u..UPDATE, d..DELETE)");
                Console.WriteLine("                -ch <none|normal|parent_wins|child_wins|newer_wins> ... conflict handling");
                return;
            }

            IFeatureDataset sourceDS, destDS;
            IFeatureClass   sourceFC;

            PlugInManager compMan = new PlugInManager();
            object        comp    = compMan.CreateInstance(source_guid);

            if (!(comp is IFeatureDataset))
            {
                Console.WriteLine("Component with GUID '" + source_guid.ToString() + "' is not a feature dataset...");
                return;
            }
            sourceDS = (IFeatureDataset)comp;
            sourceDS.ConnectionString = source_connstr;
            sourceDS.Open();
            sourceFC = GetFeatureclass(sourceDS, source_fc);
            if (sourceFC == null)
            {
                Console.WriteLine("Can't find featureclass '" + source_fc + "' in source dataset...");
                sourceDS.Dispose();
                return;
            }


            if (String.IsNullOrWhiteSpace(sourceFC.IDFieldName))
            {
                Console.WriteLine("WARNING: Souorce FeatureClass has no IDField -> Bad performance!!");
            }
            Console.WriteLine("Source FeatureClass: " + sourceFC.Name);
            Console.WriteLine("-----------------------------------------------------");
            Console.WriteLine("Shape Field: " + sourceFC.ShapeFieldName);
            if (String.IsNullOrWhiteSpace(sourceFC.IDFieldName))
            {
                Console.WriteLine("WARNING: Souorce FeatureClass has no IDField -> Bad performance!!");
            }
            else
            {
                Console.WriteLine("Id Field   : " + sourceFC.IDFieldName);
            }

            Console.WriteLine();
            Console.WriteLine("Import: " + source_fc);
            Console.WriteLine("-----------------------------------------------------");

            FieldTranslation fieldTranslation = new FieldTranslation();

            if (sourceFields != null && destFields != null)
            {
                if (sourceFields.Length != destFields.Length)
                {
                    Console.WriteLine("Error in field definition...");
                    sourceDS.Dispose();
                    return;
                }
                for (int i = 0; i < sourceFields.Length; i++)
                {
                    IField field = sourceFC.FindField(sourceFields[i]);
                    if (field == null)
                    {
                        Console.WriteLine("Error: Can't find field '" + sourceFields[i] + "'...");
                        sourceDS.Dispose();
                        return;
                    }
                    fieldTranslation.Add(field, destFields[i]);
                }
            }
            else
            {
                foreach (IField field in sourceFC.Fields)
                {
                    if (field.type == FieldType.ID ||
                        field.type == FieldType.Shape)
                    {
                        continue;
                    }

                    fieldTranslation.Add(field, FieldTranslation.CheckName(field.name));
                }
            }


            comp = compMan.CreateInstance(dest_guid);

            if (comp is IFileFeatureDatabase)
            {
                IFileFeatureDatabase fileDB = (IFileFeatureDatabase)comp;
                if (!fileDB.Open(dest_connstr))
                {
                    Console.WriteLine("Error opening destination database:" + fileDB.lastErrorMsg);
                    return;
                }
                destDS = fileDB[dest_connstr];
            }
            else if (comp is IFeatureDataset)
            {
                destDS = (IFeatureDataset)comp;
                destDS.ConnectionString = dest_connstr;
                if (!destDS.Open())
                {
                    Console.WriteLine("Error opening destination dataset:" + destDS.lastErrorMsg);
                    return;
                }
            }
            else
            {
                Console.WriteLine("Component with GUID '" + dest_guid.ToString() + "' is not a feature dataset...");
                return;
            }

            string replIDField = String.Empty;

            if (checkout)
            {
                if (!(destDS.Database is IFeatureDatabaseReplication) ||
                    !(sourceDS.Database is IFeatureDatabaseReplication))
                {
                    Console.WriteLine("Can't checkout FROM/TO databasetype...");
                    return;
                }
                replIDField = Replication.FeatureClassReplicationIDFieldname(sourceFC);
                if (String.IsNullOrEmpty(replIDField))
                {
                    Console.WriteLine("Can't checkout from source featureclass. No replication ID!");
                    return;
                }
                IDatasetElement element = destDS[dest_fc];
                if (element != null)
                {
                    List <Guid> checkout_guids = Replication.FeatureClassSessions(element.Class as IFeatureClass);
                    if (checkout_guids != null && checkout_guids.Count != 0)
                    {
                        string errMsg = "Can't check out to this featureclass\n";
                        errMsg += "Check in the following Sessions first:\n";
                        foreach (Guid g in checkout_guids)
                        {
                            errMsg += "   CHECKOUT_GUID: " + g.ToString();
                        }
                        Console.WriteLine("ERROR:\n" + errMsg);
                        return;
                    }
                }
            }

            if (destDS.Database is IFeatureDatabase)
            {
                if (destDS.Database is AccessFDB)
                {
                    //Console.WriteLine();
                    //Console.WriteLine("Import: " + source_fc);
                    //Console.WriteLine("-----------------------------------------------------");


                    FDBImport import = new FDBImport(((IFeatureUpdater)destDS.Database).SuggestedInsertFeatureCountPerTransaction);
                    import.ReportAction   += new FDBImport.ReportActionEvent(import_ReportAction);
                    import.ReportProgress += new FDBImport.ReportProgressEvent(import_ReportProgress);

                    if (checkout)
                    {
                        if (sourceDS.Database is AccessFDB)
                        {
                            treeDef = ((AccessFDB)sourceDS.Database).FcSpatialIndexDef(source_fc);
                            if (destDS.Database is AccessFDB)
                            {
                                ISpatialIndexDef dsTreeDef = ((AccessFDB)destDS.Database).SpatialIndexDef(destDS.DatasetName);
                                if (treeDef.GeometryType != dsTreeDef.GeometryType)
                                {
                                    treeDef = dsTreeDef;
                                }
                            }
                        }
                    }

                    if (!import.ImportToNewFeatureclass((IFeatureDatabase)destDS.Database, destDS.DatasetName, dest_fc, sourceFC, fieldTranslation, true, null, treeDef))
                    {
                        Console.WriteLine("ERROR: " + import.lastErrorMsg);
                    }
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Create: " + source_fc);
                    Console.WriteLine("-----------------------------------------------------");

                    FeatureImport import = new FeatureImport();

                    import.ReportAction   += new FeatureImport.ReportActionEvent(import_ReportAction2);
                    import.ReportProgress += new FeatureImport.ReportProgressEvent(import_ReportProgress2);

                    if (!import.ImportToNewFeatureclass(destDS, dest_fc, sourceFC, fieldTranslation, true))
                    {
                        Console.WriteLine("ERROR: " + import.lastErrorMsg);
                    }
                }

                if (checkout)
                {
                    IDatasetElement element = destDS[dest_fc];
                    if (element == null)
                    {
                        Console.WriteLine("ERROR: Can't write checkout information...");
                        return;
                    }
                    IFeatureClass destFC = element.Class as IFeatureClass;

                    string errMsg;
                    if (!Replication.InsertReplicationIDFieldname(destFC, replIDField, out errMsg))
                    {
                        Console.WriteLine("ERROR: " + errMsg);
                        return;
                    }

                    Replication.VersionRights    cr = Replication.VersionRights.NONE;
                    Replication.VersionRights    pr = Replication.VersionRights.NONE;
                    Replication.ConflictHandling ch = Replication.ConflictHandling.NORMAL;

                    if (child_rights.ToLower().Contains("i"))
                    {
                        cr |= Replication.VersionRights.INSERT;
                    }
                    if (child_rights.ToLower().Contains("u"))
                    {
                        cr |= Replication.VersionRights.UPDATE;
                    }
                    if (child_rights.ToLower().Contains("d"))
                    {
                        cr |= Replication.VersionRights.DELETE;
                    }

                    if (parent_rights.ToLower().Contains("i"))
                    {
                        pr |= Replication.VersionRights.INSERT;
                    }
                    if (parent_rights.ToLower().Contains("u"))
                    {
                        pr |= Replication.VersionRights.UPDATE;
                    }
                    if (parent_rights.ToLower().Contains("d"))
                    {
                        pr |= Replication.VersionRights.DELETE;
                    }

                    switch (conflict_handling.ToLower())
                    {
                    case "none":
                        ch = Replication.ConflictHandling.NONE;
                        break;

                    case "normal":
                        ch = Replication.ConflictHandling.NORMAL;
                        break;

                    case "parent_wins":
                        ch = Replication.ConflictHandling.PARENT_WINS;
                        break;

                    case "child_wins":
                        ch = Replication.ConflictHandling.CHILD_WINS;
                        break;

                    case "newer_wins":
                        ch = Replication.ConflictHandling.NEWER_WINS;
                        break;
                    }

                    if (!Replication.InsertNewCheckoutSession(sourceFC,
                                                              pr,
                                                              destFC,
                                                              cr,
                                                              ch,
                                                              SystemInformation.Replace(checkoutDescription),
                                                              out errMsg))
                    {
                        Console.WriteLine("ERROR: " + errMsg);
                        return;
                    }

                    if (!Replication.InsertCheckoutLocks(sourceFC, destFC, out errMsg))
                    {
                        Console.WriteLine("ERROR: " + errMsg);
                        return;
                    }
                }
            }
            else
            {
                Console.WriteLine("Destination dataset has no feature database...");
                Console.WriteLine("Can't create featureclasses for this kind of dataset...");
            }
            sourceDS.Dispose();
            destDS.Dispose();
        }
Esempio n. 13
0
        private void ExportAsync_fdb(object element)
        {
            if (_fdbExport == null)
            {
                return;
            }

            List <IQueryFilter> filters = null;

            if (_filter != null)
            {
                filters = new List <IQueryFilter>();
                filters.Add(_filter);
            }

            if (element is IFeatureClass)
            {
                IFeatureClass    fc  = (IFeatureClass)element;
                ISpatialIndexDef def = null;
                if (fc.Dataset != null && fc.Dataset.Database is AccessFDB)
                {
                    def = ((AccessFDB)fc.Dataset.Database).FcSpatialIndexDef(fc.Name);
                    if (_dataset.Database is AccessFDB)
                    {
                        ISpatialIndexDef dsDef = ((AccessFDB)_dataset.Database).SpatialIndexDef(_dataset.DatasetName);
                        if (dsDef.GeometryType != def.GeometryType)
                        {
                            def = dsDef;
                        }
                    }
                }
                if (!_fdbExport.ImportToNewFeatureclass(
                        _dataset.Database as IFeatureDatabase,
                        _dataset.DatasetName,
                        _targetName,
                        fc,
                        null,
                        true,
                        filters,
                        def))
                {
                    MessageBox.Show(_fdbExport.lastErrorMsg);
                }
            }
            else if (element is FeatureClassListViewItem)
            {
                FeatureClassListViewItem item = element as FeatureClassListViewItem;
                if (item.FeatureClass == null)
                {
                    return;
                }

                IFeatureClass    fc  = item.FeatureClass;
                ISpatialIndexDef def = null;
                if (fc.Dataset != null && fc.Dataset.Database is AccessFDB)
                {
                    def = ((AccessFDB)fc.Dataset.Database).FcSpatialIndexDef(fc.Name);
                    if (_dataset.Database is AccessFDB)
                    {
                        ISpatialIndexDef dsDef = ((AccessFDB)_dataset.Database).SpatialIndexDef(_dataset.DatasetName);
                        if (dsDef.GeometryType != def.GeometryType)
                        {
                            def = dsDef;
                        }
                    }
                }

                if (!_fdbExport.ImportToNewFeatureclass(
                        _dataset.Database as IFeatureDatabase,
                        _dataset.DatasetName,
                        item.TargetName,
                        fc,
                        item.ImportFieldTranslation,
                        true,
                        filters,
                        def))
                {
                    MessageBox.Show(_fdbExport.lastErrorMsg);
                }
            }
        }
Esempio n. 14
0
        async static private Task <BinaryTree2Builder> SpatialIndex2(IFeatureDatabase fdb, IFeatureClass fc, ISpatialIndexDef def, List <IQueryFilter> filters = null)
        {
            if (fc == null)
            {
                return(null);
            }

            IEnvelope bounds = null;

            if (fc.Envelope != null)
            {
                bounds = fc.Envelope;
            }
            else if (fc.Dataset is IFeatureDataset && await((IFeatureDataset)fc.Dataset).Envelope() != null)
            {
                bounds = await((IFeatureDataset)fc.Dataset).Envelope();
            }

            if (bounds == null)
            {
                return(null);
            }
            //if (_transformer != null)
            //{
            //    IGeometry transBounds = _transformer.Transform2D(bounds) as IGeometry;
            //    if (transBounds != null)
            //        bounds = transBounds.Envelope;
            //}
            int maxAllowedLevel            = ((fdb is SqlFDB) ? 62 : 30);
            BinaryTree2Builder treeBuilder =
                ((Envelope.IsNull(def.SpatialIndexBounds)) ?
                 new BinaryTree2Builder(bounds,
                                        ((def.Levels != 0) ? def.Levels : maxAllowedLevel),
                                        ((def.MaxPerNode != 0) ? def.MaxPerNode : 500),
                                        ((def.SplitRatio != 0.0) ? def.SplitRatio : 0.55),
                                        maxVerticesPerNode: 500 * 50) :
                 new BinaryTree2Builder2(def.SpatialIndexBounds, def.Levels, def.MaxPerNode, def.SplitRatio));

            if (filters == null)
            {
                QueryFilter filter = new QueryFilter()
                {
                    WhereClause = "1=1"
                };
                filter.AddField(fc.ShapeFieldName);
                filters = new List <IQueryFilter>();
                filters.Add(filter);
            }
            foreach (IQueryFilter filter in filters)
            {
                using (IFeatureCursor fCursor = await fc.GetFeatures(filter))
                {
                    if (fCursor == null)
                    {
                        throw new Exception("Fatal error: sourcedb query failed...");
                    }

                    Console.WriteLine("Calculate spatial index:");

                    IEnvelope fcEnvelope = bounds;
                    //if (_transformer != null)
                    //{
                    //    IGeometry geom = _transformer.Transform2D(fcEnvelope) as IGeometry;
                    //    if (geom == null)
                    //    {
                    //        _errMsg = "SpatialIndex: Can't project featureclass extent!";
                    //        return null;
                    //    }
                    //    fcEnvelope = geom.Envelope;
                    //}

                    int      counter = 0;
                    IFeature feat;
                    while ((feat = await fCursor.NextFeature()) != null)
                    {
                        IGeometry shape = feat.Shape;
                        //if (_transformer != null)
                        //{
                        //    shape = _transformer.Transform2D(shape) as IGeometry;
                        //}

                        feat.Shape = shape;
                        treeBuilder.AddFeature(feat);

                        counter++;
                        if (counter % 10000 == 0)
                        {
                            Console.Write($"...{ counter }");
                        }
                    }
                    Console.Write($"...{ counter }");
                }
            }

            treeBuilder.Trim();
            Console.WriteLine();

            return(treeBuilder);
        }