Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Hands-On Geospatial Analysis with R and QGIS
Hands-On Geospatial Analysis with R and QGIS

Hands-On Geospatial Analysis with R and QGIS: A beginner's guide to manipulating, managing, and analyzing spatial data using R and QGIS 3.2.2

eBook
AU$53.99 AU$60.99
Paperback
AU$75.99
Subscription
Free Trial
Renews at AU$24.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Hands-On Geospatial Analysis with R and QGIS

Setting Up R and QGIS Environments for Geospatial Tasks

This chapter will walk its readers through the different stages of setting up the R and QGIS environments. R and QGIS are both free and open source software that can be used for various geospatial tasks. R benefits from more than 10,000 packages developed by its community, and QGIS also benefits from a number of plugins that are available to QGIS users. QGIS can complement R, and vice versa, for the conduct of many sophisticated geospatial tasks, and many statistical and machine learning algorithms can be very easily applied using R with the help of QGIS.

The first segment of the book starts by discussing how to install R and getting to know its environment. That is followed by data types in R, and different operations in R, and then getting acquainted with writing functions and plotting. The second segment consists of installing QGIS, learning the QGIS environment, and getting help in QGIS.

The following topics are to be covered in this chapter:

  • Installing R
  • Basic data types and the data structure in R
  • Looping, functions, and apply family in R
  • Plotting in R
  • Installing QGIS
  • Getting to know the QGIS environment.

Installing R

R is an open source programming language and software used for statistical computing and graphics, which has benefited greatly from the continuous contributions of its user community. Graphics in R are of very high quality, and, although it was not primarily developed for GIS purposes, with the development of packages such as ggmap, tmap, sf, raster, sp, and so on, R can work as a GIS environment itself. Furthermore, R codes can be written inside QGIS and we can also work on QGIS inside R using the RQGIS package.

We will now install R with the help of snapshots of each of the step-by-step instructions provided. The following steps have been implemented in Microsoft Windows and should also be applicable to Mac or other platforms with a little tweaking. There are no specific requirements for computer configuration, but any modern desktop or laptop will be sufficient to run the examples provided in this book.

Download R software from the following site and click on download R: https://www.r-project.org/.

Now we need to select a CRAN mirror; we will use the first link to download R.

Now we will need to click on Download R for Windows:

Click install R for the first time, as we can see from the following screenshot:

Now we just need to double-click the .exe file that we have downloaded and continue to click to accept all the defaults to complete the download of R. After we have installed R, we need to open it, and it will look similar to the following screenshot. For this installation process, a 64-bit computer is being used, but if you are using a 32-bit computer, your R windows will reflect that:

We are finally ready to rock and roll using R. But before that, we need a little bit more familiarity with R, or perhaps we need a refresher.

Basic data types and data structures in R

Before we start delving deep into R for geospatial analysis, we need to have a good understanding of how R handles and stores different types of data. We also need to know how to undertake different operations on that data.

Basic data types in R

There are three main data types in R, and they are as follows:

  • Numerics
  • Logical or Boolean
  • Character

Numerics are any numbers with decimal values; thus, 21.0 and 21.1 are both numerics. We can use addition, subtraction, multiplication, division, and so on, with these numerics. Interestingly, R also considers integer numbers to be numerics. Logical or Boolean data consists of TRUE and FALSE; they are mainly used for different comparisons. The character variable consists of text, such as the name of something. We write character values in R by putting our character values inside "", or double quotes.

Variable

Just before digging any deeper, we need to know how to assign values to any variable. So, what is a variable? It's like a container, which holds different value(s) of different types (or the same type). When assigning multiple values to any variable, we write the variable name to the left, followed by an <- or = and then the value. So, if we want to assign 2 to a variable x, we can write either of the two:

x <- 2

or

x = 2
I find the latter convenient, although the R community prefers to use the former – my suggestion is to use one which you find more convenient.

Data structures in R

The data structures in R are as follows:

  • Vectors
  • Matrices
  • Arrays
  • Data frames
  • Lists
  • Factors

Vectors

Vectors are used to store single or multiple values of similar data types in a variable and are considered to be one-dimensional arrays. That means that the x variable we just defined is a vector. If we want to create a vector with multiple numeric values, we assign as before with one additional rule: we put all the values inside c() and separate all the values with , except the last value. Let's look at an example:

val = c(1, 2, 3, 4, 5, 6)

What happens if we mix different data types such as both numerics and characters? It works! (A variable's name is arbitrarily named as val, but you can name your variable anything that you feel appropriate, anything!) Except in some cases, such as variable names, shouldn't start with any special character:

x = c(1, 2.0, 3.0, 4, 5, "Hello", "OK")

What we have just learned about storing data of the same types doesn't seem to be true then, right? Well, not exactly. What R does behind the scenes is that it tries to convert all the values mentioned for the x variable to the same type. As it can't convert Hello and OK to numeric types, for conformity it converts all the numeric values 1, 2.0, 3.0, 4, and 5 to character values: that is, "1", "2.0", "3.0", "4", and "5", and adds two more values, "Hello" and "OK", and assigns all these character values to x. We can check the class (data type) of a variable in R with class(variable_name), and let's confirm that x is indeed a character variable:

class(x)

We will see that the R window will show the following output:

[1] "character"

We can also label vectors or give names to different values according to our need. Suppose we want to assign temperature values recorded at different times to a variable with a recorded time as a label. We can do so using this code:

temperature = c(morning = 20, before_noon = 23, after_noon = 25, evening = 22, night =  18)

Basic operations with vector

Suppose the prices of three commodities, namely potatoes, rice, and oil were $10, $20, and $30 respectively in January 2018, denoted by the vector jan_price, and the prices of all these three elements increased by $1, $2, and $3 respectively in March 2018, denoted by the vector increase. Then, we can add two vectors mar_price and increase to get the new price as follows:

jan_price = c(10, 20, 30)
increase = c(1, 2, 3)
mar_price = jan_price + increase

To see the contents of mar_price, we just need to write it and then press Enter:

mar_price

We now see that mar_price is updated as expected:

[1] 11 22 33

Similarly, we can subtract and multiply. Remember that R uses element-wise computation, meaning that if we multiply two vectors which are of the same size, the first element of the first vector will be multiplied by the first element of the second vector, and the second element of the second vector will be multiplied by the second element of the second vector, and as such:

x = c(10, 20, 30)
y = c(1, 2, 3)
x * y

The result of this multiplication is this:

[1] 10 40 90

If we multiply a vector with multiple values by a single value, that latter value multiplies every single element of the vector separately. This is demonstrated in the following example:

x * 2

We can see the output of the preceding command as follows:

[1] 20 40 60

As a vector does element-wise computation, if we check for any condition, the condition will be checked for each element. Thus, if we want to know which values in x are greater than 15:

x > 15

As the second and third elements satisfy this condition of being greater than 15, we see TRUE for these positions and FALSE for the first position as follows:

[1] FALSE TRUE TRUE

Indexing in R or the first element of any data type starts with 1; thus, the third or fourth element in R can be accessed with index 3 or 4. We need to access any particular index of a variable with a variable name followed by the index inside []. Thus, the third element of x can be accessed as follows:

x[3]

By pressing Enter after x[3], we see that the third element of x is this:

30

If we want to select all items but the third one, we need to use - in the following way:

x[-3]

We now see that x has all of the elements except the third one:

[1] 10 20

Matrix

Suppose, we also have the prices of these three items for the month of June as follows:

june_price = c(20, 25, 33)

Now if we want to stack all these three months in a single variable, we can't use vectors anymore; we need a new data structure. One of the data structures to rescue in this case is the matrix. A matrix is basically a two-dimensional array of data elements with a number of rows and columns fixed. Like a vector, a matrix can also contain just one type of element; a mix of two types is not allowed. To combine these three vectors with every row corresponding to a particular month's prices of different items and every column corresponding to prices of different items in a particular month, what we can do is first combine these three vectors inside a matrix() command, followed by a comma and nrow = 3, indicating the fact that there are three different items (for example, items are arranged row-wise and months are arranged column-wise).

all_prices = matrix(c(jan_price, mar_price, june_price), nrow= 3)
all_prices

The all_prices data frame will look like the following:

[,1] [,2] [,3]
[1,] 10 11 20
[2,] 20 22 25
[3,] 30 33 33

Now suppose we change our mind and want to arrange this with the items displayed column-wise and the prices displayed row-wise; that is, the first row corresponds to the prices of different items in a particular month and the first column corresponds to the first month's (January's) prices of different items, with that arrangement continuing for every other row and column. We can do so very easily by mentioning byrow = TRUE inside the matrix. byrow = TRUE arranges the values of vectors row-wise. It arranges the matrix by aligning all the elements row-wise allowing for its dimensions:

all_prices2 = matrix(c(jan_price, mar_price, june_price), nrow= 3, byrow = TRUE)  
all_prices2

The output will look like the following:

[,1] [,2] [,3]
[1,] 10 20 30
[2,] 11 22 33
[3,] 20 25 33

We can see that here jan_price is considered as the first row, mar_price as the second row, and june_price as the third row in all_prices2.

Array

Arrays are also like matrices, but they allow us to have more than two dimensions. The all_prices2 row has prices of different items for January, March, and June 2018. Now, suppose we also want to record prices for 2017. We can do so by using array() and in this case we want to add two 3x3 matrices where the first one corresponds to 2018 and the latter matrix corresponds to 2017. In array(m, n, p), m and n stand for the dimensions of the matrix and p stands for how many matrices we want to store.

In the following example, we define six vectors for three different months for two different years. Now we create an array by combining six different vectors using c() and by using them inside array() as inputs as follows:

# Create six vectors
jan_2018 = c(10, 11, 20)
mar_2018 = c(20, 22, 25)
june_2018 = c(30, 33, 33)
jan_2017 = c(10, 10, 17)
mar_2017 = c(18, 23, 21)
june_2017 = c(25, 31, 35)
# Now combine these vectors into array
combined = array(c(jan_2018, mar_2018, june_2018, jan_2017, mar_2017, june_2017),dim = c(3,3,2))
combined

We can now see that we have two matrices of 3 x 3 dimensions, as in the output as follows:

Data frames

Data frames are like matrices, except for the one additional advantage that we can now have a mix of different element types in a data frame. For example, we can now store both numeric and character elements in this data structure. Now, we can also put the names of different food items along with their prices in different months to be stored in a data frame. First, define a variable with the names of different food items:

items = c("potato", "rice", "oil")

We can define a data frame using data.frame as follows:

all_prices3 = data.frame(items, jan_price, mar_price, june_price) 
all_prices3

The data frame all_prices3 looks like the following:

Accessing elements in a data frame can be done by using either [[]] or $. To select all the values of mar_price or the second column, we can do either of the two methods provided as follows:

all_prices3$mar_price

This gives the values of the mar_price column of the all_prices3 data frame:

[1] 11 22 33

Similarly, there is the following:

all_prices3[["mar_price"]]

We now find the same output as we found by using the $ sign:

[1] 11 22 33

We can also use [] to access a data frame. In this case, we can utilize both the row and column dimensions to access an element (or elements) using the row index indicated by the number before, and the column index indicated by the number after. For example, if we wanted to access the second row and third column of all_prices3, we would write this:

 all_prices3[2, 3]

This gives the following output:

[1] 22

Here, for simplicity, we will drop items column from all_prices3 using - and rename the new variable as all_prices4 and we can define this value in a new vector pen as follows:

  all_prices4 = all_prices3[-1]
all_prices4

We can now see that the items column is dropped from the all_prices4 data frame:

We can add a row using rbind(). Now we define a new numerical vector that contains the price of the pen vector for January, March, and June, and we can add this row using rbind():

pen = c(3, 4, 3.5)
all_prices4 = rbind(all_prices4, pen)
all_prices4

Now we see from the following output that a new observation is added as a new row:

We can add a column using cbind(). Now, suppose we also have information on the prices of potato, rice, oil, and pen for August as given in the vector aug_price:

aug_price = c(22, 24, 31, 5)

We can now use cbind() to add aug_price as a new column to all_prices4:

all_prices4 = cbind(all_prices4, aug_price)
all_prices4

Now all_prices4 has a new column aug_price added to it:

Lists

Now, items jan_price and mar_price have four elements, whereas june_price has three elements. So, we can't use a data frame in this case to store all of these values in a single variable. Instead, we can use lists. Using lists, we can get almost all the advantages of a data frame in addition to its capacity for storing different sets of elements (columns in the case of data frames) with different lengths:

all_prices_list2 = list(items, jan_price, mar_price, june_price)
all_prices_list2

We can now see that all_prices_list2 has a different structure than that of a data frame:

Accessing list elements can be done by either using [] or [[]] where the former gives back a list and the latter gives back element(s) in its original data type. We can get the values of jan_price in the following way:

all_prices_list2[2]

Using [], we are returned with the second element of all_prices_list2 as a list again:

Note that, by using [], what we get back is another list and we can't use different mathematical operations on it directly.

class(all_prices_list2[2])

We can see, as follows, that the class of all_prices_list2 is a list:

We can get this data in original data types (that is, a numeric vector) by using [[]] instead of []:

all_prices_list2[[2]]

Now, we get the second element of the list as a vector:

We can see that it is numeric and we can check further to confirm that it is numeric:

class(all_prices_list2[[2]])

The following result confirms that it is indeed a numeric vector:

We can also create categorical variables with factor().

Suppose we have a numeric vector x and we want to convert it to a factor, we can do so by following the code as shown as follows:

x = c(1, 2, 3)
x = factor(x)
class(x)

Factor

We now see that the class is a factor, as we can see in the following output:

[1] "factor"

Now, we can also look at the internal structure of this vector x, using str() as follows:

str(x)

We now see that it converts 1, 2, and 3 to factors:

[1]  Factor w/ 3 levels "1", "2", "3": 1 2 3
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Understand the basics of R and QGIS to work with GIS and remote sensing data
  • Learn to manage, manipulate, and analyze spatial data using R and QGIS
  • Apply machine learning algorithms to geospatial data using R and QGIS

Description

Managing spatial data has always been challenging and it's getting more complex as the size of data increases. Spatial data is actually big data and you need different tools and techniques to work your way around to model and create different workflows. R and QGIS have powerful features that can make this job easier. This book is your companion for applying machine learning algorithms on GIS and remote sensing data. You’ll start by gaining an understanding of the nature of spatial data and installing R and QGIS. Then, you’ll learn how to use different R packages to import, export, and visualize data, before doing the same in QGIS. Screenshots are included to ease your understanding. Moving on, you’ll learn about different aspects of managing and analyzing spatial data, before diving into advanced topics. You’ll create powerful data visualizations using ggplot2, ggmap, raster, and other packages of R. You’ll learn how to use QGIS 3.2.2 to visualize and manage (create, edit, and format) spatial data. Different types of spatial analysis are also covered using R. Finally, you’ll work with landslide data from Bangladesh to create a landslide susceptibility map using different machine learning algorithms. By reading this book, you’ll transition from being a beginner to an intermediate user of GIS and remote sensing data in no time.

Who is this book for?

This book is great for geographers, environmental scientists, statisticians, and every professional who deals with spatial data. If you want to learn how to handle GIS and remote sensing data, then this book is for you. Basic knowledge of R and QGIS would be helpful but is not necessary.

What you will learn

  • Install R and QGIS
  • Get familiar with the basics of R programming and QGIS
  • Visualize quantitative and qualitative data to create maps
  • Find out the basics of raster data and how to use them in R and QGIS
  • Perform geoprocessing tasks and automate them using the graphical modeler of QGIS
  • Apply different machine learning algorithms on satellite data for landslide susceptibility mapping and prediction

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 30, 2018
Length: 354 pages
Edition : 1st
Language : English
ISBN-13 : 9781788996983
Vendor :
QGIS Development Team
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 30, 2018
Length: 354 pages
Edition : 1st
Language : English
ISBN-13 : 9781788996983
Vendor :
QGIS Development Team
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
AU$24.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
AU$249.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts
AU$349.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just AU$5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total AU$ 212.97
Mastering Geospatial Analysis with Python
AU$75.99
Learn QGIS
AU$60.99
Hands-On Geospatial Analysis with R and QGIS
AU$75.99
Total AU$ 212.97 Stars icon

Table of Contents

11 Chapters
Setting Up R and QGIS Environments for Geospatial Tasks Chevron down icon Chevron up icon
Fundamentals of GIS Using R and QGIS Chevron down icon Chevron up icon
Creating Geospatial Data Chevron down icon Chevron up icon
Working with Geospatial Data Chevron down icon Chevron up icon
Remote Sensing Using R and QGIS Chevron down icon Chevron up icon
Point Pattern Analysis Chevron down icon Chevron up icon
Spatial Analysis Chevron down icon Chevron up icon
GRASS, Graphical Modelers, and Web Mapping Chevron down icon Chevron up icon
Classification of Remote Sensing Images Chevron down icon Chevron up icon
Landslide Susceptibility Mapping Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.3
(3 Ratings)
5 star 33.3%
4 star 0%
3 star 33.3%
2 star 33.3%
1 star 0%
Stringz Jan 08, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a good book for any one wanting a career as a geospatial Analyst. it is fully loaded with an awesome selection of topics. The topics can be leveraged in most big data science analysis. I really enjoyed the progression and the topic selection.
Amazon Verified review Amazon
kensuke May 14, 2019
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
QGISの解説部分は、日本語でこの書籍より安価な本が増えましたので、必要ないです。Rでのspdepパッケージの解説については、空間計量の初歩、重み行列の生成とモラン統計量の求め方、検定手法がソースコード付きでありますので、そのへんの需要があれば買っても良いかな。特に、空間計量の検定については具体的なソースコードの例が、日本語文献だと少ないので、お手本として見るにはよいかもしれない。ただ、その箇所を見るために5000円払うかは迷うところ。
Amazon Verified review Amazon
Cliente Amazon Apr 29, 2021
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
Il libro è stampato da Amazon. Le pagine interne sono in scala di grigio e questo rende difficoltosa la fruizione delle parti illustrate relative alle.schermate di QGIS e R.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.