public void Upload()
        {
            if (client.BasePath == null)
            {
                view.ShowMessageBox("You are not logged on to Alba.  Please Logon.");
                return;
            }

            string fileName = view.OpenFileDialog("kml");

            if (string.IsNullOrWhiteSpace(fileName))
            {
                return;
            }

            var kml         = new KmlGateway().Load(fileName);
            var territories = new KmlToTerritoryConverter()
                              .TerritoryListFrom(kml)
                              .Where(t => t.Border != null && t.Border.Vertices != null && t.Border.Vertices.Count > 0)
                              .ToList();

            view.AppendResultText("Uploading " + territories.Count + " territory borders...");

            foreach (var territory in territories)
            {
                int count = territory.Border.Vertices.Count;

                var url          = RelativeUrlBuilder.RequestToAddNew(territory);
                var resultString = client.DownloadString(url);

                view.AppendResultText("Territory: " + territory.Number);
                view.AppendResultText(count + " vertices where uploaded..." + Environment.NewLine);
                view.AppendResultText(resultString + Environment.NewLine + Environment.NewLine);
                Thread.Sleep(delay);
            }
        }
Example #2
0
        public void Logon()
        {
            var credentials = new Credentials(
                view.AccountBoxText,
                view.UserBoxText,
                view.PasswordBoxText);

            client.Authenticate(credentials);

            view.LoadTerritoriesButtonEnabled              = true;
            view.UploadKmlFilesButtonEnabled               = true;
            view.DownloadAllAddressesButtonEnabled         = true;
            view.DownloadTerritoryAssignmentsButtonEnabled = true;
            view.DownloadUsersButtonEnabled   = true;
            view.UploadAddressesButtonEnabled = true;

            view.AppendResultText(Environment.NewLine + "Logged on successfully." + Environment.NewLine);
        }
        private void GeocodeCsvAddressesFrom(string path, string key)
        {
            var amWebClient = new CookieWebClient();
            var amBasePath  = new ApplicationBasePath(
                protocolPrefix: "https://",
                site: "atlas.microsoft.com",
                applicationPath: "/");

            var amClient = new AzureMapsClient(
                webClient: amWebClient,
                basePath: amBasePath,
                subscriptionKey: key);

            int geocoded       = 0;
            int alreadyGeocode = 0;
            var addresses      = LoadCsvAddresses.LoadFrom(path);

            foreach (var address in addresses)
            {
                if (address.Latitude == null ||
                    address.Longitude == null ||
                    address.Latitude == 0 ||
                    address.Longitude == 0)
                {
                    var coordinates = new AzureMapsmGeocodeAddress(view, amClient)
                                      .Geocode(address);

                    address.Latitude  = coordinates.Latitude;
                    address.Longitude = coordinates.Longitude;

                    geocoded++;
                }
                else
                {
                    alreadyGeocode++;
                }
            }

            view.AppendResultText($"\nTotal Addresses: {(geocoded + alreadyGeocode)}");
            view.AppendResultText($"\nGeocoded: {geocoded}");
            view.AppendResultText($"\nAlready Geocoded (Skipped): {alreadyGeocode}");

            var newPath = view.GetFileNameToSaveAs(path, "csv");

            if (string.IsNullOrWhiteSpace(newPath))
            {
                // Cancelled
                view.AppendResultText($"\nGeocoding not saved.");

                return;
            }

            LoadCsvAddresses.SaveTo(addresses, newPath);

            view.AppendResultText($"\nGeocoding saved to {newPath}");
        }