官术网_书友最值得收藏!

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"]]

主站蜘蛛池模板: 农安县| 秦皇岛市| 全椒县| 辛集市| 田东县| 新乐市| 华安县| 石门县| 玉林市| 靖边县| 图木舒克市| 宝清县| 秦安县| 高清| 迁安市| 阳泉市| 隆德县| 高雄县| 渭南市| 独山县| 孟州市| 泰顺县| 芜湖市| 临江市| 秦皇岛市| 葫芦岛市| 兰溪市| 开原市| 西宁市| 浦北县| 营口市| 黄梅县| 兰西县| 定安县| 武强县| 会同县| 尼玛县| 临江市| 广州市| 宽城| 东台市|