forked from Chicago/osd-building-footprints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Importing GeoJSON R Demo.R
32 lines (22 loc) · 1.52 KB
/
Importing GeoJSON R Demo.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# TITLE: Importing GeoJSON Example in R
# AUTHOR: Tom Schenk Jr., City of Chicago
# CREATED: 2013-01-23
# UPDATED: 2013-01-31
# NOTES: Caution! The street centerline data is quite large and may take a long time to complete.
# LIBRARIES: rgdal, ggplot2
# Set working directory (e.g., "C:\\Users\\username\\downloads" or "~/downloads")
setwd("path\\to\\osd-building-footprints")
# Install and load libraries
## If you need to install the RGDAL and GGPLOT2 libraries, complete this step first, otherwise, skip:
install.packages(c("rgdal", "ggplot2"))
library(rgdal) # Import data into a Spatial Data Frame in R
library(ggplot2) # Transform data from Shapefile to Data Frame
# Import data to Spatial Dataframe
ogrInfo("data\\Buildings.json", layer="OGRGeoJSON") # Checks projection type for readOGR(), number of rows (55747) and fields (44). Replace PATH\\TO with actual file path (e.g., C:\\Users\\username\\downloads)
buildings.shapefile <- readOGR(dsn="data\\Buildings.json", layer="OGRGeoJSON", p4s="+proj=tmerc +ellps=WGS84") # Imports data. Replace PATH\\TO with actual file path (e.g., C:\\Users\\username\\downloads)
head(buildings.shapefile) # Inspect the data structure.
plot(buildings.shapefile) # Test plot of spatial data frame.
# Fortify data to translate to Data Frame
buildings.df <- fortify(buildings.shapefile) # Caution, this is very memory intensive and may take several hours to complete
head(buildings.df) # Inspect the data structure
ggplot(buildings.df, aes(x=long, y=lat, group=group)) + geom_path() # Test plot of data frame.