Categories: Tools & Techniques

Geospatial Visualizations- Extracting Data with lat-long Information From Shape Files

In our previous post The Wonderful World of Geospatial Visualizations Explained in 2 Minutes, we learnt how to download google maps and overlay lat-long data. In this post, we will look at how to extract data with lat-long information from the shape files.

Very often while working on geospatial visualizations, we don’t have the lattitude longitude information. Most of the geospatial data (data with lat-long information) is in shape files and extracting the data from a shape file can be a little tricky. Let me now illustrate how we can do just this, i.e extract data out of shape files which are of the class “SpatialPointsDataFrame”.Some shape files are read as a class “SpatialPolygonDataFrame”, in order to extract data out of such files one can refer to

Below are the steps to extract data out of a file of class “SpatialPointsDataFrame”

(1) Read the shape file using rgdal() pacakage

(2) Check if the location data is in lat long format

(i) If not, convert into lat long format

(3) Combine the data and lat long information into one dataframe, and extract a .csv file

Below is the code for extracting data from a shape file.




sh<-readOGR(dsn=”.”,”DOITT_MINY_VENDOR_01_13SEPT2010″)#Reading shape file using rgdal() library’s readOGR command

## OGR data source with driver: ESRI Shapefile
## Source: “.”, layer: “DOITT_MINY_VENDOR_01_13SEPT2010”
## with 897 features
## It has 18 fields

head(sh@coords)#Notice that the coordinates don’t seem to be in lat-long format

##      coords.x1 coords.x2
## [1,]    988225    210485
## [2,]    985113    210450
## [3,]    989037    220311
## [4,]    996773    223379
## [5,]    991844    214237
## [6,]    989257    214660


## [1] “SpatialPointsDataFrame”
## attr(,”package”)
## [1] “sp”

sh<-spTransform(sh,CRS(“+init=epsg:4326″))#Using spTrandform function to convert the location data to lat-long format, +init=epsg:4326 is the coordinate refrence code for lat-long data
vendor<-data.frame(sh@data,sh@coords)#Combining data with location information

## Source: local data frame [6 x 20]
##    ID CATEGORY                     VENDORNAME
## 1 421   Hotels     The Carlton Madison Avenue
## 2 422   Hotels              The Chelsea Hotel
## 3 423   Hotels               The Empire Hotel
## 4 424   Hotels             The Franklin Hotel
## 5 425   Hotels The Helmsley Middletowne Hotel
## 6 426   Hotels          The Iroquois New York
## Variables not shown: WEBSITE (fctr), ADDRESS (fctr), ADDRESS2 (fctr), CITY
##   (fctr), STATE (fctr), ZIP (dbl), TELEPHONE (fctr), CONTACT (fctr),
##   CONTACTEMA (fctr), TYPED (fctr), CAMERAICON (fctr), BOROUGH (fctr),
##   BCODE (dbl), HOUSENUM (fctr), STREETNAME (fctr), coords.x1 (dbl),
##   coords.x2 (dbl)

write.csv(vendor,”vendor.csv”)”)#Extracting the dataset with lat-long data as a .csv file.


For creating interesting geospatial visualizations in R, I think the following workflow is most suitable:

  1. If one already has a dataset with lat-long information, then simply overlay that data on a google map using ggmap() and ggplot2()

2.If someone wants to get data with lat-long information, then the best way is to read shapefiles into R, combine the coordinates information with the data into a dataframe and then use ggmap() and ggplot2() to make visualizations.

Why don’t you try this out? And then write in and tell me all about it. Such tasks will help build your analytics skill set and confidence. At interviews you will be able to talk about it and woo your recruiters. Go on then get started!

To find out more about careers in analytics, take a look at the various analytics courses Jigsaw has to offer.

Image courtesy By vectorolie

Related Reads:

The Wonderful World of Geospatial Visualizations Explained in 2 Minutes

Weave Magic With Interactive Visualizations: Creating Polychart Using rCharts


Published by

Recent Posts

Books on Analytics

Analytics is a vast field. At the one end, it overlaps with statistics and higher…

Career in analytics in a KPO

Do you love to explore and investigate information? Do you find spreadsheets to be a…

Indian companies using analytics

India has developed into the global hub for analytics. A large number of MNCs have…

IBM: Betting big on analytics

International Business Machines Corp. Or IBM as it is popularly known recently announced its restructuring…

How to build a successful career in analytics

So you have got a job as an analyst in your dream company? Here are…

What’s the sentiment on “sentiment analysis”?

What's the sentiment on "sentiment analysis"? Is the field ready to take off?