/// <summary> /// Get the coordinate file and process the data to build Simio map route objects. /// If any are found, return is true, but unfound addresses are reported in explanation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public bool ProcessPairDataFile(string path, IMapHelper mapHelper, string mapKey, out SimioMapRoutes mapRoutes, out string explanation) { explanation = ""; string marker = "Begin"; int lineNbr = 0; mapRoutes = null; StringBuilder sbErrors = new StringBuilder(); Cursor.Current = Cursors.WaitCursor; try { string name = Path.GetFileNameWithoutExtension(path); mapRoutes = new SimioMapRoutes(name); mapRoutes.ProviderInformation = mapHelper.GetProviderInformation(); //Todo: Add processing if (string.IsNullOrEmpty(comboGisSource.Text)) { alert($"Please select a GIS Source"); return(false); } if (!LoadAddressPairsFile(textAddressPairsFilepath.Text, out List <GisFromToPair> pairList, out explanation)) { explanation = $"Cannot Load Pairs File={explanation}"; return(false); } foreach (GisFromToPair pair in pairList) { lineNbr++; marker = $"Line={lineNbr} Loading Pair={pair}"; labelStatus.Text = marker; if (!mapHelper.GetRoute(mapKey, pair.FromLocation.ToString(), pair.ToLocation.ToString(), out SimioMapRoute mapRoute, out string requestUrl, out explanation)) { sbErrors.AppendLine($"Cannot Get Route. Err={explanation}"); } mapRoutes.RouteList.Add(mapRoute); } explanation = sbErrors.ToString(); return(true); } catch (Exception ex) { explanation = $"Marker={marker} Err={ex.Message}"; return(false); } finally { Cursor.Current = Cursors.Default; } }
/// <summary> /// Construct Simio facility objects from all routes. /// </summary> /// <param name="context"></param> /// <param name="routes"></param> /// <param name="transform"></param> /// <param name="explanation"></param> /// <returns></returns> public static bool BuildSimioFacilityObjectsFromMapRoutes(IDesignContext context, SimioMapRoutes routes, SimioMapTransform transform, out string explanation) { explanation = ""; string marker = "Begin."; try { int routeCount = 0; foreach (SimioMapRoute route in routes.RouteList) { routeCount++; if (!BuildSimioFacilityObjectsFromMapRoute(context, route, transform, out explanation)) { marker = $"Route:{routeCount} Info={route} Err={explanation}"; throw new ApplicationException(marker); } } return(true); } catch (Exception ex) { explanation = $"BuildSimioFacilityObjects failure. Err={ex.Message}"; return(false); } }
/// <summary> /// Get the coordinate file using a map provider and save the results to a json file. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonProcessCoordinateFile_Click(object sender, EventArgs e) { try { //Todo: Add processing if (string.IsNullOrEmpty(comboGisSource.Text)) { alert($"Please select a GIS Source"); return; } string path = textAddressPairsFilepath.Text; if (string.IsNullOrEmpty(path)) { alert($"Please select a Address Pairs file. See Tools tab to create one."); return; } if (!LoadAddressPairsFile(path, out List <GisFromToPair> pairList, out string explanation)) { alert(explanation); return; } SimioMapRoutes mapRoutes = null; switch (comboGisSource.Text) { case "Bing": BingMapKey = textBingMapsKey.Text; if (!ProcessPairDataFile(path, BingMapHelper, BingMapKey, out mapRoutes, out explanation)) { alert($"Cannot process Address file with Bing Maps. Err={explanation}"); } break; case "Google": GoogleMapKey = textGoogleMapsKey.Text; if (!ProcessPairDataFile(path, GoogleMapHelper, GoogleMapKey, out mapRoutes, out explanation)) { alert($"Cannot process Address file with Google Maps. Err={explanation}"); } break; default: { alert($"Unsupported Map Source={comboGisSource.Text}"); return; } } if (string.IsNullOrEmpty(explanation)) { alert($"Routes were processed with some Errors: {explanation}"); } if (mapRoutes != null && mapRoutes.RouteList.Any()) { SaveFileDialog dialog = new SaveFileDialog(); dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); dialog.OverwritePrompt = true; dialog.FileName = ""; dialog.Filter = "json Files (*.json)|*.json"; DialogResult result = dialog.ShowDialog(); if (result != DialogResult.OK) { return; } path = dialog.FileName; if (!JsonHelpers.SerializeToFile <SimioMapRoutes>(path, mapRoutes, false, out explanation)) { alert($"Cannot save {mapRoutes.RouteList.Count} to File={path}. Err={explanation}"); } } } catch (Exception ex) { alert($"Cannot Process. Err={ex.Message}"); } }