/// <summary> /// Parse JSON serialized route coordinate array /// </summary> /// <param name="db"></param> /// <param name="route"></param> /// <param name="jsonShapes"></param> private void SaveNewRouteShape(TrolleyTrackerContext db, Route route, string jsonShapes) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic shapeData = jss.Deserialize(jsonShapes, typeof(object)) as dynamic; Coordinate lastCoordinate = null; var totalDistance = 0.0; int sequence = 0; int nodeCount = shapeData.Length; for (int i = 0; i < nodeCount; i++) { var node = shapeData[i]; var lon = Convert.ToDouble(node.Lon); var lat = Convert.ToDouble(node.Lat); var thisCoordinate = new Coordinate(lat, lon); double distance = 0.0; if (lastCoordinate != null) { distance = thisCoordinate.GreatCircleDistance(lastCoordinate); } lastCoordinate = thisCoordinate; totalDistance += distance; // Add new point if not duplicated (some shape editors create duplicate nodes) if (distance > 0.0) { var dbShape = new TrolleyTracker.Models.Shape(); dbShape.Lat = lat; dbShape.Lon = lon; dbShape.RouteID = route.ID; dbShape.Sequence = sequence; dbShape.DistanceTraveled = totalDistance; sequence++; db.Shapes.Add(dbShape); } } db.SaveChanges(); }
public ActionResult Create(FormCollection collection) { try { Coordinate lastCoordinate = null; var strRouteID = collection.Get("RouteID"); // Parse Geo-JSON formatted route coordinates var jsonShapes = collection.Get("JSONText"); if (jsonShapes != null && strRouteID != null) { int routeID = Convert.ToInt32(strRouteID); using (var db = new TrolleyTracker.Models.TrolleyTrackerContext()) { RemoveOldShape(routeID, db); JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic shapeData = jss.Deserialize(jsonShapes, typeof(object)) as dynamic; var features = shapeData.features; var geometryBlock = features[0]; var geometry = geometryBlock["geometry"]; var geoJSONtype = geometry["type"]; var coordinates = geometry["coordinates"]; int segmentCount = coordinates.Count; if (geoJSONtype == "LineString") segmentCount = 1; int sequence = 0; double totalDistance = 0.0; for (int seg = 0; seg < segmentCount; seg++) { var coordArray = coordinates[seg]; if (geoJSONtype == "LineString") coordArray = coordinates; int nodeCount = coordArray.Count; for (int i = 0; i < nodeCount; i++) { var node = coordArray[i]; var strLon = node[0]; var strLat = node[1]; var lon = Convert.ToDouble(strLon); var lat = Convert.ToDouble(strLat); var thisCoordinate = new Coordinate(0, lat, lon, null); double distance = 0.0; if (lastCoordinate != null) { distance = thisCoordinate.GreatCircleDistance(lastCoordinate); } lastCoordinate = thisCoordinate; totalDistance += distance; var dbShape = new TrolleyTracker.Models.Shape(); dbShape.Lat = lat; dbShape.Lon = lon; dbShape.RouteID = routeID; dbShape.Sequence = sequence; dbShape.DistanceTraveled = totalDistance; sequence++; db.Shapes.Add(dbShape); } } db.SaveChanges(); var assignStops = new AssignStopsToRoutes(); assignStops.UpdateStopsForRoute(db, routeID); } } return RedirectToAction("Index"); } catch { return View(); } }
public ActionResult Create(FormCollection collection) { try { Coordinate lastCoordinate = null; var strRouteID = collection.Get("RouteID"); // Parse Geo-JSON formatted route coordinates var jsonShapes = collection.Get("JSONText"); if (jsonShapes != null && strRouteID != null) { int routeID = Convert.ToInt32(strRouteID); using (var db = new TrolleyTracker.Models.TrolleyTrackerContext()) { RemoveOldShape(routeID, db); JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic shapeData = jss.Deserialize(jsonShapes, typeof(object)) as dynamic; var features = shapeData.features; var geometryBlock = features[0]; var geometry = geometryBlock["geometry"]; var geoJSONtype = geometry["type"]; var coordinates = geometry["coordinates"]; int segmentCount = coordinates.Count; if (geoJSONtype == "LineString") { segmentCount = 1; } int sequence = 0; double totalDistance = 0.0; for (int seg = 0; seg < segmentCount; seg++) { var coordArray = coordinates[seg]; if (geoJSONtype == "LineString") { coordArray = coordinates; } int nodeCount = coordArray.Count; for (int i = 0; i < nodeCount; i++) { var node = coordArray[i]; var strLon = node[0]; var strLat = node[1]; var lon = Convert.ToDouble(strLon); var lat = Convert.ToDouble(strLat); var thisCoordinate = new Coordinate(lat, lon); double distance = 0.0; if (lastCoordinate != null) { distance = thisCoordinate.GreatCircleDistance(lastCoordinate); } lastCoordinate = thisCoordinate; totalDistance += distance; var dbShape = new TrolleyTracker.Models.Shape(); dbShape.Lat = lat; dbShape.Lon = lon; dbShape.RouteID = routeID; dbShape.Sequence = sequence; dbShape.DistanceTraveled = totalDistance; sequence++; db.Shapes.Add(dbShape); } } db.SaveChanges(); var assignStops = new AssignStopsToRoutes(); assignStops.UpdateStopsForRoute(db, routeID); var route = db.Routes.Find(routeID); logger.Info($"Modified route shape of '{route.Description}' - '{route.LongName}'"); } } return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.Message = "Shape save exception"; logger.Error(ex, "Exception saving route shape"); ViewBag.RouteID = new SelectList(db.Routes, "ID", "ShortName"); return(View()); } }