2

QGIS has a "Move Features" tool where points can be moved. Is it possible to move features from within R?

If not, is it possible to open R spatial objects in QGIS, move points in QGIS then import the spatial object back in R?

2
  • Do you mean to move features within R using a point-and-click interface?
    – Simbamangu
    Commented Jul 9, 2019 at 5:13
  • Yes move features within R using a point-and-click interface
    – luciano
    Commented Jul 10, 2019 at 16:03

2 Answers 2

4

Its a bit obscure, but the elide function in the maptools package can be used to move sp class objects.

This works for all point, line, and polygon classes. Following the data setup in help(elide) here's a river polygon shifted 0.10 units up and across:

> plot(rivers1)
> plot(elide(rivers1, shift=c(.10,.10)),add=TRUE)
> axis(1)

For sf objects adding a point to a geometry acts as a vector addition:

> nc = st_read(system.file("shape/nc.shp", package="sf"))
Reading layer `nc' from data source `/nobackup/rowlings/RLibrary/R/x86_64-pc-linux-gnu-library/3.4/sf/shape/nc.shp' using driver `ESRI Shapefile'
Simple feature collection with 100 features and 14 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs

Let's move the first county +1 degree up and across:

> nc$geom[[1]] = nc$geom[[1]] + st_point(c(1,1))
> plot(nc$geom)

enter image description here

1
  • Hi Barry. I find this succeeds in moving the polygons, but when when I move all polygons in nc with the same offset (e.g. + st_point(c(1,0))) the output of st_bbox(nc) does not change, even though the geoms do plot in the new positions. Any idea why?
    – geotheory
    Commented Sep 24, 2020 at 14:21
0

There may be a way of building an interface in R to click and drag geometries to a new location, but probably it's just easier to load the layer in QGIS and modify it there before reloading in R. It will take only a few seconds to switch between apps and reload layers.

library(sf)
db <- "~/workspace/_temp/qgis2R.gpkg"

# Create a point layer

pts <- data.frame(name = "blue", x = 36.5, y = -2.5)
pts <- st_as_sf(pts, coords = c('x', 'y'), crs = 4326)
write_sf(pts, dsn = db, layer = "pts", driver = 'GPKG')

Open QGIS and add that point layer - plus (in this example) the "Stamen toner" layer from QuickMapServices

Edit the layer - move the point somewhere (or add new ones) then save the layer edits

Reload the layer in R - note the new XY data.

pts <- read_sf(db, layer = "pts")
pts

Simple feature collection with 1 feature and 1 field
geometry type:  POINT
dimension:      XY
bbox:           xmin: 36.8635 ymin: -2.832009 xmax: 36.8635 ymax: -2.832009
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
# A tibble: 1 x 2
  name                 geom
  <chr>             <POINT>
1 blue  (36.8635 -2.832009)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.