// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - Clients Library.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using NetTopologySuite.Geometries;
namespace Deltares.Maps
{
public static class CoverageFunction
{
public static IEnumerable GetFeaturesCovering(this IFeatureRepository repository, double x, double y)
{
var pointToTest = new Point(x, y);
/*
return (from feature in repository.Features
let target = feature.Geometry
where (pointToTest.Within(target) || pointToTest.Touches(target)) || pointToTest.Intersects(target)
select feature);
*/
/*
var e = pointToTest.EnvelopeInternal;
const int delta = 1;
var bbox = new Envelope(e.MinX - delta, e.MinY - delta, e.MaxX + delta, e.MaxY + delta);
var possibleMatches = repository.Query(bbox);
foreach (var feature in possibleMatches)
{
if (pointToTest.Within(feature.Geometry) || pointToTest.Touches(feature.Geometry) || pointToTest.Intersects(feature.Geometry))
{
yield return feature;
}
}
*/
return repository
.Query(pointToTest)
.Where(feature =>
pointToTest.Within(feature.Geometry) ||
pointToTest.Touches(feature.Geometry) ||
pointToTest.Intersects(feature.Geometry));
}
public static Func GetCoverageFunc(this IFeatureRepository repository)
{
return GetCoverageFunc(repository, repository.SupportedAttributes);
}
public static Func GetCoverageFunc(this IFeatureRepository repository, string attributeName)
{
return GetCoverageFunc(repository, attributeName, repository.SupportedAttributes);
}
public static Func GetCoverageFunc(this IFeatureRepository repository, string attributeName,
IEnumerable supportedAttributeNames)
{
Func func = GetCoverageFunc(repository, supportedAttributeNames);
return (x, y) => func(attributeName, x, y);
}
public static Func GetCoverageFunc(IFeatureRepository repository,
IEnumerable supportedAttributeNames)
{
if (repository == null)
{
throw new ArgumentNullException("repository");
}
if (supportedAttributeNames == null)
{
throw new ArgumentNullException("supportedAttributeNames");
}
if (!supportedAttributeNames.Any())
{
throw new ArgumentException("The supported attribute list should at least contain one attribute",
"supportedAttributeNames");
}
int index = 0;
foreach (string attributeName in supportedAttributeNames)
{
if (string.IsNullOrEmpty(attributeName))
{
throw new ArgumentException(string.Format("An invalid attribute found at index {0}", index));
}
index++;
}
IEnumerable supportedAttributes = supportedAttributeNames.Select(a => a.ToLowerInvariant());
return (attributeName, x, y) =>
{
if (string.IsNullOrEmpty(attributeName))
{
throw new ArgumentNullException("attributeName");
}
string attrName = attributeName.ToLowerInvariant();
if (!supportedAttributes.Contains(attrName))
{
throw new AttributeMissingException(attributeName);
}
IEnumerable matches = GetFeaturesCovering(repository, x, y);
if (!matches.Any())
{
throw new FeatureCoverageException(string.Format("No geometries found covering point ({0} {1})", x, y));
}
if (matches.Count() > 1)
{
throw new FeatureCoverageException(string.Format("Multiple geometries found covering point ({0} {1})", x, y));
}
return matches.First().Attributes[attrName];
};
}
}
}