class: center, middle, inverse, title-slide # Working with Geospatial Data ## GIS and Shapefiles ### Ben Baumer ### SDS 192April 10th, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_geo_05-shapefiles.html
) --- class: center, middle, inverse # GIS --- ## Geographic Information System (GIS) .centered[![](http://www.in.gov/gis/images/Capture9.PNG)] --- ## [Smith Spatial Analysis Lab](http://www.science.smith.edu/sal/) .footnote[http://www.science.smith.edu/sal/] .centered[![](http://www.science.smith.edu/sal/wp-content/uploads/sites/13/2015/03/Banner980x190_1.png)] - Jon Caris, Director - Tracy Tien, Spatial Data Specialist - Smith-specific data --- ## ArcGIS: Proprietary GUI .footnote[https://www.arcgis.com/] .centered[![](https://www.arc2earth.com/wp-content/uploads/2014/05/basemaps_sandiego_airports2.png)] --- ## QGIS: Open-source GUI .footnote[https://www.qgis.org/] .centered[![](https://www.qgis.org/en/_static/images/about-screenshot.png)] --- ## Spatial packages in R - Working with shapefiles - ~~`sp` for classes and basic functionality~~ - ~~`rgdal` for projections and advanced reading/writing~~ - ~~`rgeos` for spatial computations~~ - `sf` for everything - Plotting - ~~`ggmap` for static maps~~ - `ggplot2` for static maps - `leaflet` for interactive maps --- class: center, middle, inverse # Shapefiles --- ## Shapefiles aren't really files ```r library(sf) biketrails_dir <- here::here("biketrails/biketrails_arc/") biketrails_dir ``` ``` ## [1] "/home/bbaumer/Dropbox/git/sds192/biketrails/biketrails_arc/" ``` -- ```r list.files(biketrails_dir) ``` ``` ## [1] "Bicycle_Trails.lyr" "biketrails_arc.cpg" "biketrails_arc.dbf" ## [4] "biketrails_arc.prj" "biketrails_arc.sbn" "biketrails_arc.sbx" ## [7] "biketrails_arc.shp" "biketrails_arc.shp.xml" "biketrails_arc.shx" ``` -- - Shapefiles have extension `.shp` -- - all these files store different information -- - you need *all* of them! --- ## `sf` `data.frame`s ```r biketrails <- st_read(biketrails_dir) ``` ``` ## Reading layer `biketrails_arc' from data source `/home/bbaumer/Dropbox/git/sds192/biketrails/biketrails_arc' using driver `ESRI Shapefile' ## Simple feature collection with 272 features and 13 fields ## geometry type: MULTILINESTRING ## dimension: XY ## bbox: xmin: 42526.52 ymin: 794291 xmax: 328921.5 ymax: 957916.8 ## proj4string: +proj=lcc +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000 +datum=NAD83 +units=m +no_defs ``` -- ```r biketrails ``` ``` ## Simple feature collection with 272 features and 13 fields ## geometry type: MULTILINESTRING ## dimension: XY ## bbox: xmin: 42526.52 ymin: 794291 xmax: 328921.5 ymax: 957916.8 ## proj4string: +proj=lcc +lat_1=41.71666666666667 +lat_2=42.68333333333333 +lat_0=41 +lon_0=-71.5 +x_0=200000 +y_0=750000 +datum=NAD83 +units=m +no_defs ## First 10 features: ## TRAIL_STAT OWNER PREV_OWNER MANAGER STATUS_OWN STATUS_MAN ## 1 A <NA> <NA> <NA> <NA> <NA> ## 2 A <NA> <NA> <NA> <NA> <NA> ## 3 A MBTA B&M STONEHAM S M ## 4 A EOTC Penn Central <NA> S <NA> ## 5 A EOTC Penn Central <NA> S <NA> ## 6 U Chatham/Pvt. New Haven RR <NA> <NA> <NA> ## 7 E Cambridge <NA> Cambridge M M ## 8 E Boston Conrail Boston M M ## 9 E Bedford Billerica & Bedford Bedford M M ## 10 C CSX New York Central <NA> <NA> <NA> ## TRAILNAME LINENAME ## 1 <NA> <NA> ## 2 <NA> <NA> ## 3 <NA> STONEHAM BRANCH ## 4 <NA> RANDOLPH SECONDARY ## 5 <NA> E BRIDGEWATER SECONDARY ## 6 Cape Cod Rail Trail/Chatham Cape Cod/Chatham Branch ## 7 Community Path over Gilmore Bridge <NA> ## 8 East Boston Greenway <NA> ## 9 Narrow Gauge Rail Trail BILLERICA & BEDFORD ## 10 Cochituate Rail Trail Saxonville Branch ## CONTACT1 ## 1 <NA> ## 2 <NA> ## 3 <NA> ## 4 <NA> ## 5 <NA> ## 6 http://www.cctrails.org ## 7 http://www.pathfriends.org ## 8 http://www.tpl.org/ ## 9 http://www.bedforddepot.org/ ## 10 http://www.millermicro.com/crt.html ## CONTACT2 OPENED COMPLETED SHAPE_LEN ## 1 <NA> 0 0 2682.1488 ## 2 <NA> 0 0 3185.2858 ## 3 <NA> 0 0 1023.9954 ## 4 <NA> 0 0 3539.2376 ## 5 <NA> 0 0 3179.0013 ## 6 http://www.state.ma.us/dem/parks/ccrt.htm 0 0 6630.1329 ## 7 http://www.ci.cambridge.ma.us/ 1970 1970 558.3535 ## 8 <NA> 2001 0 1139.0108 ## 9 http://www.town.bedford.ma.us/ 0 0 4906.9525 ## 10 http://www.framinghamma.org/ 0 0 1535.4054 ## geometry ## 1 MULTILINESTRING ((155011.7 ... ## 2 MULTILINESTRING ((152703.3 ... ## 3 MULTILINESTRING ((236337.8 ... ## 4 MULTILINESTRING ((240197.2 ... ## 5 MULTILINESTRING ((244758.8 ... ## 6 MULTILINESTRING ((322333.3 ... ## 7 MULTILINESTRING ((235460.3 ... ## 8 MULTILINESTRING ((238231.9 ... ## 9 MULTILINESTRING ((218391.3 ... ## 10 MULTILINESTRING ((210189.7 ... ``` --- ## Where to find shapefiles? - Include `gis` or `shapefile` or `.shp` in your Google search -- - Lots of government websites: - [MassGIS](https://docs.digital.mass.gov/dataset/massgis-data-layers) - https://www.data.gov/geospatial/ -- - Other R packages: - [tigris](https://github.com/walkerke/tigris) - [tidycensus](https://github.com/walkerke/tidycensus)