Seven ways of injecting Python to QGIS
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 188 | |
Author | ||
License | CC Attribution 3.0 Germany: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/31686 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Producer | ||
Production Year | 2014 | |
Production Place | Portland, Oregon, United States of America |
Content Metadata
Subject Area | ||
Genre | ||
Abstract |
| |
Keywords |
FOSS4G 2014 Portland83 / 188
7
10
14
15
16
23
24
25
28
29
33
37
39
40
43
45
46
48
50
56
64
65
69
72
74
82
89
91
98
102
107
111
114
118
128
131
132
135
138
141
143
147
149
150
157
158
161
164
165
166
173
174
175
179
185
00:00
Service (economics)Quantum gravityGroup actionString (computer science)Uniform resource locatorPoint (geometry)Direction (geometry)Google Street ViewHome pageCodeoutputScripting languageRepresentational state transferAddress spaceRow (database)Open setFunctional (mathematics)Degree (graph theory)Parameter (computer programming)Online helpForcing (mathematics)CalculationField (computer science)Table (information)Greatest elementCNNQuery languageArchaeological field surveyEmailSlide ruleInjektivitätGame controllerNumber theoryValidity (statistics)Process (computing)Event horizonMultiplication signKeyboard shortcutType theoryForm (programming)Computer fileDimensional analysisCuboidVideo game consoleText editorPlug-in (computing)LengthMetreSampling (statistics)Volume (thermodynamics)CASE <Informatik>PolygonMenu (computing)1 (number)Flow separationWorkstation <Musikinstrument>Letterpress printingDistanceLink (knot theory)Line (geometry)Computational geometryBlogGeometryMappingTemplate (C++)Personal identification numberLogic programmingClique-widthRight angleSoftware frameworkProjektive GeometrieFile archiverPressureCore dumpWeb browserState of matterMedical imagingSeries (mathematics)LoginRule of inferenceSystem callEndliche ModelltheoriePunched cardQuicksortFocus (optics)Physical systemTexture mappingMathematical analysisMeasurementDescriptive statisticsProduct (business)Junction (traffic)Sheaf (mathematics)Bit ratePlanningGraph (mathematics)MultikollinearitätWordTrail3 (number)Order (biology)Performance appraisalDesign by contractCategory of beingXMLUML
Transcript: English(auto-generated)
00:00
Yes, my name is Jacob Lenstorp. I work at the Danish Geological Survey and Exploration Group. It's called DGE. It's not the official Danish Geological Survey. It's a private company. So I'm going to target non-advanced Tyson users
00:24
or QGIS users. So we won't go into any hardcore here. I want to get everybody to grab a few of the stuff. So using Python and QGIS without writing a plug-in,
00:41
there's several ways we can do that. We are going to focus on the last three ones down here with a Python init function, the expression engine, and how to use Python and actions. I'm going to quickly run through the four on the top.
01:05
We have the console and the editor.
01:23
The console is in the plug-in menu. That's a script-runner plug-in that's useful if you have several scripts you made and you want to run them and use them as an archive. You can run a piece of Python code
01:44
when events happen on your project, like open, close, and save. Here's a sample with some pseudocode where we need the river layer to be below the lake layer. And you can, on the save project event,
02:04
run your code to check if that's true. New comers to QGIS will probably think, well, it's a line layer. It has to be on top of the polygon layer, but not in this case. And then the processing framework is really huge.
02:20
And you can do a lot of stuff in there. We won't deal with that today. So the first problem I ran into, we need to create oil tanks that had to be rectangular and that had to be dimensioned to a given tank volume. A sample is like a 15,000 liter oil tank.
02:43
It has dimension of 1.75 meters to 6.5 meters in the length. So it could be quite difficult to draw that. You had to measure and perhaps use plugins to be able to draw the 90 degrees lines.
03:07
So I come up with a solution of creating a custom UI and use some Python code. So when you updated your tank, when you draw your oil tank, before it's saved,
03:22
it's changed to the correct dimension. There's a UI and a code I'm going to show you. Here's a code. You can read it. I just put it there for you to get when you get the slides. So let's take a look.
03:45
So let's try the old oil tank. You see, it could be quite difficult for me to get the right angles and dimensions.
04:01
And so I came up with this one. OK, I draw something. It doesn't matter what it is. And I finish drawing.
04:22
And up pops this custom form. I can pick the size of the oil tank and call it T1.
04:43
Now watch this geometry. Take a larger one. Let's call it T2.
05:06
Then you can move and rotate them so they place the correct place. So how does this work?
05:23
Up in the properties, the field tab, I can provide my own UI file. It's a path here. I did the UI in a designer called QT Designer.
05:40
You can just drag the different controls in and set up the combo box with the dimensions. And then there's this piece of code up here. This is a text file with my Python code in. And form open is a method in that file.
06:02
And that's all it takes. Take a quick look at the file here. This is a method form open defined. The feature is whatever I draw coming in. And I overwrite the OK button event to my own event.
06:27
And then grab from the feature, the geometry, just the bounding box in the center. That's a point. I get the width and length of the oil tank from a dictionary down here.
06:41
And then create the new geometry and set the geometry of the feature. And then it's changed. Pretty simple and easy.
07:03
There's a blog post from Nathan down there that will tell you in depth about how this stuff is working. OK, the expression string builder is something I found many places in QGIS.
07:21
There's the toolbar and rule-based styles. You can use expressions, labels, the field calculator, pin composer labels. It's a great tool. So what can we do? Problem. Yeah, my colleagues, I made the templates for our maps. And I put my initials on.
07:41
And they're supposed to change them to their own. Some are just, well, at least my initials was out on a lot of maps. I didn't have anything to do with them. So I thought about adding a new function to the expression engine to be used by a label in the print composer.
08:04
You all know the expression engine, I guess. So I added a Phospho-G group. And I was able to get the login, the person who's logged in on the computer. And it's quite easy.
08:22
I have my code in a user function, py. This is the code. It's just one line of the Python code, get user. And it's returned. And I can use that in the print composer. Take a look at that.
08:45
It's this one, you see, get login.
09:08
And it's just by putting in here.
09:28
Now, another problem. I need to get the temperature for a series of points. Now, how do we convert an xy point to a temperature?
09:43
Well, we go out looking for a REST API. We have the open weather. I believe, actually, it's openweathermap.org. Yes, it is. We have the request at the bottom, the link. And it just states a latitude and a longitude.
10:05
Now, when I run that expression here, that request in a browser, it will return the JSON for me. And with red, you can see there's the temperature. And it's in Kelvin. I can also get the pressure, humidity, and the distance to
10:21
the nearest weather station to the point. Then in Python, I can drag out the temperature by the query down at the bottom. Let's try and see it. Temperature, have a quick look inside.
10:45
There's already a column made ready for that called temp. I will make it edible.
11:02
I'll enter the field calculator. I want to update the temperature of each point. I go in my force for g, get temperature. Now there's a lot of help text here. And it does take some parameter. It's not valid yet.
11:20
So I need to tell it what kind of, if I want it to Fahrenheit, Celsius, or Kelvin. And I just did that with a ticket f. You see, it says 51.8.
11:41
It just does it on the fly. If I put in a c, it returns 12.
12:05
So let's try getting the degree in Fahrenheit for the points. f, OK, of course, I need to tell it which
12:29
column to enter, OK? Now we'll call the open weather map .org for each record and
12:41
updates with the temperature. That's very useful. A lot of data are now exposed through a REST API,
13:01
address data, and methodological data, all sorts of data. Let's just start looking. Now actions, problem. We need to access Google Street View for a given direction.
13:23
Solution, well, put some Python code to get this Google Street View. Let's take a look at that.
13:40
I have a points table here. I go to the actions tab. I have one here called Street View Looking West. There's a Python code. As you see here is the URL for the Street View.
14:03
The y and x is added when I click on the point. And then, well, the data is collected and loaded and shown in an image.
14:20
Not a lot of Python code in here. So what does this look like? Go up to my actions, Street View, Looking West, push.
14:58
I can put it on to, well, Looking East.
15:08
So I thought about how about building a Street View plug-in, but then, let's look at the plug-ins. And actually, a guy already did make a plug-in. And that's, of course, a lot better in my action.
15:34
Now my action is good. It's not working. The other plug-in.
15:42
Yeah, just to recap, we saw the Python init function where the tank generator was used. We saw the expression engine or expression string builder where we were able to add our own methods and a new group to the expressions.
16:01
We've got the user name. We've got the temperature from the service. And we saw QG's actions from Street View. It's also a service. So I like the idea that you, on this quite easy way, can access these REST APIs.
16:51
I saw your scripts, they're pretty well documented. Could you go over how you do that? So that, for example, the temperature thing, you've got
17:01
the little help there to explain the function. It's a XTML syntax that's shown up from here.
17:22
So if we take the expressions, point it out. So all this is, say, it's TML written in the method.
17:49
Yeah. You showed the example when you open a document, you could
18:02
bind to two or three events. The open document event, closed document event, and then you could fire whatever scripts from there. Is there other types of events that you can bind to as well? So if I zoomed into a specific area or after an edit feature, that it will automatically trigger
18:21
something, if I want to do quality control on something. Similar to the tank, I want to run a process every time a feature has been added. Yeah. You can do that. I was just asking if there's other events you can tie into in QGIS, other than the open document, closed document.
18:43
So you could basically have a whole bunch of things to do different, basically, processes. Yeah. You can also throw in a validation code. Like, you need to insert this and this. And you can also put your validation out in the front, in the UI out here.
19:03
Like, if you need to, a tank number has to be between a certain value. You can use a control with a spinnated up and down and within an interval. So you can do a lot of validating out here also. But also in Python, you can validate the input.
19:22
So would it be the kind of thing where you create your own plug-in to have in? Or do you tie into, out of the injection ways of getting Python in, which way would you recommend, basically? I like the expression way, where you extend the expression engine. I like that a lot.
19:46
But I mean, it kind of depends on your needs anymore.
20:03
For this code, no. But I'll add the stuff to the slides. So if you get the slides, you'll get the code. And where do we find the slides? Well, big secret. I'm not sure if Swarovski will compile it up.
20:21
Or I'll put it out at my home page. Email me. Thanks.