This presentation will enlighten the novice Python QGIS user with different ways of running Python code in QGIS without the need of building a QGIS Python plugin. Any QGIS user could start writing small Python scripts for automating, customizing and extending QGIS, making their daily workflow an easier and more fun task to complete.-Python through the QGIS Python Console and Script editor: This would be the most obvious place for PyQGIS newcomers. The console and editor comes with syntax highlighting, autocomplete and easy integration to QGIS.-Scriptrunner: A handy plugin for running Python scripts when objects needs to be instantiated.-Extending the Expression engine: Using a startup.py in your .qgis2 Python folder with a @qgsfunction(0, "Python") attribute. An example is shown adding the name of the current user, to a label on a Print Composer composition.-Run script on project event open, close and save: You may want to validate if a certain table is open, and notify the user if it is not, when opening or saving a QGIS project.-Python in QGIS Actions: Extend your QGIS actions with Python code. -Python Init function: A powerful feature in QGIS when creating new features. You can validate and programmatically edit your attribute input. One can also process the newly digitalized geometry. An example is shown creating rectangular oil tanks with predefined dimension.-Processing Framework: Scripting methods from the Processing Framework. Useful when you need to loop or run a batch of commands from the Processing Framework.
 Yes, my name is Jacob Lenzthorpe. I work at the Danish Geological Survey and Exploration Group. It's called GGE. It's not the official Danish Geological Survey. It's a private company. So I'm going to target non-advanced Python users 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, 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 in actions. I'm going to quickly run through the four on the top. We have the console and editor. The console is in the plug-in menu. There's a script runner plug-in that's useful if you have several scripts you've made and you want to run them and use them as an archive. You can run a piece of Python code when events happens on your project, like open, close, and save. Or here's a Samuilism's sort of code where we need the river layer to be below the lake layer. And you can, like, on the save project event, run your code to check if that's true. New, commerce, QGIS, you'll probably think, well, it's a line layer. It has to be on top of the polygon layer, but not in this case. OK, and then the processing framework is really huge. 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. Like a sample, it's like a 15,000 liter oil tank. 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, perhaps use plug-ins to be able to draw the 90 degrees lines. 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, 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. Now I just put it there for you to get when you get the slides. So let's take a look. So let's try the old oil tank. You see, it could be quite difficult for me to get the right angles and dimensions. So I came up with this one. OK, I draw something. It doesn't matter what it is. And I finish drawing. And up pops this custom form. I can pick the size of the oil tank. I can call it a T1. Now watch this geometry. Take a larger one. Let's call it T2. OK. Then you can move and rotate them. So the place to go. Then you can move and rotate them, so the place to correct place. So how does this work? 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. You can 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. 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. And then grab from the feature, the geometry, this bounding box in the center. That's the point. I get the width and length of the oil tank from a dictionary down here. And then create a new geometry and set the geometry of the feature. And then it's changed. Pretty simple and easy. There's a blog post from Nathan down there that will tell you in depth about how this stuff is working. OK, the expression engine or the expression string builder is something I found many places in QQiz. 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. And they're supposed to change them to their own. Some of them are just, well, at least my initials was out on a lot of maps I didn't have anything to do with. So I thought about adding a new function to the expression engine to be used by a label in the print composer. You all know the expression engine, I guess. So I added a phospho-g group. And I'm now able to get the person who's logged in on the computer. And it's quite easy. I have my code in a user function Py. This is a code. It's just one line of the pattern code, getUser. And it's returned. And I can use that in the print composer. Take a look at that. It's this one. You see, getLugin. And it's just by putting in here. 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? Well, we go out looking for a REST API. We have the open weather. I believe, actually, it's open weather, map.org. Yes, it is. We have the request at the bottom, the link. And it just states a latitude and a longitude. 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 a temperature. And it's in Kelvin. I can also get the pressure, humidity, and the distance to 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. That's already a column made ready for that. Call temp. I will make it edible. I'll enter the field calculator. I want to update the temperature with each point. I go in my phospho G, get temperature. Now, there's a lot of help text here. And it does take some parameter. It's not valid yet. So I need to tell it what kind of, if I wanted to Fahrenheit, Celsius or Kelvin. And I just did that with a ticket F. You see, it says 51.8. It just does it on the fly. If I put in a C, it returns 12. So let's try getting the degree in Fahrenheit for the points. F. OK, of course, I need to tell it which column to end into. Now, we'll call the own weathermap.org for each record and updates with the temperature. That's very useful. A lot of data will, on how exposed through a REST API, address data and your weather logical data, all sorts of data. Let's just start looking. Now, actions, problem, we need to access a Google Street View for a given direction. Solution, well, put some Python code to get this Google Street View. Let's take a look at that. I have a points table here. I go to the actions tab. I have one here called Street View Looking West. There's some Python code. As you see here is the URL for the Street View. 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. Not a lot of Python code in here. So what does this look like? Go up to my actions, Street View Looking West, push. I can put it on. Well, Looking East. So I thought about how about building a Street View plug-in. But then, let's look at the plugins. And right here, a guy already did make a plug-in. And that's, of course, a lot better in my action. Well, now my action is good. It's not working. The other plug-in. 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. We've got the username. We've got the temperature from the service. And we saw QGIS actions from Street View. That's also a service. So I like the idea that you, in this quite easy way, can access this REST APIs. Yeah. I saw your scripts. They're pretty well documented. Could you go over how you do that? So that, like, for example, the temperature thing, you've got the little help there to explain the function. It's a XTML syntax that's shown up from here. So if we take the expressions, point it out. So all this is, say, it's TML written in the method. Yeah. Can I just comment on the function? Yeah. You showed the example when you opened a document. You could 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 something. If I want to do quality control and something similar to the tank, I want to run a process every time a feature has been added. Yeah. You can do that. Yeah. Oh. It's on. Oh, sorry. I was just asking if there's other events you can tie into in QGIS, other than the open document, closed document. So you could basically have a whole bunch of things to do different, basically, processes. Yeah. You can also throw in validation code. And you need to enter this and this. And you can also put your validation out in the front in the UI out here. Like if you need to, a tank number has to be between a certain value, you can use a control that spin it up and down within an interval. So you can do a lot of the validating out here also. But also in Python, you can validate the input. So would you kind of think we 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? I like the expression where you extend the expression engine. I like that a lot. But I mean, it kind of depends on your needs. Any more? For this code? Yeah. Or in clicks? No. But I'll let the stuff to the slides. So if you get the slides, you'll get the code. And where do we find the slides? Well, a big secret. I'm not sure if Swarovski will compile it up or put it out on my home page. You email me. Thanks. Yeah. Thanks.
 Yes, my name is Jacob Lenzthorpe.  I work at the Danish Geological Survey and Exploration Group.  It's called GGE.  It's not the official Danish Geological Survey.  It's a private company.  So I'm going to target non-advanced Python users  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,  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 in actions.  I'm going to quickly run through the four on the top.  We have the console and editor.  The console is in the plug-in menu.  There's a script runner plug-in that's useful if you have  several scripts you've made and you want to run them and use  them as an archive.  You can run a piece of Python code when events happens on  your project, like open, close, and save.  Or here's a Samuilism's sort of code where we need the  river layer to be below the lake layer.  And you can, like, on the save project event, run your code  to check if that's true.  New, commerce, QGIS, you'll probably think, well, it's a  line layer.  It has to be on top of the polygon layer, but not in  this case.  OK, and then the processing framework is really huge.  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.  Like a sample, it's like a 15,000 liter oil tank.  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, perhaps use plug-ins to be able to draw the 90  degrees lines.  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, 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.  Now I just put it there for you to get when you get the  slides.  So let's take a look.  So let's try the old oil tank.  You see, it could be quite difficult for me to get the  right angles and dimensions.  So I came up with this one.  OK, I draw something.  It doesn't matter what it is.  And I finish drawing.  And up pops this custom form.  I can pick the size of the oil tank.  I can call it a T1.  Now watch this geometry.  Take a larger one.  Let's call it T2.  OK.  Then you can move and rotate them.  So the place to go.  Then you can move and rotate them, so the place to correct  place.  So how does this work?  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.  You can 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.  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.  And then grab from the feature, the geometry, this  bounding box in the center.  That's the point.  I get the width and length of the oil tank from a dictionary  down here.  And then create a new geometry and set the geometry of the  feature.  And then it's changed.  Pretty simple and easy.  There's a blog post from Nathan down there that will tell  you in depth about how this stuff is working.  OK, the expression engine or the expression string builder  is something I found many places in QQiz.  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.  And they're supposed to change them to their own.  Some of them are just, well, at least my initials was out on  a lot of maps I didn't have anything to do with.  So I thought about adding a new function to the expression  engine to be used by a label in the print composer.  You all know the expression engine, I guess.  So I added a phospho-g group.  And I'm now able to get the person who's logged in on  the computer.  And it's quite easy.  I have my code in a user function Py.  This is a code.  It's just one line of the pattern code, getUser.  And it's returned.  And I can use that in the print composer.  Take a look at that.  It's this one.  You see, getLugin.  And it's just by putting in here.  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?  Well, we go out looking for a REST API.  We have the open weather.  I believe, actually, it's open weather, map.org.  Yes, it is.  We have the request at the bottom, the link.  And it just states a latitude and a longitude.  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 a temperature.  And it's in Kelvin.  I can also get the pressure, humidity, and the distance  to 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.  That's already a column made ready for that.  Call temp.  I will make it edible.  I'll enter the field calculator.  I want to update the temperature with each point.  I go in my phospho G, get temperature.  Now, there's a lot of help text here.  And it does take some parameter.  It's not valid yet.  So I need to tell it what kind of, if I wanted to Fahrenheit,  Celsius or Kelvin.  And I just did that with a ticket F. You see, it says  51.8.  It just does it on the fly.  If I put in a C, it returns 12.  So let's try getting the degree in Fahrenheit for the points.  F. OK, of course, I need to tell it which column to end into.  Now, we'll call the own weathermap.org for each record  and updates with the temperature.  That's very useful.  A lot of data will, on how exposed through a REST API,  address data and your weather logical data, all sorts of data.  Let's just start looking.  Now, actions, problem, we need to access a Google Street View  for a given direction.  Solution, well, put some Python code to get this Google  Street View.  Let's take a look at that.  I have a points table here.  I go to the actions tab.  I have one here called Street View Looking West.  There's some Python code.  As you see here is the URL for the Street View.  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.  Not a lot of Python code in here.  So what does this look like?  Go up to my actions, Street View Looking West, push.  I can put it on.  Well, Looking East.  So I thought about how about building a Street View plug-in.  But then, let's look at the plugins.  And right here, a guy already did make a plug-in.  And that's, of course, a lot better in my action.  Well, now my action is good.  It's not working.  The other plug-in.  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.  We've got the username.  We've got the temperature from the service.  And we saw QGIS actions from Street View.  That's also a service.  So I like the idea that you, in this quite easy way, can access  this REST APIs.  Yeah.  I saw your scripts.  They're pretty well documented.  Could you go over how you do that?  So that, like, for example, the temperature thing, you've got  the little help there to explain the function.  It's a XTML syntax that's shown up from here.  So if we take the expressions, point it out.  So all this is, say, it's TML written in the method.  Yeah.  Can I just comment on the function?  Yeah.  You showed the example when you opened a document.  You could 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 something.  If I want to do quality control and something similar to the  tank, I want to run a process every time a feature has been  added.  Yeah.  You can do that.  Yeah.  Oh.  It's on.  Oh, sorry.  I was just asking if there's other events you can tie into  in QGIS, other than the open document, closed document.  So you could basically have a whole bunch of things to do  different, basically, processes.  Yeah.  You can also throw in validation code.  And you need to enter this and this.  And you can also put your validation out in the front in  the UI out here.  Like if you need to, a tank number has to be between a  certain value, you can use a control that spin it up and  down within an interval.  So you can do a lot of the validating out here also.  But also in Python, you can validate the input.  So would you kind of think we 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?  I like the expression where you extend the  expression engine.  I like that a lot.  But I mean, it kind of depends on your needs.  Any more?  For this code?  Yeah.  Or in clicks?  No.  But I'll let the stuff to the slides.  So if you get the slides, you'll get the code.  And where do we find the slides?  Well, a big secret.  I'm not sure if Swarovski will compile it up or put it out  on my home page.  You email me.  Thanks.  Yeah.  Thanks.
03_Jakob_Lanstorp_720p_8688_9203.png 03_Jakob_Lanstorp_720p_10812_12995.png 03_Jakob_Lanstorp_720p_12996_13055.png 03_Jakob_Lanstorp_720p_13056_13163.png 03_Jakob_Lanstorp_720p_13524_14147.png 03_Jakob_Lanstorp_720p_14148_15827.png