Ejemplo n.º 1
0
        /// <summary>
        /// Makes a REST call using the serialized RootObject and returns the json results into the RootObject
        /// </summary>
        /// <param name="rootRequestObject">The object to serialize and send in the REST call</param>
        /// <param name="token">The token to use in the REST call to ArcGIS</param>
        /// <returns>The object to deserialize after making the REST call</returns>
        public static RootResponseObject GeocodeABatchOfAddresses(RootRequestObject rootRequestObject, string token)
        {
            string jsonAddressRequest = "addresses=" + JsonConvert.SerializeObject(rootRequestObject);

            string endPoint = String.Format(
                ConfigurationManager.AppSettings["arcGISBaseAddress"].ToString(CultureInfo.InvariantCulture) +
                "/arcgis/rest/services/Geocode/USA/GeocodeServer/geocodeAddresses",
                token);
            var client = new RestfulGeocoder.HttpUtils.HttpUtils(endPoint, HttpVerb.Post, jsonAddressRequest);

            client.ContentType = "application/x-www-form-urlencoded";
            var json = client.MakeRequest(String.Format("?sourceCountry=USA&token={0}&f=json", token));

            RootResponseObject rootResponseObject = JsonConvert.DeserializeObject <RootResponseObject>(json);

            if (rootResponseObject.locations == null)
            {
                GeoCodeAddresses.Log.Debug(String.Format("rootResponseObject.locations is null"));
            }
            else
            {
                GeoCodeAddresses.Log.Debug(String.Format("GeocodeABatchOfAddresses: rootResponseObject.locations.Count = {0}",
                                                         rootResponseObject.locations.Count));
            }

            return(rootResponseObject);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Geocodes all addresses in Dimension.Location table
        /// </summary>
        public static void GeocodeAllAddresses()
        {
            GeoDatabaser       geoDatabaser = new GeoDatabaser();
            RootResponseObject root         = new RootResponseObject();
            List <Location>    locations    = new List <Location>();

            root.locations = locations;
            int locationCount = 0;

            // Get a new token every x batches based on appConfig key.
            // Token expires every 60 minutes according to ArcGIS Server settings
            geoCodeAddresses.GetToken(BatchesProcessed);

            RootRequestObject  rootRequestObject  = new RootRequestObject();
            RootResponseObject rootResponseObject = new RootResponseObject();

            rootRequestObject = geoDatabaser.GetBatchOfNonGeocodedAddressesFromDatabase(rootRequestObject);

            if (rootRequestObject.records.Count != 0)
            {
                rootResponseObject = RestServices.GeocodeABatchOfAddresses(rootRequestObject, Token);

                if (rootResponseObject.locations != null)
                {
                    Log.Info(
                        String.Format("rootResponseObject.locations.count = {0}",
                                      rootResponseObject.locations.Count));
                    AddressesProcessed += rootResponseObject.locations.Count;
                    foreach (Location location in rootResponseObject.locations)
                    {
                        Log.Debug(String.Format("Location Info: x: {0} y: {1} score: {2} address: {3}",
                                                location.location.x, location.location.y, location.score, location.address));
                        if (!(location.location.x > 0 || location.location.x < 0) &&
                            !(location.location.y > 0 || location.location.y < 0))
                        {
                            location.location.x = null;
                            location.location.y = null;
                        }
                        root.locations.Add(new Location());
                        locations[locationCount] = location;
                        locationCount++;
                    }
                    geoDatabaser.InsertBatchOfGeocodedAddressesToDatabase(root);
                    root.locations.Clear();
                }
                else
                {
                    Log.Debug(
                        String.Format("rootResponseObject is null"));
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Gets a batch of geocoded addresses from the database and returns them as a RootObject.
 /// </summary>
 /// <param name="root">The object to put the addresses into</param>
 /// <returns>RootObject</returns>
 public RootRequestObject GetBatchOfNonGeocodedAddressesFromDatabase(RootRequestObject root)
 {
     using (SqlConnection connection = new SqlConnection(ConnectionString))
     {
         using (
             SqlCommand command =
                 new SqlCommand(String.Format("SELECT L.EID, L.Street, L.City, L.State, L.ZipCode" +
                                              " FROM " +
                                              ConfigurationManager.AppSettings["sourceTableName"].ToString(
                                                  CultureInfo.InvariantCulture) + " L" +
                                              " ORDER BY L.EID OFFSET {0} ROWS FETCH NEXT {1} ROWS ONLY",
                                              GeoCodeAddresses.Offset,
                                              GeoCodeAddresses.Form.GetNumAddressesPerBatch()
                                              ), connection))
         {
             GeoCodeAddresses.Offset += GeoCodeAddresses.Form.GetNumAddressesPerBatch();
             List <Record> records = new List <Record>();
             connection.Open();
             command.CommandTimeout = 90;
             using (SqlDataReader reader = command.ExecuteReader())
             {
                 int i = 0;
                 while (reader.Read())
                 {
                     records.Add(new Record());
                     records[i].attributes.OBJECTID = Convert.ToInt32(reader[0]);
                     records[i].attributes.Address  = reader[1].ToString();
                     records[i].attributes.Address  = HttpUtils.SanitizeUrlString(records[i].attributes.Address);
                     records[i].attributes.City     = reader[2].ToString();
                     records[i].attributes.Region   = reader[3].ToString();
                     records[i].attributes.Postal   = reader[4].ToString();
                     if (records[i].attributes.Postal.Length > 5)
                     {
                         records[i].attributes.Postal = records[i].attributes.Postal.Substring(0, 5);
                     }
                     root.records.Add(records[i]);
                     i++;
                 }
             }
         }
     }
     return(root);
 }