From 87daccd7b599814d1a7605e2c57454d721edd1f7 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sun, 8 Oct 2023 11:22:58 -0400 Subject: [PATCH] `geocode`: implement US State & County FIPS code partly implements #1352. Now, we need to lookup FIPS code for US cities --- src/cmd/geocode.rs | 59 +++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/cmd/geocode.rs b/src/cmd/geocode.rs index 730c32bed..0ab5f2353 100644 --- a/src/cmd/geocode.rs +++ b/src/cmd/geocode.rs @@ -1532,25 +1532,25 @@ fn add_dyncols( let admin1_code = cityrecord.admin_division.as_ref().map(|ad| ad.code.clone()); let us_state_code = match admin1_code { Some(admin1_code) => { - if admin1_code.starts_with("US.") { + if let Some(state_fips_code) = admin1_code.strip_prefix("US.") { // admin1 code is a US state code, the two-letter state code // is the last two characters of the admin1 code - admin1_code[3..].to_string() + state_fips_code.to_string() } else { // admin1 code is not a US state code // set to empty string - "".to_string() + String::new() } }, None => { // no admin1 code // set to empty string - "".to_string() + String::new() }, }; // lookup US state FIPS code let us_state_fips_code = lookup_fips_code(&us_state_code).unwrap_or_default(); - record.push_field(&us_state_fips_code) + record.push_field(us_state_fips_code); }, "us_county_fips_code" => { let admin2_code = cityrecord @@ -1564,20 +1564,20 @@ fn add_dyncols( // is the last three characters of the admin2 code // start at index 7 to skip the US. prefix // e.g. US.NY.061 -> 061 - admin2_code[7..].to_string() + format!("{:0>3}", admin2_code[7..].to_string()) } else { // admin2 code is not a US county code // set to empty string - "".to_string() + String::new() } }, None => { // no admin2 code // set to empty string - "".to_string() + String::new() }, }; - record.push_field(&us_county_fips_code) + record.push_field(&us_county_fips_code); }, // CountryRecord fields @@ -1700,39 +1700,55 @@ fn format_result( cityrecord_map.insert("population", cityrecord.population.to_string()); // US FIPS fields - let admin1_code = cityrecord - .admin_division - .as_ref() - .map(|ad| ad.code.clone()) - .unwrap_or_default(); - let us_state_fips_code = lookup_fips_code(&admin1_code).unwrap_or_default(); + // set US state FIPS code + let admin1_code = cityrecord.admin_division.as_ref().map(|ad| ad.code.clone()); + let us_state_code = match admin1_code { + Some(admin1_code) => { + if let Some(state_fips_code) = admin1_code.strip_prefix("US.") { + // admin1 code is a US state code, the two-letter state code + // is the last two characters of the admin1 code + state_fips_code.to_string() + } else { + // admin1 code is not a US state code + // set to empty string + String::new() + } + }, + None => { + // no admin1 code + // set to empty string + String::new() + }, + }; + let us_state_fips_code = lookup_fips_code(&us_state_code).unwrap_or_default(); cityrecord_map.insert("us_state_fips_code", us_state_fips_code.to_string()); + + // set US county FIPS code cityrecord_map.insert("us_county_fips_code", { let admin2_code = cityrecord .admin2_division .as_ref() .map(|ad| ad.code.clone()); - let us_county_fips_code = match admin2_code { + match admin2_code { Some(admin2_code) => { if admin2_code.starts_with("US.") && admin2_code.len() == 9 { // admin2 code is a US county code, the three-digit county code // is the last three characters of the admin2 code // start at index 7 to skip the US. prefix // e.g. US.NY.061 -> 061 - admin2_code[7..].to_string() + format!("{:0>3}", admin2_code[7..].to_string()) } else { // admin2 code is not a US county code // set to empty string - "".to_string() + String::new() } }, None => { // no admin2 code // set to empty string - "".to_string() + String::new() }, - }; - us_county_fips_code + } }); // countryrecord fields @@ -1911,6 +1927,7 @@ fn get_cityrecord_name_in_lang(cityrecord: &CitiesRecord, lang_lookup: &str) -> } } +#[inline] fn lookup_fips_code(state: &str) -> Option<&str> { US_STATES_FIPS_CODES_LOOKUP .iter()