/// <summary> /// Convert a point from one coordinate system to /// another coordinate system. /// </summary> /// <param name="context">Web service request context.</param> /// <param name="point">Point that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>WebPoint with coordinates according to toCoordinateSystem</returns> public virtual WebPoint GetConvertedPoint(WebServiceContext context, WebPoint point, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { String cacheKey; WebPoint toPoint; // Get cached information. cacheKey = Settings.Default.PointCacheKey + Settings.Default.CacheKeyDelimiter + point.X.WebToString() + Settings.Default.CacheKeyDelimiter + point.Y.WebToString() + Settings.Default.CacheKeyDelimiter + fromCoordinateSystem.GetWkt() + Settings.Default.CacheKeyDelimiter + toCoordinateSystem.GetWkt(); toPoint = (WebPoint)(context.GetCachedObject(cacheKey)); // Data not in cache - store it in the cache. if (toPoint.IsNull()) { toPoint = GetConvertedPoint(point, fromCoordinateSystem, toCoordinateSystem); // Add information to cache. context.AddCachedObject(cacheKey, toPoint, new TimeSpan(0, 1, 0, 0), CacheItemPriority.Low); } return(toPoint); }
/// <summary> /// Convert a multi polygon from one coordinate system to /// another coordinate system. /// </summary> /// <param name="multiPolygon">Multi polygon that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Multi polygon with coordinates according to toCoordinateSystem</returns> public virtual WebMultiPolygon GetConvertedMultiPolygon(WebMultiPolygon multiPolygon, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 polygonIndex, toPolygonIndex; WebMultiPolygon toMultiPolygon; List <WebPolygon> fromPolygons, toPolygons; toMultiPolygon = null; if (multiPolygon.IsNotNull()) { fromPolygons = new List <WebPolygon>(); for (polygonIndex = 0; polygonIndex < multiPolygon.Polygons.Count; polygonIndex++) { fromPolygons.Add(multiPolygon.Polygons[polygonIndex]); } toPolygons = GetConvertedPolygons(fromPolygons, fromCoordinateSystem, toCoordinateSystem); toPolygonIndex = 0; toMultiPolygon = new WebMultiPolygon(); toMultiPolygon.Polygons = new List <WebPolygon>(); for (polygonIndex = 0; polygonIndex < multiPolygon.Polygons.Count; polygonIndex++) { toMultiPolygon.Polygons.Add(toPolygons[toPolygonIndex]); toPolygonIndex++; } } return(toMultiPolygon); }
/// <summary> /// Convert polygon from one coordinate system to /// another coordinate system. /// </summary> /// <param name="polygon">Polygon that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Polygon with coordinates according to toCoordinateSystem</returns> public virtual WebPolygon GetConvertedPolygon(WebPolygon polygon, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 linearRingIndex, toLinearRingIndex; List <WebLinearRing> fromLinearRings, toLinearRings; WebPolygon toPolygon; toPolygon = null; if (polygon.IsNotNull()) { toPolygon = new WebPolygon(); fromLinearRings = new List <WebLinearRing>(); for (linearRingIndex = 0; linearRingIndex < polygon.LinearRings.Count; linearRingIndex++) { fromLinearRings.Add(polygon.LinearRings[linearRingIndex]); } toLinearRings = GetConvertedLinearRings(fromLinearRings, fromCoordinateSystem, toCoordinateSystem); toLinearRingIndex = 0; toPolygon.LinearRings = new List <WebLinearRing>(); for (linearRingIndex = 0; linearRingIndex < polygon.LinearRings.Count; linearRingIndex++) { toPolygon.LinearRings.Add(toLinearRings[toLinearRingIndex]); toLinearRingIndex++; } } return(toPolygon); }
/// <summary> /// Convert points from one coordinate system to /// another coordinate system. /// </summary> /// <param name="points">Points that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Points with coordinates according to toCoordinateSystem</returns> public virtual List <WebPoint> GetConvertedPoints(List <WebPoint> points, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 pointIndex; ICoordinateTransformation transformator; List <double[]> fromPointsValues, toPointsValues; List <WebPoint> toPoints; toPoints = new List <WebPoint>(); if (points.IsNotEmpty()) { fromPointsValues = new List <double[]>(); for (pointIndex = 0; pointIndex < points.Count; pointIndex++) { fromPointsValues.Add(new double[] { points[pointIndex].X, points[pointIndex].Y }); } transformator = GetTransformator(fromCoordinateSystem, toCoordinateSystem); toPointsValues = transformator.MathTransform.TransformList(fromPointsValues); toPoints = new List <WebPoint>(); for (pointIndex = 0; pointIndex < points.Count; pointIndex++) { toPoints.Add(new WebPoint(toPointsValues[pointIndex][0], toPointsValues[pointIndex][1])); } } return(toPoints); }
/// <summary> /// Get region geography cache key. /// </summary> /// <param name="regionUniqueString">String that is unique for a region.</param> /// <param name="coordinateSystem">Coordinate system used in returned geography information.</param> /// <returns>Region geography cache key.</returns> private String GetRegionGeographyCacheKey(String regionUniqueString, WebCoordinateSystem coordinateSystem) { return(Settings.Default.RegionGeographyCacheKey + Settings.Default.CacheKeyDelimiter + regionUniqueString + Settings.Default.CacheKeyDelimiter + coordinateSystem.GetWkt()); }
/// <summary> /// Get coordinate converter transformator. /// </summary> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Coordinate converter transformator.</returns> private ICoordinateTransformation GetTransformator(WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { CoordinateTransformationFactory factory; ICoordinateSystem fromSystem, toSystem; factory = new CoordinateTransformationFactory(); fromSystem = CoordinateSystemWktReader.Parse(fromCoordinateSystem.GetWkt()) as ICoordinateSystem; toSystem = CoordinateSystemWktReader.Parse(toCoordinateSystem.GetWkt()) as ICoordinateSystem; return(factory.CreateFromCoordinateSystems(fromSystem, toSystem)); }
/// <summary> /// Get geography for regions. /// </summary> /// <param name="context">Web service request context.</param> /// <param name="regionIds">Region ids.</param> /// <param name="coordinateSystem">Coordinate system used in returned geography information.</param> /// <returns>Geography for regions.</returns> public virtual List <WebRegionGeography> GetRegionsGeographyByIds(WebServiceContext context, List <Int32> regionIds, WebCoordinateSystem coordinateSystem) { List <Int32> notChachedRegionIds; List <WebRegionGeography> notCachedRegionsGeography, regionsGeography; String regionGeographyGuidCacheKey; WebClientInformation clientInformation; WebRegionGeography regionGeography; regionsGeography = new List <WebRegionGeography>(); notChachedRegionIds = new List <Int32>(); if (regionIds.IsNotEmpty()) { foreach (Int32 regionId in regionIds) { // Get cached information. regionGeographyGuidCacheKey = GetRegionGeographyCacheKey(regionId, coordinateSystem); regionGeography = (WebRegionGeography)(context.GetCachedObject(regionGeographyGuidCacheKey)); if (regionGeography.IsNull()) { // Data not in cache - add GUID to not cached GUIDs. notChachedRegionIds.Add(regionId); } else { regionsGeography.Add(regionGeography); } } } if (notChachedRegionIds.IsNotEmpty()) { // Get information from geo reference service. clientInformation = GetClientInformation(context, WebServiceId.GeoReferenceService); notCachedRegionsGeography = WebServiceProxy.GeoReferenceService.GetRegionsGeographyByIds(clientInformation, notChachedRegionIds, coordinateSystem); regionsGeography.AddRange(notCachedRegionsGeography); foreach (Int32 regionId in notChachedRegionIds) { // Add information to cache. regionGeographyGuidCacheKey = GetRegionGeographyCacheKey(regionId, coordinateSystem); context.AddCachedObject(regionGeographyGuidCacheKey, notCachedRegionsGeography.Get(regionId), DateTime.Now + new TimeSpan(1, 0, 0, 0), CacheItemPriority.BelowNormal); } } return(regionsGeography); }
/// <summary> /// Convert region geography from one coordinate system to /// another coordinate system. /// </summary> /// <param name="regionGeography">Region geography that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Region geography with coordinates according to toCoordinateSystem</returns> public virtual WebRegionGeography GetConvertedRegionGeography(WebRegionGeography regionGeography, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { WebRegionGeography toRegionGeography; toRegionGeography = regionGeography; toRegionGeography.MultiPolygon = GetConvertedMultiPolygon(regionGeography.MultiPolygon, fromCoordinateSystem, toCoordinateSystem); toRegionGeography.BoundingBox = toRegionGeography.MultiPolygon.GetBoundingBox(); return(toRegionGeography); }
/// <summary> /// Get coordinate system as string. /// </summary> /// <param name="coordinateSystem">Coordinate system.</param> /// <returns>Coordinate system as string.</returns> public static String WebToString(this WebCoordinateSystem coordinateSystem) { if (coordinateSystem.IsNull()) { return(String.Empty); } else { return("Coordinate system: Id = " + coordinateSystem.Id + ", Wkt = [" + coordinateSystem.GetWkt() + "]" + coordinateSystem.DataFields.WebToString()); } }
/// <summary> /// Convert a point from one coordinate system to /// another coordinate system. /// </summary> /// <param name="point">Point that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>WebPoint with coordinates according to toCoordinateSystem</returns> public virtual WebPoint GetConvertedPoint(WebPoint point, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { double[] fromPointValues, toPointValues, middlePointValues; ICoordinateTransformation transformator; WebPoint toPoint; fromPointValues = new double[] { point.X, point.Y }; middlePointValues = new double[] { point.X, point.Y }; //if (toCoordinateSystem.Id == CoordinateSystemId.ETRS89_LAEA) //{ // WebCoordinateSystem middle = new WebCoordinateSystem(); // middle.Id = CoordinateSystemId.WGS84; // transformator = GetTransformator(fromCoordinateSystem, middle); // middlePointValues = transformator.MathTransform.Transform(fromPointValues); // ProjectionInfo fromProjection = KnownCoordinateSystems.Geographic.World.WGS1984; // //EPSG 4326 // ProjectionInfo toProjection = KnownCoordinateSystems.Projected.Europe.ETRS1989LAEA; // //toPointValues = new double[2]; // fromZ = new double[] {0}; // double[] fromXY = new double[] { middlePointValues[1], middlePointValues[0]}; // //ProjectionInfo Lamb = KnownCoordinateSystems.Projected.Europe.ETRS1989LAEA; // //Lamb.LatitudeOfOrigin = 52; // //Lamb.LongitudeOfCenter = 10; // //Lamb.FalseEasting = 4321000; // //Lamb.FalseNorthing = 3210000; // Reproject.ReprojectPoints(fromXY, fromZ, fromProjection, toProjection, 0, 1); // // Reproject.ReprojectPoints(fromXY, fromZ, fromProjection, toProjection, 0, 1); // toPointValues = fromXY; //} //else //{ transformator = GetTransformator(fromCoordinateSystem, toCoordinateSystem); toPointValues = transformator.MathTransform.Transform(fromPointValues); // } toPoint = new WebPoint(toPointValues[0], toPointValues[1]); return(toPoint); }
/// <summary> /// Convert bounding box from one coordinate system to /// another coordinate system. /// Converted bounding box is returned as a polygon /// since it probably is not a rectangle any more. /// </summary> /// <param name="boundingBox">Bounding box that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Polygon with coordinates according to toCoordinateSystem</returns> public virtual WebPolygon GetConvertedBoundingBox(WebBoundingBox boundingBox, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { WebPolygon toPolygon; toPolygon = null; if (boundingBox.IsNotNull()) { toPolygon = boundingBox.GetPolygon(); toPolygon = GetConvertedPolygon(toPolygon, fromCoordinateSystem, toCoordinateSystem); } return(toPolygon); }
public static WebCoordinateSystem GetWebCoordinateSystem(this WebGridSpecification gridSpecification) { WebCoordinateSystem gridCellCoordinateSystemAsWebCoordinateSystem = null; foreach (CoordinateSystemId coordinateSystemId in Enum.GetValues(typeof(CoordinateSystemId))) { if (coordinateSystemId.ToString().Equals(gridSpecification.GridCoordinateSystem.ToString())) { gridCellCoordinateSystemAsWebCoordinateSystem = new WebCoordinateSystem {Id = coordinateSystemId}; break; } } if (gridCellCoordinateSystemAsWebCoordinateSystem.IsNull()) { throw new ArgumentException(string.Format("GridCellCoordinateSystem don't match any existing CoordinateSystem. {0} doesn't exist in CoordinateSystem as enum value.", gridSpecification.GridCoordinateSystem)); } return gridCellCoordinateSystemAsWebCoordinateSystem; }
/// <summary> /// Test if point is located inside region. /// Currently only two dimensions are handled. /// </summary> /// <param name="regionGeography">This region geography.</param> /// <param name="context">Web service request context.</param> /// <param name="coordinateSystem">Coordinate system used in region.</param> /// <param name='point'>Point.</param> /// <returns>True if point is located inside region.</returns> public static Boolean IsPointInsideGeography(this WebRegionGeography regionGeography, WebServiceContext context, WebCoordinateSystem coordinateSystem, WebPoint point) { SqlGeography geographyMultiPolygon, geographyPoint; if (regionGeography.BoundingBox.IsPointInside(point)) { geographyPoint = point.GetGeography(); geographyMultiPolygon = regionGeography.GetMultiPolygonGeography(context, coordinateSystem); return(geographyMultiPolygon.STIntersects(geographyPoint).Value); } else { // Species observation can not be inside region // since it is not inside the regions bounding box. return(false); } }
/// <summary> /// Check that data is valid. /// </summary> /// <param name="coordinateSystem">This coordinate system.</param> public static void CheckData(this WebCoordinateSystem coordinateSystem) { String wkt; coordinateSystem.CheckNotNull("coordinateSystem"); if (coordinateSystem.Id == CoordinateSystemId.None) { // Verify externally defined WKT. // Internally defined WKT's are assumed to be correct. wkt = coordinateSystem.GetWkt(); wkt.CheckNotEmpty("coordinateSystem.wkt"); try { CoordinateSystemWktReader.Parse(wkt); } catch { throw new ArgumentException("Wrong format in coordinate system: " + wkt); } } }
/// <summary> /// Convert linear rings from one coordinate system to /// another coordinate system. /// </summary> /// <param name="linearRings">Linear rings that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Linear rings with coordinates according to toCoordinateSystem</returns> public virtual List <WebLinearRing> GetConvertedLinearRings(List <WebLinearRing> linearRings, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 linearRingIndex, pointIndex, toPointIndex; List <WebPoint> fromPoints, toPoints; List <WebLinearRing> toLinearRings; toLinearRings = new List <WebLinearRing>(); if (linearRings.IsNotEmpty()) { fromPoints = new List <WebPoint>(); for (linearRingIndex = 0; linearRingIndex < linearRings.Count; linearRingIndex++) { for (pointIndex = 0; pointIndex < linearRings[linearRingIndex].Points.Count; pointIndex++) { fromPoints.Add(linearRings[linearRingIndex].Points[pointIndex]); } } toPoints = GetConvertedPoints(fromPoints, fromCoordinateSystem, toCoordinateSystem); toLinearRings = new List <WebLinearRing>(); toPointIndex = 0; for (linearRingIndex = 0; linearRingIndex < linearRings.Count; linearRingIndex++) { toLinearRings.Add(new WebLinearRing()); toLinearRings[linearRingIndex].Points = new List <WebPoint>(); for (pointIndex = 0; pointIndex < linearRings[linearRingIndex].Points.Count; pointIndex++) { toLinearRings[linearRingIndex].Points.Add(toPoints[toPointIndex]); toPointIndex++; } } } return(toLinearRings); }
/// <summary> /// Convert multi polygons from one coordinate system to /// another coordinate system. /// </summary> /// <param name="multiPolygons">Multi polygons that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Multi polygons with coordinates according to toCoordinateSystem</returns> public virtual List <WebMultiPolygon> GetConvertedMultiPolygons(List <WebMultiPolygon> multiPolygons, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 polygonIndex, multiPolygonIndex, toPolygonIndex; List <WebMultiPolygon> toMultiPolygons; List <WebPolygon> fromPolygons, toPolygons; toMultiPolygons = new List <WebMultiPolygon>(); if (multiPolygons.IsNotEmpty()) { fromPolygons = new List <WebPolygon>(); for (multiPolygonIndex = 0; multiPolygonIndex < multiPolygons.Count; multiPolygonIndex++) { for (polygonIndex = 0; polygonIndex < multiPolygons[multiPolygonIndex].Polygons.Count; polygonIndex++) { fromPolygons.Add(multiPolygons[multiPolygonIndex].Polygons[polygonIndex]); } } toPolygons = GetConvertedPolygons(fromPolygons, fromCoordinateSystem, toCoordinateSystem); toMultiPolygons = new List <WebMultiPolygon>(); toPolygonIndex = 0; for (multiPolygonIndex = 0; multiPolygonIndex < multiPolygons.Count; multiPolygonIndex++) { toMultiPolygons.Add(new WebMultiPolygon()); toMultiPolygons[multiPolygonIndex].Polygons = new List <WebPolygon>(); for (polygonIndex = 0; polygonIndex < multiPolygons[multiPolygonIndex].Polygons.Count; polygonIndex++) { toMultiPolygons[multiPolygonIndex].Polygons.Add(toPolygons[toPolygonIndex]); toPolygonIndex++; } } } return(toMultiPolygons); }
/// <summary> /// Get envelope for polygons in JSON format. /// </summary> /// <param name="polygons">Get envelope for these polygons.</param> /// <param name="buffer">Add this buffer to envelope. Unit is meters.</param> /// <param name="coordinateSystem">Polygons are defined in this coordinate system.</param> /// <returns>Envelope for polygons in JSON format.</returns> public static String GetEnvelopeJson(this List <WebPolygon> polygons, WebCoordinateSystem coordinateSystem, Int32 buffer) { String envelopeJson; WebBoundingBox boundingBox; WebCoordinateSystem googleCoordinateSystem; envelopeJson = null; if (polygons.IsNotEmpty()) { // Convert polygons to Google Mercator. googleCoordinateSystem = new WebCoordinateSystem(); googleCoordinateSystem.Id = CoordinateSystemId.GoogleMercator; if (googleCoordinateSystem.GetWkt() != coordinateSystem.GetWkt()) { polygons = WebServiceData.CoordinateConversionManager.GetConvertedPolygons(polygons, coordinateSystem, googleCoordinateSystem); } // Get bounding box with buffer. boundingBox = polygons.GetBoundingBox(); boundingBox.AddBuffer(buffer); if (googleCoordinateSystem.GetWkt() != coordinateSystem.GetWkt()) { boundingBox = WebServiceData.CoordinateConversionManager.GetConvertedBoundingBox(boundingBox, googleCoordinateSystem, coordinateSystem).GetBoundingBox(); } envelopeJson = boundingBox.GetJson(); } return(envelopeJson); }
/// <summary> /// Convert polygons from one coordinate system to /// another coordinate system. /// </summary> /// <param name="polygons">Polygons that should be converted.</param> /// <param name="fromCoordinateSystem">From coordinate system.</param> /// <param name="toCoordinateSystem">To coordinate system.</param> /// <returns>Polygons with coordinates according to toCoordinateSystem</returns> public virtual List <WebPolygon> GetConvertedPolygons(List <WebPolygon> polygons, WebCoordinateSystem fromCoordinateSystem, WebCoordinateSystem toCoordinateSystem) { Int32 linearRingIndex, polygonIndex, toLinearRingIndex; List <WebLinearRing> fromLinearRings, toLinearRings; List <WebPolygon> toPolygons; toPolygons = new List <WebPolygon>(); if (polygons.IsNotEmpty()) { fromLinearRings = new List <WebLinearRing>(); for (polygonIndex = 0; polygonIndex < polygons.Count; polygonIndex++) { for (linearRingIndex = 0; linearRingIndex < polygons[polygonIndex].LinearRings.Count; linearRingIndex++) { fromLinearRings.Add(polygons[polygonIndex].LinearRings[linearRingIndex]); } } toLinearRings = GetConvertedLinearRings(fromLinearRings, fromCoordinateSystem, toCoordinateSystem); toPolygons = new List <WebPolygon>(); toLinearRingIndex = 0; for (polygonIndex = 0; polygonIndex < polygons.Count; polygonIndex++) { toPolygons.Add(new WebPolygon()); toPolygons[polygonIndex].LinearRings = new List <WebLinearRing>(); for (linearRingIndex = 0; linearRingIndex < polygons[polygonIndex].LinearRings.Count; linearRingIndex++) { toPolygons[polygonIndex].LinearRings.Add(toLinearRings[toLinearRingIndex]); toLinearRingIndex++; } } } return(toPolygons); }
/// <summary> /// Get a SqlGeometry instance with same information as /// property MultiPolygon in provided WebRegionGeography. /// </summary> /// <param name="regionGeography">This region geography.</param> /// <param name="context">Web service request context.</param> /// <param name="coordinateSystem">Coordinate system used in region.</param> /// <returns> /// A SqlGeometry instance with same information as /// property MultiPolygon in provided WebRegionGeography. /// </returns> private static SqlGeometry GetMultiPolygonGeometry(this WebRegionGeography regionGeography, WebServiceContext context, WebCoordinateSystem coordinateSystem) { SqlGeometry geometryMultiPolygon; String cacheKey; // Get cached information. cacheKey = "RegionSqlGeometry:" + regionGeography.Id + ":CoordinateSystem:" + coordinateSystem.GetWkt(); geometryMultiPolygon = (SqlGeometry)context.GetCachedObject(cacheKey); if (geometryMultiPolygon.IsNull()) { geometryMultiPolygon = regionGeography.MultiPolygon.GetGeometry(); // Add information to cache. context.AddCachedObject(cacheKey, geometryMultiPolygon, new TimeSpan(1, 0, 0), CacheItemPriority.BelowNormal); } return(geometryMultiPolygon); }
/// <summary> /// Get protected species observation indication access rights /// in Json format. /// </summary> /// <param name="roles">Current roles.</param> /// <param name="context">Web service request context.</param> /// <returns>Protected species observation indication access rights in Json format.</returns> public static String GetProtectedSpeciesObservationIndicationAccessRightsJson(this List <WebRole> roles, WebServiceContext context) { Boolean isFirstAuthority; List <WebAuthority> authorities; List <WebPolygon> polygons; List <WebRegionGeography> regionGeographys; StringBuilder filter; WebCoordinateSystem speciesObservationCoordinateSystem; filter = new StringBuilder(); authorities = new List <WebAuthority>(); foreach (WebRole role in roles) { if (role.Authorities.IsNotEmpty()) { foreach (WebAuthority authority in role.Authorities) { if ((authority.Identifier == AuthorityIdentifier.SightingIndication.ToString()) && authority.RegionGUIDs.IsNotEmpty()) { authorities.Add(authority); } } } } if (authorities.IsNotEmpty()) { isFirstAuthority = true; if (1 < authorities.Count) { filter.Append("{\"bool\":{ \"should\" : ["); } foreach (WebAuthority authority in authorities) { if (isFirstAuthority) { isFirstAuthority = false; } else { filter.Append(", "); } if (authority.RegionGUIDs.IsNotEmpty()) { filter.Append("{\"bool\":{ \"must\" : ["); speciesObservationCoordinateSystem = new WebCoordinateSystem(); speciesObservationCoordinateSystem.Id = CoordinateSystemId.WGS84; regionGeographys = WebServiceData.RegionManager.GetRegionsGeographyByGuids(context, authority.RegionGUIDs, speciesObservationCoordinateSystem); polygons = new List <WebPolygon>(); foreach (WebRegionGeography regionGeography in regionGeographys) { polygons.AddRange(regionGeography.MultiPolygon.Polygons); } filter.Append("{ \"geo_shape\": {"); //filter.Append(" \"_cache\": true, "); filter.Append(" \"Location\": {"); filter.Append(" \"shape\": {"); filter.Append(" \"type\": \"envelope\", "); filter.Append(" \"coordinates\": "); filter.Append(polygons.GetEnvelopeJson(speciesObservationCoordinateSystem, 10)); filter.Append("}}}}"); filter.Append(", "); filter.Append("{ \"geo_shape\": {"); //filter.Append(" \"_cache\": true, "); filter.Append(" \"Location\": {"); filter.Append(" \"shape\": {"); filter.Append(" \"type\": \"multipolygon\", "); filter.Append(" \"coordinates\": "); filter.Append(polygons.GetJson()); filter.Append("}}}}"); // End AND condition. filter.Append("]}}"); } } if (1 < authorities.Count) { // End OR condition. filter.Append("]}}"); } } return(filter.ToString()); }
/// <summary> /// Get species observation access rights in Json format. /// </summary> /// <param name="roles">Current roles.</param> /// <param name="context">Web service request context.</param> /// <returns>Species observation access rights in Json format.</returns> public static String GetSpeciesObservationAccessRightsJson(this List <WebRole> roles, WebServiceContext context) { Int32 index; List <Int32> taxonIds; List <WebAuthority> authorities; List <WebPolygon> polygons; List <WebRegionGeography> regionGeographys; StringBuilder filter; WebCoordinateSystem speciesObservationCoordinateSystem; filter = new StringBuilder(); if (roles.IsSimpleSpeciesObservationAccessRights()) { filter.Append("{ \"range\": {"); filter.Append(" \"Conservation_ProtectionLevel\": {"); filter.Append(" \"lte\": " + roles.GetMaxProtectionLevel().WebToString()); filter.Append("}}}"); } else { authorities = new List <WebAuthority>(); foreach (WebRole role in roles) { if (role.Authorities.IsNotEmpty()) { foreach (WebAuthority authority in role.Authorities) { if (authority.Identifier == AuthorityIdentifier.Sighting.ToString()) { authorities.Add(authority); } } } } if (authorities.IsNotEmpty()) { // Start OR condition. filter.Append("{\"bool\":{ \"should\" : ["); filter.Append("{ \"range\": {"); filter.Append(" \"Conservation_ProtectionLevel\": {"); filter.Append(" \"lte\": " + roles.GetMaxProtectionLevelSimpleSpeciesObservationAccessRights().WebToString()); filter.Append("}}}"); foreach (WebAuthority authority in authorities) { if (!IsSimpleSpeciesObservationAccessRights(authority)) { filter.Append(", "); if (authority.RegionGUIDs.IsNotEmpty() || authority.TaxonGUIDs.IsNotEmpty()) { // Start AND condition. filter.Append("{\"bool\":{ \"must\" : ["); } filter.Append("{ \"range\": {"); filter.Append(" \"Conservation_ProtectionLevel\": {"); filter.Append(" \"lte\": " + authority.MaxProtectionLevel.WebToString()); filter.Append("}}}"); if (authority.RegionGUIDs.IsNotEmpty()) { filter.Append(", "); speciesObservationCoordinateSystem = new WebCoordinateSystem(); speciesObservationCoordinateSystem.Id = CoordinateSystemId.WGS84; regionGeographys = WebServiceData.RegionManager.GetRegionsGeographyByGuids(context, authority.RegionGUIDs, speciesObservationCoordinateSystem); polygons = new List <WebPolygon>(); foreach (WebRegionGeography regionGeography in regionGeographys) { polygons.AddRange(regionGeography.MultiPolygon.Polygons); } filter.Append("{ \"geo_shape\": {"); //filter.Append(" \"_cache\": true, "); filter.Append(" \"Location\": {"); filter.Append(" \"shape\": {"); filter.Append(" \"type\": \"envelope\", "); filter.Append(" \"coordinates\": "); filter.Append(polygons.GetEnvelopeJson(speciesObservationCoordinateSystem, 10)); filter.Append("}}}}"); filter.Append(", "); filter.Append("{ \"geo_shape\": {"); //filter.Append(" \"_cache\": true, "); filter.Append(" \"Location\": {"); filter.Append(" \"shape\": {"); filter.Append(" \"type\": \"multipolygon\", "); filter.Append(" \"coordinates\": "); filter.Append(polygons.GetJson()); filter.Append("}}}}"); } if (authority.TaxonGUIDs.IsNotEmpty()) { filter.Append(", "); taxonIds = new List <Int32>(); foreach (String taxonGuid in authority.TaxonGUIDs) { taxonIds.Add(taxonGuid.WebParseInt32()); } taxonIds = WebServiceData.TaxonManager.GetChildTaxonIds(context, taxonIds); filter.Append("{ \"terms\": {"); filter.Append(" \"Taxon_DyntaxaTaxonID\":["); filter.Append(taxonIds[0].WebToString()); for (index = 1; index < taxonIds.Count; index++) { filter.Append(", " + taxonIds[index].WebToString()); } filter.Append("]}}"); } if (authority.RegionGUIDs.IsNotEmpty() || authority.TaxonGUIDs.IsNotEmpty()) { // End AND condition. filter.Append("]}}"); } } } // End OR condition. filter.Append("]}}"); } } return(filter.ToString()); }
/// <summary> /// Get coordinate system wkt. /// </summary> /// <param name="coordinateSystem">Coordinate system.</param> /// <returns>Coordinate system wkt.</returns> public static String GetWkt(this WebCoordinateSystem coordinateSystem) { return(coordinateSystem.Id.GetWkt(coordinateSystem.WKT)); }
/// <summary> /// Get region geography cache key. /// </summary> /// <param name="regionId">Region id.</param> /// <param name="coordinateSystem">Coordinate system used in returned geography information.</param> /// <returns>Region geography cache key.</returns> private String GetRegionGeographyCacheKey(Int32 regionId, WebCoordinateSystem coordinateSystem) { return(GetRegionGeographyCacheKey(regionId.WebToString(), coordinateSystem)); }
/// <summary> /// Constructor. /// </summary> public SpeciesObservationManager() { SpeciesObservationCoordinateSystem = new WebCoordinateSystem(); SpeciesObservationCoordinateSystem.Id = CoordinateSystemId.GoogleMercator; }