tmap icon indicating copy to clipboard operation
tmap copied to clipboard

Export tmap objects to GIS software (QGIS, ArcGIS, etc)

Open geamaro opened this issue 4 years ago • 5 comments

Sorry if this is not the right place but I could not found how to write (export) a SHP file to use in GIS software from a tmap object.

geamaro avatar Oct 13 '20 05:10 geamaro

A tmap object is a list that contains all plot specifications, including the shape. You'll have to look for the list item tm_shape that includes the shape you want to export. Note that there may be multiple tm_shape list items, so it is saver to use indexing.

An reproducible example:

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)

data(World)
data(metro)

tm = tm_shape(World) +
    tm_polygons("HPI") +
    tm_shape(metro) + 
    tm_dots("pop2020")


names(tm)
#> [1] "tm_shape"   "tm_fill"    "tm_borders" "tm_shape"   "tm_symbols"
metro_shp = tm[[4]]$shp

st_write(metro_shp, "metro_shp.shp")
#> Writing layer `metro' to data source `metro_shp.shp' using driver `ESRI Shapefile'
#> Writing 436 features with 12 fields and geometry type Point.

Created on 2020-10-16 by the reprex package (v0.3.0)

This example is a bit silly, since you can directly export the shape as follows:

data(metro)
st_write(metro, "metro_shp.shp")

What do you want to use it for? It is not possible to export the entire map (with all the layers) into one SHP file. I'm not even sure whether the SHP format would support that.

I guess that the underlying question is: how to export a map produced with tmap into GIS software, e.g. QGIS. Is that what you are after?

mtennekes avatar Oct 16 '20 08:10 mtennekes

Thanks for the answer. And you are correct. That's exactly it: how to export a map produced with tmap into GIS software, with all its layers. At the Brazilian Agricultural Research Corporation (Embrapa) we have a public map repository (Geoinfo: http://www.embrapa.br/geoinfo) where several maps generated by research projects are made available, especially to guide public policies. I am looking for a way to have a map that can be opened in all its layers by GIS software in order to be able to place it on that platform. tmap was the best tool I found in R to generate the combination of everything I need (shapes, rasters, lines, points, etc.) and to have an exceptionally well-structured visualization. However, now, I need to find a way to export this complete set (the generated map, with all its layers and the settings I used for visualization, including classes and scales) for GIS users and I have no idea how to do this (if possible).

geamaro avatar Oct 16 '20 13:10 geamaro

I don't now multilayer maps are imported into Arc/QGIS, i.e. what kind of formats are used for that purpose. Maybe people from my GIS network can help? @FrieseWoudloper @Egge-Jan ?

mtennekes avatar Oct 16 '20 14:10 mtennekes

I see what you mean @mtennekes My goal is to be able to make the same map that I created and see, be seen by the GIS user (preferably with the same layers). Maybe I should export all the layers. But I really have no clue how to do this while preserving the information (the scales, the texts, the caption) and other components that I added. Perhaps some specific format, as if it were a "project" for GIS software. I'll do more research in the meantime ... :-)

geamaro avatar Oct 16 '20 19:10 geamaro

@geamaro I think you might want to look into the default project file format of QGIS projects, QGS/QGZ, which is an open file format: https://docs.qgis.org/3.16/en/docs/user_manual/appendices/qgis_file_formats.html

It would be quite a bit of work to map the components of a tmap object to the relevant elements of a QGS file, but it would be wonderful to have that!

However, I assume @mtennekes considers this to be a bit out of scope / two big to implement for tmap? I imagine it would involve many extra function dependencies.

A simpler output could be as a geoJSON, which could contain a feature collection, but that would be without all the extra styles and labels, etc.: https://tools.ietf.org/html/rfc7946

stragu avatar Dec 15 '20 02:12 stragu