public async Task <IActionResult> JosmToLeaflet([FromBody] FeatureCollection josm) { try { // Create a list of errors to be added to. List <string> errors = new List <string>(); // Start a timer. DateTime start = DateTime.Now; // Make sure there is some data to extract. if (josm == default || josm.Features.Count == 0) { errors.Add("JOSM JSON cannot be empty."); return(BadRequest(errors)); } // Initialise a converter. Josm2Leaflet converter = new Josm2Leaflet(); // Extract relevant features. List <Feature> features = converter.ParseJOSM(josm); // Make sure there are features in this GeoJSON to work with. if (features.Count > 0) { // Serialise to JSON. string result = JsonConvert.SerializeObject(new FeatureCollection(features)); // Write the JSON to a file on the server for serving later. string path = await converter.WriteToFile(result, WebHostEnvironment); // Make sure writing the file succeeded. if (string.IsNullOrWhiteSpace(path)) { errors.Add("JOSM2Leaflet: Failed to write JSON file."); } // Stop the timer. DateTime end = DateTime.Now; // Return a message to the front-end for display to the user indicating processing time. return(Created(path, $"JOSM2Leaflet for {converter.filePrefix.ToUpper()} finished in {Math.Round((end - start).TotalMilliseconds, 2)}ms.")); } return(Created("", "No content to extract.")); } catch (Exception e) { return(StatusCode(500, e.Message)); } }
public async Task <IActionResult> BackgroundJosmToLeaflet([FromBody] FeatureCollection josm) { try { // Start a timer. DateTime start = DateTime.Now; List <string> errors = new List <string>(); // Make sure we have data to work with. if (josm == default || josm.Features.Count == 0) { errors.Add("JOSM JSON cannot be empty."); return(BadRequest(errors)); } // Create a new instance of the converter. Josm2Leaflet converter = new Josm2Leaflet(); // Remove corridor nodes. List <Feature> features = converter.RemoveCorridorNodes(josm); // Serialise result. string result = JsonConvert.SerializeObject(new FeatureCollection(features)); // Write to file. string path = await converter.WriteBackgroundToFile(result, WebHostEnvironment); // Ensure the file write succeeded. if (path == null) { errors.Add("JOSM2Leaflet: Failed to write background JSON file."); return(BadRequest(errors)); } // Stop the timer. DateTime end = DateTime.Now; return(Created(path, $"JOSM2Leaflet for background finished in {Math.Round((end - start).TotalMilliseconds, 2)}ms.")); } catch (Exception e) { return(StatusCode(500, e.Message)); } }