Project
Analysis with QGIS and Python
Heatmap Generation
This guide will walk you through setting up your environment, creating heatmaps with QGIS and Python, and visualizing data from our sorghum research field trials. These instructions are designed to be accessible for high school students and will help you explore geospatial data and its applications.
Setting Up Your Project
Download Project Files
- Before you begin, download the project files located here: Project Files.
- Create a folder in a location you’ll remember and place all downloaded files, including the Shapefile folder (
plot_boundaries
files), in it.
Install Required Software
Install QGIS
- Visit the QGIS Download Page (if you have not done already).
- Choose the Long Term Release (3.34 LTR) version.
- Select your operating system (Windows, macOS, or Linux) and follow the installation instructions.
Install Miniconda
- Visit the Miniconda Installation Page (if you have not done already).
- Choose the version for your operating system (Windows or macOS/Linux) and follow the instructions.
Set Up Python and Dependencies
Create a Working Project Directory
Windows Users:
- Create a new folder and place the
env_setup.yml
file in it.
You should know!
Remember, this file was downloaded from the google drive folder previously provided to you here: Project Files.
- Create a new folder and place the
env_setup.yml
file in it. - Open the Anaconda command line application (Anaconda PowerShell).
- Navigate to the folder containing
env_setup.yml
:cd C:\Path\To\Folder\Containing\YML\
- Create a new folder and place the
Mac/Linux Users:
- Create a new folder and place the
env_setup.yml
file in it. - Open a terminal application (e.g., iTerm or Terminal).
- Navigate to the folder containing
env_setup.yml
:cd \Path\To\Folder\Containing\YML\
- Create a new folder and place the
Activating your environment
Create and Activate Environment
While in the same directory (folder) of your env_setup.yml file, run the following command to create the environment:
conda env create -f env_setup.yml
When the installation completes:
Verify the environment was created:
conda env list
Activate the environment:
conda activate geo_ml_env
View your environment's packages (that were installed using the env_setup.yml file):
conda list
Install and Setup VS Code
- Visit the Visual Studio Code webpage (if you have not done so already).
Watch Out!
Please do not get Visual Studio Code and Visual Studio confused when you go to download the program! (Confusing, we know!) They are each their own program. You are looking for Visual Studio Code
.
Download the installer provided for your Operating System (Mac/Windows).
After installing, launch VS Code.
You should know!
Launching the application might take a moment as it is a big program. Do not worry if it takes a minute or more to open for you.
In your newly opened Visual Studio code window, navigate to the Extensions tab (
View
->Extensions
).Search for and install the following extensions:
'Python' 'Jupyter'
You should now have the following in your Extensions list under
Installed
:'Jupyter Slide Show' 'Jupyter Notebook Renderers' 'Jupyter Keymap' 'Jupyter Cell Tags' 'Jupyter' 'Python Debugger' 'Python' 'Pylance'
You should know!
Navigating Visual Studio code for the first time can be tricky! If you need help with shortcuts, visit their documentation page, or visit their Getting Started page.
Making the Heatmap
Heatmap creation
Generate a Heatmap Using PyQGIS
- Within VS Code, select
File
->Open Folder
and navigate to your project folder created at the beginning of the instructions. Highlight the folder and clickOpen
.
You should know!
Remember, the needed files were downloaded from the google drive folder previously provided to you here: Project Files.
Double-click the
are_pyqgis.ipynb
Python Notebook file in the VS Code Explorer on the left-hand side. If you do not see the Explorer, go toView
->Explorer
.At top right, click
Select Kernel
->Python Environments
->geo_ml_env
.You will now want to provide for the string constants:
CSV_IN
: Path to thefield_sensor_data.csv
file.SHP_IN
: Path to theplot_boundaries.shp
file. Ensure this file is in the same folder as the otherplot_boundaries
files (this is very important!).PROJECT_OUT
: Output the path and filename for the generated QGIS project file (e.g.,/path/to/your/qgis_heatmap.qgs
), making sure to include the.qgs
extension on the filename.
Important!
You can right-click a file in the Explorer tab and choose
Copy Path
to get its file path, to use in the above step. Paste this into the quotes for each.Example:
CSV_IN = r"<paste_path_here>.
If your file path is
C:\Code\field_sensor_data.csv
then your assignment will look like::CSV_IN = r"C:\Code\field_sensor_data.csv"`
Provide the column/data name (
label_name
string) for the data you wish to view as a heatmap.Run each cell (by pressing the
shift
+enter
keys, or pressing theplay
button at the top of the editor).
Troubleshooting tips
If any files are
not found
, check your paths and try again. Use absolute paths if necessary.If the kernel crashes, hit
Restart
and run again.
You should know!
It is common for virtual environments (like the one you are using) to sometimes crash. No need to panic!
Heatmap View
View Heatmap in QGIS
Launch QGIS and select Project → Open to load your
.qgs
file.Add the Google Satellite layer:
- Right-click XYZ Tiles in the browser pane (on the left) and select New Connection.
- Enter the following details:
- Name: Google Satellite
- URL:
http://mt0.google.com/vt/lyrs=s&hl=en&x={x}&y={y}&z={z}
- Click OK.
Drag and drop the newly created Google Satellite entry under XYZ Tiles into your Layers list.
Arrange the layers so that the Google Satellite layer is at the bottom.
You should know!
If the heatmap is not visible, right-click the Heatmap Layer and select Zoom to Layer(s).