- Hands-On Geospatial Analysis with R and QGIS
- Shammunul Islam
- 615字
- 2021-06-10 18:44:28
Plotting lines and polygons data in R
Lines data consists of lines and, in the sp package of R, it is stored as a SpatialLines class. If it contains attributes, it is saved as SpatialLinesDataFrames. Similarly for polygons, without attributes the class is defined as SpatialPolygons and with attributes the class is defined as SpatialPolygonsDataFrame.
We will load a shapefile consisting of lines attributes, which will be treated as a SpatialLinesDataFrames class in R. Let's load the shapefile of highways in Dhaka, Bangladesh:
# SpatialLines
highway = readOGR("F:/Hands-on Geospatial Analysis Using R and QGIS/Chapter02/Data","dhaka_gazipur")
plot(highway)
This gives us the following map:

Now, we will read polygons (a map of Dhaka saved as dhaka.shp) into R as SpatialPolygonsDataFrame, and will plot this:
map_dhaka = readOGR("F:/Hands-on-Geospatial-Analysis-Using-R-and-QGIS/Chapter02/Data","dhaka")
plot(map_dhaka)
This gives the following gray map:
Now, let's have a look at the structure of this SpatialPolygonsDataFrame to understand how it is being stored and how to access and manipulate it:
# Use max.level = 2 to show a reduced or succinct structure
str(map_dhaka, max.level = 2)
The structure of map_dhaka contains:
- @ data: This contains all the attribute information or it contains data.
- @ polygon: This stores information on polygons or coordinates.
- @ bbox: This contains information on the extent of the map or the coordinates of two corners of the bounding box.
These three parts of the structure are pointed out in the following screenshot:

In the preceding screenshot, we can see that it has five slots and each of these can be accessed using @. If we want to access data and see the first five rows of it, we can do so by doing the following:
# load another map
map_bd = readOGR("F:/Hands-on-Geospatial-Analysis-Using-R-and QGIS/Chapter02/Data","BGD_adm3_data_re")
head(map_bd@data)
We can see the first five rows of the attribute table now:

Now, let's examine @ polygons:
str(map_bd@polygons, max.level = 2)
What we find is another list of 66 where each list is again 66 polygons and each again has five slots just as map_bd has. The following is a snapshot of the first few lines of output. The remaining lines have not been shown here for the purposes of keeping the example succinct:

That means we can also access these lists' slots using @ and any of the five slots previously discussed. We now access the 6th element of map_bd and investigate its structure as follows:
# 6th element in the Polygons slot of map_bd
sixth_element = map_bd@polygons[[6]]
# make it succinct with max.level = 2 in str() for the 6th element of the bd@Polygons
str(sixth_element, max.level = 2)
We can see the structure of the 6th element of the polygon now:

Now, again check the structure of the 2nd polygon inside sixth_element. We can do so by writing the following:
# Structure of the 2nd polygon inside seventh_element
str(sixth_element@Polygons[[2]], max.level = 2)

Now, we can access these slots and. for demonstration purposes only, we will access coords and then will plot it:
# plot() the coords slot of the 2nd element of the Polygons slot.
plot(sixth_element@Polygons[[2]]@coords)
This gives the following graph:

To access data elements of a SpatialPolygonsdataFrame, we can use either $ or [[]] as we can do with a data frame. To access the column or attribute NAME_3, we can do the following:
map_bd$NAME_3
This will print all the values of the attribute NAME_3.
We can do the same using [[]] in the following way:
map_bd[["NAME_3"]]
- Hands-On Deep Learning with Apache Spark
- 亮劍.NET:.NET深入體驗與實戰精要
- 火格局的時空變異及其在電網防火中的應用
- 平面設計初步
- 基于LabWindows/CVI的虛擬儀器設計與應用
- 商戰數據挖掘:你需要了解的數據科學與分析思維
- PIC單片機C語言非常入門與視頻演練
- 數控銑削(加工中心)編程與加工
- MCSA Windows Server 2016 Certification Guide:Exam 70-741
- 分布式多媒體計算機系統
- SharePoint 2010開發最佳實踐
- WordPress Theme Development Beginner's Guide(Third Edition)
- Lightning Fast Animation in Element 3D
- 項目管理成功利器Project 2007全程解析
- Docker on Amazon Web Services