3

I have some coordinates for which I'd like to get the continent they belong.

coords <- data.frame(lat=29.51974, long=-94.88704)

However, sometimes the coordinates are a bit off due to some measurement error. For example, the given coordinate above is in the Galveston Bay and as such is not always labeled as in "North America."

Some similar answers use rworldmap, such as here, but the boundaries of the continent are pretty exact and thus give the continent as missing. Some other versions, such as using the google API, here do work for the given example, but not so much for some others that are a bit more off (a few miles into the ocean).

The answer for matlab I found here seems perfect. I considered exporting the shapefile generated by the matlab code and importing that into R, but I do not own Matlab.

My question is: Is there a way to have broad boundaries to label a coordinate as belonging to continent in R, as given here (same link as above for matlab solution).

Apriconer
  • 45
  • 3
  • 1
    Check out `rgeos::gBuffer`. That is, if a point does not match all given continents, expand the continents until you find one that the point(s) fit within. – r2evans Jul 18 '18 at 06:28
  • If you want this fast - the easiest solution seems to be to scrape the data from the MATLAB solution you link to and write your own function. – s_baldur Jul 18 '18 at 08:36

1 Answers1

0

You can turn the matlab example into an R list and use maps::map.where() (notice that I closed the polygons manually)

NAm=data.frame(lat=c(90,       90,  78.13,   57.5,  15,  15,  1.25,  1.25,  51,  60,    60, 90),
           lon=c(-168.75 ,-10 ,-10     ,-37.5 ,-30 ,-75 ,-82.5 ,-105  ,-180 ,-180 ,-168.75, -168.75))

NAm2 = data.frame(lat=c(51,    51,  60, 51),
          lon=c(166.6, 180, 180, 166.6))

SAm = data.frame(lat=c(1.25,   1.25,  15,  15, -60, -60, 1.25),
         lon=c(-105, -82.5,  -75, -30, -30, -105, -105))

europe=data.frame(lat=c(90,   90,  42.5, 42.5, 40.79, 41, 40.55, 40.40, 40.05, 39.17, 35.46, 
            33,   38,  35.42, 28.25, 15,  57.5,  78.13, 90),
          lon=c(-10, 77.5, 48.8, 30,   28.81, 29, 27.31, 26.75, 26.36, 25.19, 27.91,
                27.5, 10, -10,  -13,   -30, -37.5, -10, -10))

africa=data.frame(lat=c(15,  28.25 ,35.42 ,38 ,33   ,31.74 ,29.54 ,27.78 ,11.3 ,12.5 ,-60 ,-60, 15),
          lon=c(-30 ,-13   ,-10 ,10 ,27.5 ,34.58 ,34.92 ,34.46 ,44.3 ,52    ,75 ,-30, -30))

australia=data.frame(lat=c(-11.88, -10.27, -10 ,-30    ,-52.5 ,-31.88, -11.88),
            lon=c(110,      140  ,145 ,161.25 ,142.5  ,110, 110))

asia=data.frame(lat=c(90   ,42.5 ,42.5 ,40.79 ,41 ,40.55 ,40.4  ,40.05 ,39.17 ,35.46 ,33   ,
              31.74 ,29.54 ,27.78 ,11.3 ,12.5 ,-60 ,-60 ,-31.88 ,-11.88 ,-10.27 ,33.13 ,51    ,60  ,90, 90),
        lon=c(77.5 ,48.8 ,30   ,28.81 ,29 ,27.31 ,26.75 ,26.36 ,25.19 ,27.91 ,27.5 ,
              34.58 ,34.92 ,34.46 ,44.3 ,52   ,75  ,110  ,110   ,110    ,140    ,140   ,166.6 ,180 ,180, 77.5))

asia2=data.frame(lat=c(90    ,90      ,60      ,60, 90),
         lon=c(-180 ,-168.75 ,-168.75 ,-180, -180))

antarctica=data.frame(lat=c(-60, -60, -90, -90, -60),
              lon=c(-180, 180, 180, -180, -180))

continents=list(
        y=c(NAm$lat, NA, NAm2$lat, NA, SAm$lat, NA, europe$lat,NA,africa$lat,NA,
            australia$lat,NA,asia$lat,NA,asia2$lat,NA,antarctica$lat),
        x=c(NAm$lon, NA, NAm2$lon, NA, SAm$lon, NA,europe$lon,NA,africa$lon,NA,
            australia$lon,NA,asia$lon,NA,asia2$lon,NA,antarctica$lon),
        names=c("North America", "North America:2", "South America", "Europe",
            "Africa","Australia","Asia","Asia:2","Antarctica"))
class(continents) <- "map"

with this, you can use the maps package:

maps::map.where(continents, x=-94.88704, y=29.51974)
Alex Deckmyn
  • 1,017
  • 6
  • 11