Custom background image with ggmap - r

I'm using R and ggmap to analyse some geographic data. I would like to use my own background map (a tif file) instead of the ones provided by ggmap (google maps, osm, stamen, etc.).
Is there a way to load my own raster file and display it using ggmap ? I really want to keep ggmap as most of my analysis use ggmap's functions (geom_density2d, geom_tile, etc.). For example a wrapper that transform a raster file reads by the raster package to a ggmap get_map object ?
Thanks

Related

Why is my GIS Shapefile (CRS 4326) not viewed from directly above?

I have loaded two shapefiles into Netlogo. One shapefile's original projection was 4326 (osmdata) and one was originally 28992 (Dutch government data) which was st_transformed(4326) in R. When I load them into Netlogo, it does not display the shapefile directly from above, which it does do from R ggplot
Why is this and how do I get it viewed from above?
It was solved by using gis:set-world-envelope-ds instead of gis:set-world-envelope

How can I animate using SPPLOT?

I have a shapefile with 5000+ polygons.
I have a netCDF file with timeseries data for several variables with one common key with the shapefile.
Static Map
For any particular time, I plot a static map by
Merging netCDF data for one timestep into the shapefile
Plot the shapefile using spplot
Animation
Now, I want to create an animation for a small time period.
For ggplot2, there are packages such as gganimate etc.
But, how can I do this using spplot?
P.S. GGPLOT2 is too slow for this purpose.
Try the animation package?
Remember that spplot is a lattice based function, so if you're doing this in a function you'll need to print() inside the function.

How to obtain koppen-geiger climate map for ggmap

I would like to use ggmap to plot several data points on top of a koppen-geiger climate map.
The kopper-geiger data and GIS/KMZ maps can be downloaded here:
http://koeppen-geiger.vu-wien.ac.at/present.htm
I've managed to have a code to plot the points on regular maps, obtained through the get_map function but I fail to use other maps such as koppen-geiger.
Any help will be appreaciated!
Your basic problem is that the map you are attmepting to use is an image file that is not georeferenced. So unless you want to go through the unnecessary and probably time consuming process of georeferencing this image yourself, you will be better taking an alternative approach. There are perhaps a few ways to do this. But, unless you have very few data points to overlay on the map which you can place manually using the lat-long grid of the image, then the least painful method will certainly be to redraw the map yourself using the shapefile.
This is not the right place to give you an introductory lesson on GIS, but the basic steps are to
Download shapefile (which is available at the same website as the image you linked)
Project map to desired coordinate system
Plot map, coloring by climate class
Color the ocean layer
Add labels, legend, and graticule, as desired
Overplot with your own climate data, and legend for these.
If you are unsure how to approach any of these steps, then take an introductory course on GIS, and search the Web for instructional materials. You may find this resource useful.
https://cran.r-project.org/doc/contrib/intro-spatial-rl.pdf

How to create interactive online map with contour (filled) plot layer?

I would like to create an online interactive map with filled contour plot layer like the ones can be seen on openweathermaps (I would like to use my own data for the plots).
What I need is also similar to the Leaflet heatmap (heatmap.js) but without dynamically changing the colors and the extent of the graphical objects (as in case of heatmap.js). Let's call them static heat maps.
I would like to know which mapping code/library can be used to produce such maps.
I am really newbie to these things, so please bear with me.
I tried Leaflet but did not find any plugin which would create filled contour map layers (static heatmap). I created the following map with Leaflet where the rectangles are geojson polylines and the color is based on some assigned values to every rectangle (elevation)
my leaflet attempt
The problem with this approach is that if higher resolution (smaller and more rectangles) is needed the site would really slow down.
I checked OpenLayers but did not see any similar examples.
I have the data in a matrix format:
Lat; Long; Value
.
.
Values are given in every gridpoints.
(if needed I would convert into other formats, like in case of the above attempt into geojson format)
The data is static, would be saved on the server.
So what I basically want to accomplish is a site where some spatial data is represented as filled contour map (static heatmap) and it is plotted over a map.
Here is my solution to the problem using open-source programs and free, online service:
(1) Processing the data in a GIS program. I used QGIS. I interpolated my data which is in grid points to get a high resolution raster map.
(2) Save the post-processed raster map as a georeferenced *.tif image.
(3) Import the image into TileMill. Remove the basemap and keep only the image as the only layer (style it).
(4) Export the 'map' from TileMill as MBTiles. This will save numerous *.png files (tiles) corresponding to different zoom levels. These are the same type as google or openstreetmap use for their online maps.
(5) Create a free account at Mapbox and create a new map project. Upload the MBTiles created by TileMill (can be directly uploaded from it). Style it.
(6) Use the Map ID corresponding to your created project to embed the map into html sites, e.g. the javascript code:
// Provide your access token
L.mapbox.accessToken = 'Mapbox will generate this for you';
// Create a map in the div #map
var map = L.mapbox.map('map', 'username.mapid', {
minZoom: 5,
maxZoom: 10
}).setView([47, 20], 8);
Example hosted on Mapbox
Sample image(I do not how long will the above link live):
In retrospect, the question would have been better fit to GIS stack exchange.

how to convert RgoogleMaps PNG to SpatialGridDataFrame in R?

I have derived a 'static map' using the GetMap() function from the RgoogleMaps package. I can save it (MyMap) to my harddrive as a PNG. However, then it looses the spatial reference.
Has anybody succeeded in creating a spatial object (in the sense of a GDAL-readable data format) from such a PNG?
Get your RGoogleMaps object as MyMap. Make it download the tile to MyTile1.png Use the raster package.
bb = MyMap$BBOX
t = stack("MyTile.png")
extent(t)=extent(bb$ll[,2],bb$ur[,2],bb$ll[,1],bb$ur[,1])
Now t is a raster stack. Do plotRGB(t) and you should see it. Now you can try writeRaster to create a GDAL data source. GeoTIFF perhaps?
And watch out for that pesky Google image usage agreement...

Resources