Ejemplo n.º 1
0
        public static async Task <Catchment> Get(this ICatchmentsRepository repository, Guid catchmentIdentityValue)
        {
            var catchmentIdentity = CatchmentIdentity.From(catchmentIdentityValue);

            var catchment = await repository.Get(catchmentIdentity);

            return(catchment);
        }
Ejemplo n.º 2
0
        public static Task <CatchmentGeoJson> GetByIdentity(this ICatchmentsRepository catchmentsRepository, Guid catchmentIdentityValue)
        {
            var catchmentIdentity = CatchmentIdentity.From(catchmentIdentityValue);

            var gettingCatchment = catchmentsRepository.GetByIdentity(catchmentIdentity);

            return(gettingCatchment);
        }
Ejemplo n.º 3
0
 public Task <string> GetName(CatchmentIdentity identity)
 {
     return(this.ExecuteInContextAsync(async dbContext =>
     {
         var name = await dbContext.Catchments.GetByIdentity(identity).Select(x => x.Name).SingleAsync();
         return name;
     }));
 }
Ejemplo n.º 4
0
        public Task <LngLat[]> GetBoundary(CatchmentIdentity identity)
        {
            return(this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.Catchments.GetByIdentity(identity).SingleAsync();

                var lngLats = entity.Boundary.ToLngLats();
                return lngLats;
            }));
        }
Ejemplo n.º 5
0
        public async Task <List <CatchmentIdentity> > GetAllCatchmentIdentitiesContainingPoint(LngLat lngLat)
        {
            var catchmentEntities = await this.GetCatchmentEntitiesContainingPoint(lngLat);

            var catchmentIdentityList = catchmentEntities
                                        .Select(x => CatchmentIdentity.From(x.Identity))
                                        .ToList();

            return(catchmentIdentityList);
        }
Ejemplo n.º 6
0
        public async Task Delete(CatchmentIdentity identity)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                var catchmentEntity = await dbContext.Catchments.GetByIdentity(identity).SingleAsync();

                dbContext.Remove(catchmentEntity);

                await dbContext.SaveChangesAsync();
            });
        }
Ejemplo n.º 7
0
        // Getters and setters for catchment components
        public Task SetName(CatchmentIdentity identity, string name)
        {
            return(this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.Catchments.GetByIdentity(identity).SingleAsync();

                entity.Name = name;

                await dbContext.SaveChangesAsync();
            }));
        }
        public static AppTypeGeoJson ToAppTypeGeoJson(this EntityType entityType)
        {
            var appType = new AppTypeGeoJson()
            {
                Identity = CatchmentIdentity.From(entityType.Identity),
                Name     = entityType.Name,
                MultiPolygonGeoJsonText = (entityType.Boundary as MultiPolygon).ToGeoJsonMultiPolygonJsonString(),
            };

            return(appType);
        }
Ejemplo n.º 9
0
        public async Task <bool> Exists(CatchmentIdentity identity)
        {
            var exists = await this.ExecuteInContextAsync(async dbContext =>
            {
                var catchmentWithIdentityExists = await dbContext.Catchments
                                                  .Where(x => x.Identity == identity.Value)
                                                  .AnyAsync();
                return(catchmentWithIdentityExists);
            });

            return(exists);
        }
Ejemplo n.º 10
0
        public async Task <List <AnomalyIdentity> > GetAllAnomaliesInCatchment(CatchmentIdentity catchmentIdentity)
        {
            var anomalies = await this.ExecuteInContextAsync(async dbContext =>
            {
                var anomalyIdentityValues = await dbContext.AnomalyToCatchmentMappings.Where(x => x.CatchmentIdentity == catchmentIdentity.Value).Select(x => x.Anomaly.GUID).ToListAsync();

                var output = anomalyIdentityValues.Select(x => AnomalyIdentity.From(x)).ToList();
                return(output);
            });

            return(anomalies);
        }
Ejemplo n.º 11
0
        // TODO: DEPRECATE
        public async Task <EndpointIdentity> GetEndpointForCatchment(CatchmentIdentity catchment)
        {
            var endpointIdentity = await this.ExecuteInContextAsync(async dbContext =>
            {
                var endpointGuid = await dbContext.EndpointToCatchmentMappings.Where(x => x.CatchmentGUID == catchment.Value).Select(x => x.EndpointGUID).SingleAsync();

                var output = EndpointIdentity.From(endpointGuid);
                return(output);
            });

            return(endpointIdentity);
        }
Ejemplo n.º 12
0
        private async Task AddCatchmentEntity(Entities.Catchment catchmentEntity, CatchmentIdentity catchmentIdentity)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                dbContext.Catchments.Add(catchmentEntity);

                await dbContext.SaveChangesAsync();
            });

            // AFTER the catchment is added, set its grid units!
            await this.GridUnitsRepository.SetGridUnitsForCatchment(catchmentIdentity);
        }
Ejemplo n.º 13
0
        public Task <CatchmentGeoJson> GetByIdentity(CatchmentIdentity catchmentIdentity)
        {
            var gettingCatchment = this.ExecuteInContextAsync(async dbContext =>
            {
                var entityType = await dbContext.Catchments.GetByIdentity(catchmentIdentity).SingleAsync();

                var appTypeGeoJson = entityType.ToAppTypeGeoJson();
                return(appTypeGeoJson);
            });

            return(gettingCatchment);
        }
Ejemplo n.º 14
0
        // TODO: DEPRECATE
        public async Task <bool> CatchmentHasEndpoint(CatchmentIdentity catchmentIdentity)
        {
            var hasEndpoint = await this.ExecuteInContextAsync(async dbContext =>
            {
                var mappingEntityOrDefault = await dbContext.EndpointToCatchmentMappings.Where(x => x.CatchmentGUID == catchmentIdentity.Value).SingleOrDefaultAsync();

                var output = mappingEntityOrDefault is object;
                return(output);
            });

            return(hasEndpoint);
        }
Ejemplo n.º 15
0
        // TODO: DEPRECATE
        public async Task <CatchmentIdentity> GetCatchmentForEndpoint(EndpointIdentity endpointIdentity)
        {
            var catchmentIdentity = await this.ExecuteInContextAsync(async dbContext =>
            {
                var catchmentIdentityValue = await dbContext.EndpointToCatchmentMappings.Where(x => x.EndpointGUID == endpointIdentity.Value).Select(x => x.CatchmentGUID).SingleAsync();

                var output = CatchmentIdentity.From(catchmentIdentityValue);
                return(output);
            });

            return(catchmentIdentity);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Returns the single catchment identity that the anomaly is currently mapped to.
        /// </summary>
        /// <param name="anomalyIdentity"></param>
        /// <returns></returns>
        public async Task <(bool HasCatchment, CatchmentIdentity CatchmentIdentity)> HasCatchment(AnomalyIdentity anomalyIdentity)
        {
            var hasOutput = await this.ExecuteInContext(async dbContext =>
            {
                var output = await dbContext.AnomalyToCatchmentMappings.HasSingleAsync(x => x.Anomaly.GUID == anomalyIdentity.Value);
                //var output = await dbContext.AnomalyToCatchmentMappings.Include(x => x.Anomaly).HasSingleAsync(x => x.Anomaly.GUID.Value == anomalyIdentity.Value);
                //var output = dbContext.AnomalyToCatchmentMappings.Where(x => x.Anomaly.GUID.Value == anomalyIdentity.Value).
                return(output);
            });

            var catchmentIdentity = hasOutput.Exists ? CatchmentIdentity.From(hasOutput.Result.CatchmentIdentity) : null;

            return(hasOutput.Exists, catchmentIdentity);
        }
        public static AppType ToAppType(this EntityType entityType)
        {
            var appType = new AppType()
            {
                Identity = CatchmentIdentity.From(entityType.Identity),
                Name     = entityType.Name,
            };

            var lngLats = entityType.Boundary.ToLngLats();

            appType.Boundary.AddRange(lngLats);

            return(appType);
        }
Ejemplo n.º 18
0
        public static async Task <CatchmentIdentity> Add(this ICatchmentsRepository repository, string name, GeoJsonMultiPolygonJsonString geoJsonMultiPolygonJsonString)
        {
            var catchmentIdentity = CatchmentIdentity.New();

            var catchment = new Catchment()
            {
                Identity = catchmentIdentity,
                Name     = name,
            };

            await repository.Add(geoJsonMultiPolygonJsonString, catchment);

            return(catchmentIdentity);
        }
Ejemplo n.º 19
0
        public async Task <List <CatchmentIdentity> > GetCatchments(AnomalyIdentity anomalyIdentity)
        {
            var catchmentIdentities = await this.ExecuteInContext(async dbContext =>
            {
                var output = await dbContext.AnomalyToCatchmentMappings
                             .Where(x => x.Anomaly.GUID == anomalyIdentity.Value)
                             .Select(x => x.CatchmentIdentity)
                             .Select(x => CatchmentIdentity.From(x))
                             .ToListAsync();

                return(output);
            });

            return(catchmentIdentities);
        }
Ejemplo n.º 20
0
        public async Task SetBoundary(CatchmentIdentity identity, GeoJsonMultiPolygonJsonString newGeoJsonMultiPolygonJsonString)
        {
            var geographyFactory = await this.GeometryFactoryProvider.GetGeometryFactoryAsync();

            var newMultiPolygonGeometry = newGeoJsonMultiPolygonJsonString.ToMultiPolygon(geographyFactory);

            await this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.Catchments.GetByIdentity(identity).SingleAsync();

                entity.Boundary = newMultiPolygonGeometry;

                await dbContext.SaveChangesAsync();
            });
        }
Ejemplo n.º 21
0
        public static async Task <CatchmentIdentity> Add(this ICatchmentsRepository repository, string name, IEnumerable <LngLat> vertices)
        {
            var catchmentIdentity = CatchmentIdentity.New();

            var catchment = new Catchment()
            {
                Identity = catchmentIdentity,
                Name     = name,
            };

            catchment.Boundary.AddRange(vertices);

            await repository.Add(catchment);

            return(catchmentIdentity);
        }
Ejemplo n.º 22
0
        public async Task <Catchment> Get(CatchmentIdentity identity)
        {
            var catchment = await this.ExecuteInContextAsync(async dbContext =>
            {
                var catchmentEntity = await dbContext.Catchments.GetByIdentity(identity).SingleOrDefaultAsync();

                if (catchmentEntity == null)
                {
                    return(null);
                }

                var output = catchmentEntity.ToAppType();
                return(output);
            });

            return(catchment);
        }
Ejemplo n.º 23
0
        public async Task SetBoundary(CatchmentIdentity identity, IEnumerable <LngLat> boundaryVertices)
        {
            var geometryFactory = await this.GeometryFactoryProvider.GetGeometryFactoryAsync();

            var polygon = boundaryVertices.ToPolygon(geometryFactory);

            await this.ExecuteInContextAsync(async dbContext =>
            {
                var entity = await dbContext.Catchments.GetByIdentity(identity).SingleAsync();

                entity.Boundary = polygon;

                await dbContext.SaveChangesAsync();
            });

            // AFTER the catchment is updated, set its grid units!
            await this.GridUnitsRepository.SetGridUnitsForCatchment(identity);
        }
        public async Task SetGridUnitsForCatchment(CatchmentIdentity catchmentIdentity)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                // Get the right catchment (also verifies that it's in the database)
                var catchmentEntity = await dbContext.Catchments
                                      .Where(x => x.Identity == catchmentIdentity.Value)
                                      .SingleAsync();

                // Delete existing grid unit affiliations (but NOT the grid units!!)
                var matchingGridUnitAffiliations = await dbContext.CatchmentGridUnits
                                                   .Where(x => x.CatchmentIdentity == catchmentIdentity.Value)
                                                   .ToListAsync();
                if (matchingGridUnitAffiliations.Count > 0)
                {
                    dbContext.RemoveRange(matchingGridUnitAffiliations);
                    // TODO: do we need this save changes before adding the new entities?
                    await dbContext.SaveChangesAsync();
                }

                // Find its intersecting grid units
                var gridUnitIdentities = await dbContext.GridUnits
                                         .Where(x => x.Boundary.Intersects(catchmentEntity.Boundary))
                                         .Select(x => GridUnitIdentity.From(x.GUID))
                                         .ToListAsync();

                // Add them to the database
                foreach (var gridUnitIdentity in gridUnitIdentities)
                {
                    // TODO: update to addrange once I have time to check that it works
                    // (which should speed this sucker up a bit.)
                    var newCatchmentGridUnitEntity = new Entities.CatchmentGridUnit
                    {
                        GridUnitIdentity  = gridUnitIdentity.Value,
                        CatchmentIdentity = catchmentIdentity.Value
                    };
                    dbContext.Add(newCatchmentGridUnitEntity);
                }

                await dbContext.SaveChangesAsync();
            });
        }
Ejemplo n.º 25
0
        public Task <(CatchmentIdentity Identity, string Name)[]> FindIdentityNamePairsByRegexOnName(Regex regexOnName)
        {
            var catchmentIdentities = this.ExecuteInContextAsync(async dbContext =>
            {
                // Oh dear, there is no SQL-native regex concept, so the implementation must get *all* names and regex locally. (~10,000 should be ok...)
                var allNamesAndIdentities = await dbContext.Catchments
                                            .Select(x => new { x.Name, x.Identity })
                                            .ToListAsync();

                var matchingNamesAndIdentities = allNamesAndIdentities
                                                 .Where(x => regexOnName.IsMatch(x.Name ?? Strings.Empty))
                                                 .ToList();

                var output = matchingNamesAndIdentities
                             .Select(x => (CatchmentIdentity.From(x.Identity), x.Name))
                             .ToArray();

                return(output);
            });

            return(catchmentIdentities);
        }
Ejemplo n.º 26
0
        public async Task <bool> AddCatchment(AnomalyIdentity anomalyIdentity, CatchmentIdentity catchmentIdentity)
        {
            return(await this.ExecuteInContext(async dbContext =>
            {
                // Old version involved this
                // var mappingEntity = await dbContext.AnomalyToCatchmentMappings.Acquire(dbContext.Anomalies, anomalyIdentity.Value);
                // mappingEntity.CatchmentIdentity = catchmentIdentity.Value;

                // Get the anomaly ID from its GUID
                var anomalyID = await dbContext.GetAnomaly(anomalyIdentity.Value)
                                .Select(x => x.ID)
                                .SingleAsync();

                // If the anomaly and catchment are already associated, do nothing.
                var exists = await dbContext.AnomalyToCatchmentMappings
                             .Where(x => x.AnomalyID == anomalyID)
                             .Where(x => x.CatchmentIdentity == catchmentIdentity.Value)
                             .AnyAsync();

                var actuallyAdded = false;
                if (!exists)
                {
                    // Create new entity
                    var newEntity = new Entities.AnomalyToCatchmentMapping {
                        AnomalyID = anomalyID,
                        CatchmentIdentity = catchmentIdentity.Value
                    };

                    // Add it to the database and save
                    dbContext.Add(newEntity);
                    await dbContext.SaveChangesAsync();
                    actuallyAdded = true;
                }

                return actuallyAdded;
            }));
        }
Ejemplo n.º 27
0
        // TODO: DEPRECATE
        public async Task SetEndpointForCatchment(CatchmentIdentity catchment, EndpointIdentity endpoint)
        {
            await this.ExecuteInContextAsync(async dbContext =>
            {
                var mapping = await dbContext.EndpointToCatchmentMappings.Where(x => x.EndpointGUID == endpoint.Value).SingleOrDefaultAsync();

                if (mapping is object)
                {
                    mapping.CatchmentGUID = catchment.Value;
                }
                else
                {
                    mapping = new Entities.EndpointToCatchmentMapping()
                    {
                        EndpointGUID  = endpoint.Value,
                        CatchmentGUID = catchment.Value,
                    };

                    dbContext.Add(mapping);
                }

                await dbContext.SaveChangesAsync();
            });
        }
        public static IQueryable <CatchmentEntity> GetByIdentity(this IQueryable <CatchmentEntity> catchments, CatchmentIdentity catchmentIdentity)
        {
            var queryable = catchments.Where(x => x.Identity == catchmentIdentity.Value);

            return(queryable);
        }
Ejemplo n.º 29
0
        public static async Task <CatchmentInfo> GetCatchmentInfoAsync(this ICatchmentsRepository repository, CatchmentIdentity catchmentIdentity)
        {
            var name = await repository.GetName(catchmentIdentity);

            var catchmentInfo = new CatchmentInfo()
            {
                CatchmentIdentity = catchmentIdentity,
                Name = name,
            };

            return(catchmentInfo);
        }
Ejemplo n.º 30
0
        public static async Task Delete(this ICatchmentsRepository repository, Guid catchmentIdentityValue)
        {
            var catchmentIdentity = CatchmentIdentity.From(catchmentIdentityValue);

            await repository.Delete(catchmentIdentity);
        }