LecoS – Land cover statistics
I proudly present my first own QGIS-plugin. I had funny and bad times coding this piece of software. I hope it will prove useful to some open source GIS users out there. If not, then at least i learned a lot about coding plugins and landscape metrics. Please notice that this piece of software is still experimental and the author takes no responsibility for bugs or miscalculations.
Description:
LecoS – Land cover statistics is a plugin for the QGIS GIS software suite. It converts classified raster layers to arrays using the powerful numpy library. Based on a Connected Component Labeling approach it further identifies class patches and calculates landscape metrics. The user can choose to calculate single or several metrics for the raster classes. Recent versions of LecoS also include a polygon-overlay (BatchOverlay) tool, which allows the calculation of landscape metrics per polygon feature. I also created the Landscape Modifier, a nifty tool to extract edges, increase or decrease or close holes in raster-patches.
Example use case:
-> Download the current CORINE 2006 dataset (and the provided qgis colourmap) and load them into QGIS.
-> Crop to an area of interest. In my case i quickly draw a polygon over the city of Marburg, Germany and croped the landcover raster via the gdalwarp utility using the polygon as mask layer. The CORINE nodata value is -999. See the result below.
-> Now open the LecoS Land cover statistic gui via the menu (Raster/Landscape Ecology/Land cover statistics). Choose the new croped raster layer and check if the cellsize is correct (CORINE has a spatial resolution of 100m per cell). You can choose “Direct value output” just for information (in a small dialog) or save the results in a csv file for further processing.
As for me i want to save the results and calculate the following metrics from the “Select multiple Metrics” tab:
-> Now run the program and import the results in a spreadsheet software of your choice (Excel, Libreoffice).
The class values represent the grid values in your raster layer, while the other columns are the metrics you calculated. You can lookup the class values in the clc2006 legend table. As you can see in my example class number 12 (Non-irrigated arable land) is the most abundant land cover in the vicinity of Marburg. It makes up nearly 38 % of all cover types and is represented by 28 unique patches in the croped raster layer.
Example use case for the PolygonOverlay tool -> Here and here (updated Gui)
Example use case for the Landscape Modifier -> Here
Download:
You can find the plugin via the QGIS-Plugin downloader. The plugin is also downloadable from the QGIS plugin hub. Usage requires python and the libraries numpy, scipy and PIL installed (See Install).
Install:
Install via the QGIS Plugin Downloader or manual. Manual means that you download the Archiv and extract the folder to the QGIS Plugin dir (~/.qgis/python/plugins/ on Linux machines and (qgis_install folder)/python/plugins on Windows). In both cases you need to have the scientific libraries numpy and scipy installed!
If you don’t install them (default is “no” on Windows), than you’ll likely see error messages after startup. To install the libraries on Linux systems, just download them with your package manager (python-scipy, python-numpy, python-imaging) or compile them system wide. To install them on Windows download the OSGEO4W Installer , select advanced Install, search for the libraries python-numpy, python-scipy and python-imaging and check them all (besides checking the qgis binaries).
LecoS was tested under Linux Debian (Wheezy testing) and Windows XP (SP 3). If the necessary libraries are installed it should run out of the box for both QGIS 1.8 stable (Lisboa) and QGIS dev. (Master).
Errors and Bugs:
If you stumble upon any errors PLEASE don’t report them in this blog as i really lose track of all those comments. Use the official bug-tracker to report any bugs while using LecoS and try to give me as much information about your system and QGIS setup (including package versions and maybe a small data subset for testing).
Current version: 1.7.3
I will continuously adapt and improve the QGIS-plugin in the future, if i have the time for it. Please reply below this post, if you have any ideas of improvement.
See my blog posts below for more infos about the new versions
- Version 1.1
- Version 1.2
- Version 1.3
- Version 1.4 to 1.5.1 (Bugfixes and minor edits)
- Version 1.6
- Version 1.7
The development of recent versions of this plugin has been supported by the University of Évora and future version will include the options to analyze landscape vector layers, additional metrics and post-hoc result grouping.
47 Responses to “LecoS – Land cover statistics”
Trackbacks / Pingbacks
- - December 15, 2012







Please have a look to r.li module in GRASS. Moving window metrics are especially useful IMHO.
Thanks a lot!
Also, please consider adding it as a SEXTANTE plugin or module: it will be very useful to use in in more complex modelling.
Thanks again.
Can you provide me with some sort of short tutorial or introduction how to add the plugin to sextante?
You can have a look to the plugin SEXTANTE example provider, it should be clear enough. I warmly invite you however to discuss this on the qgis-dev mailing list, as other people will be interested, and could collaborate.
Thanks.
Great work! Some isolation metrices would be also very useful.
Indeed! I wanted to realize them but i couldn’t get the calculation of the patch perimeters (which is the basis of many indices) to work. Maybe later, when i have the time to code
Hi.
Please add your plugin to the Raster menu and toolbar, rather than to the generic one(it will be easier to find it). Also, the info button is somewhat non-standard, please follow other plugins.
Also, your paricipation to qgis-dev mailing list will make cooperation much easier than posting here.
Thanks a lot!
Heyho, i think i added it to the raster menu, but it only appears there if you use a qgis version above 1.8. In my version (1.9 trunk ) it appears in the raster menu and toolbar. But i will change that as well as the info button. I will try to participate in the list. Hopefully i don’t get flooded by emails all over
Please, could you write how to install the plugin without “plugin installer”?
I do not how to do this.
added to the text above
You will get a lot of mail from qgis-dev, so I suggets you to filter it. Nevertheless, it is very worhtwhile. Dispersing information is no good.
You do not need to instal it by hand, use the repo.
Please, could you write the link to the right repo for LecoS?
repo aka repository means you could install the plugin directly in qgis via the plugin downloader
Hi, I tried to install LecoS on W7 64 bits and got a message saying that I need scipy. I looked for it in osgeo4w but didnt find it there. What should I do? Tks
i added that information to the install note in the text above. Basically you’ll need to install scipy/numpy (which are very useful python extension). to your python folder. You will find all necessary information on the numpy homepage (http://numpy.scipy.org/)
H, thanks. Can you please point me the file that I need to download?. I only found installers that require me to have python 2.7 installed. The python that QGIS uses (and the only one I have) is in c:/osgeo
Uhh, that completely depends on your python version and system. I am only using linux (where python and numpy/scipy are installed and used system wide). I don’t know how to proceed with some custom install under windows. You should download the current archives from sourceforge (http://sourceforge.net/projects/numpy/files/ and http://sourceforge.net/projects/scipy/files/ ) and get your python (which qgis uses) to recognize these libraries. Maybe ask someone from the osgeo development team how to install additional libraries
Sorry, I think it is rather pointless to discuss basic QGIS issues here. I would suggest to go to the main QGIS resources, especially the mailing list
Hi
with Qgis 1.8.0 in Windows, after followig the text above, I got this error:
Couldn’t load plugin LecoS due an error when calling its classfactory()
method
Traceback (most recent call last):
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py”, line 164, in startPlugin
plugins[packageName] = package.classFactory(iface)
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins\LecoS\__init__.py”, line 35, in classFactory
from lecos_main import LecoS
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py”, line 309, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins\LecoS\lecos_main.py”, line 29, in
from lecos_dlg import LecosDialog
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py”, line 309, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins\LecoS\lecos_dlg.py”, line 35, in
import landscape_statistics as lcs
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py”, line 309, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins\LecoS\landscape_statistics.py”, line 29, in
import gdal, numpy, sys, scipy, string, math, ogr, os
File “C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python\qgis\utils.py”, line 309, in _import
mod = _builtin_import(name, globals, locals, fromlist, level)
ImportError: No module named scipy
Versione Python:
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
Versione di QGIS:
1.8.0-Lisboa Lisboa, 6416f38
What can I do?
Just do what your output is reporting (last line: ImportError). Install numpy and scipy! Search the web for any how tos
Is there a bugtracker available for this? The plugin has to be fixed due to the API change.
An error has occured while executing Python code:
Traceback (most recent call last):
File “/home/paolo/.qgis//python/plugins/LecoS/lecos_main.py”, line 108, in runBatch
dlg = BatchDialog( self.iface )
File “/home/paolo/.qgis//python/plugins/LecoS/lecos_dlg.py”, line 400, in __init__
self.startup()
File “/home/paolo/.qgis//python/plugins/LecoS/lecos_dlg.py”, line 416, in startup
ras = func.getRasterLayersNames()
File “/home/paolo/.qgis//python/plugins/LecoS/lecos_functions.py”, line 133, in getRasterLayersNames
if layer.type() == QgsMapLayer.RasterLayer and ( layer.usesProvider() and layer.providerKey() == ‘gdal’ ):
AttributeError: ‘QgsRasterLayer’ object has no attribute ‘usesProvider’
Hello Curlew, this is Thomas. So can’t get your plugin to work – my python imaging library is 1.1.7-4. I’m using QGIS 1.8 in Linux Mint 13. Error report here: http://www.replicamag.co.uk/error_msg.txt
Thanks, for providing the data. mhh, the reason because it doesn’t work definitely has something to do with your vector shapes. If you make a test-polygon somewhere on your raster, the polygon overlay works (doesn’t appear in the attribute-table until reloading). I also resampled your raster (and set a no-data value) to make sure the error doesn’t come from the raster. I’ll suggest to somehow explode or simplify your polygons and try again. I’ll look into the reason for this error in the mean-time and report back as soon as i find something (will upload a fix to plugin downloader).
EDIT:
I also got it to work with lecos. First you have to define the value zero as correct no-data value for your raster. Then use the “polygon parts to separate polygons” SAGA module in the Sextante toolbox or a similar tool to explode your polygon features into separate parts. Now you can slowly work through the parts of the features with lecos. However i advise you to use SAGA in the mean time. Maybe i can get a clue why lecos fails (i believe it has something to do with multiple hoverlapping inner and outer polygon rings).
Hi Curlew
first, thanks a lot for the great work. I followed your descriptions, but receive the following error message when I try to open the Lecos menu. Any help would be really appreciated.
cheers
Klaus
sorry, here is the message
Traceback (most recent call last):
File “C:/PROGRA~1/Quantum GIS Lisboa/apps/qgis/./python/plugins\LecoS\lecos_dlg.py”, line 105, in setNoData
self.NoDataVal.setValidator(QDoubleValidator(-999999,999999,5))
TypeError: arguments did not match any overloaded call:
QDoubleValidator(QObject parent=None): argument 1 has unexpected type ‘int’
QDoubleValidator(float, float, int, QObject): not enough arguments
Python-Version:
2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
QGIS-Version:
1.8.0-Lisboa Lisboa, f440b60
Hej, this error appears, because you’ve quite a old version of QT shipping along with your QGis version. Can you make a screenshot of the first tab from the About windows (Menu -> About (Über)) and upload it to imgur ? I will to make a bugfix as soon as possible.
Hi, the plugin is not working anymore in QGIS 1.9 (due to API change perhaps, also mentioned in another comment already). Error message is: ‘module’ object has not attribute ‘AllRegister’
Mhh, AllRegister is only mentioned once in every python-code in the beginning
gdal.AllRegister() # register all gdal drivers
The plugin itselfs works for me without any problems (QGis nightlybuild – Debian 64bit, Gdal-version 1.9.0). Maybe you’re using a newer gdal-version where this register-command isn’t necessary anymore
When I installed the plugin I got the following message: “The gdal driver register command failed. LecoS might still work, but there is a chance of non working gdal file support.” When I click Ok, a new window appear with this python error message: “module object has no attribute UseExceptions” Have you ever met this problem? ps.: I’have used the LeCos with 1.8 QGIS and it worked perfectly. I wrote a user manual in hungarian also, but now the plugin doesn’t work. My OS: linux mint14, QGIS ver.: 1.9. Thank you in advance for your answer.
ahh, this was actually a fix for another user. I’ll try to look into it as soon as possible. For now you can simply comment this lines out (paste a #) before line 38 and 39 in the landscape.statistics.py file (look into your python folder ~/.qgis/python/plugins/LecoS )
EDIT:
The Error obviously comes up because of recent changes in the GDAL library. I’m also using the Development Version as well, but GDAL-version 1.9.0
I build a try-catch frame around this register-command, so download LecoS again (still has the same version number) and try it out.
Thank you for your quick reply. I tried your suggestions, and I got the following error message: “gdal.AllRegister() # register all gdal drivers
AttributeError: ‘module’ object has no attribute ‘AllRegister’” I think I will reinstall the 1.8 version.
I also have szemanistvan’s error…but on 1.8. Running QGIS 1.8 on Ubuntu 12.04 with python-gdal 1.9.2, I get “gdal.AllRegister() # register all gdal drivers AttributeError: ‘module’ object has no attribute ‘AllRegister’” as well.
I cannot find LecoS at the QGIS plugin installer (QGIS 1.8.0 for Windows)
It is an experimental plugin and comes without any warranty. You have to enable the option to allow experimental plugins to appear in the download list (Plugin-Downloader Options).
Thank you verym uch. I installed the plug-in but is asking me for scypy, which I installed but probably not in the right directory, How can I install scipy to run LecoS?
Right now there is an error occurring within the QGis plugin upload server, so i can’t upload any new updates and i will just say the following:
- All the errors related to GDAL don’t occur at my system. I am using gdal version 1.9.0. If your’re using something never than this you are probably are affected by some kind of recent API-change. Try to look into the error. It names the row and the file where the error happens. Go to the row and insert a “#” before the code (makes a comment). Or wait for the update certainly to be uploaded in the near feature (with some nice functions) and see if the error still occurs (in this case i need some professional advice).
Thanks for the great plugin! It took me a bit to get it running because of path issues. The problem I was having probably has a more elegant solution, but here is my work around that I thought I would pass along.
Problem: I have a windows XP machine, with another copy/version of Python (2.7.3) on it at ‘C:\Python27′. The ‘site-package’ directory for this installation already has numpy and scipy and dependencies loaded and working well. When I recently loaded QGIS, it came with it’s own copy of Python (2.7.2), along with ‘site-packages’ that included numpy but not scipy. I tried to ‘install’ another copy of scipy in this new site-packages directory by unzipping the tarball there. Sometimes this seems to work for a module, but it didn’t work for this one. I guess I needed to run ‘python setup.py install’, but avoided it because I didn’t want to screw up my paths. I guess there may be additional options for installing it in a non-standard location, but I was too uncertain to do it. I avoided the scipy Windows installer, because it didn’t see the QGIS python installation and didn’t give me the option to install it under QGIS and I didn’t want to screw up my paths.
Hack: So here is what I did. I went into the ‘C:\Program Files\Quantum GIS Lisboa\apps\Python27\Lib’ folder and found the site.py file. Right below the import statements, I found
# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
and I changed it to read
PREFIXES = [sys.prefix, sys.exec_prefix, 'C:\\Python27\\Lib\\site-packages']
Now the QGIS python installation is able to find my original scipy module and it seems to be working fine. Presumably, other QGIS plugins will be able to take advantage of the many other modules I have installed in this directory, also.
Hope this is useful for others. If anything, maybe it will prompt someone to describe a more elegant solution.
Best,
Tim
Great ! Thanks for testing. I always have issues with Windows systems because i don’t own one and therefore can’t do testing on it . On Linux the python path for QGIS is the same as for the system-wide install.
first, thanks a lot for the great work.
Get the following message when I try in LecoS 1.6.1, in Landcover Analysis to Save results as .csv
> An error has occured while executing Python code:
>
> Traceback (most recent call last):
> File “/home/gtsil/.qgis//python/plugins/LecoS/lecos_dlg.py”, line 250, in accept
> if path.exists(self.SaveCsv.text()):
> NameError: global name ‘path’ is not defined
>
> Python version:
> 2.7.3 (default, Aug 1 2012, 05:27:35)
> [GCC 4.6.3]
>
>
> QGIS version:
> 1.8.0-Lisboa Lisboa, exported
>
> Python path: ['/home/gtsil/.qgis//python/plugins/LecoS', '/home/gtsil/.qgis//python/plugins/sextante_animove', '/home/gtsil/.qgis//python/plugins/sextante_taudem', '/home/gtsil/.qgis//python/plugins/sextante', '/home/gtsil/.qgis//python/plugins/permaclim', '/home/gtsil/.qgis//python/plugins/sextantelwgeomprovider', '/home/gtsil/.qgis//python/plugins/sextanteexampleprovider', '/usr/share/qgis/python', '/home/gtsil/.qgis//python', '/home/gtsil/.qgis//python/plugins', '/usr/share/qgis/python/plugins', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode', '/home/gtsil/.qgis/python/plugins/mmqgis/forms',
'/usr/share/qgis/python/plugins/fTools/tools', '/home/gtsil/Desktop/Grassdata/Location_LandScape/MP_Typology/Project']
>
>
What can I do?
First and this is for all you people:
Please try to report future bugs on the official bug-tracker –> http://hub.qgis.org/projects/lecos/issues?set_filter=1
This blog page becomes a bit crowded.
Second:
Yeah, this is a short bug, because i somehow deleted the import of the path module. You can fix the bug yourself, if you open the named python file “lecos_dlg.py” and insert the following somewhere in the import section (line 22 and following)
“from os import path”
Thanks !!!
Hello,
I think this plugin will be very useful for many people. Congratulations! How we should cite this software? Maybe you should write a paper to one of the (landscape) ecological journals to promote this tool?
Hey, thanks that you appreciate my work
In fact i am considering of writing a little short communication as soon as the plugin reached a stable state (right now it is still experimental and bugs can occur).
If you still wanna use it in a publication than you could use this provisional reference (can also be found in the about dialog):
Martin Jung, 2012, LecoS – A QGIS plugin to conduct landscape ecology statistics, http://plugins.qgis.org/plugins/LecoS/
Hi, I have a Phyton error running in a clipped area of CORINE 2006 data set. Can you help me?
I am on W7 64BITS
Phyton Error:
An error has occured while executing Python code:
Traceback (most recent call last):
File “C:/Users/GIS 02/.qgis//python/plugins\LecoS\lecos_dlg.py”, line 327, in accept
name, result = cl_analys.execSingleMetric(m,cl)
File “C:/Users/GIS 02/.qgis//python/plugins\LecoS\landscape_statistics.py”, line 196, in execSingleMetric
return unicode(name), self.f_returnAvgShape(self.labeled_array,self.cl_array,self.numpatches)
File “C:/Users/GIS 02/.qgis//python/plugins\LecoS\landscape_statistics.py”, line 385, in f_returnAvgShape
feature = self.f_returnPatch(labeled_array,i)
File “C:/Users/GIS 02/.qgis//python/plugins\LecoS\landscape_statistics.py”, line 238, in f_returnPatch
feature = numpy.zeros_like(labeled_array, dtype=int)
TypeError: zeros_like() got an unexpected keyword argument ‘dtype’
Python version:
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
QGIS version:
1.8.0-Lisboa Lisboa, 6416f38
Python path: ['C:/Users/GIS 02/.qgis//python/plugins\\LecoS', 'C:/Users/GIS 02/.qgis//python/plugins\\sextante', 'C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python', 'C:/Users/GIS 02/.qgis//python', 'C:/Users/GIS 02/.qgis//python/plugins', 'C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins', 'C:\\Windows\\system32\\python27.zip', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\DLLs', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\plat-win', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\lib-tk', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\qgis\\bin', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\PIL', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\win32', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode', 'C:\\PROGRA~2\\Quantum GIS Lisboa\\apps\\qgis\\python\\plugins\\fTools\\tools']
Best regards
Carlos Ríos
Hi Carlos,
please as i said in the page text: raise a bug-report for those errors in the future (http://hub.qgis.org/projects/lecos) as i really lose track of comments and other people might benefit more if they can search for a bug and see if other people had the same problem.
Regarding your problem:
The actual error (TypeError: zeros_like() got an unexpected keyword argument ‘dtype’) says that a provided parameter is not known to this numpy function. As this parameter certainly exists i advise you to update your numpy library. You can get the current version used in QGIS by typing (“import numpy” and “numpy.version.version”) into the python console. Mine is 1.6.2 and it definetly works here
But please move future correspondence (if upgrading don’t solve your problem) to a bug-report on the official tracker.