Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a lat-long grid to a projected map?

EDIT: question really pertains to how one can add lat/long gridlines to a projected map. I changed title to match.

I have some layers in geographic coords. I want to plot them in LCC projection, but have a geographic (lat/long) grid. From mapproj I can use map.grid() to add a grid, with the limits set by the lim argument. It takes a vector or a range object:

a vector of 4 numbers specifying limits: c(lon.low, lon.high, lat.low, lat.high). lim can also be a list with a component named range, such as the result of map, from which limits are taken.

I construct my map by clipping a large vector layer with a clipping polygon:

myPoly <- readOGR(dsn=".", layer="myPolygon")  # just a shapefile in geographic coords
library(raster)  # To convert an 'extent' object to a SpatialPolygons object
cp <- as(extent(146, 149, -39, -37.5), "SpatialPolygons")
proj4string(cp) <- CRS(proj4string(myPoly))  # copy from shapefile

# Transform and plot:
lcc <- CRS("+init=epsg:3111")
myPoly.proj <- spTransform(myPoly, lcc)
cp.proj <- spTransform(cp, lcc)  # transform the clip box
myPoly.proj.clip <- gIntersection(myPoly.proj, cp.proj, byid=TRUE)
plot(myPoly.proj.clip) 

# Then finally, add a lat/long grid:
map.grid(lim=as.vector(cp.proj@bbox), labels=TRUE)

That last line is not correct, as the @bbox returned is xmin, ymin, xmax, ymax, but needs to be in xmin, xmax, ymin, ymax. There must be a simple solution to all this, but as usual I am lost in the vortex. I could manually create a limits vector, but really?

like image 938
a different ben Avatar asked Dec 05 '25 16:12

a different ben


1 Answers

EDIT: the OP points out rgdal::llgridlines which is a better solution.

You are using context from sp/rgdal which uses a different system to that of mapproj/maps.

Try this (untested):

library(rgdal)
gl <- gridlines(myPoly)
cp.gl <- spTransform(gl, lcc)
plot(cp.gl, add = TRUE)

See ?gridlines for more on using this with labels. I find it works well as long as you stay away from circumpolar maps.

like image 129
mdsumner Avatar answered Dec 08 '25 09:12

mdsumner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!