Reader small image

You're reading from  Mastering Geospatial Analysis with Python

Product typeBook
Published inApr 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788293334
Edition1st Edition
Languages
Right arrow
Authors (3):
Silas Toms
Silas Toms
author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

Paul Crickard
Paul Crickard
author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

Eric van Rees
Eric van Rees
author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees

View More author details
Right arrow

Chapter 8. Automating QGIS Analysis

This book has introduced you to using Python from the command line, in a Jupyter Notebook, and in an IDE to perform geospatial tasks. While these three tools will allow you to accomplish your tasks, there are many times when work needs to be done using desktop GIS software.

QGIS, a popular open source GIS application, provides desktop GIS functionality with the ability to work in a Python console and the ability to write toolboxes and plugins using Python. In this chapter, you will learn how to manipulate desktop GIS data using Python and how to automate these tasks using toolboxes and plugins.

In this chapter, you will learn how to:

  • Load and save layers
  • Create layers from API data sources
  • Add, edit, and delete features
  • Select specific features
  • Call geoprocessing functions
  • Write geoprocessing toolboxes
  • Write plugins

Working in the Python console


The QGIS Python console is a Python console. You can perform all of your normal Python tasks with the added benefit of having the QGIS libraries added. From the console, you can manipulate GIS data and display it on the screen, or not.

The Python console is located under the Plugins menu on the QGIS toolbar. You can also access it by pressing Ctrl Alt P on the keyboard. The console will usually open in the bottom of the main window. You can undock it by clicking on the title bar (where it says Python Console), holding down the mouse button, and dragging the window to another location on the screen or by clicking the window button at the top-right of the console:

A screenshot of the Python console

The console has buttons for clearing the window, importing GIS and QGIS specific libraries, running the current command (you can press Enter instead of clicking this button), showing the editor, modifying options, and viewing theHelpfiles. The editor launches a simplified...

Writing custom toolboxes


Writing toolboxes will allow you to automate several tasks and make that code available to users as a GUI, or to other developers as an algorithm that can be executed using processing. In this section, you will learn how to create a toolbox and call it from processing.

In this chapter, you have learned how to load data from a file and from a PostGIS database. In this example, you will learn how to bring data in to QGIS from the SeeClickFix Application Program Interface (API).

Note

SeeClickFix is a 311 reporting system that is used by many cities in the United States. It contains geospatial data and has a very well documented, and user-friendly API.

To create a new script, open the processing toolbox in QGIS. This will open an editor window. You will write your code in this window and save it using the save icon. The file name will become a toolbox under Tools|User scripts|File name. Save the file and name it SeeClickFix.

Now that you have an empty toolbox, we can start...

Summary


In this chapter, you have learned how to use Python in QGIS. You started by learning the basics of loading a layer and displaying it on the map, and then progressed to adding, editing, and deleting features. You learned how to select features, highlight the selection, and how to use expressions. Then, we took advantage of pre-built geoprocessing tools and you learned how to call toolbox algorithms using processing. Lastly, you learned how to write your own toolbox.

In the next chapter, you will learn how to use Python with Esri tools. You will learn how to use Jupyter Notebooks in a browser to interact with cloud-based datasets and how to use the Esri API for Python to perform basic geospatial analysis and to create ArcGIS Online web maps.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Geospatial Analysis with Python
Published in: Apr 2018Publisher: PacktISBN-13: 9781788293334
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Silas Toms

Silas Toms is a long-time geospatial professional and author who has previously published ArcPy and ArcGIS and Mastering Geospatial Analysis with Python. His career highlights include developing the real-time common operational picture used at Super Bowl 50, building geospatial software for autonomous cars, designing computer vision for next-gen insurance, and developing mapping systems for Zillow. He now works at Volta Charging, predicting the future of electric vehicle adoption and electric charging infrastructure.
Read more about Silas Toms

author image
Paul Crickard

Paul Crickard authored a book on the Leaflet JavaScript module. He has been programming for over 15 years and has focused on GIS and geospatial programming for 7 years. He spent 3 years working as a planner at an architecture firm, where he combined GIS with Building Information Modeling (BIM) and CAD. Currently, he is the CIO at the 2nd Judicial District Attorney's Office in New Mexico.
Read more about Paul Crickard

author image
Eric van Rees

Eric van Rees was first introduced to Geographical Information Systems (GIS) when studying Human Geography in the Netherlands. For 9 years, he was the editor-in-chief of GeoInformatics, an international GIS, surveying, and mapping publication and a contributing editor of GIS Magazine. During that tenure, he visited many geospatial user conferences, trade fairs, and industry meetings. He focuses on producing technical content, such as software tutorials, tech blogs, and innovative new use cases in the mapping industry.
Read more about Eric van Rees