static void Main(string[] args) { //This sample shows how to perform a back geocode request from a console app. var bingMapsKey = System.Configuration.ConfigurationManager.AppSettings.Get("BingMapsKey"); var geocodeFeed = new GeocodeFeed() { Entities = new List <GeocodeEntity>() { new GeocodeEntity("New York, NY"), new GeocodeEntity("Seattle, WA") } }; Console.WriteLine(string.Format("Creating batch geocode job consisting of {0} entities.\n", geocodeFeed.Entities.Count)); var geocodeManager = new BatchGeocodeManager(); var r = geocodeManager.Geocode(geocodeFeed, bingMapsKey).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(r.Error)) { Console.WriteLine("Error: " + r.Error); } else { Console.WriteLine("Batch geocode job complete:\n"); if (r.Succeeded != null && r.Succeeded.Entities != null) { Console.WriteLine(string.Format("Succeeded: {0}\n", r.Succeeded.Entities.Count)); Console.WriteLine("Query\tLatitude\tLongitude\n----------------------------------"); foreach (var e in r.Succeeded.Entities) { Console.WriteLine(string.Format("{0}\t{1}\t{2}", e.GeocodeResponse[0].Name, e.GeocodeResponse[0].GeocodePoint[0].Latitude, e.GeocodeResponse[0].GeocodePoint[0].Longitude)); } } if (r.Failed != null && r.Failed.Entities != null) { Console.WriteLine(string.Format("Failed: {0}\n", r.Failed.Entities.Count)); Console.WriteLine("Query\n-----------"); foreach (var e in r.Failed.Entities) { Console.WriteLine(e.GeocodeRequest.Query); } } } Console.ReadLine(); }
private async void ProcessBatchGeocode_Clicked(object sender, RoutedEventArgs e) { SucceededBatchGeocodes.Text = string.Empty; FailedBatchGeocodes.Text = string.Empty; var type = (BatchFileFormat)Enum.Parse(typeof(BatchFileFormat), ((string)(BatchFileFormatCbx.SelectedItem as ComboBoxItem).Content)); string filters, defaultExt; FileExtensionUtilities.GetFileExtensions(type, out defaultExt, out filters); var ofd = new OpenFileDialog() { DefaultExt = defaultExt, Filter = filters }; var b = ofd.ShowDialog(); if (b.HasValue && b.Value) { using (var s = ofd.OpenFile()) { var batchFile = await GeocodeFeed.ReadAsync(s, type); var batchManager = new BatchGeocodeManager(); batchManager.StatusChanged += (msg) => { BatchGeocodeStatus.Text = msg; }; var results = await batchManager.Geocode(batchFile, BingMapsKey); var serializer = new XmlSerializer(typeof(GeocodeFeed)); using (var writer = new StringWriter()) { serializer.Serialize(writer, results.Succeeded); SucceededBatchGeocodes.Text = writer.ToString(); } using (var writer = new StringWriter()) { serializer.Serialize(writer, results.Failed); FailedBatchGeocodes.Text = writer.ToString(); } } } }
public void GeoCode(List <GeocodeEntity> reqEntities, string incId) { //This sample shows how to perform a back geocode request from a console app. // var bingMapsKey = "Apiu5N8o7_72FUmE33bvupI31kyg2Wz7sDIQviRwPttMKfu7Pmmhf219uIso-1ra"; var bingMapsKey = Environment.GetEnvironmentVariable("BingMapKey", EnvironmentVariableTarget.Process); var geocodeFeed = new GeocodeFeed() { Entities = reqEntities }; LGSEBingMapFunction.logger.LogInformation(string.Format("Creating batch geocode job consisting of {0} entities.\n", geocodeFeed.Entities.Count)); var geocodeManager = new BatchGeocodeManager(); var r = geocodeManager.Geocode(geocodeFeed, bingMapsKey).GetAwaiter().GetResult(); if (!string.IsNullOrEmpty(r.Error)) { LGSEBingMapFunction.logger.LogInformation("Error: " + r.Error); } else { LGSEBingMapFunction.logger.LogInformation("Batch geocode job complete:\n"); DBUtility dBUtility = new DBUtility(); if (r.Succeeded != null && r.Succeeded.Entities != null) { LGSEBingMapFunction.logger.LogInformation(string.Format("Succeeded: {0}\n", r.Succeeded.Entities.Count)); LGSEBingMapFunction.logger.LogInformation("Query\tLatitude\tLongitude\n----------------------------------"); foreach (var e in r.Succeeded.Entities) { LGSEBingMapFunction.logger.LogInformation(string.Format("{0}\t{1}\t{2}", e.GeocodeResponse[0].Name, e.GeocodeResponse[0].GeocodePoint[0].Latitude, e.GeocodeResponse[0].GeocodePoint[0].Longitude)); } dBUtility.UpdateSuccessLatLong(r.Succeeded.Entities, incId); } if (r.Failed != null && r.Failed.Entities != null) { LGSEBingMapFunction.logger.LogInformation(string.Format("Failed: {0}\n", r.Failed.Entities.Count)); LGSEBingMapFunction.logger.LogInformation("Query\n-----------"); foreach (var e in r.Failed.Entities) { LGSEBingMapFunction.logger.LogInformation(e.GeocodeRequest.Query); } } } }
public static async Task <BatchGeocoderResults> InitializeBingMap() { var bingMapsKey = "AuFfksssSZ6g0kxOTVSlouU3CnPwnvku1ZDUdBFVX-Xpdm967HxuGsKkyfzAV1U2"; var geocodeFeed = new GeocodeFeed() { Entities = new List <GeocodeEntity>() { new GeocodeEntity("New York, NY"), new GeocodeEntity("Seattle, WA") } }; var geocodeManager = new BatchGeocodeManager(); var r = await geocodeManager.Geocode(geocodeFeed, bingMapsKey); return(r); }
public async override Task <IEnumerable <Trip> > Process(TripDetectionContext input, ILogger logger) { var savedTrips = new List <Trip>(); var locationIdsToGeocode = new HashSet <int>(); logger.LogDebugSerialize("Recieved trips {0}", input.ResultantTrips); foreach (var trip in input.ResultantTrips) { var savedTrip = await this.tripRepository.AddAsync(trip); var points = input.OriginalPoints.Where(p => p.TripId == trip.Id); await trackingPointRepository.AssignPointsToTripAsync(savedTrip.Id, points); logger.LogDebugSerialize("Assigned points {1} to trip {0}", points, trip); savedTrips.Add(savedTrip); foreach (var leg in trip.TripLegs) { locationIdsToGeocode.Add(leg.StartLocationId); locationIdsToGeocode.Add(leg.EndLocationId); } } var locations = (await this.locationRepository.GetAllAsync()) .Where(l => locationIdsToGeocode.Contains(l.Id) && l.Address == "Auto-Generated") .ToList(); //Batch reverse geocode var geocodeFeed = new GeocodeFeed() { Entities = locations.Select((l, i) => new GeocodeEntity() { ReverseGeocodeRequest = new ReverseGeocodeRequest() { Location = new GeodataLocation(l.Latitude, l.Longitude) }, Id = i.ToString() }).ToList() }; if (locations.Count > 0) { var geocodeManager = new BatchGeocodeManager(); var res = await geocodeManager.Geocode(geocodeFeed, bingMapsKey); var locationsDict = new Dictionary <int, Location>(); if (res.Succeeded != null) { logger.LogDebugSerialize("Reverse Geocode result (Succeeded)", res.Succeeded); foreach (var entity in res.Succeeded.Entities) { var loc = locations[int.Parse(entity.Id)]; loc.Address = entity.GeocodeResponse.First <GeocodeResponse>().Address.FormattedAddress; loc.Name = entity.GeocodeResponse.First <GeocodeResponse>().Name; locationsDict.Add(loc.Id, loc); } await locationRepository.UpdateAsync(locationsDict); } else { logger.LogDebugSerialize("Reverse Geocode result (Failed)", res.Failed); logger.LogDebugSerialize("Reverse Geocode result (Errors)", res.Error); } } logger.LogDebugSerialize("Saved trips output", savedTrips); return(savedTrips); }