I am interested in learning about how to work with "Road Network" files - for example, I would like to find out the driving distance between two sets of geographical coordinates (i.e. longitude and latitude).
I found this shapefile for the Canadian Road Networks : https://www12.statcan.gc.ca/census-recensement/2011/geo/RNF-FRR/files-fichiers/lrnf000r22a_e.zip - now, I am trying to import this file into R.
Below, this is the code I am using to first download the shapefile to a temporary folder and then try to read the shapefile:
library(sf)
library(rgdal)
# Set the URL for the shapefile
url <- "https://www12.statcan.gc.ca/census-recensement/2011/geo/RNF-FRR/files-fichiers/lrnf000r22a_e.zip"
# Create a temporary folder to download and extract the shapefile
temp_dir <- tempdir()
temp_file <- file.path(temp_dir, "lrnf000r22a_e.zip")
# Download the shapefile to the temporary folder
download.file(url, temp_file)
# Extract the shapefile from the downloaded zip file
unzip(temp_file, exdir = temp_dir)
# Read the shapefile using the rgdal package
library(rgdal)
shapefile <- readOGR(dsn = temp_dir, layer = "lrnf000r22a_e")
But when trying to run the last line of code (readOGR), I get the following error:
OGR data source with driver: ESRI Shapefile
Source: "C:\Users\me\AppData\Local\Temp\RtmpwDKofs", layer: "lrnf000r22a_e"
with 2246324 features
It has 21 fields
Integer64 fields read as strings: OBJECTID
Error: memory exhausted (limit reached?)
In addition: Warning messages:
This seems to be a very large shapefile and my computer does not have enough memory to work with this file.
First, I tried to inspect the properties (e.g. number of columns) of this file:
> ogrInfo(dsn = temp_dir, layer = "lrnf000r22a_e")
Source: "C:\Users\me\AppData\Local\Temp\RtmpwXsVlD", layer: "lrnf000r22a_e"
Driver: ESRI Shapefile; number of rows: 2246324
Feature type: wkbLineString with 2 dimensions
Extent: (3696309 665490.8) - (9015653 4438073)
CRS: +proj=lcc +lat_0=63.390675 +lon_0=-91.8666666666667 +lat_1=49 +lat_2=77 +x_0=6200000 +y_0=3000000 +datum=NAD83 +units=m +no_defs
LDID: 87
Number of fields: 21
name type length typeName
1 OBJECTID 12 10 Integer64
2 NGD_UID 4 9 String
3 NAME 4 50 String
4 TYPE 4 6 String
5 DIR 4 2 String
6 AFL_VAL 4 9 String
7 ATL_VAL 4 9 String
8 AFR_VAL 4 9 String
9 ATR_VAL 4 9 String
10 CSDUID_L 4 7 String
11 CSDNAME_L 4 100 String
12 CSDTYPE_L 4 3 String
13 CSDUID_R 4 7 String
14 CSDNAME_R 4 100 String
15 CSDTYPE_R 4 3 String
16 PRUID_L 4 2 String
17 PRNAME_L 4 100 String
18 PRUID_R 4 2 String
19 PRNAME_R 4 100 String
20 RANK 4 4 String
21 CLASS 4 4 String
I then tried to see if if its possible to read this in "chunks" (e.g. https://gis.stackexchange.com/questions/324374/read-n-number-of-rows-from-shapefile-using-geopandas) - for example, perhaps I could just read the file in chunks of "1000" rows until the whole file is imported:
test <- st_read(dsn = temp_dir, layer = "lrnf000r22a_e", n_max = 100, layer_options = c("GEOMETRY=AS_WKT", "FEATURE_TYPE=wkbLineString"))
I tried this but got the following error:
Reading layer `lrnf000r22a_e' from data source `C:\Users\me\AppData\Local\Temp\RtmpwXsVlD' using driver `ESRI Shapefile'
Error in st_sf(x, ..., agr = agr, sf_column_name = sf_column_name) :
no simple features geometry column present
Has anyone ever encountered these kinds of problems before? Is there a way to fix this?
Thanks!
UPDATE:
# https://stackoverflow.com/questions/6457290/how-to-check-the-amount-of-ram
install.packages("memuse")
library(memuse)
memuse::Sys.meminfo()
Totalram: 7.648 GiB
Freeram: 678.750 MiB
# after re-starting my computer and opening R
memuse::Sys.meminfo()
Totalram: 7.648 GiB
Freeram: 1.467 GiB