讀取 shapefile
Shapefile 是分享空間資料最常見的格式之一,可以用 rgdal 套件的 readOGR() 輕鬆讀入 R。readOGR() 有兩個重要引數:dsn 和 layer。實際要傳入什麼值,取決於你要讀入的資料類型。你在影片中學到,對於 shapefile,dsn 應該是包含該 shapefile 所有檔案的目錄路徑,而 layer 則是特定 shapefile 的檔名(不含副檔名)。
在這張地圖中,你需要社區邊界。我們已從紐約市規劃局的[Open Data Platform](開放資料平台)下載了由紐約市定義的 Neighborhood Tabulation Areas(社區統計區),連結為:https://www1.nyc.gov/site/planning/data-maps/open-data/dwn-nynta.page。下載檔是 zip 壓縮檔,我們已將解壓後的內容放在你的工作目錄中。
你將先使用 base R 的 dir() 函式檢視工作目錄的內容,接著把該 shapefile 讀入 R。
本練習屬於課程
在 R 視覺化地理空間資料
練習說明
- 使用不帶引數的
dir()來找出 shapefile 所在的目錄名稱。 - 對
dir()傳入該 shapefile 目錄的路徑,查看其中的檔案。 - 現在你已知道目錄與檔名。使用
readOGR()將該社區 shapefile 讀入為名為neighborhoods的物件。 - 呼叫
summary()檢查neighborhoods的內容。 - 繪圖檢查
neighborhoods的內容。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
library(sp)
library(rgdal)
# Use dir() to find directory name
# Call dir() with directory name
# Read in shapefile with readOGR(): neighborhoods
# summary() of neighborhoods
# Plot neighborhoods