Beispiel #1
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);
        }
Beispiel #2
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);
        }