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:
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.
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:
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.
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.