I have a lot of GML files to loop through in R, but none of them are extremely heavy (max file size is 600 Mb, but normally they range between 10-60 Mb). My problem is that readOGR function from the {rgdal} package takes too long to read them, and it slows down the loop a lot. It takes between 3 minutes up to 30 minutes to read a single file...
I have looped through .shp files and they do not take up so much time to read.
So my question is actually two:
Code to my readOGR:
llayer<-ogrListLayers(gmls)[1]
a<- readOGR(dsn=gmls, layer=llayer, encoding = "UTF-8", disambiguateFIDs = T)
As @LoBu said in the comment section, sf::st_read()
is much, much faster than readOGR. The only matter to keep in mind is that now you work with sf
objects and you must either perform your operations using the sf
package or convert the sf object to a SpatialPolygonsDataframe with as(file, "Spatial")
.
Warning, for people using work laptops: errors may occur as I mention in the comment section. Make sure you are using a laptop with full admin permits