Example #1
0
        public static Json.GeometryDocument AppendGeneralizedShapes(
            this Json.GeometryDocument geometryDocument,
            IGeometry geometry, ISpatialReference sRef,
            int generalizationLevel)
        {
            if (generalizationLevel < 0)
            {
                return(geometryDocument);
            }

            var generalized = geometry.Generalize(sRef, generalizationLevel);

            for (int i = 0; i < generalized.Length; i++)
            {
                if (generalized[i] != null)
                {
                    var propertyInfo = geometryDocument.GetType().GetProperty($"ShapeGeneralized{i}");
                    if (propertyInfo != null)
                    {
                        var wkb = gView.Framework.OGC.OGC.GeometryToWKB(generalized[i], 0, Framework.OGC.OGC.WkbByteOrder.Ndr);
                        propertyInfo.SetValue(geometryDocument, wkb);
                        //propertyInfo.SetValue(geometryDocument, generalized[i].ToGeoJsonGeometry<GeoJson2DGeographicCoordinates>());
                    }
                }
            }
            return(geometryDocument);
        }
Example #2
0
        async public Task <bool> Insert(IFeatureClass fClass, List <IFeature> features)
        {
            try
            {
                var featureCollection = await GetFeatureCollection(fClass);

                if (featureCollection == null)
                {
                    throw new Exception("No feature collection. Open database before running CreateFeatureClass method");
                }

                var spatialCollectionItem = await(await _spatialCollectionRef.FindAsync(s => s.Name == fClass.Name)).FirstOrDefaultAsync();
                if (spatialCollectionItem == null)
                {
                    throw new Exception("Feature class not exists");
                }

                IEnvelope bounds = spatialCollectionItem.FeatureBounds?.ToEnvelope();

                int fieldCount = fClass.Fields.Count;

                int degreeOfParallelism = 1;
                var containers          = new List <Json.GeometryDocument> [degreeOfParallelism];
                for (int i = 0; i < degreeOfParallelism; i++)
                {
                    containers[i] = new List <Json.GeometryDocument>();
                }

                var generalizationLevel = fClass is MongoDbFeatureClass ?
                                          ((MongoDbFeatureClass)fClass).GeneralizationLevel :
                                          -1;

                int counter = 0;
                foreach (var feature in features)
                {
                    var document           = new Json.GeometryDocument();
                    var documentProperties = new Dictionary <string, object>();

                    if (feature.Shape != null)
                    {
                        document.Shape = feature.Shape.ToGeoJsonGeometry <GeoJson2DGeographicCoordinates>();
                        if (bounds == null)
                        {
                            bounds = new Envelope(feature.Shape.Envelope);
                        }
                        else
                        {
                            bounds.Union(feature.Shape.Envelope);
                        }
                        if (fClass.GeometryType != geometryType.Point)
                        {
                            document.Bounds = feature.Shape.Envelope.ToPolygon(0).ToGeoJsonGeometry <GeoJson2DGeographicCoordinates>();
                            document.AppendGeneralizedShapes(feature.Shape, fClass.SpatialReference, generalizationLevel);
                        }

                        #region Generalize

                        // dpm = 96/0.0254
                        // tol = pix * s / dpm
                        //
                        // pix = dpm/s   (tol=1) -> 1 Pixel sind beim 1:s pix[m]

                        #endregion
                    }

                    for (int f = 0; f < fieldCount; f++)
                    {
                        var field = fClass.Fields[f];
                        if (field.type == FieldType.ID ||
                            field.type == FieldType.Shape)
                        {
                            continue;
                        }

                        var fieldValue = feature.FindField(field.name);
                        if (fieldValue != null)
                        {
                            if (fieldValue.Value == DBNull.Value)
                            {
                                fieldValue.Value = null;
                            }

                            documentProperties.Add(fieldValue.Name, fieldValue.Value);
                        }
                    }

                    document.Properties = documentProperties;

                    containers[counter % degreeOfParallelism].Add(document);
                    counter++;
                }

                //Task[] tasks = new Task[degreeOfParallelism];
                //for (int i = 0; i < degreeOfParallelism; i++)
                //{
                //    tasks[i] = Task.Factory.StartNew(async (object index) =>
                //    {
                //        await featureCollection.InsertManyAsync(containers[(int)index]);
                //    }, (object)i);
                //}
                //await Task.WhenAll(tasks);

                await featureCollection.InsertManyAsync(containers[0]);

                var updateResult = await _spatialCollectionRef.UpdateOneAsync <Json.SpatialCollectionItem>(
                    c => c.Id == spatialCollectionItem.Id,
                    Builders <Json.SpatialCollectionItem> .Update.Set(i => i.FeatureBounds, new Json.SpatialCollectionItem.Bounds(bounds)));

                return(true);
            }
            catch (Exception ex)
            {
                lastException    = ex;
                LastErrorMessage = ex.Message;

                return(false);
            }
        }