knitr::include_graphics("images/network/Data Management_General.png")
For this chapter, you will need the following R Packages:
library(arc2r) library(sf) library(ggplot2) library(dplyr)
Sorting out features in ascending or descending order seems a quite primitive operation in any programming language or software package. Even though it is indeed primitive, it is also quite important for filtering and cleaning our datasets. In ArcGIS pro this operation is performed using the tool Sort
, which is part of the General toolset of the Data Managenent Toolbox. Below we present how we can perform the aforementioned operation using R. For our example we use the Simple Feature object bezirke, which depicts the districts within the country of Switzerland. Furthermore for performing the sorting operation, we use the record(column) that represents the area (in square km) of every of the districts.
# Read the dataset depicting the districts (Bezirke) in the country of Switzerland data("bezirke") # sort the dataset based on the Area in ascending order bezirke_asc <- bezirke[order(bezirke$area_km2),] head(bezirke_asc) # sort the dataset based on the Area in descending order bezirke_desc <- bezirke[order(-bezirke$area_km2),] head(bezirke_desc)
The beauty of R is that offers more than one option to perform a specific operation. In the example above, for performing the sorting operation, we used a simple subsetting method integrated within the so called base R. Nevertheless using the the function arrange()
of the dpyr package
we will be able to produce the exact same result.
# sort the dataset based on the Area in ascending order bezirke_arrange_asc <- arrange(bezirke,area_km2) # by default the function sorts in ascendind order head(bezirke_arrange_asc) # sort the dataset based on the Area in descending order bezirke_arrange_desc <- arrange(bezirke,-area_km2) head(bezirke_arrange_desc)
Rename
tool in ArcGIS pro serves as a very simple way of changing the name of a dataset. This applies to any of the available data types, such as feature dataset, raster, table, and shapefile. Let's see below how we can perform a similar operation in R. The easiest way to do it is by reassigning the dataset to a new variable. R is smart enough not to make a copy if the variable is exactly the same.
# Reading the dataset that depicts all the swimming spots in the canton of Zurich data("badeplaetze_zh") # Renaming the dataset above to "swimming_spots_zh" swimming_spots_zh <- badeplaetze_zh # Retrieving the address in memory for the two datasets tracemem(badeplaetze_zh) # --> <000001F24AB616E8> tracemem(swimming_spots_zh) # --> <000001F24AB616E8>
As we can see both objects point to the same address. R makes a new copy in the memory only if one of them is modified.
Merge
tool in ArcGIS pro is mainly used for combining datasets from different sources into a new, single output dataset. The main prerequisite for this operation is that the merging datasets have to be of the same geometry class. In R the aforementioned operation could be performed as follows:
# Using the dataset that depicts all the 26 Cantons of Switzerland data("kantonsgebiet") # Selecting the Canton of Zug zug <- filter(kantonsgebiet, NAME == "Zug") # depicting the Canton of Zug ggplot(zug) + geom_sf() # depicting the Canton of Zug # Selecting the Canton of Zürich zurich <- filter(kantonsgebiet, NAME == "Zürich") ggplot(zurich) + geom_sf() # depicting the Canton of Zurich # merging the two sf objects merged <- rbind(zug,zurich) ggplot(merged) + geom_sf() # depicting the product of the merge operation
Dissolve
in ArcGIS pro serves as a basic tool for aggregating features based on specified attributes. In R the respective operation could be easily performed using some basic functionalities of the sf
package. In the example below we use again the dataset that depicts all the 26 Cantons of Switzerland. Our aim is to transform the given dataset to one unified spatial polygon.
# The study area from the previous example head(kantonsgebiet) ggplot(kantonsgebiet) + geom_sf() # depicting all the 26 Cantons of Switzerland # Dissolving all the cantons into one unified area kantonsgebiet_dissolved <- st_union(kantonsgebiet) # use of the sf__st_union() function head(kantonsgebiet_dissolved) # Plot the dissolved output ggplot(kantonsgebiet_dissolved) + geom_sf()
In ArcGIS pro Find Identical
tool identifies records in a feature class or table that have identical values in a list of fields. As an outcome it produces a table listing those identical findings. In R we obtain a similar result for spatial features using the st_equals
function of the sf
package.
# create the duplicates addDupli <- kantonsgebiet[1:3,] # Combine it with the original dataset (kantonsgebiet) kantonDuplic <- rbind(kantonsgebiet, addDupli) # Examine if there are any identical values ident_results <- st_equals(kantonDuplic)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.