1

I am currently working with data (N of 271,848) in R that looks as follows:

Observation   Longitude     Latitude
--------------------------------------
      1        116.38800    39.928902
      2        53.000000    32.000000
      3          NA          NA
      4          NA          NA

And I am using a reverse geocode function from the following post: Convert latitude and longitude coordinates to country name in R

When I run the coords2country(points) line, I get the following error:

"Error in .checkNumericCoerce2double(obj) : non-finite coordinates"

My best guess is that the function does not know how to treat missing values. When I run the code on a subset of observations (excluding NA's/missing values), it works.

I attempted to modify the function slightly (see last line below) to fix this problem, but that still produced the error I am referring to above.

Reproducible example:

Data <- data.frame(
  Observation = 1:5,
  Longitude = c(116.3880005, 53, -97, NA, NA), 
  Latitude = c(39.92890167, 32, 32, NA, NA))

library(sp)
library(rworldmap)
    coords2country = function(points)
       {  
       countriesSP <- getMap(resolution='low')
       #countriesSP <- getMap(resolution='high') #you could use high res map from rworldxtra if       you were concerned about detail

      # convert our list of points to a SpatialPoints object
      #pointsSP = SpatialPoints(points, proj4string=CRS("+proj=longlat +datum=wgs84"))
      #! andy modified to make the CRS the same as rworldmap
      #pointsSP = SpatialPoints(points, proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
      # new changes in worldmap means you have to use this new CRS (bogdan):
      pointsSP = SpatialPoints(points, proj4string=CRS(" +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))

      # use 'over' to get indices of the Polygons object containing each point 
      indices = over(pointsSP, countriesSP)

      # return the ADMIN names of each country
      indices$ADMIN  
      #indices$ISO3 # returns the ISO3 code
      #The line below is what I thought could resolve the problem.
      na.action = na.omit
        }
Community
  • 1
  • 1
ealfons1
  • 353
  • 1
  • 6
  • 24

1 Answers1

0

Better use this instead:

coords2country_NAsafe <- function(points)
{
    bad <- with(points, is.na(lon) | is.na(lat))
    result <- character(length(bad))
    result[!bad] <- coords2country(points[!bad,])
    result
}
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
  • I am using the rworldmaps package. When I performed the code you provided above, and uploaded the RgoogleMaps library, I kept getting the following error: Error in coords2country(points[!bad, ]) : could not find function "getMap" – ealfons1 Aug 20 '13 at 01:48
  • @ealfons1, please type `require("rworldmap")` and see if that fixes it. – Ferdinand.kraft Aug 20 '13 at 13:22
  • Actually, the code you provided me ended up working, but now I have a new problem. The values produced by the code I originally provided in my question is not producing ISO3 numeric values that match up correctly. For example, for the lat=39.928902 and long=116.388000 (which is China), I got an ISO3 code of 42 (a nonexistent value). – ealfons1 Aug 21 '13 at 01:21
  • @ealfons1, consider posting a new question, with a minimal reproducible example of your new problem. – Ferdinand.kraft Aug 21 '13 at 01:42