public static double GetDistance(GeographyPoint point1, GeographyPoint point2) { if (point1 == null) { throw new ArgumentNullException("point1"); } if (point2 == null) { throw new ArgumentNullException("point2"); } return point1.Distance(point2).Value; }
public static bool GetIsIntersects(GeographyPoint point, GeographyPolygon polygon) { if (point == null) { throw new ArgumentNullException("point"); } if (polygon == null) { throw new ArgumentNullException("polygon"); } return point.Intersects(polygon).Value; }
static ODataSpatialTypeUtil() { // Geometry type values. GeometryValue = GeometryFactory.Point(32.0, -10.0).Build(); GeometryPointValue = GeometryFactory.Point(33.1, -11.0).Build(); GeometryLineStringValue = GeometryFactory.LineString(33.1, -11.5).LineTo(35.97, -11).Build(); GeometryPolygonValue = GeometryFactory.Polygon().Ring(33.1, -13.6).LineTo(35.97, -11.15).LineTo(11.45, 87.75).Ring(35.97, -11).LineTo(36.97, -11.15).LineTo(45.23, 23.18).Build(); GeometryCollectionValue = GeometryFactory.Collection().Point(-19.99, -12.0).Build(); GeometryMultiPointValue = GeometryFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build(); GeometryMultiLineStringValue = GeometryFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build(); GeometryMultiPolygonValue = GeometryFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build(); // Geography type values. GeographyValue = GeographyFactory.Point(32.0, -100.0).Build(); GeographyPointValue = GeographyFactory.Point(33.1, -110.0).Build(); GeographyLineStringValue = GeographyFactory.LineString(33.1, -110.0).LineTo(35.97, -110).Build(); GeographyPolygonValue = GeographyFactory.Polygon().Ring(33.1, -110.0).LineTo(35.97, -110.15).LineTo(11.45, 87.75).Ring(35.97, -110).LineTo(36.97, -110.15).LineTo(45.23, 23.18).Build(); GeographyCollectionValue = GeographyFactory.Collection().Point(-19.99, -12.0).Build(); GeographyMultiPointValue = GeographyFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build(); GeographyMultiLineStringValue = GeographyFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build(); GeographyMultiPolygonValue = GeographyFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build(); }
// just for simplicity, assume: // it's x-y plane // polygon hasn't inner rings, it is a rectangle private bool IsIntersection(GeographyPoint point, GeographyPolygon polygon) { var outRing = polygon.Rings.First(); //int pointNum = outRing.Points.Count; double maxLat = double.MinValue; double maxLon = double.MinValue; double minLat = double.MaxValue; double minLon = double.MaxValue; foreach (var pt in outRing.Points) { if (maxLat < pt.Latitude) { maxLat = pt.Latitude; } if (maxLon < pt.Longitude) { maxLon = pt.Longitude; } if (minLat > pt.Latitude) { minLat = pt.Latitude; } if (minLon > pt.Longitude) { minLon = pt.Longitude; } } if (point.Latitude < minLat || point.Latitude > maxLat || point.Longitude < minLon || point.Longitude > maxLon) return false; return true; }
private static double CalculateDistance(GeographyPoint p1, GeographyPoint p2) { // using Haversine formula // refer to http://en.wikipedia.org/wiki/Haversine_formula. var lat1 = Math.PI*p1.Latitude/180; var lat2 = Math.PI*p2.Latitude/180; var lon1 = Math.PI*p1.Longitude/180; var lon2 = Math.PI*p2.Longitude/180; var item1 = Math.Sin((lat1 - lat2)/2)*Math.Sin((lat1 - lat2)/2); var item2 = Math.Cos(lat1)*Math.Cos(lat2)*Math.Sin((lon1 - lon2)/2)*Math.Sin((lon1 - lon2)/2); return Math.Asin(Math.Sqrt(item1 + item2)); }
/// <summary> /// Writes out a geography point's Co-ordinates in JSON /// </summary> /// <param name="writer">The Json Writer being used to write out the results.</param> /// <param name="point">The Point to serialize into JSON</param> private static void WritePointCoordinates(JsonWriter writer, GeographyPoint point) { writer.StartArrayScope(); writer.WriteValue(point.Longitude); writer.WriteValue(point.Latitude); writer.EndScope(); }
/// <summary> /// Writes out a GeographyPoint value in geojson format. /// { /// "__metadata":{"type":"Edm.GeographyPoint"}, /// "type":"Point", /// "coordinates":[Lattitude,Longitude], /// "crs":{"type":"name","properties":{"name":"EPSG:EPSGValue"}} /// } /// </summary> /// <param name="geographyLineStringValue">The geography value to serialize into Json</param> /// <param name="writer">The Json writer being used to write out a Json payload</param> private static void WriteGeographyPoint(GeographyPoint point, JsonWriter writer) { // see http://geojson.org/geojson-spec.html#id9 // { writer.StartObjectScope(); // "__metadata": writer.WriteName(JsonMetadataString); // { writer.StartObjectScope(); // "type" writer.WriteName(JsonTypeString); // "Edm.GeographyPoint" writer.WriteValue(Gml_Edm_GeographyPointName); // } writer.EndScope(); // "type":"Point", writer.WriteName(JsonTypeString); writer.WriteValue(GmlPoint); // "coordinates":[-122.1202778,47.6741667], writer.WriteName(JsonCoOrdinatesString); WritePointCoordinates(writer, point); // "crs": {"type": "name", "properties": {"name": "EPSG:4326"}} WriteCrsElement(writer, point.CoordinateSystem.EpsgId); // } writer.EndScope(); }