Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# ==========================================================================
# This is a simple script for downloading, unzipping and reading a shapefile
# from www.naturalearthdata.com
# ==========================================================================
library(rgdal)
# ~~~~~~~~~~~ Download shapefile from www.naturalearthdata.com ~~~~~~~~~~~ #
# __ Example 1 - download countries data
download.file(url = "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_admin_0_countries.zip",
destfile = "ne_110m_admin_0_countries.zip")
# unzip the shapefile in the directory mentioned with "exdir" argument
unzip(zipfile="ne_110m_admin_0_countries.zip", exdir = "ne_110m_admin_0_countries")
# delete the zip file
file.remove("ne_110m_admin_0_countries.zip")
# read the shapefile with readOGR
NE_countries <- readOGR(dsn = "ne_110m_admin_0_countries", layer = "ne_110m_admin_0_countries")
# __ Example 2 - download main cities data
download.file(url = "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/cultural/ne_110m_populated_places.zip",
destfile = "ne_110m_populated_places.zip")
# unzip the shapefile in the directory mentioned with "exdir" argument
unzip(zipfile="ne_110m_populated_places.zip", exdir = "ne_110m_populated_places")
# delete the zip file
file.remove("ne_110m_populated_places.zip")
# read the shapefile with readOGR
NE_places <- readOGR(dsn = "ne_110m_populated_places", layer = "ne_110m_populated_places", stringsAsFactors=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment