/// <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> /// Test if point is located inside bounding box. /// Currently only two dimensions are handled. /// </summary> /// <param name="boundingBox">Bounding box.</param> /// <param name='point'>Point.</param> /// <returns>True if point is located inside bounding box.</returns> public static Boolean IsPointInside(this WebBoundingBox boundingBox, WebPoint point) { return(boundingBox.IsNotNull() && point.IsNotNull() && (boundingBox.Max.X >= point.X) && (boundingBox.Min.X <= point.X) && (boundingBox.Max.Y >= point.Y) && (boundingBox.Min.Y <= point.Y)); }
/// <summary> /// Get a SqlGeometry instance with same /// information as provided WebPoint. /// </summary> /// <param name="point">This point.</param> /// <returns> /// a SqlGeometry instance with same /// information as provided WebPoint. /// </returns> public static SqlGeometry GetGeometry(this WebPoint point) { // Check data. if (point.IsMSpecified && !(point.IsZSpecified)) { throw new ArgumentException("Creating SqlGeometry point with M value but no Z value is not supported!"); } return(SqlGeometry.Parse(new SqlString(point.GetWkt()))); }
/// <summary> /// Convert point to JSON format. /// </summary> /// <param name="point">Point that should be converted.</param> /// <returns>Point in JSON format.</returns> public static String GetJson(this WebPoint point) { if (point.IsNotNull()) { return("[" + point.X.WebToStringR() + ", " + point.Y.WebToStringR() + "]"); } else { return(String.Empty); } }
/// <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> /// 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> /// Test if two points has the same coordinates. /// </summary> /// <param name="point1">Point 1.</param> /// <param name="point2">Point 2.</param> /// <returns>True if two points has the same coordinates.</returns> public static Boolean Equal(this WebPoint point1, WebPoint point2) { if (point1.IsNull() || point2.IsNull()) { // No coordinates to compare. return(false); } if ((point1.IsMSpecified != point2.IsMSpecified) || (point1.IsZSpecified != point2.IsZSpecified)) { // Different dimensions. return(false); } return(((Math.Abs(point1.X - point2.X) < Settings.Default.CoordinateDifferenceLimit)) && ((Math.Abs(point1.Y - point2.Y) < Settings.Default.CoordinateDifferenceLimit)) && ((!point1.IsZSpecified) || ((Math.Abs(point1.Z - point2.Z) < Settings.Default.CoordinateDifferenceLimit))) && ((!point1.IsMSpecified) || ((Math.Abs(point1.M - point2.M) < Settings.Default.CoordinateDifferenceLimit)))); }
/// <summary> /// Get point in Wkt format. /// </summary> /// <param name="point">This point.</param> /// <returns>Point in Wkt format.</returns> public static String GetWkt(this WebPoint point) { StringBuilder wkt; wkt = new StringBuilder("POINT"); wkt.Append("("); wkt.Append(point.X.WebToStringR().Replace(",", ".")); wkt.Append(" "); wkt.Append(point.Y.WebToStringR().Replace(",", ".")); if (point.IsZSpecified) { wkt.Append(" "); wkt.Append(point.Z.WebToStringR().Replace(",", ".")); } if (point.IsMSpecified) { wkt.Append(" "); wkt.Append(point.M.WebToStringR().Replace(",", ".")); } wkt.Append(")"); return(wkt.ToString()); }
/// <summary> /// Get point from SqlGeometry. /// </summary> /// <param name="geometryPoint">Point geometry.</param> /// <returns>Point.</returns> public static WebPoint GetPoint(this SqlGeometry geometryPoint) { WebPoint point; if (geometryPoint.GetGeometryType() != SqlGeometryType.Point) { throw new ArgumentException("Wrong geometry data type in GetPoint. Expected type 'Point', actual type : " + geometryPoint.GetGeometryType().ToString()); } point = new WebPoint(); point.X = (Double)geometryPoint.STX; point.Y = (Double)geometryPoint.STY; point.IsMSpecified = !(geometryPoint.M.ToString().Equals("Null")); if (point.IsMSpecified) { point.M = (Double)geometryPoint.M; } point.IsZSpecified = !(geometryPoint.Z.ToString().Equals("Null")); if (point.IsZSpecified) { point.Z = (Double)geometryPoint.Z; } return(point); }
/// <summary> /// Test if point is located inside polygon. /// Currently only two dimensions are handled. /// </summary> /// <param name="polygon">This polygon.</param> /// <param name='point'>Point.</param> /// <returns>True if point is located inside polygon.</returns> public static Boolean IsPointInsideGeometry(this WebPolygon polygon, WebPoint point) { return(polygon.GetGeometry().STIntersects(point.GetGeometry()).Value); }
/// <summary> /// Check that data is valid. /// </summary> /// <param name="point">This point.</param> public static void CheckData(this WebPoint point) { if (point.IsNotNull()) { } }