Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating and reading KML data


In this recipe, we will learn how to read and write geographic data in Google's Keyhole Markup Language (KML) format, which can be used to visualize geographic data with Google Earth and Google Maps.

Getting ready

We will use the rgdal package in this recipe. So, let's make sure it's installed and load it:

install.packages("rgdal")
library(rgdal)

How to do it...

We will use data from the cities shapefile that's installed as part of the rgdal package. First, we will write a KML file and then read it:

cities <- readOGR(system.file("vectors", 
package = "rgdal")[1],"cities")

writeOGR(cities, "cities.kml", "cities", driver="KML")

df <- readOGR("cities.kml", "cities")

How it works...

In the preceding example, we first used the readOGR() function to read the cities shapefile dataset. The first argument is the folder (directory) where the data shapefile is and the second argument is the name of the shapefile (without the .shp extension). We stored the object returned...