Skip to content

Commit ade3727

Browse files
authored
Update 4-AI_image_multimodal_location_GPT.R
Improvements on reverse geocoding algorithm
1 parent 3ac97d5 commit ade3727

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

4-AI_image_multimodal_location_GPT.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,18 +701,35 @@ if (length(landmarks$latitude)==1 && landmarks$latitude=="") {
701701
# Find the address by reverse geocoding the coordinates using ggmap
702702
if (lon!="") {
703703
address <- revgeocode(c(lon, lat), output = "all")
704-
if (address$results[[1]]$address_components[[4]]$long_name != ""){
704+
if (address$results[[1]]$address_components[[1]]$long_name != ""){
705705
city_check <- sapply(address$results[[1]]$address_components, has_any_word, words = "locality")
706-
city <- address$results[[1]]$address_components[city_check]
707-
city <- city[[1]]$long_name
706+
if (length(address$results[[1]]$address_components[city_check])!=0){
707+
city <- address$results[[1]]$address_components[city_check]
708+
city <- city[[1]]$long_name
709+
} else {
710+
city_check <- sapply(address$results[[1]]$address_components, has_any_word, words = "postal_town")
711+
if (length(address$results[[1]]$address_components[city_check])!=0) {
712+
city <- address$results[[1]]$address_components[city_check]
713+
city <- city[[1]]$long_name
714+
} else {
715+
city <- ""
716+
}
717+
}
708718
street_check <- sapply(address$results[[1]]$address_components, has_any_word, words = "route")
709-
street <- address$results[[1]]$address_components[street_check]
710-
street <- street[[1]]$long_name
719+
if (length(address$results[[1]]$address_components[street_check])!=0) {
720+
street <- address$results[[1]]$address_components[street_check]
721+
}
722+
if (length(street)>0){
723+
street <- street[[1]]$long_name
724+
} else {
725+
street <- ""
726+
}
711727
} else {
712728
city <- revgeo(lon, lat, provider = "bing", API = bing_api_key, output = NULL, item = "city")
713729
}
714-
if (address$results[[1]]$address_components[[5]]$long_name != ""){
715-
country_check <- sapply(address$results[[1]]$address_components, has_any_word, words = "country")
730+
731+
country_check <- sapply(address$results[[1]]$address_components, has_any_word, words = "country")
732+
if (length(address$results[[1]]$address_components[country_check])!=0) {
716733
country <- address$results[[1]]$address_components[country_check]
717734
country <- country[[1]]$long_name
718735
} else {

0 commit comments

Comments
 (0)