我有兴趣了解如何使用“道路网络”文件 - 例如,我想找出两组地理坐标(即经度和纬度)之间的行驶距离。
我为加拿大道路网络找到了这个形状文件:https://www12.statcan.gc.ca/census-recensement/2011/geo/RNF-FRR/files-fichiers/lrnf000r22a_e.zip - 现在,我正在尝试将此文件导入 R.
下面,这是我用来首先将 shapefile 下载到临时文件夹然后尝试读取 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")
但是当尝试运行最后一行代码(readOGR)时,出现以下错误:
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:
这似乎是一个非常大的 shapefile,我的电脑没有足够的内存来处理这个文件。
首先,我尝试检查此文件的属性(例如列数):
> 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
然后我试着看看是否有可能以“块”的形式阅读它(例如https://gis.stackexchange.com/questions/324374/read-n-number-of-rows-from-shapefile-using- geopandas) - 例如,也许我可以按“1000”行的块读取文件,直到导入整个文件:
test <- st_read(dsn = temp_dir, layer = "lrnf000r22a_e", n_max = 100, layer_options = c("GEOMETRY=AS_WKT", "FEATURE_TYPE=wkbLineString"))
我试过了,但出现以下错误:
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
有没有人遇到过这类问题?有办法解决这个问题吗?
谢谢!