2

I have a shapefile/GML file with many thousands of polygons insides a specific layer.

I know the set of IDs that reference the set of polygons that I want to get the coordinates data from. Is there a way to get just this information easily, without having to load the whole shapefile layer into memory first?

I am currently using R with the rgdal package to load the shapefile/GML file, but am happy to look into other tools that might be able to help.

3
  • Can you just subset the layer first? For example go into QGIS and select just the ids that you want and export a new version? Commented May 11, 2017 at 16:53
  • 1
    I don't know if it's possible to read just one polygon from a Shapefile. However, I'd suggest you to have a look at the sf package, which at least will grant you a great improvement in reading speed.
    – lbusett
    Commented May 11, 2017 at 18:44
  • This can be done using OGRSQL on FID in recent versions of GDAL. It's long been possible, just not efficient until recently. You need to craft a VRT and R can read that. I'm also working on this kind of flexibility via Rcpp/GDAL here: github.com/hypertidy/vapour I'd be happy to target a very specific application if you are interested.
    – mdsumner
    Commented Aug 5, 2017 at 5:46

1 Answer 1

8

Time to ditch the shapefile! Here's a reproducible example using a combination of packages sf, gdalUtils and dplyr:

library(sf)
library(dplyr)
library(mapview)
library(gdalUtils)

## as an example we take nc shapefile from package sf
shpfile = system.file("shape/nc.shp", package = "sf")

## set working directory (where to save the converted file)
setwd("test_folder")

## covert to gpkg
target_file = gsub(".shp", ".gpkg", basename(shpfile))
ogr2ogr(src_datasource_name = shpfile, 
        dst_datasource_name = target_file, 
        f = "GPKG")

## use dplyr to connect to gpkg file
conn = target_file
db = src_sqlite(conn)

## read fid 10 from gpkg file
pol = tbl(db, "nc") %>% 
  filter(fid == 10) %>% 
  collect %>% 
  st_sf(crs = 4267)

## view with mapview
mapview(pol)

## check with original
mapview(read_sf(shpfile), col.regions = "grey") + pol

This is possible because gpkg is essentially a self-contained database. Therefore we can read using dplyr database functionality and hence filter by fid before collecting the data into memory. Afterwards we convert to a spatial object (sf in this case).

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.