Neighborhoods_sp <- as(neighborhoods_crs, Class = "Spatial") # Convert neighborhoods object to a Spatial object Neighborhoods_crs <- st_transform(neighborhoods, crs = st_crs(canopy, asText = TRUE)) # Transform the neighborhoods CRS to match the canopy layer # Confirm that you have the neighborhood density results In this exercise you will compute average tree canopy by neighborhood as a percentage so that we can compare if the results are similar. In the previous exercises you computed tree density by neighborhood using tree counts. # Compute average tree canopy by neighborhood Neighborhoods_counts <- mutate(neighborhoods_counts, Neighborhoods_counts <- left_join(neighborhoods_area, Neighborhoods_area <- mutate(neighborhoods, area = areas) # Add the areas to the neighborhoods object # Compute areas and unclass to remove units In this exercise you will compute neighborhood areas, add them to the neighborhood sf object and then you'll join in the non-spatial tree counts data frame from the previous exercise. We have the tree counts (from the previous exercise). # Compute tree density by neighborhood (II) Tree_counts_renamed <- rename(tree_counts_no_geom, tree_cnt = n) Tree_counts_no_geom <- st_set_geometry(tree_counts, NULL) # Compute the counts of all trees by hood The result should be a data frame with no geometry. You will produce counts of all trees by neighborhood in NYC and create a single data frame with a column for total trees. And you need the tree counts by neighborhood which is the focus of this exercise. You will need to know the area of the neighborhoods, which you will compute in the next exercise. In order to compute tree density by neighborhood you need two things. # Compute tree density by neighborhood (I) # Plot the result (low tree canopy and high impervious areas) # Create a new object of just the parks geometryĬanopy_imperv_overlay <- overlay(canopy, impervious, fun = f) # Plot just the acres attribute of the parks data # Plot the parks object using all defaults You can either create a new object or you can nest st_geometry() within the plot() function. For this, you can use the st_geometry() function to extract the geometry and plot the result. Instead, if you want to create a map of a single attribute you can extract that attribute using, as an example, plot(my_data ).įrequently you just want to plot the raw geometry with no attribute color-coding (e.g., adding county boundaries to a map of points). The plot() function, when applied to sf objects, will create a set of maps, one for each attribute in your data. The default, though, may not be what you want. You can, for example, type plot(my_data) to see your spatial object. The function for making a quick map/plot is a function you are already familiar with, plot(). # First look at plotting vector spatial objects # or you need to convert val's class to units, for example: # Instead you need to either remove the units with unclass(): # Create a standard, non-spatial data frame with one column In this exercise, you will convert the data frame to what's called a tibble with tibble::as_tibble() (Note that dplyr::tbl_df() is now deprecated). The list column allows you to store far more information in a single variable and sf takes advantage of this by storing all geographic information for each feature in the list. The main difference is that instead of a standard value such as a single number, character or boolean value, each observation value in that column is a piece of an R list and this list can be as complex as needed. This is possible thanks, in part, to the list-column.Ī list-column behaves, to a certain extent, like any other R column. # Use head() to look at the first few recordsĪ major innovation in sf is that spatial objects are data frames. Honeylocust_lim % select(tree_id, boroname) # Limit to tree_id and boroname variables To learn more you can check out the course Data Manipulation in R with dplyr. Will create a vector 1 to 10 and "pipe" it over to the mean() function, then it will pipe this result to the log() function (the result in this case is 1.704748). If you're not familiar with the pipe, just think of it as the joints between an R workflow (pipeline) taking the result from the left hand side and sending it on to the next function. Trees %) to save us some typing and simplify our code. If you've read in the files correctly, you will see a standard R data frame except it will show some header metadata about the file and you'll see a special geometry column which we will discuss later. In this exercise you will read in three shapefiles (one point file and two polygon files) using st_read(). The sf package, created by Edzer Pebesma and colleagues, has dramatically simplified reading vector spatial data into R. Title: "Spatial Analysis with sf and raster in R "
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |