r-tmap / tmap

R package for thematic maps
https://r-tmap.github.io/tmap
GNU General Public License v3.0
855 stars 119 forks source link

georeferenced output #914

Open potrykus opened 4 weeks ago

potrykus commented 4 weeks ago

tmap produces PDFs wih clean vector data. Shouldn't tmap automatically georeference that output - i.e. make its output geo-enabled? Here is a python overview: https://gis.stackexchange.com/questions/49646/is-it-possible-to-georeference-an-existing-un-georeferenced-pdf. This is what I found for the R raster package: https://rpubs.com/Rubio-Polania/1123497.

Nowosad commented 3 weeks ago

Related discussion: https://github.com/r-tmap/tmap/issues/383

potrykus commented 2 weeks ago

Thanks. Avenza is an app for mobile use of geo-referenced PDFs (including in a disconnected back-country hiking use case.) They are an end-user platform for US Bureau of Land Management final products https://www.blm.gov/maps/georeferenced-PDFs (expand "How to download and use"). Right now those two major players might be georeferencing maps manually, and may be locked into an old ArcGIS workflow: Avenza-ready Google Earth imagery georeferenced PDF in QGIS. (QGIS is a GIS system that cannot compete with ArcGIS for feature-completeness.)

Given this technological state, tmap georeferenced output would be a powerful and potentially widely-consumed application of the R sf + tmap architecture.

mtennekes commented 1 week ago

Thx for this input @potrykus

Just tested, and it is already possible in sf:

library(sf)
library(tmap)

data(World)

st_write(World, "World.pdf", driver = "PDF")

So now the question is how replace the sf plotting mechanism with tmap. Any ideas @edzer or @tim-salabim ?

If someone could help me with the gdal part, I am happy to implement the other part (passing on the map coordinates of the reference objects).

edzer commented 1 week ago

So now the question is how replace the sf plotting mechanism with tmap.

st_write() only writes data, it doesn't plot: there are no visual elements passed on to it (colors, symbols, line properties etc). If you want to create a GeoPDF from a tmap plot, you'd have to add the geo elements to R's pdf driver - I'd guess these are offset & scale for x and y axes, and a coordinate reference system.

mtennekes commented 1 week ago

Can I use st_write for that purpose? E.g. like this:

tm = tm_shape(World) + tm_poygons()
tmap_save(tm, "World.pdf")

#within tmap_save:
pdf(file)
print(tm)
dev.off()

sf::st_write(?, "World.pdf", offset = , scale = , crs = )

Is so, do you know via which of its arguments these can be passed on (..., dataset_options, layer_options, ... ?

edzer commented 1 week ago

I think st_write() does offset, scale and CRS by itself, as it is an OGR (GDAL) driver; it however takes an sf object, not a tmap object.

tim-salabim commented 1 week ago

I cannot add much here, as mapshot does only simple pdf rendering via png screenshots of the html. The only thing I found is https://gdal.org/en/latest/drivers/vector/pdf.html#feature-style-support

edzer commented 1 week ago

Oh, yes, and there's the raster driver too: https://gdal.org/en/latest/drivers/raster/pdf.html#raster-pdf

tim-salabim commented 1 week ago

Oh, yes, and there's the raster driver too: https://gdal.org/en/latest/drivers/raster/pdf.html#raster-pdf

Yes, but if I understand that correctly, outpu can either be RGB(A) or not. In case it's not I guess it'll be greyscale?