public static GeographyLineString ConvertLineStringTo(DbGeography dbGeo) { Debug.Assert(dbGeo.SpatialTypeName == "LineString"); SpatialBuilder builder = SpatialBuilder.Create(); var pipeLine = builder.GeographyPipeline; pipeLine.SetCoordinateSystem(CoordinateSystem.DefaultGeography); pipeLine.BeginGeography(SpatialType.LineString); int numPionts = dbGeo.PointCount ?? 0; for (int n = 0; n < numPionts; n++) { DbGeography pointN = dbGeo.PointAt(n + 1); double lat = pointN.Latitude ?? 0; double lon = pointN.Longitude ?? 0; double? alt = pointN.Elevation; double? m = pointN.Measure; GeographyPosition position = new GeographyPosition(lat, lon, alt, m); if (n == 0) { pipeLine.BeginFigure(position); } else { pipeLine.LineTo(position); } } pipeLine.EndFigure(); pipeLine.EndGeography(); return((GeographyLineString)builder.ConstructedGeography); }
private static DbGeography GetRandomPointInZone(DbGeography validZone, Random rnd) { DbGeography pos = null; double minlat = double.MaxValue; double maxlat = double.MinValue; double minlon = double.MaxValue; double maxlon = double.MinValue; for (int i = 1; i <= validZone.PointCount; i++) { var point = validZone.PointAt(i); if (point.Latitude != null && minlat > point.Latitude) minlat = (double)point.Latitude; if (point.Longitude != null && minlon > point.Longitude) minlon = (double)point.Longitude; if (point.Latitude != null && maxlat < point.Latitude) maxlat = (double)point.Latitude; if (point.Longitude != null && maxlon < point.Longitude) maxlon = (double)point.Longitude; } int retry = 0; while (pos == null || !pos.Intersects(validZone)) //Aquí comprobamos que la posición aleatoria está dentro de la zona. { var latitude = GetRandomDouble(rnd, minlat, maxlat); var longitude = GetRandomDouble(rnd, minlon, maxlon); var wkt = String.Format("POINT({0} {1})", longitude.ToString(CultureInfo.InvariantCulture), latitude.ToString(CultureInfo.InvariantCulture)); pos = DbGeography.PointFromText(wkt, 4326); retry++; if (retry > 1000) throw new Exception("Parece un bucle infinito, tras 1000 reintentos no se ha podido obtener una posición dentro de la zona válida"); } return pos; }
/// <summary> /// Convert a DbGeography to Edm GeographyPoint /// </summary> /// <param name="geography"> The DbGeography to be converted</param> /// <returns>A Edm GeographyLineString</returns> public static GeographyLineString ToGeographyLineString(this DbGeography geography) { if (geography == null) { return(null); } if (geography.SpatialTypeName != GeographyTypeNameLineString) { throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, Resources.InvalidLineStringGeographyType, geography.SpatialTypeName)); } SpatialBuilder builder = SpatialBuilder.Create(); GeographyPipeline pipleLine = builder.GeographyPipeline; pipleLine.SetCoordinateSystem(CoordinateSystem.DefaultGeography); pipleLine.BeginGeography(SpatialType.LineString); int numPoints = geography.PointCount ?? 0; if (numPoints > 0) { DbGeography point = geography.PointAt(1); pipleLine.BeginFigure(new GeographyPosition( point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure)); for (int n = 2; n <= numPoints; n++) { point = geography.PointAt(n); pipleLine.LineTo(new GeographyPosition( point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure)); } pipleLine.EndFigure(); } pipleLine.EndGeography(); GeographyLineString lineString = (GeographyLineString)builder.ConstructedGeography; return(lineString); }
public static IEnumerable <Point> FromDbLine(DbGeography line) { for (int i = 1; i <= line.PointCount; i++) { var p = line.PointAt(i); yield return(new Point() { Latitude = p.Latitude.Value, Longitude = p.Longitude.Value }); } }
public static LatLngBounds FindBoundingBox(this DbGeography geography) { if (geography == null) { return(null); } if (geography.IsEmpty || geography.StartPoint == null || !geography.PointCount.HasValue || geography.PointCount.Value < 1) { return(null); } double minLat = geography.StartPoint.Latitude.Value; double minLng = geography.StartPoint.Longitude.Value; double maxLat = geography.StartPoint.Latitude.Value; double maxLng = geography.StartPoint.Longitude.Value; if (geography.PointCount < 2) { return new LatLngBounds { SouthWest = new LatLng { Lat = minLat, Lng = minLng }, NorthEast = new LatLng { Lat = maxLat, Lng = maxLng } } } ; for (int i = 2; i <= geography.PointCount; i++) { var point = geography.PointAt(i); minLat = Math.Min(minLat, point.Latitude.Value); minLng = Math.Min(minLng, point.Longitude.Value); maxLat = Math.Max(maxLat, point.Latitude.Value); maxLng = Math.Max(maxLng, point.Longitude.Value); } return(new LatLngBounds { SouthWest = new LatLng { Lat = minLat, Lng = minLng }, NorthEast = new LatLng { Lat = maxLat, Lng = maxLng } }); } }
public static LatLngPath ToLatLngPath(this DbGeography geography) { if (geography == null) { return(null); } if (geography.IsEmpty || geography.StartPoint == null || !geography.PointCount.HasValue || geography.PointCount.Value < 1) { return(null); } var result = new LatLngPath(); for (var i = 1; i <= geography.PointCount.Value; i++) { result.Points.Add(geography.PointAt(i).ToLatLng()); } return(result); }
public static IEnumerable <Pnt> ToPoints(DbGeography geo, bool invertPointCoordinatesInArray = false) { if (geo == null || geo.Length <= 0) { yield break; } for (var i = 1; i < geo.PointCount; i++) { var p = geo.PointAt(i); if (p.Longitude == null || p.Latitude == null) { throw new Exception("не удалось получить координаты точки"); } yield return(new Pnt() { Longitude = p.Longitude.Value, Latitude = p.Latitude.Value }); } }