Blog d’Anita Graser

https://anitagraser.com

  • Stand-alone PyQGIS scripts with OSGeo4W 3 mars 2019
    PyQGIS scripts are great to automate spatial processing workflows. It’s easy to run these scripts inside QGIS but it can be even more convenient to run PyQGIS scripts without even having to launch QGIS. To create a so-called “stand-alone” PyQGIS script, there are a few things that need to be taken care of. The following steps show how to set up PyCharm for stand-alone PyQGIS development on Windows10 with OSGeo4W. An essential first step is to ensure that all environment variables are set correctly. The most reliable approach is to go to C:\OSGeo4W64\bin (or wherever OSGeo4W is installed on your machine), make a copy of qgis-dev-g7.bat (or any other QGIS version that you have installed) and rename it to pycharm.bat: Instead of launching QGIS, we want that pycharm.bat launches PyCharm. Therefore, we edit the final line in the .bat file to start pycharm64.exe: In PyCharm itself, the main task to finish our setup is configuring the project interpreter: First, we add a new “system interpreter” for Python 3.7 using the corresponding OSGeo4W Python installation. To finish the interpreter config, we need to add two additional paths pointing to QGIS\python and QGIS\python\plugins: That’s it …
  • Easy Processing scripts comeback in QGIS 3.6 2 mars 2019
    When QGIS 3.0 was release, I published a Processing script template for QGIS3. While the script template is nicely pythonic, it’s also pretty long and daunting for non-programmers. This fact didn’t go unnoticed and Nathan Woodrow in particular started to work on a QGIS enhancement proposal to improve the situation and make writing Processing scripts easier, while – at the same time – keeping in line with common Python styles. While the previous template had 57 lines of code, the new template only has 26 lines – 50% less code, same functionality! (Actually, this template provides more functionality since it also tracks progress and ensures that the algorithm can be cancelled.) from qgis.processing import alg from qgis.core import QgsFeature, QgsFeatureSink @alg(name= »split_lines_new_style », label=alg.tr(« Alg name »), group= »examplescripts », group_label=alg.tr(« Example Scripts »)) @alg.input(type=alg.SOURCE, name= »INPUT », label= »Input layer ») @alg.input(type=alg.SINK, name= »OUTPUT », label= »Output layer ») def testalg(instance, parameters, context, feedback, inputs): «  » » Description goes here. (Don’t delete this! Removing this comment will cause errors.) «  » » source = instance.parameterAs …
  • Movement data in GIS #20: Trajectools v1 released! 2 février 2019
    In previous posts, I already wrote about Trajectools and some of the functionality it provides to QGIS Processing including: Creating trajectories from points Clipping trajectories by extent Splitting trajectories by date There are also tools to compute heading and speed which I only talked about on Twitter. Trajectools is now available from the QGIS plugin repository. The plugin includes sample data from MarineCadastre downloads and the Geolife project. Under the hood, Trajectools depends on GeoPandas! If you are on Windows, here’s how to install GeoPandas for OSGeo4W: OSGeo4W installer: install python3-pip Environment variables: add GDAL_VERSION = 2.3.2 (or whichever version your OSGeo4W installation currently includes) OSGeo4W shell: call C:\OSGeo4W64\bin\py3_env.bat OSGeo4W shell: pip3 install geopandas (this will error at fiona) From https://www.lfd.uci.edu/~gohlke/pythonlibs/#fiona: download Fiona-1.7.13-cp37-cp37m-win_amd64.whl OSGeo4W shell: pip3 install path-to-download\Fiona-1.7.13-cp37-cp37m-win_amd64.whl OSGeo4W shell: pip3 install geopandas (optionally) From https://www.lfd.uci.edu/~gohlke/pythonlibs/#rtree: download Rtree-0.8.3-cp37-cp37m-win_amd64.whl and pip3 instal …
  • Dealing with delayed measurements in (Geo)Pandas 27 janvier 2019
    Yesterday, I learned about a cool use case in data-driven agriculture that requires dealing with delayed measurements. As Bert mentions, for example, potatoes end up in the machines and are counted a few seconds after they’re actually taken out of the ground: Yield mapping in agriculture, there’s a delay of ~10 – 15 seconds in data logging between intake of potato and the measurement which is at the ‘end’ of the processing in the machine, during which the machine drives forward. pic.twitter.com/Bo60oUIH2e — Bert Rijk (@BertRijk) January 26, 2019 https://platform.twitter.com/widgets.js Therefore, in order to accurately map yield, we need to take this temporal offset into account. We need to make sure that time and location stay untouched, but need to shift the potato count value. To support this use case, I’ve implemented apply_offset_seconds() for trajectories in movingpandas: def apply_offset_seconds(self, column, offset): self.df[column] = self.df[column].shift(offset, freq=’1s’) The following test illustrates its use: you can see how the value column is shifted by 120 second. Geometry and time remain unchanged but the value column is shifted accordingly. In this test, we look at …
  • Movement data in GIS #19: splitting trajectories by date 26 janvier 2019
    Many current movement data sources provide more or less continuous streams of object locations. For example, the AIS system provides continuous locations of vessels (mostly ships). This continuous stream of locations – let’s call it track – starts when we first record the vessel and ends with the last record. This start and end does not necessarily coincide with the start or end of a vessel voyage from one port to another. The stream start and end do not have any particular meaning. Instead, if we want to see what’s going on, we need to split the track into meaningful segments. One such segmentation – albeit a simple one – is to split tracks by day. This segmentation assumes that day/night changes affect the movement of our observed object. For many types of objects – those who mostly stay still during the night – this will work reasonably well. For example, the following screenshot shows raw data of one particular vessel in the Boston region. By default, QGIS provides a Points to Path to convert points to lines. This tool takes one “group by” and one “order by” field. Therefore, if we want one trajectory per ship per day, we’d first have to create a new field that combines ship ID …
  • From CSV to GeoDataFrame in two lines 23 janvier 2019
    Pandas is great for data munging and with the help of GeoPandas, these capabilities expand into the spatial realm. With just two lines, it’s quick and easy to transform a plain headerless CSV file into a GeoDataFrame. (If your CSV is nice and already contains a header, you can skip the header=None and names=FILE_HEADER parameters.) usecols=USE_COLS is also optional and allows us to specify that we only want to use a subset of the columns available in the CSV. After the obligatory imports and setting of variables, all we need to do is read the CSV into a regular DataFrame and then construct a GeoDataFrame. import pandas as pd from geopandas import GeoDataFrame from shapely.geometry import Point FILE_NAME = « /temp/your.csv » FILE_HEADER = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘x’, ‘y’] USE_COLS = [‘a’, ‘x’, ‘y’] df = pd.read_csv( FILE_NAME, delimiter= »; », header=None, names=FILE_HEADER, usecols=USE_COLS) gdf = GeoDataFrame( df.drop([‘x’, ‘y’], axis=1), crs={‘init’: ‘epsg:4326’}, geometry=[Point(xy) for xy in zip(df.x, df.y)]) It’s also possible to create the point objects using a lambda function as shown by weiji14 on GIS.SE. …
  • PyQGIS101 part 10 published! 5 janvier 2019
    PyQGIS 101: Introduction to QGIS Python programming for non-programmers has now reached the part 10 milestone! Beyond the obligatory Hello world! example, the contents so far include: Loading a vector layer Viewing vector layer attributes Filtering features Styling vector layers Loading a raster layer Running Processing tools Exporting layouts Creating & editing a new vector layer Chaining Processing tools If you’ve been thinking about learning Python programming, but never got around to actually start doing it, give PyQGIS101 a try. I’d like to thank everyone who has already provided feedback to the exercises. Every comment is important to help me understand the pain points of learning Python for QGIS. I recently read an article – unfortunately I forgot to bookmark it and cannot locate it anymore – that described the problems with learning to program very well: in the beginning, it’s rather slow going, you don’t know the right terminology and therefore don’t know what to google for when you run into issues. But there comes this point, when you finally get it, when the terminology becomes clearer, when you start thinking “that might work” and it actually does! I hope that PyQGIS101 …
  • Movement data in GIS #18: creating evaluation data for trajectory predictions 15 décembre 2018
    We’ve seen a lot of explorative movement data analysis in the Movement data in GIS series so far. Beyond exploration, predictive analysis is another major topic in movement data analysis. One of the most obvious movement prediction use cases is trajectory prediction, i.e. trying to predict where a moving object will be in the future. The two main categories of trajectory prediction methods I see are those that try to predict the actual path that a moving object will take versus those that only try to predict the next destination. Today, I want to focus on prediction methods that predict the path that a moving object is going to take. There are many different approaches from simple linear prediction to very sophisticated application-dependent methods. Regardless of the prediction method though, there is the question of how to evaluate the prediction results when these methods are applied to real-life data. As long as we work with nice, densely, and regularly updated movement data, extracting evaluation samples is rather straightforward. To predict future movement, we need some information about past movement. Based on that past movement, we can then try to predict future positions. …
  • Movement data in GIS #17: spatial analysis of GeoPandas trajectories 2 décembre 2018
    In Movement data in GIS #16, I presented a new way to deal with trajectory data using GeoPandas and how to load the trajectory GeoDataframes as a QGIS layer. Following up on this initial experiment, I’ve now implemented a first version of an algorithm that performs a spatial analysis on my GeoPandas trajectories. The first spatial analysis algorithm I’ve implemented is Clip trajectories by extent. Implementing this algorithm revealed a couple of pitfalls: To achieve correct results, we need to compute spatial intersections between linear trajectory segments and the extent. Therefore, we need to convert our point GeoDataframe to a line GeoDataframe. Based on the spatial intersection, we need to take care of computing the corresponding timestamps of the events when trajectories enter or leave the extent. A trajectory can intersect the extent multiple times. Therefore, we cannot simply use the global minimum and maximum timestamp of intersecting segments. GeoPandas provides spatial intersection functionality but if the trajectory contains consecutive rows without location change, these will result in zero length lines and those cause an empty intersection result. So far, the clip resu …
  • TimeManager 3.0.2 released! 29 novembre 2018

    Bugfix release 3.0.2 fixes an issue where “accumulate features” was broken for timestamps with milliseconds.

    If you like TimeManager, know your way around setting up Travis for testing QGIS plugins, and want to help improve TimeManager stability, please get in touch!