Importing a Terrain type Google API static map into R without labels - r

I'm looking to import a Google API static map into R using the GetMap function of the RGoogleMaps package. I then plan to plot data points onto the graph in R using PlotOnStaticMap. I would like to use the "terrain" maptype, but don't want the labels on it. I have found a previous stackoverflow question that addresses how to remove the labels on the map by using "style=feature:all|element:labels|visibility:off".
Map with labels: (http://maps.googleapis.com/maps/api/staticmap?center=29.4,-89.2&zoom=9&size=600x500&maptype=terrain&sensor=false)
Map without labels: (http://maps.googleapis.com/maps/api/staticmap?center=29.4,-89.2&zoom=9&size=600x500&maptype=terrain&sensor=false&style=feature:all|element:labels|visibility:off)
This second code produces the exact map I would like. However, when I save it as a PNG file and then go to import it into R using the GetMaps function, it gives me the original map with all the labels still attached.
Does anyone know how I can get the map without labels imported correctly into R? It seems like it shouldn't be that hard, but I haven't been able to come up with a solution.
Thanks!
Codes in R:
smap<-read.table("D:/sediment/Rfiles/smap.txt", header=TRUE, sep= "");
library(RgoogleMaps)
library(rgdal)
MyMap <- GetMap(center=c(29.4, -89.2), zoom=9,
destfile = "D:/sediment/Rfiles/map.png", maptype="terrain")
map<-PlotOnStaticMap(MyMap, lat=smap$lat, lon=smap$lon,
col=c('black'), add=F,cex=1.1,pch=19)
*smap is a data file of lat, lon coordinates to be plotted on the map

Like this?
MyMap <- GetMap(center=c(29.4, -89.2), zoom=9,
destfile = "D:/sediment/Rfiles/map.png", maptype="terrain",
path = "&style=feature:all|element:labels|visibility:off")

Related

How do I use draw_ext() to choose my own basemap bounds with the basemaps package in R?

I am trying to learn how to properly use the basemap package in R. As practice, I am trying to load the 'WorldImagery' map provided by ESRI and overlay World Magnetic Map .csv file I have created to plot inclination angle isolines. I would like to choose a bounding box to see this at a finer scale using the draw_ext() option in the package, but I can't figure out how to get it to run. I know I could load a basemap with other packages, but I'd like to figure out how the draw_ext() function works specifically. The error message appears when I press "done"
library(basemaps)
library(ggplot2)
draw_ext() #not sure if I choose map here or in the ggplot function
set_defaults(map_service = "esri", map_type = "world_imagery")
ggplot() +
geom_raster(data=WorldMagneticMap2022,aes(x=Longitude,y=Latitude,group=Inclination,fill=Inclination),color = '#00000000',interpolate = T)+
basemap_gglayer(draw_ext()) + #Or if I need to choose map here
scale_fill_identity() +
coord_sf()
The interactive map appears correctly in my Viewer, but then I receive the following error:
Error in attr(ed_record, "recorder") <- attr(ed, "recorder", exact = TRUE) :
attempt to set an attribute on NULL

Is there an issue with how I'm reading in my shapefile and plotting it?

I've tried to read in my shape file and then plot it but it seems as though RStudio is stuck on something and will not finish running the plot function. Right now I have:
library(rgdal)
new_county_path <- paste(county_path, "tl_2014_us_county.shp",sep='/')
county1 <- readOGR(new_county_path)
plot(county1)
But it doesn't produce a plot and it seems to be continuously be stuck on something as it only says
>plot(county1)
in the R console. Am I doing something wrong to cause this and is there a better way to read and plot shapefiles?
Without a reproducible example, I can't say why your code doesn't work. If you have a valid shapefile, you should be able to read it in and plot it using the code that you provided:
# first get and save a shapefile to make the code easily reproducible
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
st_write(obj = nc, dsn = 'test/nc.shp')
# now there's a shapefile named "nc.shp" saved in the "test" folder.
# the functions you're using will work on a valid path to a valid shapefile:
library(rgdal)
nc1 <- readOGR("test/nc.shp")
plot(nc1)
I typically use the sf package, which provides more versatility (particularly for plotting):
library(sf)
nc2 <- st_read("test/nc.shp")
plot(st_geometry(nc2))
If your code doesn't work, there's likely a problem with either the path you provided to the readOGR function or with the shapefile itself.

Corrupt KML file when converting shapefile to kml in R using kml function from plotKML

i have a shapefile and i am trying to convert it to a kml file so that i can use it in Qlik Sense. I would like to have the kml area and kml name (tagged as Sector_ID) stored in the kml. Below is the code i used
I have tried various packages but none seems to be working. I can only get the kml area without the right labels (Sector_ID) so far. One of the ways i have tried is the kml function in the plotKML package but it is corrupted when i tried to read them in Qlik.
mapping_sector <- readOGR("data/Sectors/Sectors.shp",
ogrListLayers("data/Sectors/Sectors.shp"))
crs(mapping_sector) <- "+init=epsg:3414"
mapping_sector <- spTransform(mapping_sector, "+init=epsg:4326")
kml(obj=mapping_sector, folder.name="", file.name="Test.kml",
kmz=FALSE,labels=Sector_ID)
Appreciate if any help can be given to my code above or if any other ways i can solve it using R. Thank you.
With sf :
library(sf)
library(dplyr)
read_sf("data/Sectors/Sectors.shp") %>%
st_set_crs(3414L) %>%
st_transform(4326L) %>%
write_sf("Test.kml", dataset_options = c("NameField=Sector_ID"))

how to add my own shape file trajLevel() in openair package R

I am using this following code for plotting the trajectories...........
library(openair)
load("GDASNDL1000m.Rdata")
trajLevel(traj,method="hexbin",col="jet",xbin=40,parameters=NULL,
orientation=c(90,0,0),projection="mercator")
result https://github.com/adeckmyn/maps/files/2667752/GDASNDL1000m.zip
Here, I would like to change the base world map with my own shape file.
my shape file is follows....
z1=maptools::readShapePoly("/home/sateeshm/shapefiles/ncmrwf/india_map")
library(maps)
map(z1)
https://github.com/adeckmyn/maps/files/2667336/World-India.zip
#
Now, the actual question is how to link z1 to trajLevel?
To avoid the hard-coded call to "world" in openair, you wil have to create a new world database in the same file based format as that of the "maps" package.
Probably the simplest way to do this, is to use the mapMaker package. This package is not on CRAN but can be found on github. It is the package I used to create the standard world map. The documentation is minimal, but if you don't care about polygon names etc, you can create a "quick and dirty" world map as follows:
# get your new map as a simple list of polygons (or lines)
z1=maps::map(maptools::readShapePoly("india_map"), plot=FALSE)
# create internal representation
z2=mapMaker::map.make(z1)
# write binary files:
mapMaker::map.export.bin(z2, "/my/path/to/world")
# To make map() call this new database:
library(maps)
worldMapEnv="MYMAP"
Sys.setenv("MYMAP"="/my/path/to/") # don't add the "world" !
now map("world") will draw your version of the world map.

Saving ggmap object to a file which can be reused?

Is it possible to save a ggmap to a local file?
Context: I need a high resolution map of a big region, which takes the stamen server quite a long time to get. I think it would be better, to get the map once, save it to a file and from then on, work on the local copy.
My code for getting the map is:
library(ggmap)
map <- get_stamenmap(bbox = c(left = 8.7, bottom = 46.8, right = 9.7, top = 47.6), zoom = 14, maptype = c("toner"))
I know I can save it as an image for example with:
png(filename=name, width=1280, height=1280)
print(map)
dev.off()
But this doesn't help, because I want to use the map later on to plot different points and polygons on it, which is only possible if the map knows the 'lat' and 'lon' of every point on the picture.
So, is there a way to save the map in it's raw data form?
As suggested by user3710546 save the ggmap object to a RData file with save function, and then, read it back with load. For example:
library(ggmap)
map <- get_map(location="California", zoom=6, maptype="terrain")
save(map, file = "my_map.RData")
load(file = "my_map.RData")
You can use write.table(map,file="filename") to save it in a file.

Resources