Skip to content

Commit

Permalink
geocode: tweak formats
Browse files Browse the repository at this point in the history
- reverse/now default format is '%city-admin1-country'
- correct bug that capital wasnt being fetched
- optimize format match order
- return country code instead of country name in formats
  • Loading branch information
jqnatividad committed Sep 8, 2023
1 parent 2a1de4a commit aac7f9f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 79 deletions.
22 changes: 12 additions & 10 deletions src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ geocode options:
- '%+' - use the subcommand's default format.
suggest - '%location'
suggestnow - '{name}, {admin1} {country}: {latitude}, {longitude}'
reverse & reversenow - '%city-admin1'
reverse & reversenow - '%city-admin1-country'
If an invalid format is specified, it will be treated as '%+'.
Expand Down Expand Up @@ -1139,6 +1139,8 @@ fn search_index(
}
};

let country = cityrecord.country.clone().unwrap().code;

if formatstr == "%+" {
// default for suggest is location - e.g. "(lat, long)"
if mode == GeocodeSubCmd::SuggestNow {
Expand All @@ -1147,7 +1149,7 @@ fn search_index(
"{name}, {admin1} {country}: {latitude}, {longitude}",
name = cityrecord.name.clone(),
admin1 = get_admin_names(cityrecord, 1).0,
country = cityrecord.country.clone().unwrap().name,
country = country,
latitude = cityrecord.latitude,
longitude = cityrecord.longitude
));
Expand All @@ -1159,7 +1161,6 @@ fn search_index(
));
}

let country = cityrecord.country.clone().unwrap().name;
let capital = engine
.capital(&country)
.map(|cr| cr.name.clone())
Expand Down Expand Up @@ -1195,18 +1196,19 @@ fn search_index(
return None;
};

let country = cityrecord.country.clone().unwrap().code;

if formatstr == "%+" {
// default for reverse is city-admin1 - e.g. "Brooklyn, New York"
// default for reverse is city, admin1 country - e.g. "Brooklyn, New York US"
let (admin1_name, _admin2_name) = get_admin_names(cityrecord, 1);

return Some(format!(
"{city}, {admin1}",
"{city}, {admin1} {country}",
city = cityrecord.name.clone(),
admin1 = admin1_name.clone()
));
}

let country = cityrecord.country.clone().unwrap().name;
let capital = engine
.capital(&country)
.map(|cr| cr.name.clone())
Expand Down Expand Up @@ -1272,14 +1274,14 @@ fn format_result(
if formatstr.starts_with('%') {
// if formatstr starts with %, then we're using a predefined format
match formatstr {
"%city-admin1" | "%city-state" => format!("{}, {}", cityrecord.name, admin1_name),
"%lat-long" => format!("{}, {}", cityrecord.latitude, cityrecord.longitude),
"%city-state" | "%city-admin1" => format!("{}, {}", cityrecord.name, admin1_name),
"%location" => format!("({}, {})", cityrecord.latitude, cityrecord.longitude),
"%city-country" => format!("{}, {}", cityrecord.name, country),
"%city" => cityrecord.name.clone(),
"%city-state-country" | "%city-admin1-country" => {
format!("{}, {} {}", cityrecord.name, admin1_name, country)
},
"%lat-long" => format!("{}, {}", cityrecord.latitude, cityrecord.longitude),
"%city-country" => format!("{}, {}", cityrecord.name, country),
"%city" => cityrecord.name.clone(),
"%city-county-state" | "%city-admin2-admin1" => {
format!("{}, {}, {}", cityrecord.name, admin2_name, admin1_name,)
},
Expand Down
132 changes: 63 additions & 69 deletions tests/test_geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn geocode_suggestnow_default() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Brooklyn, New York United States: 40.6501, -73.94958"],
svec!["Brooklyn, New York US: 40.6501, -73.94958"],
];
assert_eq!(got, expected);
}
Expand Down Expand Up @@ -79,15 +79,15 @@ fn geocode_suggest_intl() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Paris, Île-de-France Region France"],
svec!["Manila, National Capital Region Philippines"],
svec!["London, England United Kingdom"],
svec!["Berlin, Germany"],
svec!["Moscow, Moscow Russia"],
svec!["Paris, Île-de-France Region FR"],
svec!["Manila, National Capital Region PH"],
svec!["London, England GB"],
svec!["Berlin, DE"],
svec!["Moscow, Moscow RU"],
svec!["This is not a Location and it will not be geocoded"],
svec!["Brasília, Federal District Brazil"],
svec!["Brasília, Federal District BR"],
svec!["95.213424, 190,1234565"],
svec!["Havana, La Habana Province Cuba"],
svec!["Havana, La Habana Province CU"],
];
assert_eq!(got, expected);
}
Expand Down Expand Up @@ -120,15 +120,15 @@ fn geocode_suggest_intl_country_filter() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Paris, Texas United States"],
svec!["Manteca, California United States"],
svec!["Sterling, Virginia United States"],
svec!["Burlington, North Carolina United States"],
svec!["Moscow, Idaho United States"],
svec!["Paris, Texas US"],
svec!["Manteca, California US"],
svec!["Sterling, Virginia US"],
svec!["Burlington, North Carolina US"],
svec!["Moscow, Idaho US"],
svec!["This is not a Location and it will not be geocoded"],
svec!["Bradley, Illinois United States"],
svec!["Bradley, Illinois US"],
svec!["95.213424, 190,1234565"],
svec!["Savannah, Georgia United States"],
svec!["Savannah, Georgia US"],
];
assert_eq!(got, expected);
}
Expand Down Expand Up @@ -167,7 +167,7 @@ fn geocode_suggestnow() {
.args(["-f", "%city-admin1-country"]);

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![svec!["Location"], svec!["Paris, Texas United States"]];
let expected = vec![svec!["Location"], svec!["Paris, Texas US"]];
assert_eq!(got, expected);
}

Expand Down Expand Up @@ -260,15 +260,15 @@ fn geocode_suggest_intl_multi_country_filter() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Paris, Île-de-France Region France"],
svec!["Manteca, California United States"],
svec!["Sterling, Virginia United States"],
svec!["Burlington, North Carolina United States"],
svec!["Moscow, Moscow Russia"],
svec!["Paris, Île-de-France Region FR"],
svec!["Manteca, California US"],
svec!["Sterling, Virginia US"],
svec!["Burlington, North Carolina US"],
svec!["Moscow, Moscow RU"],
svec!["This is not a Location and it will not be geocoded"],
svec!["Bradley, Illinois United States"],
svec!["Bradley, Illinois US"],
svec!["95.213424, 190,1234565"],
svec!["Savannah, Georgia United States"],
svec!["Savannah, Georgia US"],
];
assert_eq!(got, expected);
}
Expand All @@ -294,30 +294,30 @@ fn geocode_suggest_filter_country_admin1() {
let mut cmd = wrk.command("geocode");
cmd.arg("suggest")
.arg("Location")
.args(["-f", "{name}, {admin1}, {admin2}, {country}"])
.args(["-f", "{name}, {admin1}, {admin2} {country}"])
.args(["--country", "US"])
.args(["--admin1", "US.NY,New J,Metro Manila"])
.arg("data.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Melrose, New York, Bronx County, United States"],
svec!["East Flatbush, New York, Kings, United States"],
svec!["New York City, New York, , United States"],
svec!["Brooklyn, New York, Kings, United States"],
svec!["East Harlem, New York, New York County, United States"],
svec!["Melrose, New York, Bronx County US"],
svec!["East Flatbush, New York, Kings US"],
svec!["New York City, New York, US"],
svec!["Brooklyn, New York, Kings US"],
svec!["East Harlem, New York, New York County US"],
svec!["This is not a Location and it will not be geocoded"],
// Jersey City matched as the admin1 filter included "New J"
// which starts_with match "New Jersey"
svec!["Jersey City, New Jersey, Hudson, United States"],
svec!["Jersey City, New Jersey, Hudson US"],
// suggest expects a city name, not lat, long
svec!["(41.90059, -87.85673)"],
// Makati did not match, even with the Metro Manila admin1 filter
// as the country filter was set to US
// as a result, the country filter takes precedence over the admin1 filter
// and the closest match for Makati in the US is McAllen in Texas
svec!["McAllen, Texas, Hidalgo, United States"],
svec!["McAllen, Texas, Hidalgo US"],
];
assert_eq!(got, expected);
}
Expand Down Expand Up @@ -458,13 +458,13 @@ fn geocode_suggest_fmt() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Elmhurst, New York United States"],
svec!["East Flatbush, New York United States"],
svec!["New York City, New York United States"],
svec!["East Harlem, New York United States"],
svec!["Elmhurst, New York US"],
svec!["East Flatbush, New York US"],
svec!["New York City, New York US"],
svec!["East Harlem, New York US"],
svec!["This is not a Location and it will not be geocoded"],
svec!["40.71427, -74.00597"], // suggest doesn't work with lat, long
svec!["Makati City, National Capital Region Philippines"],
svec!["Makati City, National Capital Region PH"],
];
assert_eq!(got, expected);
}
Expand Down Expand Up @@ -578,13 +578,13 @@ fn geocode_reverse() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Melrose, New York"],
svec!["East Flatbush, New York"],
svec!["Manhattan, New York"],
svec!["East Harlem, New York"],
svec!["East Harlem, New York"],
svec!["Melrose, New York US"],
svec!["East Flatbush, New York US"],
svec!["Manhattan, New York US"],
svec!["East Harlem, New York US"],
svec!["East Harlem, New York US"],
svec!["This is not a Location and it will not be geocoded"],
svec!["East Flatbush, New York"],
svec!["East Flatbush, New York US"],
svec!["95.213424, 190,1234565"], // invalid lat, long
svec![
"The coordinates are 40.66472342 latitude, -73.93867227 longitudue. This should NOT \
Expand Down Expand Up @@ -619,10 +619,10 @@ fn geocode_reverse_fmtstring() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Melrose, New York United States"],
svec!["East Flatbush, New York United States"],
svec!["Manhattan, New York United States"],
svec!["East Harlem, New York United States"],
svec!["Melrose, New York US"],
svec!["East Flatbush, New York US"],
svec!["Manhattan, New York US"],
svec!["East Harlem, New York US"],
svec!["This is not a Location and it will not be geocoded"],
svec!["95.213424, 190,1234565"], // invalid lat,long
];
Expand Down Expand Up @@ -654,10 +654,10 @@ fn geocode_reverse_fmtstring_intl() {
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["Location"],
svec!["Barcelona, Catalonia Spain"],
svec!["Amsterdam, North Holland Netherlands"],
svec!["Berlin, Germany"],
svec!["Makati City, National Capital Region Philippines"],
svec!["Barcelona, Catalonia ES"],
svec!["Amsterdam, North Holland NL"],
svec!["Berlin, DE"],
svec!["Makati City, National Capital Region PH"],
svec!["This is not a Location and it will not be geocoded"],
svec!["95.213424, 190,1234565"], // invalid lat,long
];
Expand Down Expand Up @@ -776,35 +776,29 @@ fn geocode_suggest_dyncols_fmt() {
"Melrose Park",
"Illinois",
"Cook",
"United States"
"US"
],
svec![
"East Flatbush, New York",
"East Flatbush",
"New York",
"Kings",
"United States"
],
svec![
"Manhattan, New York",
"New York City",
"New York",
"",
"United States"
"US"
],
svec!["Manhattan, New York", "New York City", "New York", "", "US"],
svec![
"Brooklyn, New York",
"Brooklyn Park",
"Minnesota",
"Hennepin",
"United States"
"US"
],
svec![
"East Harlem, New York",
"East Harlem",
"New York",
"New York County",
"United States"
"US"
],
svec![
"This is not a Location and it will not be geocoded",
Expand All @@ -818,15 +812,15 @@ fn geocode_suggest_dyncols_fmt() {
"Jersey City",
"New Jersey",
"Hudson",
"United States"
"US"
],
svec!["95.213424, 190,1234565", "", "", "", ""],
svec![
"Makati, Metro Manila, Philippines",
"Makati City",
"National Capital Region",
"",
"Philippines"
"PH"
],
];
assert_eq!(got, expected);
Expand Down Expand Up @@ -873,35 +867,35 @@ fn geocode_reverse_dyncols_fmt() {
"40.812126, -73.9041813",
"Melrose",
"America/New_York",
"",
"Washington",
"22470"
],
svec![
"40.66472342, -73.93867227",
"East Flatbush",
"America/New_York",
"",
"Washington",
"178464"
],
svec![
"(40.766672, -73.9568128)",
"Manhattan",
"America/New_York",
"",
"Washington",
"1487536"
],
svec![
"( 40.819342, -73.9532127 )",
"East Harlem",
"America/New_York",
"",
"Washington",
"115921"
],
svec![
"< 40.819342,-73.9532127 >",
"East Harlem",
"America/New_York",
"",
"Washington",
"115921"
],
svec![
Expand All @@ -916,7 +910,7 @@ fn geocode_reverse_dyncols_fmt() {
geocoded.",
"East Flatbush",
"America/New_York",
"",
"Washington",
"178464"
],
svec!["95.213424, 190,1234565", "", "", "", ""],
Expand Down

0 comments on commit aac7f9f

Please sign in to comment.