Exemple #1
0
        public void UpdateLatLong(Location Loc)
        {
            var coord = DoGeocoding.GetCoordinateForAddress(Loc.Address);

            if (coord != null)
            {
                Loc.Coordinate    = coord;
                Loc.Latitude      = coord.Latitude;
                Loc.Longitude     = coord.Longitude;
                Loc.GeocodeStatus = GeocodeStatus.Ok;
                this.Update(Loc);
                this.UpdateDbGeography(Loc);
            }
        }
        internal void UpdateLatLong(EditableLocation Loc)
        {
            LogHelper.Info <LocationRepository>(string.Format("UpdateLatLong for {0} STARTED", Loc.Name));
            var coord = DoGeocoding.GetCoordinateForAddress(Loc.Address);

            if (coord != null)
            {
                Loc.Coordinate    = coord;
                Loc.Latitude      = coord.Latitude;
                Loc.Longitude     = coord.Longitude;
                Loc.GeocodeStatus = GeocodeStatus.Ok;
                this.Update(Loc);
                this.UpdateDbGeography(Loc);
            }
            LogHelper.Info <LocationRepository>(string.Format("UpdateLatLong for {0} COMPLETED", Loc.Name));
        }
Exemple #3
0
        private static StatusMessage ImportRow(DataRow row, LocationType LocType, int GeocodeCount, DataColumnCollection ImportColumns, out int geocodeCountReturn)
        {
            string locName = row.Field <string>("LocationName");

            var ReturnMsg = new StatusMessage();

            ReturnMsg.ObjectName = locName;
            ReturnMsg.Success    = true;
            var Msg             = new StringBuilder();
            var geocodeCountNew = GeocodeCount;

            //Create new EditableLocation for row
            var newLoc = new EditableLocation(locName, LocType.Key);

            Repositories.LocationRepo.Insert(newLoc);

            try
            {
                //Default Props
                var locationTypeService = new LocationTypeService();
                var defaultLocType      = locationTypeService.GetLocationType(Constants.DefaultLocationTypeKey);
                foreach (var prop in defaultLocType.Properties)
                {
                    string colName = prop.Alias;
                    if (ImportColumns.Contains(colName))
                    {
                        newLoc.AddPropertyData(colName, row.Field <object>(colName));
                    }
                    else
                    {
                        newLoc.AddPropertyData(colName, null);
                        Msg.AppendLine(string.Concat("Data for '", colName, "' was not included in the import file."));
                    }
                }

                //Custom Props
                if (LocType.Key != defaultLocType.Key)
                {
                    foreach (var prop in LocType.Properties)
                    {
                        string colName = prop.Alias;

                        if (ImportColumns.Contains(colName))
                        {
                            newLoc.AddPropertyData(colName, row.Field <object>(colName));
                        }
                        else
                        {
                            newLoc.AddPropertyData(colName, null);
                            Msg.AppendLine(string.Concat("Data for '", colName, "' was not included in the import file."));
                        }
                    }
                }

                // SAVE properties of new location to db
                try
                {
                    Repositories.LocationRepo.Update(newLoc);
                }
                catch (Exception eSave)
                {
                    ReturnMsg.Success          = false;
                    ReturnMsg.RelatedException = eSave;
                    ReturnMsg.Code             = ReturnMsg.Code != "" ? ReturnMsg.Code + ",InsertError" : "InsertError";
                    Msg.AppendLine("There was a problem saving the new location data.");
                }

                //Check for Lat/Long values - import if present
                if (ImportColumns.Contains("Latitude"))
                {
                    int convertedInt = 0;
                    Int32.TryParse(row.Field <string>("Latitude"), out convertedInt);
                    newLoc.Latitude = convertedInt;
                }

                if (ImportColumns.Contains("Longitude"))
                {
                    int convertedInt = 0;
                    Int32.TryParse(row.Field <string>("Longitude"), out convertedInt);
                    newLoc.Longitude = convertedInt;
                }

                //If Lat/Long are both 0... attempt geocoding
                if (newLoc.Latitude == 0 && newLoc.Longitude == 0)
                {
                    //TODO: make dynamic based on provider limit
                    if (GeocodeCount >= MAX_GEOCODE)
                    {
                        ReturnMsg.Success = true;
                        ReturnMsg.Code    = "GeocodingProblem";
                        Msg.AppendLine(
                            "This address exceeded the limits for geo-coding in a batch. Please run maintenance to update geo-codes later.");
                    }
                    else
                    {
                        try
                        {
                            var newCoordinate = DoGeocoding.GetCoordinateForAddress(newLoc.Address);
                            newLoc.Latitude  = newCoordinate.Latitude;
                            newLoc.Longitude = newCoordinate.Longitude;

                            geocodeCountNew++;
                        }
                        catch (Exception e1)
                        {
                            ReturnMsg.Success          = true;
                            ReturnMsg.RelatedException = e1;
                            ReturnMsg.Code             = "GeocodingProblem";
                            Msg.AppendLine(
                                "There was a problem geo-coding the address. Please run maintenance to update geo-codes later.");
                            LogHelper.Error(
                                typeof(Import),
                                string.Format("Geo-coding error while importing '{0}'", ReturnMsg.ObjectName),
                                e1);
                        }
                    }
                }

                // SAVE properties of new location to db again
                try
                {
                    Repositories.LocationRepo.Update(newLoc);
                }
                catch (Exception eSave)
                {
                    ReturnMsg.Success          = false;
                    ReturnMsg.RelatedException = eSave;
                    ReturnMsg.Code             = ReturnMsg.Code != "" ? ReturnMsg.Code + ",InsertError" : "InsertError";
                    Msg.AppendLine("There was a problem saving the new location data.");
                }

                // UPDATE Geography DB field using Lat/Long
                try
                {
                    if (newLoc.Latitude != 0 && newLoc.Longitude != 0)
                    {
                        Repositories.LocationRepo.UpdateDbGeography(newLoc);
                    }
                    else
                    {
                        newLoc.DbGeogNeedsUpdated = true;
                        Repositories.LocationRepo.Update(newLoc);
                    }
                }
                catch (Exception eGeoDB)
                {
                    ReturnMsg.Success          = true;
                    ReturnMsg.RelatedException = eGeoDB;
                    ReturnMsg.Code             = ReturnMsg.Code != "" ? ReturnMsg.Code + ",UnableToUpdateDBGeography" : "UnableToUpdateDBGeography";
                    Msg.AppendLine("Unable to update the coordinates in the database.");
                }
            }
            catch (Exception ex)
            {
                ReturnMsg.Success          = false;
                ReturnMsg.RelatedException = ex;
                ReturnMsg.Code             = ReturnMsg.Code != "" ? ReturnMsg.Code + ",UnknownException" : "UnknownException";
                Msg.AppendLine(ex.Message);
                LogHelper.Error(typeof(Import), string.Format("ImportRow: Error importing location '{0}'", locName), ex);
            }

            ReturnMsg.Message = Msg.ToString();

            geocodeCountReturn = geocodeCountNew;

            return(ReturnMsg);
        }