Skip to content

Commit

Permalink
geocode: implement US State & County FIPS code
Browse files Browse the repository at this point in the history
partly implements #1352.

Now, we need to lookup FIPS code for US cities
  • Loading branch information
jqnatividad committed Oct 8, 2023
1 parent 15127a9 commit 87daccd
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 87daccd

Please sign in to comment.