public ActionResult CreateConfirm(FormCollection collection) { var resultText = "Unknown error"; var errorText = ""; try { using (var db = new TrolleyTracker.Models.TrolleyTrackerContext()) { var strRouteID = collection.Get("RouteID"); int routeID = Convert.ToInt32(strRouteID); var route = db.Routes.Find(routeID); resultText = ""; var jsonShapes = collection.Get("RouteShapeJSON"); if (jsonShapes != null && strRouteID != null) { RemoveOldShape(routeID, db); SaveNewRouteShape(db, route, jsonShapes); var jsonStops = collection.Get("NewRouteStops"); var newStopCount = SaveNewRouteStops(db, jsonStops); var assignStops = new AssignStopsToRoutes(); assignStops.UpdateStopsForRoute(db, routeID); logger.Info($"Modified route shape of '{route.ShortName}' - '{route.LongName}, added {newStopCount} stops.'"); resultText = $"Route path for {route.ShortName} Saved, added {newStopCount} stops."; } } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { logger.Info("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } errorText = "Oops - Shape save exception. This has been logged"; logger.Error(dbEx, "Exception processing route shape"); } catch (Exception ex) { // Unexpected error in parse or preview errorText = "Oops - Shape save exception. This has been logged"; logger.Error(ex, "Exception processing route shape"); } ViewBag.RouteID = GetRouteSelectList(null); ViewBag.ErrorMessage = errorText; ViewBag.Message = resultText; return(View("Create")); }
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()); } }