Skip to content

Instantly share code, notes, and snippets.

@valentinitnelav
Last active January 1, 2017 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valentinitnelav/a415f3fbfd90f72ea06b5411fb16df16 to your computer and use it in GitHub Desktop.
Save valentinitnelav/a415f3fbfd90f72ea06b5411fb16df16 to your computer and use it in GitHub Desktop.
# ==========================================================================
# 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