Raster to polygon r sf

This is the first practical chapter of the book, and therefore it comes with some software requirements. We assume that you have an up-to-date version of R installed and that you are comfortable using software with a command-line interface such as the integrated development environment IDE RStudio. If you are new to R, we recommend reading Chapter 2 of the online book Efficient R Programming by Gillespie and Lovelace and learning the basics of the language with reference to resources such as Grolemund and Wickham or DataCamp before proceeding.

Organize your work e. R to document the code you write as you learn. The packages used in this chapter can be installed with the following commands: 5. This chapter will provide brief explanations of the fundamental geographic data models: vector and raster. We will introduce the theory behind each data model and the disciplines in which they predominate, before demonstrating their implementation in R. The vector data model represents the world using points, lines and polygons.

These have discrete, well-defined borders, meaning that vector datasets usually have a high level of precision but not necessarily accuracy as we will see in Section 2. The raster data model divides the surface up into cells of constant size. Raster datasets are the basis of background images used in web-mapping and have been a vital source of geographic data since the origins of aerial photography and satellite-based remote sensing devices.

Rasters aggregate spatially specific features to a given resolution, meaning that they are consistent over space and scalable many worldwide raster datasets are available. There is much overlap in some fields and raster and vector datasets can be used together: ecologists and demographers, for example, commonly use both vector and raster data.

Furthermore, it is possible to convert between the two forms see Section 5. Whether your work involves more use of vector or raster datasets, it is worth understanding the underlying data model before using them, as discussed in subsequent chapters.

This book uses sf and raster packages to work with vector data and raster datasets, respectively. The geographic vector data model is based on points located within a coordinate reference system CRS. Points can represent self-standing features e. In this system London, for example, can be represented by the coordinates c This means that its location is Simple features is an open standard developed and endorsed by the Open Geospatial Consortium OGCa not-for-profit organization whose activities we will revisit in a later chapter in Section 7.

Simple Features is a hierarchical data model that represents a wide range of geometry types. Of 17 geometry types supported by the specification, only 7 are used in the vast majority of geographic research see Figure 2. Pebesma The package is well documented, as can be seen on its website and in 6 vignettes, which can be loaded as follows:. We will use the world dataset provided by the spDataloaded at the beginning of this chapter see nowosad.

The sf package provides a plot method for visualizing geographic data: the following command creates Figure 2. Note that instead of creating a single map, as most GIS programs would, the plot command has created multiple maps, one for each variable in the world datasets. This behavior can be useful for exploring the spatial distribution of different variables and is discussed further in Section 2.In the preceding examples we have used the base plot command to take a quick look at our spatial objects.

In this section we will explore several alternatives to map spatial data with R. Mapping packages are in the process of keeping up with the development of the new sf package, so they typicall accept both sp and sf objects. However, there are a few exceptions. Of the packages shown here spplotwhich is part of the good old sp package, only takes sp objects. The development version of ggplot2 can take sf objects, though ggmap seems to still have issues with sf.

Both tmap and leaflet can also handle both sp and sf objects. As we have already briefly seen, the sf package extends the base plot command, so it can be used on sf objects. If used without any arguments it will plot all the attributes. This can be done by using the breaks argument for the sf plot function.

We can change the color palette using a library called RColorBrewer For more about ColorBrewer palettes read this. To make the color palettes from ColorBrewer available as R palettes we use the brewer.

It takes two arguments: - the number of different colors desired and - the name of the palette as character string. Like plot, by default spplot maps all everything it can find in the attribute table.

Sometimes this does not work, depending on the data types in the attribute table. In order to select specific values to map we can provide the spplot function with the name or names of the attribute variable s we want to plot.

Many improvements can be made here as well, below is an example It is not specifically geared towards mapping, but one can generate great maps. You can start with a layer showing the raw data then add layers of annotations and statistical summaries.

This allows to easily superimpose either different visualizations of one dataset e. For an introduction to ggplot check out this book by the package creator or this for more pointers.

In order to build a plot you start with initializing a ggplot object. In order to do that ggplot takes:. In addition, minimally a geometry to be used to determine how the values should be displayed. So all we have to do is:. Homicide rate is a continuous variable and is plotted by ggplot as such. We need to tell it the location or the boundaries of the map, the zoom level, and what kind of map service we like default is Google terrain.

It will actually download the tile. In order to view the map we then use ggmap. Then we can reuse the code from the ggplot example above, just replacing the first line, where we initialized a ggplot object above. We also have to set inherit. The ggmap package also includes functions for distance calculations, geocoding, and calculating routes. It borrows from teh ggplot syntax and takes care of a lot of the styling and aesthetics. This reduces our amount of code significantly. We only need:.

The tmap library also includes functions for simple spatial operations, geocoding and reverse geocoding using OSM.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

So I could just do raster::crop raster, polygon. To get this working with an sf object, I can call raster::crop raster, as. I assume this problem generalized to all raster functions? Did I overlook something or is it just the case that the raster package does not yet work with sf objects? For future reference, it works now! Here's some slightly modified example code from? The last release of "raster" package was in June So, for now, you need to use your "ugly workarounds" Learn more.

R: Handling of sf objects in raster package Ask Question. Asked 3 years ago. Active 1 year, 11 months ago. Viewed 4k times. I am just "upgrading" my scripts to use sf for reading and manipulating polygons. You don't want as. Active Oldest Votes. I'm still getting this error with sf 0. Did this get un-fixed? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.

Email Required, but never shown. The Overflow Blog. Socializing with co-workers while social distancing. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Dark Mode Beta - help us root out low-contrast and un-converted bits. Technical site integration observational experiment live on Stack Overflow.

Triage needs to be fixed urgently, and users need to be notified upon…. Visit chat. Related 3.The previous three chapters have demonstrated how geographic datasets are structured in R Chapter 2 and how to manipulate them based on their non-geographic attributes Chapter 3 and spatial properties Chapter 4. This chapter extends these skills.

After reading it — and attempting the exercises at the end — you should understand and have control over the geometry column in sf objects and the geographic location of pixels represented in rasters. Section 5. Unary operations work on a single geometry in isolation.

Binary transformations modify one geometry based on the shape of another. This includes clipping and geometry unions, covered in Sections 5. Type transformations from a polygon to a line, for example are demonstrated in Section 5. This involves changing the size and number of the underlying pixels, and assigning them new values. It teaches how to change the resolution also called raster aggregation and disaggregationthe extent and the origin of a raster. These operations are especially useful if one would like to align raster datasets from diverse sources.

Aligned raster objects share a one-to-one correspondence between pixels, allowing them to be processed using map algebra operations, described in Section 4. The final Section 5. This section is about operations that in some way change the geometry of vector sf objects. It is more advanced than the spatial data operations presented in the previous chapter in Section 4.

Simplification is a process for generalization of vector objects lines and polygons usually for use in smaller scale maps. Another reason for simplifying objects is to reduce the amount of memory, disk space and network bandwidth they consume: it may be wise to simplify complex geometries before publishing them as interactive maps. Figure 5. The simplified geometry was created by the following command:. This is apparent, with the result being visually simpler Figure 5.

Simplification is also applicable for polygons. By default it uses the Visvalingam algorithm, which overcomes some limitations of the Douglas-Peucker algorithm Visvalingam and Whyatt Centroid operations identify the center of geographic objects. All of them create single point representations of more complex vector objects. The most commonly used centroid operation is the geographic centroid. Geographic centroids have many uses, for example to create a simple point representation of complex geometries, or to estimate distances between polygons.

Sometimes the geographic centroid falls outside the boundaries of their parent objects think of a doughnut. In such cases point on surface operations can be used to guarantee the point will be in the parent object e. Notice that these red points always lie on their parent objects. Other types of centroids exist, including the Chebyshev center and the visual center. Buffers are polygons representing the area within a given distance of a geometric feature: regardless of whether the input is a point, line or polygon, the output is a polygon.

Unlike simplification which is often used for visualization and reducing file size buffering tends to be used for geographic data analysis. How many points are within a given distance of this line? Which demographic groups are within travel distance of this new shop?

These kinds of questions can be answered and visualized by creating buffers around the geographic entities of interest. Note the colors, which reflect the fact that one buffer is created per geometry feature.Today I needed to convert a raster to a polygon shapefile for further processing and plotting in R.

The fantastic raster package has a function that can do this rasterToPolygonsbut it seems to take a very, very long time and chew up all system resources at least on my not particularly souped up X laptop. To check if this is the case, run the following in R:.

raster to polygon r sf

In the latter case, you can either use the pypath argument accepted by the function below to specify the path, or modify your PATH variable. Thanks mate! Please feel free to leave me a comment if you have any problems or suggestions. The raster object resulting from using rasterToPolygons. Note that the following takes an argument outshapewhich specifies the path where the converted polygon shapefile will reside. Default is NULLwhich saves the shapefile to a temporary file.

Just over 20 seconds if we begin with a raster object, and only 3 seconds if we already have the raster saved as a file on disk. This is just one place where it is not particularly efficient.

I tried to alter the call to system in your function — but without success. In case this is still of interest, Francisco has modified the function to appease Windows users.

See his comment here.

raster to polygon r sf

Thank you for posting this — very helpful! Nice work, Francisco! Best wishes and happy coding. For future reference, you should just be able to enter the desired path, with or without the. For example: polygonizer 'myfile. Is there a possibility to include the dissolution into your function?

My guess is that your data are continuous, and few adjacent cells have the same value. You might consider, e. That said, you can dissolve SpatialPolygons objects with either aggregate from the sp package, or gUnaryUnion from the rgeos package. However, this will probably create a single rectangular polygon equal to the extent of your raster because polygons are continuous across the extent of the raster, and adjoining polygons are merged by these functionswhich is almost certainly not what you want.

I also had the same problem. The function transforms these pixels into independent polygons, with the same identifier called "DN".

I solved this problem by applying the unionSpatialPolygons function of the Maptools package after vectoring. My example command:.

Thank you for the code. We are wrangling with a lot of raster files that need to be converted into polygons. I am getting this error with our raster files:. Error in. Such data cannot be stored in arc-ascii format. Thanks for pointing this out. Now it allows unequal vertical and horizontal res. Is there a way to do this without dissolving based on the raster values?

A simple way to do this is to assign unique values to the raster cells, then polygonize, and finally add a new attribute to the polygons to restore the original cell values. Note that above I use polygonizerwhich is the version of the function that I maintain as a Gist here.

Thanks a lot for the code.For each cell in our sampling grid, we want to know which single class of the predictor variable covers the largest area of the cell. For example a 50m x 50m sample cell may encompass forest, grassland and shrubland. Which vegetation type is most common, and covers the largest area? We will attribute the cell with this vegetation type.

5. Plotting Simple Features

Now we read in our 50m x 50m grid spatial data — this code will download the file from my server as a Geopackage. The grid has one column — Id — a simple sequential number. This is necessary for summarizing the data later. We will now load in our vegetation file. This dataset has the same spatial domain as the grid, and contains two vegetation types; eucalypt forest, and agricultural land. If we plot this new object, we can see both variables plotted over these combined geometries.

Now the summary calculation. The grid now has the majority vegetation type has a column. First we load in the two packages we need — sf and of course tidyverse.

Leave a Reply Cancel reply. Next Next post: Using the ggeffects library to plot marginal effects from model output.This package offers support for both sp and sf spatial objects. The following smoothing methods are available:. In addition to these smoothing functions, smoothr offers functions for filling polygon holes and dropping line and polygon fragments based on a size threshold, as well as densification i. This package comes with two simple spatial datasets in sf format to test the smoothing algorithms on.

Notice that these polygons have a range of complexities, some have holes, and some are mutlipart polygons. It is a 25x25 grid of square kilometer cells in a North American centered Albers Equal Area projection.

This raster can be used to experiment with smoothing polygons generated from rasters. All are accessed with the smooth function, and all methods work on spatial lines and polygons in sf and sp format.

This method applies a moderate amount of smoothing of sharp corners without extensive generalization, and is a good choice when the desire is to smooth without drastically altering the input features.

Getting rasters into shape from R

In addition, this algorithm has the benefit of only requiring a single, easily interpretable parameter: the number of smoothing iterations. This method applies Gaussian kernel regression to the x and y coordinates independently using the built-in ksmooth function.

Kernel smoothing simultaneously smooths and generalizes curves, and can be tuned to produce drastically smoothed curves. Choosing a suitable bandwidth is critical for correctly smoothing features using this algorithm and typically users will want to explore a range of bandwidth to find a value that works for their particular scenario.

This method applies a spline interpolation to the x and y coordinates independently using the built-in spline function. Unlike the corner cutting algorithm, this method results in a curve that passes through the vertices of the original curve, which may be a desirable feature.

Unfortunately, this results in an unnaturally wiggly curve. Spline interpolation requires a parameter specifying the number of points to interpolate at, which can either be an absolute number or a relative increase in the number of vertices. Densification is the process of adding additional points to the line segments defining polylines or polygons.

Each line segment is split into multiple sub-segments of equal length, and the original vertices are always included in the densified feature. Note that the densification algorithm treats all vertices as Euclidean points, i. The degree of densification can either be specified as the number of sub-segments each line segment is split into n :. For multipart features, this function dives into the individual component features and applies the threshold.

The threshold can either be provided as a number, assumed to be in the units of the coordinates reference system meters for unprojected coordinatesor as a units object.

This latter approach provides more flexibility because a threshold can be given in any type of units and it will be converted to the correct units automatically. Recall that the cells is this raster are square kilometers each. There are two holes in the large central polygon.

Clip Raster in ArcMap (Basic processing in GIS)

For a greater degree of smoothing and generalization, increase the smoothness parameter:. Setup library raster library sf library units library smoothr. Example data This package comes with two simple spatial datasets in sf format to test the smoothing algorithms on.

Kernel smoothing This method applies Gaussian kernel regression to the x and y coordinates independently using the built-in ksmooth function. Spline interpolation This method applies a spline interpolation to the x and y coordinates independently using the built-in spline function.

Densification Densification is the process of adding additional points to the line segments defining polylines or polygons. An algorithm for high speed curve generation. Kernel smoothing: Nadaraya-Watson Optimal Bandwidth Spline interpolation: Create polygon from set of points distributed Smoothing polygons in contour map?

raster to polygon r sf