public Geocode <Census> GeocodeAddress (string street, string city = "", string state = "", string zip = "") { Census json = null; var geocode = new Geocode <Census>(); HttpResponseMessage response = geocode.GetResponse (this.apiUrl, this.GetUrlParameters(street, city, state, zip)); using (response) { if (response.IsSuccessStatusCode) { var data = response.Content.ReadAsStringAsync().Result; json = JsonConvert.DeserializeObject <Census>(data); if (json != null) { geocode.Latitude = json.result.addressMatches[0].coordinates.x; geocode.Longitude = json.result.addressMatches[0].coordinates.y; geocode.CongressionalDistrictName = json.result.addressMatches[0].geographies.congressionalDistricts[0].NAME; } } return(geocode); } }
private List <CENSUS_LOCATION> buildGeocode(Patient iPatient) { List <CENSUS_LOCATION> result = new List <CENSUS_LOCATION>(); foreach (var add in iPatient.Address) { CENSUS_LOCATION new_location = new CENSUS_LOCATION(); string address = ""; string street = ""; string city = ""; string state = ""; string postal_code = ""; int c = 0; foreach (var line in add.Line) { if (c == 0) { street = line; } address = address + line + " "; c++; } address = address + ", " + add.City + ", " + add.State + " " + add.PostalCode; city = add.City; state = add.State; postal_code = add.PostalCode; // use US Gov Census REST API to get location Uri geocoder = new Uri("https://geocoding.geo.census.gov"); var client = new RestClient(); client.BaseUrl = geocoder; // use US Gov Census API to get Census Tract try { var geographies_request = new RestRequest(); geographies_request.Resource = "/geocoder/geographies/address?street=" + street + "&city=" + city + "&state=" + state + "&benchmark=Public_AR_Census2010&vintage=Census2010_Census2010&layer=14" + "&format=json"; IRestResponse geographies_response = client.Execute(geographies_request); if (geographies_response.IsSuccessful) { Census json = null; json = JsonConvert.DeserializeObject <Census>(geographies_response.Content); // fill in CENSUS_LOCATION new_location.PERSON_ID = iPatient.Id; if (add.Period != null && add.Period.StartElement.ToDateTime().HasValue) { new_location.LOC_START = add.Period.StartElement.ToDateTime().Value; } else { new_location.LOC_START = DateTime.Now; } if (add.Period != null && add.Period.EndElement.ToDateTime().HasValue) { new_location.LOC_END = add.Period.EndElement.ToDateTime().Value; } else { new_location.LOC_END = null; } // GEOCODE if (json != null && json.result != null && json.result.addressMatches != null && json.result.addressMatches.Count() > 0) { new_location.GEOCODE = json.result.addressMatches[0].geographies.censusblocks[0].GEOID; // CITY_GEOCODE - using lat/log determine the city ; // GEOCODE_BOUNDARY_YEAR // based on benchmark=Public_AR_Census2010 on Gov CENSUS Request new_location.GEOCODE_BOUNDARY_YEAR = 2010; // GEOLEVEL - should be based on the size of the GEOCODE new_location.GEOLEVEL = "T"; // MATCH_STRENGTH new_location.MATCH_STRENGTH = null; // LATITUDE if (json != null) { new_location.LATITUDE = Convert.ToDecimal(json.result.addressMatches[0].coordinates.x); } // LONGITUDE if (json != null) { new_location.LATITUDE = Convert.ToDecimal(json.result.addressMatches[0].coordinates.y); } // GEOCODE_APP new_location.GEOCODE_APP = "US Census API"; try { result.Add(new_location); } catch (Exception ex) { log.Info(ex.Message); } } else { // not able to geocode location log.Info("Error: Patient (" + iPatient.Id + ") Not able to geocode '" + address + "'"); } } else { // not able to geocode location log.Info("Error: Not able to geocode '" + address + "'"); } } catch (Exception ex) { log.Info("Error: " + ex.Message); } } return(result); }