Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a geocoder for US Census Bureau #116

Open
DR9885 opened this issue Dec 17, 2018 · 5 comments
Open

Implement a geocoder for US Census Bureau #116

DR9885 opened this issue Dec 17, 2018 · 5 comments

Comments

@DR9885
Copy link
Contributor

DR9885 commented Dec 17, 2018

There is a free api found here:
https://geocoding.geo.census.gov/

@DR9885
Copy link
Contributor Author

DR9885 commented Dec 17, 2018

possible example:

    public class UsCensusBureauGeocoder : IGeocoder
    {
        private readonly int _benchmark;
        private readonly HttpClient _client;
    
        private class UsCensusBureauAddress : Address
        {
            private const string Key = "UsCensusBureau";
        
            public UsCensusBureauAddress(string formattedAddress, Location coordinates) 
                : base(formattedAddress, coordinates, Key)
            {
            
            }
        }

        public UsCensusBureauGeocoder(int benchmark = 9)
        {
            _benchmark = benchmark;
            _client = new HttpClient { BaseAddress = new Uri("https://geocoding.geo.census.gov/geocoder/") };
        }
        
        public async Task<IEnumerable<Address>> GeocodeAsync(string address)
        {
            // Build Query String
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            queryString["address"] = address;
            queryString["benchmark"] = "4";
            queryString["format"] = "json";
            
            // Get Request
            var response = await _client.GetStringAsync($"locations/onelineaddress?" + queryString);
            
            // Read Result
            return GetAddresses(response);
        }

        public async  Task<IEnumerable<Address>> GeocodeAsync(string street, string city, string state, string postalCode, string country)
        {
            // Build Query String
            var queryString = HttpUtility.ParseQueryString(string.Empty);
            queryString["street"] = street;
            queryString["city"] = city;
            queryString["state"] = state;
            queryString["zip"] = postalCode;
            queryString["benchmark"] = "4";
            queryString["format"] = "json";
            
            // Get Request
            var response = await _client.GetStringAsync($"locations/address?" + queryString);
                
            // Read Result
            return GetAddresses(response);
        }

        public Task<IEnumerable<Address>> ReverseGeocodeAsync(Location location)
        {
            throw new NotImplementedException();
        }

        public Task<IEnumerable<Address>> ReverseGeocodeAsync(double latitude, double longitude)
        {
            throw new NotImplementedException();
        }

        private static IEnumerable<UsCensusBureauAddress> GetAddresses(string response) 
        {
            var result = JObject.Parse(response)["result"];

            return result["addressMatches"]
                .Select(match =>
                {
                    var formatted = match["matchedAddress"].ToString();
                    var coordinates = match["coordinates"];
                    var x = Convert.ToDouble((object) coordinates["x"]);
                    var y = Convert.ToDouble((object) coordinates["y"]);
                    return new UsCensusBureauAddress(formatted, new Location(y, x));
                })
                .ToArray();
        }
    }

@DR9885
Copy link
Contributor Author

DR9885 commented Dec 22, 2018

Opened Pull Request with tests:
#117

@DR9885
Copy link
Contributor Author

DR9885 commented Dec 22, 2018

when are you guys publishing the new nuget packages? I saw you guys added Here support as well.

@DR9885
Copy link
Contributor Author

DR9885 commented Dec 22, 2018

using a different pull request
#118

@DR9885
Copy link
Contributor Author

DR9885 commented Jan 28, 2020

@chadly is there anything were missing or can we pull it back in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants