Shortly after I spent hours in Processing figuring out how to map coordinates onto an image, someone mentioned that what I REALLY want to be using is QGIS.
QGIS is to ArcGIS as Blender is to Maya and Gimp is to Photoshop. It’s open-source. Completely free for us broke folks. And it speaks Python.
So by way of introduction, I’m going to walk through a few basic commands to get me going.
Add a Base Map
You may notice that upon opening QGIS, you will be faced with a completely blank screen. Don’t panic. You’ve just got to add in a map background.
The Ireland QGIS User Group Blog actually posted the code to get ESRI’s ArcGIS Online World Imagery to make it really easy:
qgis.utils.iface.addRasterLayer("http://server.arcgisonline.com/arcgis/rest/services/ESRI_Imagery_World_2D/MapServer?f=json&pretty=true","raster")
Once that’s pasted in, hit return, and you should see this:
Add Some Data
Not so long ago, I posted a script to scrape Airbnb data into a csv file. I haven’t been checking Airbnb’s HTML to make sure it hasn’t gotten screwier since I wrote said scraper. Because I’m weird, but not that weird. I still have the last csv file I scraped, so I’m using that. I was going to upload it here, but apparently WordPress doesn’t support csv uploads (Why WordPress? Are csvs not good enough for you?? And what the heck is a .3g2? Why can I upload that?) for reasons I’m not clear on.
Anyway. Back on topic.
Under Layer, go to Add Layer, and click on Add Delimited Text Layer. Choose your file, make sure you don’t mix up your X and Y values (or your points may end up in Antarctica), and let it fly.
Zooming into your layer, you should see something like this:
Now we’ve got a something to work with.
Throw in Some Python
Say I wanted to buffer those locations. Digital Geography has an awesome tutorial on how to do this which I’m going to summarize here.
First, you have to import the processing library to get access to the fixeddistancebuffer function.
import processing
Now, to keep life easy, we’re going to assign the active layer to a variable called layer.
layer = iface.activeLayer()
Here’s the template for the actual function:
processing.runalg(“qgis:fixeddistancebuffer”, LayerToBuffer, Distance, Segment, Dissolve True/False,”C:/Filepath.shp“)
Since our active layer is stored in the variable layer–we can just type .name() and trust QGIS to fill in the name. For example:
processing.runalg("qgis:fixeddistancebuffer",layer.name(),0.1,10,False,"C:/Users/Desktop/TestBuffer.shp")
That will save your buffer geometry to a shape file called TestBuffer. If you’re following along at home, you may have noticed that no shapes appeared on the map as a result. Don’t start verbally abusing QGIS yet. There’s two more lines:
newLayer=QgsVectorLayer('C:/Users/Desktop/TestBuffer.shp', "Buffer Layer", "ogr")
That one actually LOADS the layer, which is important, and assigns it to a variable which can be used to add it to the Map Layer Registry.
QgsMapLayerRegistry.instance().addMapLayer(newlayer)
Presto. Buffer layer.
Run a Loop
Say you want multiple buffer layers at different distances. You can push the lines that create and load the shapefile into a while loop. Then assign the distance value to a variable that increases with each iteration. Like so:
x=.001 while x < .01: fp="C:/Users/Desktop/Buffer" + str(x) +".shp" processing.runalg("qgis:fixeddistancebuffer", layer.name(), x, 10, False, fp) newlayer=QgsVectorLayer(fp, "Buffer Layer", "ogr") QgsMapLayerRegistry.instance().addMapLayer(newlayer) x+=.001
You can even use the buffer distance as part of the file nameāif you convert the float value to a string using str().
The result should look something like this:
Which you can then manipulate to get some animation going like the gif in my header or like this one.
More QGIS to come.
I didn’t know about QGIS. Thanks š
LikeLiked by 1 person
Very good post I’ll have to give QGIS a try.
LikeLiked by 1 person
Thank you kindly, always nice to hear š
LikeLike
Nice post (… and blog)! You may also have a look at one similar post I wrote about the buffering of vectors. Even if thought for the creation of buffer from fields (it could be easily adapted to your case), it explains how to get buffers without recurring to the processing library: in this way, the execution time of the whole process will be shorter, mainly when dealing with many features.
LikeLiked by 1 person
YOUR BLOG IS AMAZING. I’m definitely trying out some of that code this weekend. Thanks for posting š
LikeLike
Hey that’s really cool. How are you manipulating the buffers to create the .gif?
LikeLike
After creating the layers, I exported them to .jpgs, which I then plopped into GIMP and saved as a .gif. Photoshop would work too! š
LikeLike
awesome thanks. I’ll give that a go and link to your post when I write it up. cheers lucas
LikeLiked by 1 person