library(osmdata)
## Warning: package 'osmdata' was built under R version 3.6.1
## Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(ggmap)
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.1
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
mad_map <- get_map(getbb("Cairo"),maptype = "toner-background",source = "stamen")
## Source : http://tile.stamen.com/terrain/11/1201/842.png
## Source : http://tile.stamen.com/terrain/11/1202/842.png
## Source : http://tile.stamen.com/terrain/11/1203/842.png
## Source : http://tile.stamen.com/terrain/11/1204/842.png
## Source : http://tile.stamen.com/terrain/11/1205/842.png
## Source : http://tile.stamen.com/terrain/11/1201/843.png
## Source : http://tile.stamen.com/terrain/11/1202/843.png
## Source : http://tile.stamen.com/terrain/11/1203/843.png
## Source : http://tile.stamen.com/terrain/11/1204/843.png
## Source : http://tile.stamen.com/terrain/11/1205/843.png
## Source : http://tile.stamen.com/terrain/11/1201/844.png
## Source : http://tile.stamen.com/terrain/11/1202/844.png
## Source : http://tile.stamen.com/terrain/11/1203/844.png
## Source : http://tile.stamen.com/terrain/11/1204/844.png
## Source : http://tile.stamen.com/terrain/11/1205/844.png
## Source : http://tile.stamen.com/terrain/11/1201/845.png
## Source : http://tile.stamen.com/terrain/11/1202/845.png
## Source : http://tile.stamen.com/terrain/11/1203/845.png
## Source : http://tile.stamen.com/terrain/11/1204/845.png
## Source : http://tile.stamen.com/terrain/11/1205/845.png
## Source : http://tile.stamen.com/terrain/11/1201/846.png
## Source : http://tile.stamen.com/terrain/11/1202/846.png
## Source : http://tile.stamen.com/terrain/11/1203/846.png
## Source : http://tile.stamen.com/terrain/11/1204/846.png
## Source : http://tile.stamen.com/terrain/11/1205/846.png
ggmap(mad_map)

library(OpenStreetMap)
## Warning: package 'OpenStreetMap' was built under R version 3.6.1
library(ggplot2)

data <- read.csv(header = T, sep = ",", dec = ".", quote= "'",
text = "'','name','longitude','latitude','sp_sum'
'1','Modern Armenian',45,40,'both'
'2','Modern Armenian',45,40,'both'
'3','Modern Armenian',45,40,'spatial'
'4','Dieri',138,-28.1667,'both'
'5','Dieri',138,-28.1667,'both'
'6','Finnish',25.5577,64.7628,'non-spatial'
'7','Crimean Tatar',28.1418,43.8398,'spatial'
'8','Sochiapam Chinantec',-96.6079,17.7985,'non-spatial'
'9','Ese Ejja',-67.515,-11.7268,'non-spatial'
'10','Makhuwa',38.8052,-14.8509,'non-spatial'
'11','Mualang',111.077,0.31083,'non-spatial'
'12','Martuthunira',116.607,-20.9294,'non-spatial'
'13','Evenki',108.626,53.85,'both'
'14','Afrikaans',30,-22,'both'
'15','Male (Ethiopia)',36.9892,5.91975,'both'
'16','Manchu',126.557,47.3122,'both'
'17','Dime',36.3329,6.20951,'non-spatial'
'18','Koorete',37.8679,5.80545,'non-spatial'
'19','Wolaytta',37.7537,6.32668,'both'
'20','Dizin',35.5763,6.1405,'both'")

map <- openproj(openmap(c(80, -180), c(-80,180), zoom = 2, type = "esri"))
plot <- autoplot(map) + 
  geom_point(data = data, aes(x = longitude, y = latitude),
             color = "black", alpha = 0.8, size = 4) +
  geom_point(data = data, aes(x = longitude, y = latitude, color = sp_sum),
             alpha = 0.8, size = 2)+ labs(x="",y="")
plot

# plot bing map in native mercator coords
library(maps)

#plot bing map in native mercator coords
map <- openmap(c(20,97), c(18,99.5),type='bing')
map
## List of 2
##  $ tiles:List of 1
##   ..$ :List of 5
##   .. ..$ colorData : chr [1:694902] "#7C745D" "#7C755B" "#877F68" "#958E74" ...
##   .. ..$ bbox      :List of 2
##   .. .. ..$ p1: num [1:2] 10797991 2273031
##   .. .. ..$ p2: num [1:2] 11076289 2037549
##   .. ..$ projection:Formal class 'CRS' [package "sp"] with 1 slot
##   .. .. .. ..@ projargs: chr "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
##   .. ..$ xres      : int 767
##   .. ..$ yres      : int 906
##   .. ..- attr(*, "class")= chr "osmtile"
##  $ bbox :List of 2
##   ..$ p1: num [1:2] 10797991 2273031
##   ..$ p2: num [1:2] 11076289 2037549
##  - attr(*, "zoom")= int 9
##  - attr(*, "class")= chr "OpenStreetMap"
## NULL
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.2.1 --
## v tibble  2.1.3     v purrr   0.3.3
## v tidyr   1.0.0     v dplyr   0.8.3
## v readr   1.3.1     v stringr 1.4.0
## v tibble  2.1.3     v forcats 0.4.0
## Warning: package 'tidyr' was built under R version 3.6.1
## Warning: package 'purrr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter()   masks stats::filter()
## x dplyr::lag()      masks stats::lag()
## x .GlobalEnv::map() masks purrr::map(), maps::map()
library(osmdata)
library(sf)
## Warning: package 'sf' was built under R version 3.6.1
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(ggmap)
available_features()
##   [1] "4wd only"                "abandoned"              
##   [3] "abutters"                "access"                 
##   [5] "addr"                    "addr:city"              
##   [7] "addr:conscriptionnumber" "addr:country"           
##   [9] "addr:district"           "addr:flats"             
##  [11] "addr:full"               "addr:hamlet"            
##  [13] "addr:housename"          "addr:housenumber"       
##  [15] "addr:inclusion"          "addr:interpolation"     
##  [17] "addr:place"              "addr:postcode"          
##  [19] "addr:province"           "addr:state"             
##  [21] "addr:street"             "addr:subdistrict"       
##  [23] "addr:suburb"             "admin level"            
##  [25] "aerialway"               "aeroway"                
##  [27] "agricultural"            "alt name"               
##  [29] "amenity"                 "area"                   
##  [31] "atv"                     "backward"               
##  [33] "barrier"                 "basin"                  
##  [35] "bdouble"                 "bicycle"                
##  [37] "bicycle road"            "biergarten"             
##  [39] "boat"                    "border type"            
##  [41] "boundary"                "bridge"                 
##  [43] "building"                "building:fireproof"     
##  [45] "building:flats"          "building:levels"        
##  [47] "building:min level"      "building:soft storey"   
##  [49] "busway"                  "cables"                 
##  [51] "charge"                  "circuits"               
##  [53] "construction"            "covered"                
##  [55] "craft"                   "crossing"               
##  [57] "crossing:island"         "cuisine"                
##  [59] "cutting"                 "cycleway"               
##  [61] "denomination"            "diet"                   
##  [63] "direction"               "dispensing"             
##  [65] "disused"                 "disused:railway"        
##  [67] "disused:shop"            "drink"                  
##  [69] "drive in"                "drive through"          
##  [71] "driving side"            "ele"                    
##  [73] "electrified"             "embankment"             
##  [75] "embedded rails"          "emergency"              
##  [77] "end date"                "entrance"               
##  [79] "est width"               "fee"                    
##  [81] "fire object:type"        "fire operator"          
##  [83] "fire rank"               "foot"                   
##  [85] "ford"                    "forestry"               
##  [87] "forward"                 "frequency"              
##  [89] "fuel"                    "gauge"                  
##  [91] "generator:method"        "generator:output"       
##  [93] "generator:source"        "golf cart"              
##  [95] "goods"                   "hazmat"                 
##  [97] "healthcare"              "healthcare:speciality"  
##  [99] "height"                  "hgv"                    
## [101] "highway"                 "historic"               
## [103] "horse"                   "ice road"               
## [105] "incline"                 "industrial"             
## [107] "inline skates"           "inscription"            
## [109] "internet access"         "is in:city"             
## [111] "is in:country"           "junction"               
## [113] "landuse"                 "lanes"                  
## [115] "layer"                   "leaf cycle"             
## [117] "leaf type"               "leisure"                
## [119] "lhv"                     "line"                   
## [121] "lit"                     "location"               
## [123] "man made"                "maxheight"              
## [125] "maxlength"               "maxspeed"               
## [127] "maxstay"                 "maxweight"              
## [129] "maxwidth"                "military"               
## [131] "minspeed"                "mofa"                   
## [133] "moped"                   "motor vehicle"          
## [135] "motorboat"               "motorcar"               
## [137] "motorcycle"              "motorroad"              
## [139] "mountain pass"           "mtb scale"              
## [141] "mtb:description"         "mtb:scale:imba"         
## [143] "name"                    "narrow"                 
## [145] "natural"                 "noexit"                 
## [147] "non existent levels"     "note"                   
## [149] "nudism"                  "office"                 
## [151] "official name"           "old name"               
## [153] "oneway"                  "opening hours"          
## [155] "operator"                "organic"                
## [157] "oven"                    "overtaking"             
## [159] "parking:condition"       "parking:lane"           
## [161] "passing places"          "place"                  
## [163] "power"                   "produce"                
## [165] "proposed"                "protected area"         
## [167] "psv"                     "public transport"       
## [169] "railway"                 "railway:preserved"      
## [171] "railway:track ref"       "recycling type"         
## [173] "ref"                     "religion"               
## [175] "residential"             "resource"               
## [177] "roadtrain"               "route"                  
## [179] "sac scale"               "service"                
## [181] "service times"           "shelter type"           
## [183] "shop"                    "sidewalk"               
## [185] "site"                    "ski"                    
## [187] "smoothness"              "social facility"        
## [189] "start date"              "step count"             
## [191] "substation"              "surface"                
## [193] "tactile paving"          "tank"                   
## [195] "tidal"                   "toilets:wheelchair"     
## [197] "toll"                    "tourism"                
## [199] "tracks"                  "tracktype"              
## [201] "traffic calming"         "traffic sign"           
## [203] "trail visibility"        "tunnel"                 
## [205] "turn"                    "type"                   
## [207] "usage"                   "vehicle"                
## [209] "vending"                 "voltage"                
## [211] "water"                   "wheelchair"             
## [213] "wholesale"               "width"                  
## [215] "winter road"             "wires"                  
## [217] "wood"
q <- getbb("Chiang Mai")%>%
      opq()%>%
       add_osm_feature("shop")

str(q) #query structure
## List of 4
##  $ bbox    : chr "18.630876,98.828048,18.950876,99.148048"
##  $ prefix  : chr "[out:xml][timeout:25];\n(\n"
##  $ suffix  : chr ");\n(._;>;);\nout body;"
##  $ features: chr " [\"shop\"]"
##  - attr(*, "class")= chr [1:2] "list" "overpass_query"
shops <- osmdata_sf(q)
shops
## Object of class 'osmdata' with:
##                  $bbox : 18.630876,98.828048,18.950876,99.148048
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 5095 points
##             $osm_lines : NULL
##          $osm_polygons : 'sf' Simple Features Collection with 396 polygons
##        $osm_multilines : 'sf' Simple Features Collection with 3 multilinestrings
##     $osm_multipolygons : 'sf' Simple Features Collection with 3 multipolygons
mad_map <- get_map(getbb("Chiang Mai"),maptype = "toner-background")
## maptype = "toner-background" is only available with source = "stamen".
## resetting to source = "stamen"...
## Source : http://tile.stamen.com/terrain/12/3172/1828.png
## Source : http://tile.stamen.com/terrain/12/3173/1828.png
## Source : http://tile.stamen.com/terrain/12/3174/1828.png
## Source : http://tile.stamen.com/terrain/12/3175/1828.png
## Source : http://tile.stamen.com/terrain/12/3176/1828.png
## Source : http://tile.stamen.com/terrain/12/3172/1829.png
## Source : http://tile.stamen.com/terrain/12/3173/1829.png
## Source : http://tile.stamen.com/terrain/12/3174/1829.png
## Source : http://tile.stamen.com/terrain/12/3175/1829.png
## Source : http://tile.stamen.com/terrain/12/3176/1829.png
## Source : http://tile.stamen.com/terrain/12/3172/1830.png
## Source : http://tile.stamen.com/terrain/12/3173/1830.png
## Source : http://tile.stamen.com/terrain/12/3174/1830.png
## Source : http://tile.stamen.com/terrain/12/3175/1830.png
## Source : http://tile.stamen.com/terrain/12/3176/1830.png
## Source : http://tile.stamen.com/terrain/12/3172/1831.png
## Source : http://tile.stamen.com/terrain/12/3173/1831.png
## Source : http://tile.stamen.com/terrain/12/3174/1831.png
## Source : http://tile.stamen.com/terrain/12/3175/1831.png
## Source : http://tile.stamen.com/terrain/12/3176/1831.png
## Source : http://tile.stamen.com/terrain/12/3172/1832.png
## Source : http://tile.stamen.com/terrain/12/3173/1832.png
## Source : http://tile.stamen.com/terrain/12/3174/1832.png
## Source : http://tile.stamen.com/terrain/12/3175/1832.png
## Source : http://tile.stamen.com/terrain/12/3176/1832.png
#final map
ggmap(mad_map)+
  geom_sf(data=shops$osm_points,
          inherit.aes =FALSE,
          colour="#841323",
          fill="#450019",
          alpha=.35,
          size=2,
          shape='.')+
  labs(x="",y="")
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

#bounding box for the Iberian Peninsula
m <- matrix(c(98.908,18.71,99.068,18.87),ncol=2,byrow=TRUE)

row.names(m) <- c("x","y")
names(m) <- c("min","max")

bb = c(98.908,18.71,99.068,18.87)
names(bb) = c("left","bottom","right","top")
x = get_map(bb,maptype="satellite")
## Source : http://tile.stamen.com/terrain/13/6346/3658.png
## Source : http://tile.stamen.com/terrain/13/6347/3658.png
## Source : http://tile.stamen.com/terrain/13/6348/3658.png
## Source : http://tile.stamen.com/terrain/13/6349/3658.png
## Source : http://tile.stamen.com/terrain/13/6350/3658.png
## Source : http://tile.stamen.com/terrain/13/6346/3659.png
## Source : http://tile.stamen.com/terrain/13/6347/3659.png
## Source : http://tile.stamen.com/terrain/13/6348/3659.png
## Source : http://tile.stamen.com/terrain/13/6349/3659.png
## Source : http://tile.stamen.com/terrain/13/6350/3659.png
## Source : http://tile.stamen.com/terrain/13/6346/3660.png
## Source : http://tile.stamen.com/terrain/13/6347/3660.png
## Source : http://tile.stamen.com/terrain/13/6348/3660.png
## Source : http://tile.stamen.com/terrain/13/6349/3660.png
## Source : http://tile.stamen.com/terrain/13/6350/3660.png
## Source : http://tile.stamen.com/terrain/13/6346/3661.png
## Source : http://tile.stamen.com/terrain/13/6347/3661.png
## Source : http://tile.stamen.com/terrain/13/6348/3661.png
## Source : http://tile.stamen.com/terrain/13/6349/3661.png
## Source : http://tile.stamen.com/terrain/13/6350/3661.png
## Source : http://tile.stamen.com/terrain/13/6346/3662.png
## Source : http://tile.stamen.com/terrain/13/6347/3662.png
## Source : http://tile.stamen.com/terrain/13/6348/3662.png
## Source : http://tile.stamen.com/terrain/13/6349/3662.png
## Source : http://tile.stamen.com/terrain/13/6350/3662.png
#building the query
q <- m %>% 
      opq (timeout=25*100) %>%
         add_osm_feature("highway") 

#query
mercadona <- osmdata_sf(q)

#final map
ggplot(mercadona$osm_points)+
  geom_sf(colour="#08519c",
          fill="#08306b",
          alpha=.5,
          size=1,
          shape=21)+
  coord_sf(datum=NA)+
  theme_void()

ggmap(x)

b1<- get_map(c(left = 99.02, bottom = 18.79, right =99.07, top = 18.83), maptype="terrain")
## Source : http://tile.stamen.com/terrain/14/12698/7319.png
## Source : http://tile.stamen.com/terrain/14/12699/7319.png
## Source : http://tile.stamen.com/terrain/14/12700/7319.png
## Source : http://tile.stamen.com/terrain/14/12698/7320.png
## Source : http://tile.stamen.com/terrain/14/12699/7320.png
## Source : http://tile.stamen.com/terrain/14/12700/7320.png
## Source : http://tile.stamen.com/terrain/14/12698/7321.png
## Source : http://tile.stamen.com/terrain/14/12699/7321.png
## Source : http://tile.stamen.com/terrain/14/12700/7321.png
dat = read.csv("T2019-11-28.csv")
ggmap(b1)+
  geom_point(data=dat,aes(x=lon, y=la))