![]() |
In spatial data analysis it is often necessary to combine spatial geometries with the attribute data. In R the SpatialPolygonDataFrame class from the sp package is used to represent spatial polygons with the associated data. Sometimes, we may have simple data. frame with the attributes that we want to attach to the SpatialPolygonDataFrame. Here, we will discuss the process of attaching data. frame to the SpatialPolygonDataFrame. What is a Spatial Polygon DataFrame?A Spatial Polygon DataFrame is a specific data structure in R, used to represent and manage spatial data. It is particularly useful in geographic information systems (GIS) and spatial analysis. A Spatial Polygon DataFrame combines the spatial data of polygon shapes with associated attribute data, allowing for comprehensive spatial analysis and visualization. Before starting to make sure we have the necessary packages installed, we’ll need the sp package for handling spatial objects and rgdal for spatial data manipulation.
Now we will discuss step by step of How to attach a simple data.frame to a Spatial Polygon DataFrame in R Programming Language. Step 1: Load the Required LibrariesFirst we will Load the Required Libraries.
Step 2: Create a Spatial Polygons ObjectAssume we have a set of polygons and want to create a
Step 3: Create a data.frame with Attribute DataCreate a data.frame with the attributes that we want to attach to the polygons and also the IDs match those of the SpatialPolygons object.
Step 4: Combine the SpatialPolygons Object with the Data FrameThe Combine the SpatialPolygons object with the data.frame to the create a SpatialPolygonsDataFrame.
Step 5: Verify the ResultsCheck the resulting SpatialPolygonsDataFrame to the ensure that the attributes have been correctly attached.
Output: An object of class "SpatialPolygonsDataFrame"
Slot "data":
ID Name Area
1 1 Polygon1 1
2 2 Polygon2 1
Slot "polygons":
[[1]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 0.5 0.5
Slot "area":
[1] 1
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] 0 0
[2,] 0 1
[3,] 1 1
[4,] 1 0
[5,] 0 0
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] 0.5 0.5
Slot "ID":
[1] "1"
Slot "area":
[1] 1
[[2]]
An object of class "Polygons"
Slot "Polygons":
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 2.5 2.5
Slot "area":
[1] 1
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
[,1] [,2]
[1,] 2 2
[2,] 2 3
[3,] 3 3
[4,] 3 2
[5,] 2 2
Slot "plotOrder":
[1] 1
Slot "labpt":
[1] 2.5 2.5
Slot "ID":
[1] "2"
Slot "area":
[1] 1
Slot "plotOrder":
[1] 2 1
Slot "bbox":
min max
x 0 3
y 0 3
Slot "proj4string":
Coordinate Reference System:
Deprecated Proj.4 representation: NA
ConclusionAttaching a |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 25 |