async public Task <List <IDatasetElement> > Elements() { if (_layers != null) { return(_layers); } List <IDatasetElement> layers = new List <IDatasetElement>(); foreach (var collectionItem in await _spatialCollectionRef.Find <Json.SpatialCollectionItem>(_ => true).ToListAsync()) { layers.Add(new DatasetElement(await MongoDbFeatureClass.Create(this, collectionItem))); } _layers = layers; return(_layers); }
async static public Task <MongoDbFeatureClass> Create(MongoDbDataset dataset, Json.SpatialCollectionItem spatialCollectoinItem) { var fc = new MongoDbFeatureClass(); fc.Dataset = dataset; fc.Name = spatialCollectoinItem.Name; if (spatialCollectoinItem.GeometryDef != null) { fc.HasZ = spatialCollectoinItem.GeometryDef.HasZ; fc.HasM = spatialCollectoinItem.GeometryDef.HasM; fc.GeometryType = spatialCollectoinItem.GeometryDef.GeometryType; fc.SpatialReference = dataset._spatialReference; // ToDo } var fields = new Fields(); //fields.Add(new Field("_id", FieldType.ID)); fields.Add(new Field("_shape", FieldType.Shape)); if (spatialCollectoinItem.Fields != null) { foreach (var field in spatialCollectoinItem.Fields) { fields.Add(new Field() { name = field.Name, type = field.FieldType }); } } fc.Fields = fields; fc.MongoCollection = await dataset.GetFeatureCollection(fc); fc.Envelope = spatialCollectoinItem.FeatureBounds?.ToEnvelope() ?? new Envelope(-180, -90, 180, 90); fc.GeneralizationLevel = spatialCollectoinItem.GeneralizationLevel; return(fc); }
async public Task <IDatasetElement> Element(string title) { try { var collectionItem = await _spatialCollectionRef .Find <Json.SpatialCollectionItem>(c => c.Name == title) .FirstOrDefaultAsync(); if (collectionItem == null) { return(null); } return(new DatasetElement(await MongoDbFeatureClass.Create(this, collectionItem)) { Title = collectionItem.Name }); } catch (Exception ex) { this.lastException = ex; return(null); } }
public MongoDbFeatureCursor(MongoDbFeatureClass fc, IQueryFilter filter) : base(fc.SpatialReference, filter.FeatureSpatialReference) { IFindFluent <Json.GeometryDocument, Json.GeometryDocument> findFluent = null; int bestLevel = -1; if (fc.GeneralizationLevel >= 0 && fc.GeometryType != Framework.Geometry.geometryType.Point && filter.MapScale > 0) { var resolution = filter.MapScale / _dpm; bestLevel = resolution.BestResolutionLevel(); } if (bestLevel >= 0) { bestLevel = Math.Max(fc.GeneralizationLevel, bestLevel); _shapeFieldname = $"_shapeGen{bestLevel}"; _shapePropertyInfo = typeof(Json.GeometryDocument).GetProperty($"ShapeGeneralized{bestLevel}"); _isWkbShape = true; } if (filter is ISpatialFilter) { ISpatialFilter sFilter = (ISpatialFilter)filter; var env = sFilter.Geometry.Envelope; env.minx = Math.Max(-180, env.minx); env.miny = Math.Max(-90, env.miny); env.maxx = Math.Min(180, env.maxx); env.maxy = Math.Min(90, env.maxy); GeoJson2DGeographicCoordinates bottomleft = new GeoJson2DGeographicCoordinates(env.minx, env.miny); GeoJson2DGeographicCoordinates topleft = new GeoJson2DGeographicCoordinates(env.minx, env.maxy); GeoJson2DGeographicCoordinates topright = new GeoJson2DGeographicCoordinates(env.maxx, env.maxy); GeoJson2DGeographicCoordinates bottomright = new GeoJson2DGeographicCoordinates(env.maxx, env.miny); GeoJson2DGeographicCoordinates[] coord_array = new GeoJson2DGeographicCoordinates[] { bottomleft, topleft, topright, bottomright, bottomleft }; GeoJsonLinearRingCoordinates <GeoJson2DGeographicCoordinates> ringcoord = new GeoJsonLinearRingCoordinates <GeoJson2DGeographicCoordinates>(coord_array); GeoJsonPolygonCoordinates <GeoJson2DGeographicCoordinates> boxcoord = new GeoJsonPolygonCoordinates <GeoJson2DGeographicCoordinates>(ringcoord); GeoJsonPolygon <GeoJson2DGeographicCoordinates> box = new GeoJsonPolygon <GeoJson2DGeographicCoordinates>(boxcoord); var bsonFilter = fc.GeometryType == Framework.Geometry.geometryType.Point ? Builders <Json.GeometryDocument> .Filter .GeoIntersects(x => x.Shape, box) : Builders <Json.GeometryDocument> .Filter .GeoIntersects(x => x.Bounds, box); //.GeoWithinBox(x => x.Shape, env.minx, env.miny, env.maxx, env.maxy); var findOptions = new FindOptions() { BatchSize = 10000 }; findFluent = fc.MongoCollection.Find(bsonFilter, options: findOptions); } else { findFluent = fc.MongoCollection.Find(_ => true); //filter.Limit = 1000; } if (filter.Limit > 0) { findFluent = findFluent.Limit(filter.Limit); } if (filter.BeginRecord > 1) { findFluent = findFluent.Skip(filter.BeginRecord - 1); } if (filter.SubFields != "*") { StringBuilder project = new StringBuilder(); project.Append("{"); foreach (var field in filter.SubFields.Split(' ')) { if (project.Length > 1) { project.Append(","); } if (field == "_id") { project.Append($"{field}:1"); } else if (field == "_shape") { project.Append($"{_shapeFieldname}:1"); } else { project.Append($"\"properties.{field}\":1"); } } project.Append("}"); findFluent = findFluent.Project <Json.GeometryDocument>(project.ToString()); } else { string project = String.Join(",", Enumerable.Range(0, 15).Select(i => $"{{_shapeGen{i}:0}}")); findFluent = findFluent.Project <Json.GeometryDocument>(project); } _cursor = findFluent.ToCursor(); }