Search code examples
ggmapr-sf

How to batch printouts of a ggmap at maximum zoom level within a defined area


Aim: Print several maps at the maximum zoom layer from ggmap within a specific area.

A powerpoint illustration of the problem

Consider the white-shaded area.

enter image description here

It was made by adding a sf polygon to a ggmap (zoom level=14).

We would like to manually count (for lack of a better system), certain tree types in certain places. We can somewhat do that using zoom level 21.

enter image description here

Question:

How can we take the larger polygon, subset it into as many sections with zoom level 21 (maximum) and print them all out automatically.

Here is the polygon in question:

structure(list(structure(list(structure(c(2.22115657379836, 2.22108333593348, 
2.2210832657233, 2.2208747529956, 2.22087469846485, 2.22069824851244, 
2.22069821321369, 2.22049350748363, 2.22042571855732, 2.22029424363981, 
2.22024071919476, 2.22015518978752, 2.22010045846771, 2.22009117617204, 
2.22006341991893, 2.21992513926521, 2.21972970842387, 2.21954783290676, 
2.21882331004279, 2.21838793641161, 2.21799690413346, 2.21730736573345, 
2.21712567697392, 2.21700077956106, 2.21645222577587, 2.21607459238001, 
2.21522606303022, 2.21478001155017, 2.21433348184483, 2.21303093201606, 
2.21027046304305, 2.2095245229537, 2.20863404195809, 2.20766409847129, 
2.20724390396454, 2.20805400219807, 2.20791406929967, 2.20674073907976, 
2.20674041492358, 2.208142815945, 2.20706313796755, 2.20608234438664, 
2.20593833970737, 2.20589479833775, 2.20557719638154, 2.20550345393983, 
2.20537658045522, 2.2049993552272, 2.20479176687792, 2.20439848222521, 
2.20402561745217, 2.20296449368024, 2.20238034847157, 2.20191974086683, 
2.20145735779761, 2.20131589763529, 2.20119057318329, 2.20118339312404, 
2.20118241987674, 2.20113869648959, 2.20107433557215, 2.201008456099, 
2.2009352715015, 2.20059183965962, 2.20019657809117, 2.19987047900584, 
2.19969073911632, 2.19960649865742, 2.19947946541358, 2.19911081455115, 
2.19920221156404, 2.19906916031089, 2.1989425618428, 2.19879067421686, 
2.19858505993154, 2.19847479311932, 2.19759102451623, 2.19696304262038, 
2.19657201750591, 2.19567162584306, 2.19489357804266, 2.19441125534295, 
2.19419094157669, 2.19393269378947, 2.19391425486373, 2.19390337204527, 
2.19419659774364, 2.19423643125757, 2.19424446448497, 2.19421899307008, 
2.19388934027598, 2.1934920869662, 2.19301315233493, 2.19266282114487, 
2.19235452382726, 2.19200317442062, 2.19175917165025, 2.19099835465887, 
2.19077071347836, 2.19035318188367, 2.19007757410439, 2.18982456684276, 
2.18975222950887, 2.18958637521227, 2.18913469936509, 2.18894370300847, 
2.18812739688908, 2.18797068725867, 2.18797068613092, 2.18841477748147, 
2.19044467541825, 2.19061890001484, 2.19063918581555, 2.19066468511554, 
2.19065163648568, 2.19065162937658, 2.19035400509906, 2.19134420395442, 
2.19134430556649, 2.19166574678999, 2.19172447852441, 2.19186540676093, 
2.19187619712481, 2.19187631579536, 2.1921241951311, 2.19263222571845, 
2.1930634815795, 2.19316800297356, 2.19321488803215, 2.19324848443611, 
2.19325745930946, 2.19325793227032, 2.19325793474767, 2.19307649883738, 
2.19307126292433, 2.19307126180355, 2.1933810122191, 2.19358909020357, 
2.19377497202991, 2.1938808969701, 2.1938915559309, 2.1938916745837, 
2.19406734095441, 2.19416012343099, 2.19458244768694, 2.19490736318908, 
2.19497553715063, 2.1950333178871, 2.19545281497625, 2.19574175111424, 
2.19593051757347, 2.19638209483768, 2.19659451277603, 2.19671918175478, 
2.19681153565218, 2.19692488655162, 2.19688485844789, 2.196428220432, 
2.19619337800405, 2.19605197687594, 2.19598888025555, 2.19600932765516, 
2.19600933075069, 2.19669027201866, 2.1969823182471, 2.19698243818394, 
2.19722642792227, 2.19737686980799, 2.19742245018152, 2.19733576705201, 
2.19712473683159, 2.19667951653103, 2.19699171506765, 2.19713009049724, 
2.1972461536512, 2.19753737622364, 2.1979433496177, 2.19817537077521, 
2.19818636697705, 2.1981864845947, 2.19841905927668, 2.19890976756638, 
2.1993661025029, 2.2008058005685, 2.20242149569994, 2.20279580789805, 
2.202795810205, 2.20289917667669, 2.20300727500183, 2.20304553234465, 
2.20298318678291, 2.20295393308141, 2.20295393298928, 2.2029497267644, 
2.20294972187475, 2.2029443572395, 2.20286923463354, 2.20308585444507, 
2.20323123131268, 2.2034108872256, 2.20358105905846, 2.20370455162952, 
2.20400942309341, 2.20400941294097, 2.20400797495908, 2.2040001736209, 
2.20404616374356, 2.20405446182339, 2.20409626990119, 2.20409632015545, 
2.20425188400969, 2.20448982083269, 2.2047385394296, 2.20492701358133, 
2.20537066685531, 2.20537078071999, 2.20575843609918, 2.20793581773529, 
2.20833116846838, 2.20908955282484, 2.20945862384346, 2.20982531098688, 
2.21111067386528, 2.21147532321673, 2.21173434902099, 2.21188459462616, 
2.21189318660148, 2.21218435977914, 2.21270328243467, 2.21352430142304, 
2.21352442019724, 2.213880686517, 2.21424821415182, 2.2151410994521, 
2.21563467374774, 2.21761923899252, 2.21761935784161, 2.22383936740499, 
2.22384734593697, 2.22416058596656, 2.22419470374653, 2.22419555501584, 
2.22548347198032, 2.22619016278172, 2.22759900082387, 2.22806401317922, 
2.22808052756525, 2.22836601974774, 2.22852127045585, 2.22867624272038, 
2.22976273753877, 2.22996784043195, 2.23094443542124, 2.23293568240132, 
2.23341314015027, 2.23361908082224, 2.23369093739271, 2.23359928823221, 
2.23335374667644, 2.23320863449082, 2.23314798896623, 2.23314797563239, 
2.23312759585012, 2.23298906630452, 2.23277448925152, 2.23221838814132, 
2.23185657421099, 2.23181521242187, 2.2316152548112, 2.23191911155188, 
2.23191913956263, 2.23152603598646, 2.23092254138644, 2.23092253366687, 
2.23087212450846, 2.23077103141938, 2.23059424363093, 2.23050734169699, 
2.23040469945002, 2.23040468732049, 2.23039389079477, 2.23031244099327, 
2.23037753020828, 2.23037747794233, 2.22985118716481, 2.22938853998867, 
2.22915096876563, 2.22915084995906, 2.22883433840107, 2.22874116404214, 
2.22848262650968, 2.22763553509903, 2.2274789231352, 2.22741057990175, 
2.22741046713687, 2.22674820328281, 2.22536987031378, 2.2253007198407, 
2.22524416137236, 2.22506384158575, 2.22486356889279, 2.22454056584265, 
2.22454044826008, 2.22418787952341, 2.2237824770994, 2.22347992472229, 
2.22320436430499, 2.22292326521108, 2.2225136608347, 2.22241711630168, 
2.22234478298235, 2.22185484493909, 2.2212741358726, 2.22115657379836, 
41.5316425105264, 41.5315713936213, 41.5315713508114, 41.5314420778065, 
41.5314420269961, 41.5312766795763, 41.5312766243925, 41.5309543932229, 
41.5308476853418, 41.5306407284891, 41.5305332687232, 41.5303615508624, 
41.5299718785914, 41.5299057893933, 41.5297081682289, 41.5293647725404, 
41.5290530558183, 41.5287629582104, 41.5274311741527, 41.5269253395979, 
41.5262912705927, 41.5256124751619, 41.5253222847887, 41.5250540073854, 
41.5244938921445, 41.5239241289351, 41.5241477631837, 41.5243018085951, 
41.5244853029096, 41.5252422061932, 41.5255571102458, 41.5256599864295, 
41.5258306808226, 41.526079275382, 41.526253105529, 41.5273288426016, 
41.5273895860922, 41.5278989157442, 41.5278990567243, 41.5297493591, 
41.5309798908958, 41.5302258826553, 41.5303355475596, 41.5303687054087, 
41.5306105690536, 41.5306667245169, 41.5307633419939, 41.5311843507043, 
41.5315641961281, 41.5323269195629, 41.5329386395589, 41.5343560716373, 
41.5351754725476, 41.5358519705388, 41.5366722114986, 41.5368673186532, 
41.5371540517678, 41.5373466867035, 41.5373727803617, 41.5385456802878, 
41.5395386484725, 41.5399434293703, 41.5402304342413, 41.5405678947388, 
41.5408788725802, 41.5412295110846, 41.5416988024551, 41.5421819080004, 
41.5426058088318, 41.5435770223238, 41.543923988692, 41.5441257266694, 
41.544523507778, 41.5448426600388, 41.5449858696266, 41.545062669922, 
41.5454812956175, 41.5456141861892, 41.5455787601343, 41.5459266036019, 
41.5462271744003, 41.5465440084603, 41.5467711602937, 41.5472530434285, 
41.5473378527338, 41.5474612661244, 41.5480933790111, 41.5483521676867, 
41.5494346283411, 41.5497576315067, 41.5503207998493, 41.551141912387, 
41.5516071136352, 41.5521054609678, 41.5526848975737, 41.5532641208374, 
41.5538601295869, 41.5550611276823, 41.5555072784015, 41.556188984329, 
41.5567400915245, 41.557344048783, 41.5575190913499, 41.557702211816, 
41.5601744967289, 41.5614284925948, 41.5650217663022, 41.5659385903205, 
41.5659386803849, 41.5660271117714, 41.5666708372536, 41.5667510564253, 
41.5667862373881, 41.5668304622853, 41.5669444029918, 41.5669444921135, 
41.5673724950821, 41.5676339766218, 41.5676340034575, 41.5671710371431, 
41.567145148893, 41.5671542478272, 41.567154946969, 41.5671549523068, 
41.5672479907465, 41.5674386729291, 41.5675670784519, 41.567620504308, 
41.5676821717043, 41.5677263627695, 41.5678931497387, 41.5679018900791, 
41.5679019801688, 41.5684011509518, 41.568822293548, 41.5688223836126, 
41.5692898700535, 41.5694844438913, 41.5695734770426, 41.5695822307838, 
41.5695831143647, 41.5695831215017, 41.5695579608711, 41.5696201297209, 
41.5701936063444, 41.5706348027286, 41.570749311677, 41.5708463638555, 
41.5710676039072, 41.5712199840505, 41.5712808340336, 41.5714264000769, 
41.5715219345038, 41.5715780037071, 41.5716752064533, 41.5719656708788, 
41.572360359371, 41.5724186909033, 41.5725047806792, 41.5726003504735, 
41.572968576177, 41.573205748969, 41.5732057895232, 41.5729734752075, 
41.5729755135077, 41.5729755143454, 41.5731001659953, 41.5732240740489, 
41.5733209497548, 41.5737154027058, 41.5741354694642, 41.5747817862721, 
41.5750560719572, 41.5752150251173, 41.5752772639331, 41.5753758525832, 
41.5756333179124, 41.5758192232447, 41.575828036042, 41.5758281278352, 
41.5759263061323, 41.5759472867263, 41.5759153344838, 41.5757672660097, 
41.5755677973996, 41.5755529166785, 41.5755528266222, 41.5747547803033, 
41.5745185485284, 41.5742554413654, 41.5741534274363, 41.5741055574704, 
41.5741054673974, 41.5734910540091, 41.5734909639028, 41.5734207603678, 
41.5724370990899, 41.5719295090963, 41.5715091572432, 41.5711592081466, 
41.5706337320658, 41.5700903691885, 41.5690653811398, 41.5690653288275, 
41.5690578951051, 41.5690175787324, 41.5689252126162, 41.5688802122421, 
41.568653449839, 41.5686533619159, 41.5683560251683, 41.5680153046079, 
41.5677537416925, 41.5676320980914, 41.5676615455164, 41.5676615517077, 
41.5674249951275, 41.565819597611, 41.5654616675682, 41.5638635894172, 
41.5627766970186, 41.5618747958003, 41.5597048471954, 41.5589673086358, 
41.5579208172486, 41.5575855368069, 41.5575664048299, 41.5569167050915, 
41.557002492955, 41.5569875723446, 41.5569875677526, 41.5569488426554, 
41.5568811855235, 41.5568135998021, 41.5567347279087, 41.5564817201212, 
41.5564817092204, 41.556007264707, 41.5560063187568, 41.5559691826765, 
41.5559696407612, 41.5559696519091, 41.5558336389596, 41.5557590024234, 
41.5555819253889, 41.5555003492198, 41.5554974525602, 41.5554473680391, 
41.5554405725669, 41.5554571939128, 41.5557247817237, 41.5557005739143, 
41.5553864448538, 41.5548130897548, 41.5546031591924, 41.5543652970882, 
41.5540579966537, 41.5531467497617, 41.5522600591699, 41.5515408506241, 
41.550899128194, 41.550899038933, 41.5507706385086, 41.5498976291211, 
41.5482594841354, 41.5471836366588, 41.5462877888534, 41.5460961978584, 
41.5451699939956, 41.5447789803725, 41.5447789436298, 41.5446600340365, 
41.5444774794054, 41.5444774226078, 41.5440982957722, 41.5438835163651, 
41.5435079016111, 41.5430203861845, 41.5414735063532, 41.5414734170999, 
41.5414077375457, 41.5409120040132, 41.5408521808942, 41.5408521400116, 
41.5404404492162, 41.5399039397321, 41.539628436564, 41.5396283456947, 
41.539387080806, 41.5392900787866, 41.5390209188441, 41.5383945433425, 
41.5382221734983, 41.5381469538066, 41.5381468593731, 41.5375646873159, 
41.5361108259941, 41.535992634787, 41.535895965741, 41.5357426570989, 
41.5355723826411, 41.5351849646074, 41.5351848728408, 41.534904351193, 
41.5342702956555, 41.5340162494946, 41.5337848643848, 41.5334834733767, 
41.5332024710372, 41.5331039296135, 41.5330301012372, 41.5323205263023, 
41.5317566599916, 41.5316425105264), .Dim = c(314L, 2L))), class = c("XY", 
"POLYGON", "sfg"))), n_empty = 0L, precision = 0, crs = structure(list(
    epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), .Names = c("epsg", 
"proj4string"), class = "crs"), class = c("sfc_POLYGON", "sfc"
), bbox = structure(c(2.18797068613092, 41.5239241289351, 2.23369093739271, 
41.5759472867263), .Names = c("xmin", "ymin", "xmax", "ymax"), class = "bbox"))

Here is the ggmap:

#Coordinates for Mollet del Vallès
mdv<-c(lon=2.2089,lat=41.5451)
#Download the relevant map
png(filename = "big_map.png",width=1280,height=1280)
big_map<-get_map(location=mdv,zoom=14,maptype="satellite",source="google")

gen_mdv<-ggmap(big_map) + 
geom_sf(data = mdv_pol, inherit.aes = FALSE,alpha=0.3)

This is a step to solve an urban forestry problem so maybe @aspiringurbandatascientist might have an idea.


Solution

  • This should do the trick:

    Get required cell size

    small_map <- get_map(location = mdv, zoom = 21, maptype = "satellite", source = "google")
    bb <- attr(small_map, 'bb')
    cellsize <- c(diff(bb[c(1, 3)] %>% unlist), diff(bb[c(2,4)] %>% unlist))
    

    Get grid using st_make_grid (centre points) and intersect with polygon

    poly.grid <- st_make_grid(polygon, what = 'centers', cellsize = cellsize) %>%
      st_intersection(polygon) 
    
    ggplot() + 
      geom_sf(data = polygon %>% st_sf) + 
      geom_sf(data = poly.grid %>% st_sf, size = 0.1)
    

    enter image description here

    Get vector coordinates for each point as element of list

    poly.grid <- lapply(poly.grid, function(x) st_coordinates(x))
    

    Generate list of small maps at zoom 21 for each grid centre point. This will take a while.

    small_maps <- lapply(poly.grid, function(x) get_map(location = x, zoom = 21, maptype = "satellite", source = "google")) 
    
    ggmap(small_maps[[1]])
    

    enter image description here

    to print, use ggsave. You can adjust filename and other arguments passed to ggsave like resolution, width, height:

    lapply(seq_along(small_maps), function(x) {
      ggsave(filename = paste0("map_", x,".png"), plot = ggmap(small_maps[[x]]), device = 'png')
    })
    

    make sure to read the help of ggsave, e.g. ?ggsave. Width and height arguments are in inches so don't use width = 1000!

    Another tip: use lapply(seq_along(small_maps[1:2]), ...) to only try it on a subset of the maps. Where ... includes the rest of the function (I got lazy) :)