0

I am getting an error while getting latitude-longitude for cities using package and geocode function.

library(ggmap)
# get frequencies
mycities<- as.character(Chennai$City1)
freq <- as.data.frame(table(mycities))
library(plotrix)
library(ggmap)
freq$Freq <- rescale(freq$Freq, c(1,10)) # c(scale_min, scale_max)
geocode(freq$mycities[1])
dput(head(freq))

And the data structure is

structure(list(mycities = structure(1:6, .Label = c("Alamanda Rs", 
"Aluva", "Ambasamudram", "Ambattur", "Ameerpet", "Amreli", "Anakapalle", 
"ANATHAPUR", "Anekal", "Aranthangi", "Aravakurichi", "Ariyalur", 
"Asifnagar", "Bagalkot", "Banaganapalle", "Bandlaguda", "Bangalore", 
"BANGALORE", "Bangalore North", "Bangalore South", "Bapatla", 
"Belgaum", "Bellary", "BELLARY", "Bhadrachalam", "Bhadravati", 
"Bibinagar", "Bidar", "Bikkavolu", "Bilgi", "Bobbili", "Buchinaidu Kandriga", 
"Chengalpattu", "Chennai", "Chennai City Corporation", "Chikodi", 
"Chillakur", "Chincholi", "Chinglepet", "Chittapur", "Chittur", 
"Chityal", "CHOUTUPPAL", "COCHIN", "Coimbatore South", "Coonoor", 
"Cuddalore", "Dachepalle", "Davangere", "Dharapuram", "Dindigul", 
"Doddaballapura", "Duggirala", "Egmore", "Ernakulam", "Erode", 
"Etcherla", "Ettayapuram", "Ghanpur (Mulug)", "Gokak", "Gokavaram", 
"Gooty", "Gopalapuram Mandalam", "Gopannapalem", "Gulbarga", 
"Gummidipundi", "Guntur", "Haliyal", "Hassan", "Hindupur", "Hoskote", 
"Hospet", "Hosur", "Humnabad", "Huvinahadagali", "Huzurnagar", 
"Hyd", "Hyderabad", "HYDERABAD", "Iluppur", "INDRAKARAN VILLAGE", 
"Jamkhandi", "Jogipet", "KAKINADA", "Kakinada (Urban)", "Kamalapuram", 
"Kamareddy", "Kanakapura", "Kanayannur", "Kanchipuram", "Kariapatti", 
"Karkala", "Karthikappally", "Karunagapally", "Karur", "Karwar", 
"Khairatabad", "Khammam", "KOCHI", "Kondapalli", "Koppal", "Kothakota", 
"KOTHUR", "Kovvur", "Kozhikode", "Krishna", "Krishnagiri", "Kunigal", 
"Kunnathunad", "Kurnool", "Lalgudi", "Laveru Mandal", "Luxettipet", 
"Maddur", "Madurantakam", "MAHESWARAM MANDAL", "Malur", "Mamidikuduru", 
"Manachanallur", "Manapparai", "Mandapeta", "Mangalore", "MANGALORE", 
"Mangapet", "Mannargudi", "Manugurq", "Manuguru", "Mayiladuthurai", 
"Medikonduru", "Melur", "Mettupalayam", "Mettur", "Miryalguda", 
"Muddebihal", "Mudhol", "Mukundapuram", "Musheerabad", "Muthukur", 
"Mysore", "MYSORE", "NA", "Nagapattinam", "Naidupeta", "Nalgonda", 
"NALGONDA", "Nampally", "Nanguneri", "Nanjangud", "Narsapur", 
"Narsipatnam", "Nedumangad", "Nedungadu Commune Panchayat", "Nelamangala", 
"Nellore", "Nilakkottai", "Ongole", "Palakkad", "Paloncha", "Palwancha", 
"Paravada", "Paravur", "Patancheru", "PATHANAMTHITTA", "Pedagantyada", 
"Peddapalli", "Peddapuram", "Pennagaram", "Perambur Purasawalkam", 
"Periyakulam", "Pollachi", "Pondicherry", "Ponneri", "Prathipadu", 
"Pusapatirega", "Qutubullapur", "R.c.puram", "Radhapuram", "Raichur", 
"Rajahmundry (Urban)", "Ramagundam", "Ramanagara", "Ramanathapuram", 
"Ramdurg", "Ranasthalam Mandal", "Ranebennur", "RANGA REDDY", 
"Rangareddy", "Renigunta", "Saidabad", "Saidapet", "Salem", "Samalkota", 
"Sandur", "Sangareddy", "Sankari", "Sathankulam", "Sathyamangalam", 
"Secunderabad", "Sedam", "Shahapur", "Shaikpet", "Singanamala", 
"Sira", "Sirpur (t)", "Sivakasi", "Somwarpet", "SRIKAKULAM", 
"Sriperumbudur", "SRIPERUMBUDUR", "Stn. Jadcherla", "Sullurpeta", 
"Suryapet", "SURYAPET", "Tada", "TADA", "Tadipatri", "Tallarevu", 
"Tambaram", "Tanuku", "Tanuku (mdl)", "Thanjavur", "Tharangambadi", 
"Thimmajipet", "Thirumalairayan Pattinam Commune Panchayat", 
"Thiruvananthapuram", "Thiruvidaimarudur", "Thondamanadu", "Thoothukkudi", 
"Tindivanam", "Tiruchendur", "Tiruchengodu", "TIRUCHIRAPALLI", 
"Tirukkoyilur", "Tiruklalikundram", "Tirumalagiri", "Tirunelveli", 
"Tiruvallur", "Tiruvannamalai", "Tiruvidamarudur", "Tumkur", 
"Tuni", "TUTICORIN", "Udupi", "Uppal", "Uppalaguptam", "Vaikom", 
"Vedaranyam", "Vedasandur", "Veepangandla", "Vemula", "Venkatachalam", 
"Vepada", "Villianur Commune Panchayat", "Vinjamoor", "Virudhachalam", 
"Virudhunagar", "Virur", "VISAKHAPATNAM", "Vontimitta", "Walajapet", 
"Yerraguntla", "Zaheerabad"), class = "factor"), Freq = c(1, 
1, 1, 4.33333333333333, 1.16666666666667, 1)), row.names = c(NA, 
6L), class = "data.frame")

i am getting error like

Error in geocode(freq$mycities[1]) : is.character(location) is not TRUE

SeGa
  • 9,454
  • 3
  • 31
  • 70
Balaji N
  • 363
  • 1
  • 5
  • 15
  • 2
    Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check?dput()). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Dec 18 '18 at 07:10
  • The workflow of your code is hard to follow. What exactly is the data you posted relative to your code? Is it what you started with, or what you ended with? Please be more clear about your questions when posting. – Cyrus Mohammadian Dec 18 '18 at 07:21
  • 1
    `geocode(as.character(freq$mycities))` That should work. `freq$mycities` was a factor. I'm gonna call this a duplicate of https://stackoverflow.com/questions/20937682/r-trying-to-find-latitude-longitude-data-for-cities-in-europe-and-getting-geocod – Cyrus Mohammadian Dec 18 '18 at 07:26
  • Possible duplicate of [R trying to find latitude/longitude data for cities in europe and getting geocode error messege](https://stackoverflow.com/questions/20937682/r-trying-to-find-latitude-longitude-data-for-cities-in-europe-and-getting-geocod) – Cyrus Mohammadian Dec 18 '18 at 07:27
  • ##Example from stackoverflow: mycities1<- c("Hyderabad","Chennai","Bangalore","Cochin","ARNHEM","London") str(mycities1) geocode(mycities1[1])..i have created this simple vector with city names and used the geocode command on the same.. still it is throwing error like. Warning message: geocode failed with status OVER_QUERY_LIMIT, location = "Hyderabad" – Balaji N Dec 18 '18 at 08:33
  • Cyrus, I have referred the same example which you have suggested. but the thing is even with the same example i am getting an error. – Balaji N Dec 18 '18 at 08:49

1 Answers1

1

I think its because the google API changed and thats what its using primarly which will result in this error:

geocode failed with status OVER_QUERY_LIMIT

If you change the source to dsk and convert to character it should work:

geocode(as.character(freq$mycities[1]), source = "dsk")

Information from URL : http://www.datasciencetoolkit.org/maps/api/geocode/json?address=Alamanda%20Rs&sensor=false

lon lat
1 -54 -30

Community
  • 1
  • 1
SeGa
  • 9,454
  • 3
  • 31
  • 70