Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Implement a geocoder for US Census Bureau #116

Open

Description

@DR9885

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

Activity

  1. DR9885 commented on Dec 17, 2018

    @DR9885
    ContributorAuthor

    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();
            }
        }
  2. DR9885 commented on Dec 22, 2018

    @DR9885
    ContributorAuthor

    Opened Pull Request with tests:
    #117

  3. DR9885 commented on Dec 22, 2018

    @DR9885
    ContributorAuthor

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

  4. DR9885 commented on Dec 22, 2018

    @DR9885
    ContributorAuthor

    using a different pull request
    #118

  5. DR9885 commented on Jan 28, 2020

    @DR9885
    ContributorAuthor

    @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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions