Posted on Leave a comment

GIS Week Sale – 35% off geospatial e-books

35 off discount for ebook by locate press

11-day sale to Celebrate November and GIS Week

Enjoy a 35% discount on all Locate Press e-books from Nov 8-19th:

  • Find an e-book here
  • Select Buy PDF
  • Enter Coupon Code: GISDAY2021
  • Add to Cart, Checkout, and save big!

Sign up for our newsletter to get receive future subscriber-only discounts.

Share the sale with your friends

buy bulk to beat amazon from locate press discount-bulk-sales

Beat Amazon! Save on your print orders by ordering direct and getting 20% off 5 or more books of any titles (can be 5 different titles).

Contact us below with your order and mailing address – we’ll supply a quote and a link to process credit card payments.

We print in USA, UK, and Australia where we offer the best shipping rates.

locate-press-swag-now-available

Posted on 1 Comment

Setting up PyQGIS3 with VSCode & Python3 on Windows

ModuleNotFoundError: No module named 'qgis'

This error got you down? Here’s how I solved it in my vscode and Python and QGIS environment on Windows.


This blog post by Gary is excellent for building a development environment for PyQGIS on Windows: Quick Guide to Getting Started with PyQGIS3 On Windows (Spatialgalaxy.net).

In this post we put a slight twist on it, just to be different, and to learn to use Visual Studio Code, on Windows, and without external setup scripts. There were multiple issues that held me back on it this but ultimately it was just my knowledge of vscode that needed an upgrade.

In a nutshell, for a minimal working example, you need four components: a QGIS install, a vscode workspace file, a .env file, and a Python script in the same folder.

QGIS install

In this case, I use a standard release installer from QGIS.org to get set up on Windows (not OSGeo4W). The install location is on a secondary drive, not the long file name with a couple of spaces – that is not a concern with this approach.

Note that the Python modules for both PyQGIS, PyQT5 are included in this install! More below on that.

D:\Program Files\QGIS 3.12\

vscode workspace

I’ve worked on various development projects with vscode before and never worried much about this workspace concept with them.

If you create a file by hand, it will then allow you to open the folder as a workspace. I suggest creating all the files below first, then opening it in vscode (File-> Open Folder).

If you do not have one of these JSON format files already, it will prompt you to create/save one. The one it created for me wasn’t very helpful as it had the wrong paths in it and it messed me up further.

Here is my workspace definition file, saved in:

c:\users\tyler\source\repos\diy-gis\workspace.code-workspace
{
	"folders": [
		{
			"name": "app",
			"path": "C:\\Users\\tyler\\source\\repos\\diy-gis"
		}
	]
}

All the file does is define the path for the rest of the project. This seems important when you go to launch the Python script,we didn’t worry about any other settings in this file when doing my testing. But it doesn’t mean it’s good enough for further uses especially build/deploy scenarios.

vscode .env file

The .env file is used to set environment variables before running a Python project in a vscode. It appears to require a workspace to be defined as well, see above.

Common system-wide/global Windows environment settings should work but I wanted this to all be self-contained in a single project workspace.

In the .env file, we simply put two lines, one for the PATH and one for the PYTHONPATH to point to our QGIS installation. Adjust for your version and path.

PATH=$PATH;"D:\Program Files\QGIS 3.18\bin\";"D:\Program Files\QGIS 3.18\apps\qgis\bin"
PYTHONPATH="D:\Program Files\QGIS 3.12\apps\qgis\python"

Basic Python app with QGIS module loaded

As we were early in this project let’s ensure that PyQT5 and QGIS modules would load into the Python project.

In the QGIS apps\qgis\python folder are the modules that Python will use, per the .env file above.

Some places will tell you to install PyQT5 with pip and that is not necessary if you have things set up this way. Plus you’ll know that your versions of QT and PyQGIS will be compatible if you use the same location for the modules.

This code uses both those modules and builds a very basic Hello World app. The file is app1.py in my workspace.

import sys
import qgis

from PyQt5.QtWidgets import QApplication, QWidget, QLabel

def window():
   app = QApplication(sys.argv)
   widget = QWidget()

   textLabel = QLabel(widget)
   textLabel.setText("Hello World!")
   textLabel.move(110,85)

   widget.setGeometry(50,50,320,200)
   widget.setWindowTitle("PyQt5 Example")
   widget.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   window()

Next, with the app1.py open in vscode, I run the app script from the Run -> Run Without Debugging tool and see the application window pop up!

If things go wrong, check the terminal/console for any errors, here is my terminal output from vscode:

PS C:\Users\tyler\source\repos\diy-gis>  c:; cd 'c:\Users\tyler\source\repos\diy-gis'; & 'C:\python38\python.exe' 'c:\Users\tyler\.vscode\extensions\ms-python.python-2021.10.1365161279\pythonFiles\lib\python\debugpy\launcher' '54781' '--' 'c:\Users\tyler\source\repos\diy-gis\app1.py' 

If I comment out the PYTHONPATH from the .env file, I get my errors finding the PyQGIS module: qgis. Note that I do not get errors for PyQt because I also had them installed by pip in my main Python installation.

Traceback (most recent call last):
  File "c:\Users\tyler\source\repos\diy-gis\app1.py", line 2, in <module>
    import qgis
ModuleNotFoundError: No module named 'qgis'
PS C:\Users\tyler\source\repos\diy-gis>

Resources

The PyQGIS Programmer's Guide 3 - Extending QGIS 3 with Python 3 by Gary Sherman

Posted on Leave a comment

October Newsletter (2021)

buy bulk to beat amazon from locate press discount-bulk-sales

Hi and welcome to our short October Newsletter. Whether you are a new subscriber or an old customer, we want to stay in touch. This newsletter highlights one way to save on print orders, how to update your profile, and how to stay in touch.
We also include a link to an interesting podcast on Geospatial Python with Anita Graser and MapScaping. Watch our next newsletter for the upcoming e-book sale in November.

Nearing 10 years of being in business

Locate Press has grown a lot in just under 10 years, from 1 title to over a dozen. During that time we’ve moved to more digital products and look for new ways to stay in touch with both e-book and print book customers. 

We are using this newsletter forum to help stay in touch but in a focused way – e.g., sharing updates about your favorite books – previously we did not have a way to communicate with focus.

Now you can update your profile to opt-in to specific topics of interest.

Save money with 5 or more books

Need books for an event or course? Normally we limit bulk discounts to larger orders, but anyone can place an order with us directly – for at least 5 books and we will give a 20% retail discount. That’s like buying four and getting one for free.

What you do with them after is up to you – resell, giveaway, or get them autographed at an event – your call!  

Larger orders may enjoy even steeper discounts. We ship from USA, UK, and Australia.

Email tyler@locatepress.com with your order. 

Stay connected – follow more regular updates and discussion here

One last thing.

We regularly look for friends and affiliates that share topics of interest with our members. This MapScaping podcast is a good one. Locate Press author Anita Graser lays out the Geospatial Python landscape. We think you’ll enjoy it.

Locate Press swag now available: hats, shirts, stickers, and more.

Posted on Leave a comment

Locate Press sponsors FOSS4GNL – book giveaway

We were happy to help provide some competitive reading material to the event including Discover QGIS 3.x and QGIS for Hydrological Applications. Contact us if you are having an event and need some giveaways.