diff --git a/Landuse_classification.ipynb b/Landuse_classification.ipynb deleted file mode 100644 index 07c61e1..0000000 --- a/Landuse_classification.ipynb +++ /dev/null @@ -1,4765 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# TODO list\n", - "\n", - "- Add the following metrics:\n", - " 1. Density of built floor area (sum of built pixels' height / area of block)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "
\n", - "
Table of contents
\n", - "
" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "
" - ] - }, - { - "cell_type": "code", - "execution_count": 109, - "metadata": {}, - "outputs": [ - { - "data": { - "application/javascript": [ - "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "%%javascript\n", - "$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "

Define working environment

\n", - "\n", - "The following cells are used to :\n", - "- Set the environment variables for Python and GRASS GIS and R statistical computing \n", - "- Define the [\"GRASSDATA\" folder](https://grass.osgeo.org/grass73/manuals/helptext.html), the name of \"location\" and \"mapset\" where you will to work." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Import libraries**" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "## Import libraries needed for setting parameters of operating system \n", - "import os\n", - "import sys" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Set 'Python' and 'GRASS GIS' environment variables**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Here, we set [the environment variables allowing to use of GRASS GIS](https://grass.osgeo.org/grass64/manuals/variables.html) inside this Jupyter notebook. Please change the directory path according to your own system configuration. Here after are the environment variables defined on a Linux Mint system." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "### Define GRASS GIS environment variables for LINUX UBUNTU Mint 18.1 (Serena)\n", - "# Check is environmental variables exists and create them (empty) if not exists.\n", - "if not 'PYTHONPATH' in os.environ:\n", - " os.environ['PYTHONPATH']=''\n", - "if not 'LD_LIBRARY_PATH' in os.environ:\n", - " os.environ['LD_LIBRARY_PATH']=''\n", - "# Set environmental variables\n", - "os.environ['GISBASE'] = '/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu'\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'bin')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'script')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'lib')\n", - "#os.environ['PATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'etc','python')\n", - "os.environ['PYTHONPATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'etc','python')\n", - "os.environ['PYTHONPATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'etc','python','grass')\n", - "os.environ['PYTHONPATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'etc','python','grass','script')\n", - "os.environ['PYTHONLIB'] = '/usr/lib/python2.7'\n", - "os.environ['LD_LIBRARY_PATH'] += os.pathsep + os.path.join(os.environ['GISBASE'],'lib')\n", - "os.environ['GIS_LOCK'] = '$$'\n", - "os.environ['GISRC'] = os.path.join(os.environ['HOME'],'.grass7','rc')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['HOME'],'.grass7','addons')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['HOME'],'.grass7','addons','bin')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['HOME'],'.grass7','addons')\n", - "os.environ['PATH'] += os.pathsep + os.path.join(os.environ['HOME'],'.grass7','addons','scripts')\n", - "\n", - "## Define GRASS-Python environment\n", - "sys.path.append(os.path.join(os.environ['GISBASE'],'etc','python'))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Import GRASS Python packages**" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "## Import libraries needed to launch GRASS GIS in the jupyter notebook\n", - "import grass.script.setup as gsetup\n", - "\n", - "## Import libraries needed to call GRASS using Python\n", - "import grass.script as gscript\n", - "from grass.script import core as grass" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Display current environment variables of your computer**" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "MDMSESSION = mate \t\n", - "MANDATORY_PATH = /usr/share/gconf/mate.mandatory.path \t\n", - "MATE_DESKTOP_SESSION_ID = this-is-deprecated \t\n", - "LESSOPEN = | /usr/bin/lesspipe %s \t\n", - "MDM_LANG = fr_BE.UTF-8 \t\n", - "LOGNAME = tais \t\n", - "USER = tais \t\n", - "HOME = /home/tais \t\n", - "XDG_VTNR = 9 \t\n", - "PATH = /usr/local/bin:/home/tais/BIN:/home/tais/bin:/home/tais/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/bin:/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/script:/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/lib:/home/tais/.grass7/addons:/home/tais/.grass7/addons/bin:/home/tais/.grass7/addons:/home/tais/.grass7/addons/scripts \t\n", - "CLICOLOR = 1 \t\n", - "DISPLAY = :0.0 \t\n", - "SSH_AGENT_PID = 5974 \t\n", - "LANG = fr_BE.UTF-8 \t\n", - "TERM = xterm-color \t\n", - "SHELL = /bin/bash \t\n", - "GIS_LOCK = $$ \t\n", - "XAUTHORITY = /home/tais/.Xauthority \t\n", - "SESSION_MANAGER = local/tais-HP-Z620-Workstation:@/tmp/.ICE-unix/5837,unix/tais-HP-Z620-Workstation:/tmp/.ICE-unix/5837 \t\n", - "SHLVL = 1 \t\n", - "QT_LINUX_ACCESSIBILITY_ALWAYS_ON = 1 \t\n", - "INSIDE_CAJA_PYTHON = \t\n", - "QT_ACCESSIBILITY = 1 \t\n", - "LD_LIBRARY_PATH = :/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/lib \t\n", - "COMPIZ_CONFIG_PROFILE = mate \t\n", - "WINDOWPATH = 9 \t\n", - "GTK_OVERLAY_SCROLLING = 0 \t\n", - "PYTHONPATH = :/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/etc/python:/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/etc/python/grass:/home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu/etc/python/grass/script \t\n", - "GISBASE = /home/tais/SRC/GRASS/grass_trunk/dist.x86_64-pc-linux-gnu \t\n", - "CLUTTER_BACKEND = x11 \t\n", - "USERNAME = tais \t\n", - "XDG_SESSION_DESKTOP = mate \t\n", - "GDM_XSERVER_LOCATION = local \t\n", - "XDG_RUNTIME_DIR = /run/user/1000 \t\n", - "JPY_PARENT_PID = 7955 \t\n", - "QT_STYLE_OVERRIDE = gtk \t\n", - "SSH_AUTH_SOCK = /run/user/1000/keyring/ssh \t\n", - "VTE_VERSION = 4205 \t\n", - "GDMSESSION = mate \t\n", - "GISRC = /home/tais/.grass7/rc \t\n", - "GIT_PAGER = cat \t\n", - "XDG_CONFIG_DIRS = /etc/xdg/xdg-mate:/etc/xdg \t\n", - "XDG_CURRENT_DESKTOP = MATE \t\n", - "XDG_SESSION_ID = c21 \t\n", - "DBUS_SESSION_BUS_ADDRESS = unix:abstract=/tmp/dbus-oiw1S789SI,guid=e626cdc47bce079de737e4fe5a3fcda7 \t\n", - "_ = /usr/local/bin/jupyter \t\n", - "XDG_SESSION_COOKIE = 8441891e86e24d76b9616edf516d5734-1514130855.90561-444848216 \t\n", - "DESKTOP_SESSION = mate \t\n", - "WINDOWID = 56623110 \t\n", - "LESSCLOSE = /usr/bin/lesspipe %s %s \t\n", - "DEFAULTS_PATH = /usr/share/gconf/mate.default.path \t\n", - "MPLBACKEND = module://ipykernel.pylab.backend_inline \t\n", - "MDM_XSERVER_LOCATION = local \t\n", - "GTK_MODULES = gail:atk-bridge \t\n", - "XDG_DATA_DIRS = /usr/share/mate:/usr/local/share/:/usr/share/:/usr/share/mdm/ \t\n", - "PWD = /media/tais/data/Dropbox/ULB/MAUPP/Traitements/Landscape_metrics/r.li/Landuse_mapping \t\n", - "COLORTERM = mate-terminal \t\n", - "PYTHONLIB = /usr/lib/python2.7 \t\n", - "LS_COLORS = rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36: \t\n", - "PAGER = cat \t\n", - "XDG_SEAT = seat0 \t\n" - ] - } - ], - "source": [ - "## Display the current defined environment variables\n", - "for key in os.environ.keys():\n", - " print \"%s = %s \\t\" % (key,os.environ[key])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "

Define functions

" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This section of the notebook is dedicated to defining functions which will then be called later in the script. If you want to create your own functions, define them here." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for computing processing time" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The \"print_processing_time\" is used to calculate and display the processing time for various stages of the processing chain. At the beginning of each major step, the current time is stored in a new variable, using [time.time() function](https://docs.python.org/2/library/time.html). At the end of the stage in question, the \"print_processing_time\" function is called and takes as argument the name of this new variable containing the recorded time at the beginning of the stage, and an output message." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "## Import library for managing time in python\n", - "import time \n", - "\n", - "## Function \"print_processing_time()\" compute processing time and printing it.\n", - "# The argument \"begintime\" wait for a variable containing the begintime (result of time.time()) of the process for which to compute processing time.\n", - "# The argument \"printmessage\" wait for a string format with information about the process. \n", - "def print_processing_time(begintime, printmessage): \n", - " endtime=time.time() \n", - " processtime=endtime-begintime\n", - " remainingtime=processtime\n", - "\n", - " days=int((remainingtime)/86400)\n", - " remainingtime-=(days*86400)\n", - " hours=int((remainingtime)/3600)\n", - " remainingtime-=(hours*3600)\n", - " minutes=int((remainingtime)/60)\n", - " remainingtime-=(minutes*60)\n", - " seconds=round((remainingtime)%60,1)\n", - "\n", - " if processtime<60:\n", - " finalprintmessage=str(printmessage)+str(seconds)+\" seconds\"\n", - " elif processtime<3600:\n", - " finalprintmessage=str(printmessage)+str(minutes)+\" minutes and \"+str(seconds)+\" seconds\"\n", - " elif processtime<86400:\n", - " finalprintmessage=str(printmessage)+str(hours)+\" hours and \"+str(minutes)+\" minutes and \"+str(seconds)+\" seconds\"\n", - " elif processtime>=86400:\n", - " finalprintmessage=str(printmessage)+str(days)+\" days, \"+str(hours)+\" hours and \"+str(minutes)+\" minutes and \"+str(seconds)+\" seconds\"\n", - " \n", - " return finalprintmessage" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for creation of configuration file for r.li (landscape units provided as polygons) (multiprocessed)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "##### Function that create the r.li configuration file for a list of landcover raster.\n", - "### It enable to create in one function as many configuration file as the number of raster provided in 'listoflandcoverraster'.\n", - "### It could be use only in case study with a several landcover raster and only one landscape unit layer.\n", - "### So, the landscape unit layer if fixed and there are the landcover raster which change. \n", - "# 'listoflandcoverraster' wait for a list with the name (string) of landcover rasters.\n", - "# 'landscape_polygons' wait for the name (string) of the vector layer containing the polygons to be used as landscape units.\n", - "# 'masklayerhardcopy' wait for a boolean value (True/False) depending if the user want to create hard copy of the landscape units mask layers or not.\n", - "# 'returnlistpath' wait for a boolean value (True/False) according to the fact that a list containing the path to the configuration files is desired.\n", - "# 'ncores' wait for a integer corresponding to the number of desired cores to be used for parallelization.\n", - "\n", - "# Import libraries for multiprocessing \n", - "import multiprocessing\n", - "from multiprocessing import Pool\n", - "from functools import partial \n", - "\n", - "# Function that copy the landscape unit raster masks on a new layer with name corresponding to the current 'landcover_raster'\n", - "def copy_landscapeunitmasks(current_landcover_raster,base_landcover_raster,landscape_polygons,landscapeunit_bbox,cat):\n", - " ### Copy the landscape units mask for the current 'cat'\n", - " # Define the name of the current \"current_landscapeunit_rast\" layer\n", - " current_landscapeunit_rast=current_landcover_raster.split(\"@\")[0]+\"_\"+landscape_polygons.split(\"@\")[0]+\"_\"+str(cat) \n", - " base_landscapeunit_rast=base_landcover_raster.split(\"@\")[0]+\"_\"+landscape_polygons.split(\"@\")[0]+\"_\"+str(cat) \n", - " # Copy the the landscape unit created for the first landcover map in order to match the name of the current landcover map\n", - " gscript.run_command('g.copy', overwrite=True, quiet=True, raster=(base_landscapeunit_rast,current_landscapeunit_rast))\n", - " # Add the line to the text variable\n", - " text=\"MASKEDOVERLAYAREA \"+current_landscapeunit_rast+\"|\"+landscapeunit_bbox[cat]\n", - " return text\n", - "\n", - "# Function that create the r.li configuration file for the base landcover raster and then for all the binary rasters\n", - "def create_rli_configfile(listoflandcoverraster,landscape_polygons,\n", - " masklayerhardcopy=False,returnlistpath=True,ncores=2):\n", - " # Check if 'listoflandcoverraster' is not empty\n", - " if len(listoflandcoverraster)==0:\n", - " sys.exit(\"The list of landcover raster is empty and should contain at least one raster name\")\n", - " # Check if rasters provided in 'listoflandcoverraster' exists to avoid error in mutliprocessing \n", - " for cur_rast in listoflandcoverraster:\n", - " try:\n", - " mpset=cur_rast.split(\"@\")[1]\n", - " except:\n", - " mpset=\"\"\n", - " if cur_rast.split(\"@\")[0] not in [x[0] for x in gscript.list_pairs(type='raster',mapset=mpset)]:\n", - " sys.exit('Raster <%s> not found' %cur_rast)\n", - " # Check if rasters provided in 'listoflandcoverraster' have the same extend and spatial resolution \n", - " raster={}\n", - " for x, rast in enumerate(raster_list):\n", - " raster[x]=gscript.raster_info(rast)\n", - " key_list=raster.keys()\n", - " for x in key_list[1:]:\n", - " for info in ('north','south','east','west','ewres','nsres'):\n", - " if not raster[0][info]==raster[x][info]:\n", - " sys.exit(\"Some raster provided in the list have different spatial resolution or extend, please check\") \n", - " # Get the version of GRASS GIS \n", - " version=grass.version()['version'].split('.')[0]\n", - " # Define the folder to save the r.li configuration files\n", - " if sys.platform==\"win32\":\n", - " rli_dir=os.path.join(os.environ['APPDATA'],\"GRASS\"+version,\"r.li\")\n", - " else: \n", - " rli_dir=os.path.join(os.environ['HOME'],\".grass\"+version,\"r.li\")\n", - " if not os.path.exists(rli_dir):\n", - " os.makedirs(rli_dir)\n", - " ## Create an ordered list with the 'cat' value of landscape units to be processed.\n", - " list_cat=[int(x) for x in gscript.parse_command('v.db.select', quiet=True, \n", - " map=landscape_polygons, column='cat', flags='c')]\n", - " list_cat.sort()\n", - " # Declare a empty dictionnary which will contains the north, south, east, west values for each landscape unit\n", - " landscapeunit_bbox={}\n", - " # Declare a empty list which will contain the path of the configation files created\n", - " listpath=[]\n", - " # Declare a empty string variable which will contains the core part of the r.li configuration file\n", - " maskedoverlayarea_1=\"\"\n", - " # Duplicate 'listoflandcoverraster' in a new variable called 'tmp_list'\n", - " tmp_list=list(listoflandcoverraster)\n", - " # Set the current landcover raster as the first of the list\n", - " base_landcover_raster=tmp_list.pop(0) #The pop function return the first item of the list and delete it from the list at the same time\n", - " # Loop trough the landscape units\n", - " for cat in list_cat:\n", - " # Extract the current landscape unit polygon as temporary vector\n", - " tmp_vect=\"tmp_\"+base_landcover_raster.split(\"@\")[0]+\"_\"+landscape_polygons.split(\"@\")[0]+\"_\"+str(cat)\n", - " gscript.run_command('v.extract', overwrite=True, quiet=True, \n", - " input=landscape_polygons, cats=cat, output=tmp_vect)\n", - " # Set region to match the extent of the current landscape polygon, with resolution and alignement matching the landcover raster\n", - " gscript.run_command('g.region', vector=tmp_vect, align=base_landcover_raster)\n", - " # Rasterize the landscape unit polygon\n", - " landscapeunit_rast=tmp_vect[4:]\n", - " gscript.run_command('v.to.rast', overwrite=True, quiet=True, input=tmp_vect, output=landscapeunit_rast, use='cat', memory='3000')\n", - " # Remove temporary vector\n", - " gscript.run_command('g.remove', quiet=True, flags=\"f\", type='vector', name=tmp_vect)\n", - " # Set the region to match the raster landscape unit extent and save the region info in a dictionary\n", - " region_info=gscript.parse_command('g.region', raster=landscapeunit_rast, flags='g')\n", - " n=str(round(float(region_info['n']),5)) #the config file need 5 decimal for north and south\n", - " s=str(round(float(region_info['s']),5))\n", - " e=str(round(float(region_info['e']),6)) #the config file need 6 decimal for east and west\n", - " w=str(round(float(region_info['w']),6))\n", - " # Save the coordinates of the bbox in the dictionary (n,s,e,w)\n", - " landscapeunit_bbox[cat]=n+\"|\"+s+\"|\"+e+\"|\"+w\n", - " # Add the line to the maskedoverlayarea_1 variable\n", - " maskedoverlayarea_1+=\"MASKEDOVERLAYAREA \"+landscapeunit_rast+\"|\"+landscapeunit_bbox[cat]+\"\\n\"\n", - "\n", - " # Compile the content of the r.li configuration file\n", - " config_file_content=\"SAMPLINGFRAME 0|0|1|1\\n\"\n", - " config_file_content+=maskedoverlayarea_1\n", - " config_file_content+=\"RASTERMAP \"+base_landcover_raster+\"\\n\"\n", - " config_file_content+=\"VECTORMAP \"+landscape_polygons+\"\\n\"\n", - "\n", - " # Create a new file and save the content\n", - " configfilename=base_landcover_raster.split(\"@\")[0]+\"_\"+landscape_polygons.split(\"@\")[0]\n", - " path=os.path.join(rli_dir,configfilename)\n", - " listpath.append(path)\n", - " f=open(path, 'w')\n", - " f.write(config_file_content)\n", - " f.close()\n", - " \n", - " # Continue creation of r.li configuration file and landscape unit raster the rest of the landcover raster provided\n", - " while len(tmp_list)>0:\n", - " # Initialize 'maskedoverlayarea_2' variable as an empty string\n", - " maskedoverlayarea_2=\"\"\n", - " # Set the current landcover raster as the first of the list\n", - " current_landcover_raster=tmp_list.pop(0) #The pop function return the first item of the list and delete it from the list at the same time\n", - " if masklayerhardcopy: # If the user asked for hard copy of the landscape units mask layers\n", - " # Copy all the landscape units masks for the current landcover raster\n", - " p=Pool(ncores) #Create a pool of processes and launch them using 'map' function\n", - " func=partial(copy_landscapeunitmasks,current_landcover_raster,base_landcover_raster,landscape_polygons,landscapeunit_bbox) # Set fixed argument of the function\n", - " maskedoverlayarea_2=p.map(func,list_cat) # Launch the processes for as many items in the list and get the ordered results using map function\n", - " p.close()\n", - " p.join()\n", - " # Compile the content of the r.li configuration file\n", - " config_file_content=\"SAMPLINGFRAME 0|0|1|1\\n\"\n", - " config_file_content+=\"\\n\".join(maskedoverlayarea_2)+\"\\n\"\n", - " config_file_content+=\"RASTERMAP \"+current_landcover_raster+\"\\n\"\n", - " config_file_content+=\"VECTORMAP \"+landscape_polygons+\"\\n\"\n", - " else: # If the user not asked for hard copy\n", - " # Compile the content of the r.li configuration file\n", - " config_file_content=\"SAMPLINGFRAME 0|0|1|1\\n\"\n", - " config_file_content+=maskedoverlayarea_1 # If user do not asked for hard copy, the mask layers are the same than for the first configuration file\n", - " config_file_content+=\"RASTERMAP \"+current_landcover_raster+\"\\n\" # But the name of the RASTERMAP should be the one of the current landcover raster\n", - " config_file_content+=\"VECTORMAP \"+landscape_polygons+\"\\n\"\n", - " # Create a new file and save the content\n", - " configfilename=current_landcover_raster.split(\"@\")[0]+\"_\"+landscape_polygons.split(\"@\")[0]\n", - " path=os.path.join(rli_dir,configfilename)\n", - " listpath.append(path)\n", - " f=open(path, 'w')\n", - " f.write(config_file_content)\n", - " f.close()\n", - " \n", - " # Return a list of path of configuration files creates if option actived\n", - " if returnlistpath:\n", - " return list_cat,listpath\n", - " else:\n", - " return list_cat" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for creation of binary raster from a categorical raster (multiprocessed)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "###### Function creating a binary raster for each category of a base raster. \n", - "### The function run within the current region. If a category do not exists in the current region, no binary map will be produce\n", - "# 'categorical_raster' wait for the name of the base raster to be used. It is the one from which one binary raster will be produced for each category value\n", - "# 'prefix' wait for a string corresponding to the prefix of the name of the binary raster which will be produced\n", - "# 'setnull' wait for a boolean value (True, False) according to the fact that the output binary should be 1/0 or 1/null\n", - "# 'returnlistraster' wait for a boolean value (True, False) regarding to the fact that a list containing the name of binary raster is desired as return of the function\n", - "# 'category_list' wait for a list of interger corresponding to specific category of the base raster to be used \n", - "# 'ncores' wait for a integer corresponding to the number of desired cores to be used for parallelization\n", - "\n", - "# Import libraries for multiprocessing \n", - "import multiprocessing\n", - "from multiprocessing import Pool\n", - "from functools import partial \n", - "\n", - "def create_binary_raster(categorical_raster,prefix=\"binary\",setnull=False,returnlistraster=True,category_list=None,ncores=2):\n", - " # Check if raster exists to avoid error in mutliprocessing \n", - " try:\n", - " mpset=categorical_raster.split(\"@\")[1]\n", - " except:\n", - " mpset=\"\"\n", - " if categorical_raster not in gscript.list_strings(type='raster',mapset=mpset):\n", - " sys.exit('Raster <%s> not found' %categorical_raster)\n", - " # Check for number of cores doesnt exceed available\n", - " nbcpu=multiprocessing.cpu_count()\n", - " if ncores>=nbcpu:\n", - " ncores=nbcpu-1\n", - " returnlist=[] #Declare empty list for return\n", - " #gscript.run_command('g.region', raster=categorical_raster, quiet=True) #Set the region\n", - " null='null()' if setnull else '0' #Set the value for r.mapcalc\n", - " minclass=1 if setnull else 2 #Set the value to check if the binary raster is empty\n", - " if category_list == None: #If no category_list provided\n", - " category_list=[cl for cl in gscript.parse_command('r.category',map=categorical_raster,quiet=True)]\n", - " for i,x in enumerate(category_list): #Make sure the format is UTF8 and not Unicode\n", - " category_list[i]=x.encode('UTF8')\n", - " category_list.sort(key=float) #Sort the raster categories in ascending.\n", - " p=Pool(ncores) #Create a pool of processes and launch them using 'map' function\n", - " func=partial(get_binary,categorical_raster,prefix,null,minclass) # Set the two fixed argument of the function\n", - " returnlist=p.map(func,category_list) # Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - " p.close()\n", - " p.join()\n", - " if returnlistraster:\n", - " return returnlist\n", - "\n", - "#### Function that extract binary raster for a specified class (called in 'create_binary_raster' function)\n", - "def get_binary(categorical_raster,prefix,null,minclass,cl):\n", - " binary_class=prefix+\"_\"+cl\n", - " gscript.run_command('r.mapcalc', expression=binary_class+'=if('+categorical_raster+'=='+str(cl)+',1,'+null+')',overwrite=True, quiet=True)\n", - " if len(gscript.parse_command('r.category',map=binary_class,quiet=True))>=minclass: #Check if created binary is not empty\n", - " return binary_class\n", - " else:\n", - " gscript.run_command('g.remove', quiet=True, flags=\"f\", type='raster', name=binary_class)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for computation of spatial metrics at landscape level (multiprocessed)" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "##### Function that compute different landscape metrics (spatial metrics) at landscape level. \n", - "### The metric computed are \"dominance\",\"pielou\",\"renyi\",\"richness\",\"shannon\",\"simpson\".\n", - "### It is important to set the computation region before runing this script so that it match the extent of the 'raster' layer.\n", - "# 'configfile' wait for the path (string) to the configuration file corresponding to the 'raster' layer.\n", - "# 'raster' wait for the name (string) of the landcover map on which landscape metrics will be computed.\n", - "# 'returnlistresult' wait for a boolean value (True/False) according to the fact that a list containing the path to the result files is desired.\n", - "# 'ncores' wait for a integer corresponding to the number of desired cores to be used for parallelization.\n", - "\n", - "# Import libraries for multiprocessing \n", - "import multiprocessing\n", - "from multiprocessing import Pool\n", - "from functools import partial \n", - "\n", - "def compute_landscapelevel_metrics(configfile, raster, spatial_metric):\n", - " filename=raster.split(\"@\")[0]+\"_%s\" %spatial_metric\n", - " outputfile=os.path.join(os.path.split(configfile)[0],\"output\",filename)\n", - " if spatial_metric=='renyi': # The alpha parameter was set to 2 as in https://en.wikipedia.org/wiki/R%C3%A9nyi_entropy\n", - " gscript.run_command('r.li.%s' %spatial_metric, overwrite=True,\n", - " input=raster,config=configfile,alpha='2', output=filename)\n", - " else:\n", - " gscript.run_command('r.li.%s' %spatial_metric, overwrite=True,\n", - " input=raster,config=configfile, output=filename)\n", - " return outputfile\n", - " \n", - "def get_landscapelevel_metrics(configfile, raster, returnlistresult=True, ncores=2):\n", - " # Check if raster exists to avoid error in mutliprocessing \n", - " try:\n", - " mpset=raster.split(\"@\")[1]\n", - " except:\n", - " mpset=\"\"\n", - " if raster not in gscript.list_strings(type='raster',mapset=mpset):\n", - " sys.exit('Raster <%s> not found' %raster)\n", - " # Check if configfile exists to avoid error in mutliprocessing \n", - " if not os.path.exists(configfile):\n", - " sys.exit('Configuration file <%s> not found' %configfile)\n", - " # List of metrics to be computed\n", - " spatial_metric_list=[\"dominance\",\"pielou\",\"renyi\",\"richness\",\"shannon\",\"simpson\"]\n", - " # Check for number of cores doesnt exceed available\n", - " nbcpu=multiprocessing.cpu_count()\n", - " if ncores>=nbcpu:\n", - " ncores=nbcpu-1\n", - " if ncores>len(spatial_metric_list):\n", - " ncores=len(spatial_metric_list) #Adapt number of cores to number of metrics to compute\n", - " #Declare empty list for return\n", - " returnlist=[] \n", - " # Create a new pool\n", - " p=Pool(ncores)\n", - " # Set the two fixed argument of the 'compute_landscapelevel_metrics' function\n", - " func=partial(compute_landscapelevel_metrics,configfile, raster)\n", - " # Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - " returnlist=p.map(func,spatial_metric_list)\n", - " p.close()\n", - " p.join()\n", - " # Return list of paths to result files\n", - " if returnlistresult:\n", - " return returnlist" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for computation of spatial metrics at class level (multiprocessed)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "##### Function that compute different landscape metrics (spatial metrics) at class level. \n", - "### The metric computed are \"patch number (patchnum)\",\"patch density (patchdensity)\",\"mean patch size(mps)\",\n", - "### \"coefficient of variation of patch area (padcv)\",\"range of patch area size (padrange)\",\n", - "### \"standard deviation of patch area (padsd)\", \"shape index (shape)\", \"edge density (edgedensity)\".\n", - "### It is important to set the computation region before runing this script so that it match the extent of the 'raster' layer.\n", - "# 'configfile' wait for the path (string) to the configuration file corresponding to the 'raster' layer.\n", - "# 'raster' wait for the name (string) of the landcover map on which landscape metrics will be computed.\n", - "# 'returnlistresult' wait for a boolean value (True/False) according to the fact that a list containing the path to the result files is desired.\n", - "# 'ncores' wait for a integer corresponding to the number of desired cores to be used for parallelization.\n", - "\n", - "# Import libraries for multiprocessing \n", - "import multiprocessing\n", - "from multiprocessing import Pool\n", - "from functools import partial \n", - "\n", - "def compute_classlevel_metrics(configfile, raster, spatial_metric):\n", - " filename=raster.split(\"@\")[0]+\"_%s\" %spatial_metric\n", - " gscript.run_command('r.li.%s' %spatial_metric, overwrite=True,\n", - " input=raster,config=configfile,output=filename)\n", - " outputfile=os.path.join(os.path.split(configfile)[0],\"output\",filename)\n", - " return outputfile\n", - " \n", - "def get_classlevel_metrics(configfile, raster, returnlistresult=True, ncores=2):\n", - " # Check if raster exists to avoid error in mutliprocessing \n", - " try:\n", - " mpset=raster.split(\"@\")[1]\n", - " except:\n", - " mpset=\"\"\n", - " if raster not in [x.split(\"@\")[0] for x in gscript.list_strings(type='raster',mapset=mpset)]:\n", - " sys.exit('Raster <%s> not found' %raster)\n", - " # Check if configfile exists to avoid error in mutliprocessing \n", - " if not os.path.exists(configfile):\n", - " sys.exit('Configuration file <%s> not found' %configfile)\n", - " # List of metrics to be computed\n", - " spatial_metric_list=[\"patchnum\",\"patchdensity\",\"mps\",\"padcv\",\"padrange\",\"padsd\",\"shape\",\"edgedensity\"]\n", - " # Check for number of cores doesnt exceed available\n", - " nbcpu=multiprocessing.cpu_count()\n", - " if ncores>=nbcpu:\n", - " ncores=nbcpu-1\n", - " if ncores>len(spatial_metric_list):\n", - " ncores=len(spatial_metric_list) #Adapt number of cores to number of metrics to compute\n", - " # Declare empty list for return\n", - " returnlist=[] \n", - " # Create a new pool\n", - " p=Pool(ncores)\n", - " # Set the two fixed argument of the 'compute_classlevel_metrics' function\n", - " func=partial(compute_classlevel_metrics,configfile, raster)\n", - " # Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - " returnlist=p.map(func,spatial_metric_list)\n", - " p.close()\n", - " p.join()\n", - " # Return list of paths to result files\n", - " if returnlistresult:\n", - " return returnlist" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function for computation of each land cover class proportion in landscape units" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "### Function that compute the proportion of each class of landcover\n", - "import itertools\n", - "import multiprocessing\n", - "from functools import partial\n", - "import sys, csv\n", - "import grass.script as gscript\n", - "\n", - "def random_string(N):\n", - " import random, string\n", - " prefix=random.choice(string.ascii_uppercase + string.ascii_lowercase)\n", - " suffix=''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(N))\n", - " return prefix+suffix\n", - "\n", - "def compute_prop_landcover(outputfolder,landscape_units_raster,binary_class_raster):\n", - " # Save the current class in a new variable \n", - " current_class=binary_class_raster.split(\"@\")[0].split(\"_\")[-1]\n", - " # Initialize a temp files\n", - " temp_file=os.path.join(outputfolder,random_string(10))\n", - " tmp_copy='%s_%s'%(random_string(4),current_class)\n", - " # Make copy of the raster and fill the null values with zero\n", - " gscript.run_command('g.copy', overwrite=True, raster='%s,%s'%(binary_class_raster,tmp_copy))\n", - " gscript.run_command('r.null', map=tmp_copy, null='0')\n", - " # Compute area of landscape unit and total pixels of the current binary class raster\n", - " gscript.run_command('i.segment.stats', flags=\"r\", quiet=True, overwrite=True, \n", - " map=landscape_units_raster, area_measures='area', \n", - " rasters=tmp_copy, raster_statistics='sum', \n", - " csvfile=temp_file, separator='pipe')\n", - " gscript.run_command('g.remove', flags='f', type='raster', name=tmp_copy)\n", - " # Create outputfile and compute proportion of class\n", - " outfile=os.path.join(outputfolder, \"prop_%s\"%current_class)\n", - " writer=csv.writer(open(outfile,'w'), delimiter=\"|\")\n", - " reader=csv.reader(open(temp_file,'r'), delimiter=\"|\")\n", - " header=reader.next()\n", - " crash_report=[]\n", - " content=[]\n", - " content.append([header[0],\"prop_%s\"%current_class]) #Create new header with first original column and current class related name for proportion\n", - " for row in reader:\n", - " try:\n", - " prop=100*float(row[2])/float(row[1])\n", - " content.append([row[0],\"{0:.5f}\".format(prop)])\n", - " except ZeroDivisionError:\n", - " crash_report.append(row[0])\n", - " continue\n", - " writer.writerows(content)\n", - " os.remove(temp_file)\n", - " # Print notification of ZeroDivisionError if it happened\n", - " if len(crash_report)>0:\n", - " print \"An 'ZeroDivisionError' has been registered for the following <%s>\"%header[0]+\"\\n\".join(crash_report)\n", - " # Return the result file\n", - " return outfile\n", - " \n", - "def get_classproportions(outputfolder,landscape_units_raster,binary_class_raster_list,\n", - " returnlistresult=True,ncores=2):\n", - " # Check if raster exists to avoid error in mutliprocessing \n", - " try:\n", - " mpset=landscape_units_raster.split(\"@\")[1]\n", - " except:\n", - " mpset=\"\"\n", - " if landscape_units_raster not in [x.split(\"@\")[0] for x in gscript.list_strings(type='raster',mapset=mpset)]:\n", - " sys.exit('Raster <%s> not found' %landscape_units_raster)\n", - " # Check for number of cores doesnt exceed available\n", - " nbcpu=multiprocessing.cpu_count()\n", - " if ncores>=nbcpu:\n", - " ncores=nbcpu-1\n", - " if ncores>len(binary_class_raster_list):\n", - " ncores=len(binary_class_raster_list) #Adapt number of cores to number of metrics to compute\n", - " # Declare empty list for return\n", - " returnlist=[] \n", - " # Create a new pool\n", - " p=Pool(ncores)\n", - " # Set the two fixed argument of the 'compute_prop_landcover' function\n", - " func=partial(compute_prop_landcover,outputfolder,landscape_units_raster)\n", - " # Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - " returnlist=p.map(func,binary_class_raster_list)\n", - " p.close()\n", - " p.join()\n", - " # Return list of paths to result files\n", - " if returnlistresult:\n", - " return returnlist" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Function that enable for join of multiple different .csv files" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def atoi(text):\n", - " '''\n", - " Function that return integer if text is digit - Used in 'natural_keys' function\n", - " '''\n", - " return int(text) if text.isdigit() else text\n", - "\n", - "def natural_keys(text): # Trick was found here: https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside\n", - " '''\n", - " Return key to be used for sorting string containing numerical values - Used in 'leftjoin_2csv' function\n", - " '''\n", - " import re #Import needed library\n", - " return [ atoi(c) for c in re.split('(\\d+)', text) ] #Split the string\n", - "\n", - "def join_2csv(file1,file2,separator=\";\",join='inner',fillempty='NULL'):\n", - " '''\n", - " Function that join two csv files according to the first column (primary key).\n", - " 'file1' and 'file2' wait for complete path (strings) to the corresponding files. Please not that 'file1' is assume to be the left-one in the join\n", - "\t'separator' wait for the character to be considered as .csv delimiter (string)\n", - "\t'join' parameter wait either for 'left' or 'inner' according to type of join\n", - "\t'fillempty' wait for the string to be use to fill the blank when no occurance is found for the join operation\n", - " '''\n", - " import tempfile,csv,os\n", - " header_list=[]\n", - " file1_values_dict={}\n", - " file2_values_dict={}\n", - " reader1=csv.reader(open(file1), delimiter=separator) #Csv reader for file 1\n", - " reader2=csv.reader(open(file2), delimiter=separator) #Csv reader for file 2\n", - " # Make a list of headers\n", - " header_list1=[ x for x in reader1.next()]\n", - " header_list2=[ x for x in reader2.next()[1:]]\n", - " # Make a list of unique IDs from the first and second table according to type of join\n", - " if join=='inner':\n", - " id_list=[ row[0] for row in reader1]\n", - " [id_list.append(row[0]) for row in reader2]\n", - " id_list=list(set(id_list))\n", - " id_list.sort(key=natural_keys)\n", - " if join=='left':\n", - " id_list=[ row[0] for row in reader1]\n", - " id_list=list(set(id_list))\n", - " id_list.sort(key=natural_keys)\n", - " # Build dictionnary for values of file 1\n", - " reader1=csv.reader(open(file1), delimiter=separator)\n", - " reader1.next()\n", - " values_dict1={rows[0]:rows[1:] for rows in reader1}\n", - " # Build dictionnary for values of file 2\n", - " reader2=csv.reader(open(file2), delimiter=separator)\n", - " reader2.next()\n", - " values_dict2={rows[0]:rows[1:] for rows in reader2}\n", - " # Built new content\n", - " new_content=[]\n", - " new_header=header_list1+header_list2\n", - " new_content.append(new_header)\n", - " for key in id_list:\n", - " new_row=[key]\n", - " try:\n", - " [new_row.append(value) for value in values_dict1[key]]\n", - " except:\n", - " [new_row.append('%s'%fillempty) for x in header_list1[1:]]\n", - " try:\n", - " [new_row.append(value) for value in values_dict2[key]]\n", - " except:\n", - " [new_row.append('%s'%fillempty) for x in header_list2]\n", - " new_content.append(new_row)\n", - " #Return the result\n", - " outfile=os.path.join(tempfile.gettempdir(),\"temp\")\n", - " writer=csv.writer(open(outfile,\"w\"), delimiter=separator)\n", - " writer.writerows(new_content) #Write multiples rows in the file\n", - " return outfile\n", - "\n", - "def join_multiplecsv(fileList,outfile,separator=\";\",join='inner', fillempty='NULL', overwrite=False):\n", - " '''\n", - " Function that apply join on multiple csv files\n", - " '''\n", - " import os, sys, shutil\n", - " # Stop execution if outputfile exitst and can not be overwriten\n", - " if os.path.isfile(outfile) and overwrite==False:\n", - " print \"File '%s' aleady exists and overwrite option is not enabled.\"%outfile\n", - " else:\n", - " if os.path.isfile(outfile) and overwrite==True: # If outputfile exitst and can be overwriten\n", - " #os.remove(outfile)\n", - " print \"File '%s' will be overwrited.\"%outfile\n", - " nbfile=len(fileList)\n", - " if nbfile<=1: #Check if there are at least 2 files in the list\n", - " sys.exit(\"This function require at least two .csv files to be jointed together.\")\n", - " # Copy the list of file in a queue list\n", - " queue_list=list(fileList)\n", - " # Left join on the two first files\n", - " file1=queue_list.pop(0)\n", - " file2=queue_list.pop(0)\n", - " tmp_file=join_2csv(file1,file2,separator=separator,join=join, fillempty=fillempty)\n", - " # Left join on the rest of the files in the list\n", - " while len(queue_list)>0:\n", - " file2=queue_list.pop(0)\n", - " tmp_file=join_2csv(tmp_file,file2,separator=separator,join=join, fillempty=fillempty)\n", - " #Copy the temporary file to the desired output path\n", - " shutil.copy2(tmp_file,outfile)\n", - " # Print what happend\n", - " print \"%s individual .csv files were joint together.\"%nbfile\n", - "\n", - "def create_csvt(csv_file,separator=\";\",first_col_type=\"Integer\",rest_type=\"Real\"):\n", - " '''\n", - " Function that create a .csvt file with the same type of all columns except first one\n", - " '''\n", - " import csv\n", - " writer=csv.writer(open(csv_file+\"t\",\"w\"),delimiter=separator)\n", - " reader=csv.reader(open(csv_file,\"r\"),delimiter=separator)\n", - " header=reader.next()\n", - " typecolumn=[]\n", - " typecolumn.append(first_col_type)\n", - " for columns in header[1:]:\n", - " typecolumn.append(rest_type)\n", - " writer.writerow(typecolumn)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "

User inputs

" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "## Define a empty dictionnary for saving user inputs\n", - "user={}" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "## Enter the path to GRASSDATA folder\n", - "user[\"gisdb\"] = \"/home/tais/Documents/GRASSDATA_Spie2017subset_Ouaga\"\n", - "\n", - "## Enter the name of the location (existing or for a new one)\n", - "user[\"location\"] = \"SPIE_subset\"\n", - "\n", - "## Enter the EPSG code for this location \n", - "user[\"locationepsg\"] = \"32630\"\n", - "\n", - "## Enter the name of the mapset to use for segmentation\n", - "user[\"mapsetname\"] = \"test_rli\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Compute landscape metrics (spatial metrics) from the land cover map, using r.li" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Launch GRASS GIS working session**" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "You are now working in mapset 'test_rli'\n" - ] - } - ], - "source": [ - "## Set the name of the mapset in which to work\n", - "mapsetname=user[\"mapsetname\"]\n", - "\n", - "## Launch GRASS GIS working session in the mapset\n", - "if os.path.exists(os.path.join(user[\"gisdb\"],user[\"location\"],mapsetname)):\n", - " gsetup.init(os.environ['GISBASE'], user[\"gisdb\"], user[\"location\"], mapsetname)\n", - " print \"You are now working in mapset '\"+mapsetname+\"'\" \n", - "else: \n", - " print \"'\"+mapsetname+\"' mapset doesn't exists in \"+user[\"gisdb\"]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Set the path to the r.li folder for configuration file and for results**" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "GRASS GIS add-on's r.li configuration files will be saved under .\n", - "GRASS GIS add-on's r.li outputs will be saved under .\n" - ] - } - ], - "source": [ - "# Define path of the outputfile (in r.li folder)\n", - "version=grass.version()['version'].split('.')[0] # Get the version of GRASS GIS \n", - "if sys.platform==\"win32\":\n", - " rli_config_dir=os.path.join(os.environ['APPDATA'],\"GRASS\"+version,\"r.li\")\n", - " rli_output_dir=os.path.join(os.environ['APPDATA'],\"GRASS\"+version,\"r.li\",\"output\")\n", - "else: \n", - " rli_config_dir=os.path.join(os.environ['HOME'],\".grass\"+version,\"r.li\")\n", - " rli_output_dir=os.path.join(os.environ['HOME'],\".grass\"+version,\"r.li\",\"output\")\n", - "if not os.path.exists(rli_config_dir):\n", - " os.makedirs(rli_config_dir)\n", - "if not os.path.exists(rli_output_dir):\n", - " os.makedirs(rli_output_dir)\n", - "# Print\n", - "print \"GRASS GIS add-on's r.li configuration files will be saved under <%s>.\"%(rli_config_dir,)\n", - "print \"GRASS GIS add-on's r.li outputs will be saved under <%s>.\"%(rli_output_dir,)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Define the name of the base landcover map and landscape units polygons" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the name of the 'base' landcover map\n", - "baselandcoverraster=\"classif@test_rli\"\n", - "# Set the name of the vector polygon layer containing the landscape units\n", - "landscape_polygons=\"streetblocks\"" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Import shapefile containing street blocks polygons" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the path to the shapefile containing streetblocks polygons\n", - "pathtoshp=\"/media/tais/data/Dropbox/ULB/MAUPP/Landuse_mapping/Test_spatial_metrics_computation/Data/streetblocks_subset.shp\"" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Import shapefile\n", - "gscript.run_command('v.in.ogr', quiet=True, overwrite=True, input=pathtoshp, output=landscape_polygons)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Create binary rasters from the base landcover map" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Extraction of binary rasters achieved in 4.4 seconds'" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "\n", - "# Create as many binary raster layer as categorical values existing in the base landcover map\n", - "gscript.run_command('g.region', raster=baselandcoverraster, quiet=True) #Set the region\n", - "pref=baselandcoverraster.split(\"@\")[0]+\"_cl\" #Set the prefix\n", - "\n", - "raster_list=[] # Initialize a empty list for results\n", - "raster_list=create_binary_raster(baselandcoverraster,\n", - " prefix=pref,setnull=True,returnlistraster=True,\n", - " category_list=None,ncores=15) #Extract binary raster \n", - "\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Extraction of binary rasters achieved in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "['classif@test_rli',\n", - " 'classif_cl_11',\n", - " 'classif_cl_13',\n", - " 'classif_cl_14',\n", - " 'classif_cl_20',\n", - " 'classif_cl_30',\n", - " 'classif_cl_31',\n", - " 'classif_cl_41',\n", - " 'classif_cl_51']" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Insert the name of the base landcover map at first position in the list\n", - "raster_list.insert(0,baselandcoverraster)\n", - "# Display the raster to be used for landscape analysis\n", - "raster_list" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Create r.li configuration file for a list of landcover rasters" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Creation of r.li configuration files achieved in 3 minutes and 33.1 seconds'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Run creation of r.li configuration file and associated raster layers\n", - "list_cats,list_configfile=create_rli_configfile(raster_list,landscape_polygons,masklayerhardcopy=False,returnlistpath=True,ncores=20)\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Creation of r.li configuration files achieved in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[u'/home/tais/.grass7/r.li/classif_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_11_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_13_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_14_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_20_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_30_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_31_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_41_streetblocks',\n", - " u'/home/tais/.grass7/r.li/classif_cl_51_streetblocks']" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Display the path to the configuration files created\n", - "list_configfile" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Compute spatial metrics at landscape level" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Initialize an empty list which will contains the resultfiles \n", - "resultfiles=[]" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Computation of spatial metric achieved in 41.3 seconds'" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Get the path to the configuration file for the base landcover raster\n", - "currentconfigfile=list_configfile[0]\n", - "# Get the name of the base landcover raster\n", - "currentraster=raster_list[0]\n", - "# Set the region to match the extent of the base raster\n", - "gscript.run_command('g.region', raster=currentraster, quiet=True)\n", - "# Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - "resultfiles.append(get_landscapelevel_metrics(currentconfigfile, currentraster, returnlistresult=True, ncores=15))\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Computation of spatial metric achieved in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[[u'/home/tais/.grass7/r.li/output/classif_dominance',\n", - " u'/home/tais/.grass7/r.li/output/classif_pielou',\n", - " u'/home/tais/.grass7/r.li/output/classif_renyi',\n", - " u'/home/tais/.grass7/r.li/output/classif_richness',\n", - " u'/home/tais/.grass7/r.li/output/classif_shannon',\n", - " u'/home/tais/.grass7/r.li/output/classif_simpson']]" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "resultfiles" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Compute spatial metrics at class level" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Computation of spatial metric achieved in 6 minutes and 1.1 seconds'" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Get a list with paths to the configuration file for class level metrics\n", - "classlevelconfigfiles=list_configfile[1:]\n", - "# Get a list with name of binary landcover raster for class level metrics\n", - "classlevelrasters=raster_list[1:]\n", - "\n", - "for x,currentraster in enumerate(classlevelrasters[:]):\n", - " # Get the path to the configuration file for the base landcover raster\n", - " currentconfigfile=classlevelconfigfiles[x]\n", - " # Set the region to match the extent of the base raster\n", - " gscript.run_command('g.region', raster=currentraster, quiet=True)\n", - " # Launch the processes for as many items in the 'functions_name' list and get the ordered results using map function\n", - " resultfiles.append(get_classlevel_metrics(currentconfigfile, currentraster, returnlistresult=True, ncores=10))\n", - "\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Computation of spatial metric achieved in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[[u'/home/tais/.grass7/r.li/output/classif_dominance',\n", - " u'/home/tais/.grass7/r.li/output/classif_pielou',\n", - " u'/home/tais/.grass7/r.li/output/classif_renyi',\n", - " u'/home/tais/.grass7/r.li/output/classif_richness',\n", - " u'/home/tais/.grass7/r.li/output/classif_shannon',\n", - " u'/home/tais/.grass7/r.li/output/classif_simpson'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_11_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_13_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_14_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_20_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_30_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_31_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_41_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_edgedensity'],\n", - " [u'/home/tais/.grass7/r.li/output/classif_cl_51_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_edgedensity']]" - ] - }, - "execution_count": 26, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "resultfiles" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Change the results files from r.li to get the correct 'cat' value for each landscape unit" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Flat the 'resultfiles' list which contains several lists\n", - "resultfiles=[item for sublist in resultfiles for item in sublist]" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [], - "source": [ - "import csv, shutil\n", - "from itertools import izip\n", - "\n", - "for f in resultfiles:\n", - " f_in=open(f)\n", - " f_tmp=open(f+'_tmp',\"w\")\n", - " f_in_reader=csv.reader(f_in,delimiter='|')\n", - " f_tmp_writer=csv.writer(f_tmp,delimiter='|')\n", - " f_tmp_writer.writerow(['cat',\"_\".join(os.path.split(f)[-1].split(\"_\")[1:])])\n", - " for i,row in enumerate(f_in_reader):\n", - " newrow=[]\n", - " try:\n", - " newrow.append(list_cats[i])\n", - " newrow.append(row[1])\n", - " f_tmp_writer.writerow(newrow)\n", - " except:\n", - " continue\n", - " f_in.close()\n", - " f_tmp.close()\n", - " os.remove(f)\n", - " shutil.copy2(f+'_tmp',f)\n", - " os.remove(f+'_tmp')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# Compute some special metrics" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "### Creating a raster layer of landscape units" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Creating a raster layer of landscape units with resolution corresponding to the one of land cover raster" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 30, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create a raster corresponding to the landscape units (for computing statistics using i.segment.stats)\n", - "gscript.run_command('g.region', raster=baselandcoverraster, quiet=True) #Set the region\n", - "raster_landscapeunits=\"temp_%s\"%landscape_polygons.split(\"@\")[0]\n", - "gscript.run_command('v.to.rast', overwrite=True, input=landscape_polygons, output=raster_landscapeunits, use='cat') " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "### Shape statistics for the landscape units" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the path to the file for i.segment.stats results for landscape units shape metrics\n", - "landscape_units_shape_metrics=os.path.join(rli_output_dir,\"landscape_units_shape_metrics\")" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Metrics computed in 1.0 seconds'" - ] - }, - "execution_count": 32, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Run i.segment.stats\n", - "gscript.run_command('i.segment.stats', overwrite=True, map=raster_landscapeunits,\n", - " area_measures='area,perimeter,compact_circle,compact_square,fd',\n", - " csvfile=landscape_units_shape_metrics,\n", - " processes='1')\n", - "\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Metrics computed in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "resultfiles.append(landscape_units_shape_metrics)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "### Mean and standard deviation of SAR textures, NDVI, NDWI" - ] - }, - { - "cell_type": "code", - "execution_count": 34, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the name of the nDSM layer\n", - "ndsm=\"ndsm\"\n", - "# Set the name of the NDVI layer\n", - "ndvi=\"ndvi\"\n", - "# Set the name of the NDWI layer\n", - "ndwi=\"ndwi\"\n", - "# Set the prefix of SAR textures layer\n", - "SAR_prefix=\"SAR_w\"" - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "metadata": { - "scrolled": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Layer to be used :\n", - "\n", - "ndvi\n", - "ndwi\n", - "SAR_w11.1@PERMANENT\n", - "SAR_w11.2@PERMANENT\n", - "SAR_w11.3@PERMANENT\n", - "SAR_w11.4@PERMANENT\n", - "SAR_w11.5@PERMANENT\n", - "SAR_w11.6@PERMANENT\n", - "SAR_w11.7@PERMANENT\n", - "SAR_w7.1@PERMANENT\n", - "SAR_w7.2@PERMANENT\n", - "SAR_w7.3@PERMANENT\n", - "SAR_w7.4@PERMANENT\n", - "SAR_w7.5@PERMANENT\n", - "SAR_w7.6@PERMANENT\n", - "SAR_w7.7@PERMANENT\n" - ] - } - ], - "source": [ - "# Set up a list with name of raster layer to be used\n", - "ancillarylayers=[]\n", - "ancillarylayers.append(ndvi)\n", - "ancillarylayers.append(ndwi)\n", - "[ancillarylayers.append(x) for x in gscript.list_strings(\"rast\", pattern=SAR_prefix, flag='r')] #Append SAR textures\n", - "print \"Layer to be used :\\n\\n\"+'\\n'.join(ancillarylayers)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the path to the file for i.segment.stats results for metrics_ndvi_ndwi_sar\n", - "metrics_ndvi_ndwi_sar=os.path.join(rli_output_dir,\"metrics_ndvi_ndwi_sar\")" - ] - }, - { - "cell_type": "code", - "execution_count": 37, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'Metrics computed in 11.2 seconds'" - ] - }, - "execution_count": 37, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "###### Compute shape metrics as well as mean and stddev of ancillary layers for each landscape unit\n", - "## Set number of cores to be used\n", - "ncores=len(ancillarylayers) \n", - "nbcpu=multiprocessing.cpu_count()\n", - "if ncores>=nbcpu:\n", - " ncores=nbcpu-1\n", - " if ncores>len(ancillarylayers):\n", - " ncores=len(ancillarylayers) #Adapt number of cores to number of metrics to compute\n", - "# Run i.segment.stats\n", - "gscript.run_command('i.segment.stats', overwrite=True, map=raster_landscapeunits,\n", - " raster_statistics='stddev,median',\n", - " rasters=','.join(ancillarylayers),\n", - " csvfile=metrics_ndvi_ndwi_sar,\n", - " processes=ncores,\n", - " flags='s')\n", - "\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Metrics computed in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 38, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "resultfiles.append(metrics_ndvi_ndwi_sar)" - ] - }, - { - "cell_type": "code", - "execution_count": 39, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[u'/home/tais/.grass7/r.li/output/classif_dominance',\n", - " u'/home/tais/.grass7/r.li/output/classif_pielou',\n", - " u'/home/tais/.grass7/r.li/output/classif_renyi',\n", - " u'/home/tais/.grass7/r.li/output/classif_richness',\n", - " u'/home/tais/.grass7/r.li/output/classif_shannon',\n", - " u'/home/tais/.grass7/r.li/output/classif_simpson',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/landscape_units_shape_metrics',\n", - " u'/home/tais/.grass7/r.li/output/metrics_ndvi_ndwi_sar']" - ] - }, - "execution_count": 39, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "resultfiles" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "### Mean and standard deviation of building's height" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Create raster with nDSM value of 'buildings' pixels**" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set pixel value of 'buildings' on the 'baselandcoverraster'\n", - "buildpixel=11\n", - "# Set the name of the new layer containing height of buildings\n", - "buildings_height='buildings_height'\n", - "# Set the path to the file for i.segment.stats results for metrics_ndvi_ndwi_sar\n", - "metrics_buildings_height=os.path.join(rli_output_dir,\"metrics_buildings_height\")\n", - "# Create temp fil which will contain intermediate results\n", - "TMP_sumheights=grass.tempfile()+'_sumheights.csv'\n", - "TMP_nbrbuildpixels=grass.tempfile()+'_nbrbuildpixels.csv'" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Creation of layer in 5.9 seconds'" - ] - }, - "execution_count": 41, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Create a raster layer with height of pixels classified as 'buildings'\n", - "gscript.run_command('g.region', raster=baselandcoverraster, quiet=True) #Set the region\n", - "formula=\"%s=if(%s==%s, %s, 0)\"%(buildings_height,baselandcoverraster,buildpixel,ndsm)\n", - "gscript.mapcalc(formula, overwrite=True)\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Creation of layer in \")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Compute the metric**" - ] - }, - { - "cell_type": "code", - "execution_count": 42, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "'i.segment.stats run in 10.4 seconds'" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Compute sum of build pixels's height using i.segment.stats \n", - "gscript.run_command('i.segment.stats', overwrite=True, map=raster_landscapeunits,\n", - " raster_statistics='sum', flags='s', rasters='buildings_height',\n", - " csvfile=TMP_sumheights,processes=ncores)\n", - "# Compute number of built pixels using i.segment.stats\n", - "binary_builtup_raster=\"%s_cl_%s\"%(baselandcoverraster.split(\"@\")[0],buildpixel)\n", - "gscript.run_command('g.copy', overwrite=True, raster='%s,tmp'%binary_builtup_raster)\n", - "gscript.run_command('r.null', map='tmp', null=0)\n", - "gscript.run_command('i.segment.stats', overwrite=True, map=raster_landscapeunits,\n", - " raster_statistics='sum', flags='s', rasters='tmp',\n", - " csvfile=TMP_nbrbuildpixels,processes=ncores)\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"i.segment.stats run in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"Mean build pixels's height computed in 0.0 seconds\"" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Save time for computing processin time\n", - "begintime=time.time()\n", - "# Improt library to be able to iterate on two files in the same loop\n", - "from itertools import izip\n", - "# Declare empty dictionnary\n", - "tmp_dic={}\n", - "for i, (line_from_file_1, line_from_file_2) in enumerate(izip(open(TMP_sumheights), open(TMP_nbrbuildpixels))):\n", - " if i==0:\n", - " continue\n", - " f1_items=line_from_file_1.split(\"\\n\")[0].split(\"|\")\n", - " f2_items=line_from_file_2.split(\"\\n\")[0].split(\"|\")\n", - " key=f1_items[0]\n", - " sumheight=f1_items[1]\n", - " nbpixel=f2_items[1]\n", - " try:\n", - " mean_height=float(sumheight)/float(nbpixel)\n", - " except ZeroDivisionError:\n", - " mean_height=0\n", - " tmp_dic[key]=mean_height\n", - "# Get the name of the first colum\n", - "with open(TMP_sumheights) as f:\n", - " column_a=f.next().split(\"\\n\")[0].split(\"|\")[0]\n", - "# Built the content of the file\n", - "content=[]\n", - "content.append((column_a,'mean_build_height'))\n", - "for key in tmp_dic.keys():\n", - " content.append((key,tmp_dic[key]))\n", - "# Create a new file\n", - "fout=open(metrics_buildings_height,\"w\")\n", - "writer=csv.writer(fout, delimiter='|')\n", - "writer.writerows(content)\n", - "fout.close()\n", - "# Compute and print processing time\n", - "print_processing_time(begintime,\"Mean build pixels's height computed in \")" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Remove temporary layers\n", - "gscript.run_command('g.remove', flags='ef', type='raster', name='tmp')\n", - "# Remove temporary files\n", - "os.remove(TMP_sumheights)\n", - "os.remove(TMP_nbrbuildpixels)" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "resultfiles.append(metrics_buildings_height)" - ] - }, - { - "cell_type": "code", - "execution_count": 63, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[u'/home/tais/.grass7/r.li/output/classif_dominance',\n", - " u'/home/tais/.grass7/r.li/output/classif_pielou',\n", - " u'/home/tais/.grass7/r.li/output/classif_renyi',\n", - " u'/home/tais/.grass7/r.li/output/classif_richness',\n", - " u'/home/tais/.grass7/r.li/output/classif_shannon',\n", - " u'/home/tais/.grass7/r.li/output/classif_simpson',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/landscape_units_shape_metrics',\n", - " u'/home/tais/.grass7/r.li/output/metrics_ndvi_ndwi_sar',\n", - " u'/home/tais/.grass7/r.li/output/metrics_buildings_height']" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "resultfiles" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "### Proportion of each of individual classes in the landcover map" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set a tuple with name of land cover class binary raster to be processed \n", - "listofbinary=tuple(raster_list[1:])\n", - "# Compute proportion of each land cover class in landscape units\n", - "proportion_results=get_classproportions(rli_output_dir,raster_landscapeunits,listofbinary,returnlistresult=True,ncores=10)" - ] - }, - { - "cell_type": "code", - "execution_count": 64, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[None, None, None, None, None, None, None, None]" - ] - }, - "execution_count": 64, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[resultfiles.append(x) for x in proportion_results]" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[u'/home/tais/.grass7/r.li/output/classif_dominance',\n", - " u'/home/tais/.grass7/r.li/output/classif_pielou',\n", - " u'/home/tais/.grass7/r.li/output/classif_renyi',\n", - " u'/home/tais/.grass7/r.li/output/classif_richness',\n", - " u'/home/tais/.grass7/r.li/output/classif_shannon',\n", - " u'/home/tais/.grass7/r.li/output/classif_simpson',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_11_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_13_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_14_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_20_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_30_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_31_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_41_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchnum',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_patchdensity',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_mps',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padcv',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padrange',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_padsd',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_shape',\n", - " u'/home/tais/.grass7/r.li/output/classif_cl_51_edgedensity',\n", - " u'/home/tais/.grass7/r.li/output/landscape_units_shape_metrics',\n", - " u'/home/tais/.grass7/r.li/output/metrics_ndvi_ndwi_sar',\n", - " u'/home/tais/.grass7/r.li/output/metrics_buildings_height',\n", - " u'/home/tais/.grass7/r.li/output/prop_11',\n", - " u'/home/tais/.grass7/r.li/output/prop_13',\n", - " u'/home/tais/.grass7/r.li/output/prop_14',\n", - " u'/home/tais/.grass7/r.li/output/prop_20',\n", - " u'/home/tais/.grass7/r.li/output/prop_30',\n", - " u'/home/tais/.grass7/r.li/output/prop_31',\n", - " u'/home/tais/.grass7/r.li/output/prop_41',\n", - " u'/home/tais/.grass7/r.li/output/prop_51']" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "resultfiles" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "# File management (join, cleaning, etc)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Combine all .csv files together" - ] - }, - { - "cell_type": "code", - "execution_count": 67, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Define the path for the .csv with final results\n", - "outfile=os.path.join(rli_output_dir,\"land_use_metrics.csv\")" - ] - }, - { - "cell_type": "code", - "execution_count": 68, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "81 individual .csv files were joint together.\n" - ] - } - ], - "source": [ - "# Join (inner join) all result files together in a new .csv file\n", - "join_multiplecsv(resultfiles,outfile,separator=\"|\",join='inner',fillempty='NULL',overwrite=True)\n", - "# Create .csvt file\n", - "create_csvt(outfile,separator=\"|\",first_col_type=\"Integer\",rest_type=\"Real\")" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Make a copy of the .csv file with results, where 'null' values are empty cells " - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**For .csvt**" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Define path to the .csv files\n", - "afile=outfile+'t'\n", - "pathtofile,extension=os.path.splitext(afile)\n", - "bfile=pathtofile+\"_blanknull\"+extension\n", - "# Make copy of the file\n", - "shutil.copy2(afile,bfile)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**For .csv**" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Define path to the .csv files\n", - "afile=outfile\n", - "pathtofile,extension=os.path.splitext(afile)\n", - "outfile_withoutnull=pathtofile+\"_blanknull\"+extension" - ] - }, - { - "cell_type": "code", - "execution_count": 71, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import csv\n", - "# Create a copy by removing the 'NULL' values\n", - "reader=csv.reader(open(afile,'r'),delimiter=\"|\")\n", - "writer=csv.writer(open(outfile_withoutnull,'w'),delimiter=\"|\")\n", - "for row in reader:\n", - " newline=[]\n", - " [newline.append(x) if x != \"NULL\" else newline.append(\"\") for x in row]\n", - " writer.writerow(newline)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Check if all the lines have the same number of items" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "#### Function that check if rows of a .csv file have the same number of items than number of column in the first line (header)\n", - "### If rows's lenght are identical, the function return 0. If not, it return a list containing the index number of row(s) that failed\n", - "### The index is starting with value 1 for the first line of data (the header row is indexed 0)\n", - "# 'csvfile' wait for the complete path (string) to the csvfile to be checked\n", - "# 'separator' wait for the character used as separator in the .csv file (string)\n", - "# 'allowemptycell' wait for a boolean value (True, False) depending if a empty cell should not be considered as a problem in the file\n", - "\n", - "def check_lenght_row(csvfile,separator,allowemptycell=True):\n", - " listofunequalrow=[]\n", - " afile=open(csvfile,'r')\n", - " header=afile.next() # Save header row and go to the next one\n", - " nb_item_control=len(header.split(separator))\n", - " for x,row in enumerate(afile,1): # Start counting at \"1\" because header is already skipped\n", - " row_items=row.split(separator)\n", - " if len(row_items) != nb_item_control: # Check it number of items is identical than the number of items in the header\n", - " listofunequalrow.append(x)\n", - " if not allowemptycell:\n", - " if \"\" in row_items:\n", - " listofunequalrow.append(x)\n", - " listofunequalrow=list(set(listofunequalrow)) # Recreate a list with uniques values from the original list\n", - " if len(listofunequalrow)>0:\n", - " return listofunequalrow # Return a list of indexes for line whose length is not equal to the header\n", - " else:\n", - " return 0" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The csv looks good.\n" - ] - } - ], - "source": [ - "# Check number of row in the .csv file\n", - "fout=check_lenght_row(outfile_withoutnull,\";\",allowemptycell=True)\n", - "if fout>0:\n", - " print \"Rows in csv do not have the same lenght. Please check the following row(s) index(es).\"\n", - " print \"/n\".join(fout)\n", - "else:\n", - " print \"The csv looks good.\"" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Replace current delimiter by ';'" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import csv\n", - "reader=csv.reader(open(outfile,'r'),delimiter=\"|\")\n", - "newfile=[]\n", - "for row in reader:\n", - " newline=[]\n", - " [newline.append(x) for x in row]\n", - " newfile.append(newline)\n", - "writer=csv.writer(open(outfile,'w'),delimiter=\";\")\n", - "writer.writerows(newfile)" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import csv\n", - "reader=csv.reader(open(outfile+'t','r'),delimiter=\"|\")\n", - "newfile=[]\n", - "for row in reader:\n", - " newline=[]\n", - " [newline.append(x) for x in row]\n", - " newfile.append(newline)\n", - "writer=csv.writer(open(outfile+'t','w'),delimiter=\";\")\n", - "writer.writerows(newfile)" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import csv\n", - "reader=csv.reader(open(outfile_withoutnull,'r'),delimiter=\"|\")\n", - "newfile=[]\n", - "for row in reader:\n", - " newline=[]\n", - " [newline.append(x) for x in row]\n", - " newfile.append(newline)\n", - "writer=csv.writer(open(outfile_withoutnull,'w'),delimiter=\";\")\n", - "writer.writerows(newfile)" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "import csv\n", - "reader=csv.reader(open(outfile_withoutnull+'t','r'),delimiter=\"|\")\n", - "newfile=[]\n", - "for row in reader:\n", - " newline=[]\n", - " [newline.append(x) for x in row]\n", - " newfile.append(newline)\n", - "writer=csv.writer(open(outfile_withoutnull+'t','w'),delimiter=\";\")\n", - "writer.writerows(newfile)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Visual check of the table (display the .csv using pandas)" - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
catdominancepielourenyirichnessshannonsimpsoncl_11_patchnumcl_11_patchdensitycl_11_mps...SAR_w7_7_medianmean_build_heightprop_11prop_13prop_14prop_20prop_30prop_31prop_41prop_51
010.2137080.8672161.25261351.3957300.714243189086.3200400.011006...1.0422402.68204539.175360.000000.0000032.036396.382569.655410.0000012.75028
120.8750100.5116470.65683460.9167490.4815102278501.3380910.001274...1.6436600.5960331.072940.000000.0000024.1933867.675803.214042.514381.32945
230.2377620.8673021.47581661.5539970.7714082514138.2723030.007073...1.2252602.80086128.722840.000000.0000029.1126918.6314711.317770.4710711.74416
340.3736150.6599210.62917130.7249970.466967218433.1797240.005425...0.7973371.28088232.607060.000000.0000065.289260.000000.000000.000002.10368
450.6859880.3755870.21789530.4126250.1957900NaN0.000000...0.8943130.0000000.000000.000000.000005.319155.3191589.361700.000000.00000
560.4406340.6821500.84347240.9456600.5697860NaN0.000000...1.3099100.0000000.000000.000000.0000042.4694749.479877.010400.000001.04025
671.3272860.2592280.24601360.4644740.2180883260491.4933840.001653...1.7707900.3134030.111010.000000.000007.3466188.021244.154730.001310.36509
780.8904490.1894790.09242930.2081630.0882860NaN0.000000...2.2523000.0000000.000000.000000.0000095.406033.758700.000000.000000.83527
890.1735360.8748201.09590941.2127580.6657640NaN0.000000...0.7914200.0000000.000000.000000.0000044.4889833.4669311.222440.0000010.82164
9100.4166840.7674441.20151261.3750750.6992611410588.0128570.009445...1.3195302.30961333.561770.310930.0000040.814778.934587.443370.000008.93458
10110.4860610.2987620.10566820.2070860.1002770NaN0.000000...3.1518700.0000000.000000.000000.0000094.705885.294120.000000.000000.00000
11121.3026260.3305830.36310470.6432840.3044866970951.1568120.001409...1.3533000.7864190.994590.000000.0654511.6214282.500503.402840.408321.00687
12130.4476330.7699621.32505571.4982770.7342124013314.4711660.007511...1.2165502.70358635.528030.000002.1907532.914505.7385310.693590.2808712.65374
13140.5052610.6860640.92001151.1041770.6014851418685.3520190.005352...0.3147601.39336433.872060.000000.0000052.542956.023961.311030.000006.25000
14150.6796980.6206530.94139561.1120610.609917188412.1976870.011888...0.4488821.73690743.876560.000000.0000043.779165.987591.543040.256324.55734
15160.5954000.6300570.83645451.0140380.5667561738288.2882880.002612...1.2541100.23137827.093820.000000.0000058.7185412.173910.000000.640731.37300
16170.6144000.6570971.00467661.1773590.633837108338.5449240.011992...0.8476331.36717743.541800.000000.0000040.855046.862121.697380.217856.82581
17180.3377150.8115171.30907061.4540440.729929297805.1406270.012812...1.1646902.91576538.953690.000000.0000028.8679812.101287.724160.3643211.98857
18190.4022120.7755211.21470261.3895480.7032029730003.0931020.003333...0.3616861.35938823.319390.000000.0000045.1871816.438267.741270.091967.22194
19200.8613030.5858010.99589481.2181390.630607328199.3466150.012196...0.6464501.68131045.472020.002912.3127839.483271.671963.480820.760256.81600
20210.8459770.5278510.79724760.9457830.5494321115907.4475780.006286...0.8396611.37835039.356860.000000.0000054.240181.067162.618100.128062.58964
21220.1169040.9273641.39680151.4925340.7526134810865.2594650.009204...1.3690802.87212634.173270.000000.0000028.8783615.399349.595820.0000011.95320
22230.6465700.5982640.83090750.9628680.564346971005.9171600.001408...2.3810000.3804681.066090.000000.0000047.4167845.698853.841710.000001.97658
23240.8359730.5703950.85604371.1099370.5751605820043.1965440.004989...1.0564601.28989324.319780.071440.0000059.510459.582941.502260.409714.60342
24250.6408940.6423100.97016561.1508660.6209792615271.6593250.006548...0.8362101.57787533.286080.000000.0000050.652529.159781.246400.078215.57701
25260.5105420.7150611.04540861.2812180.6484526024115.7556270.004147...0.8552761.07609524.683150.000000.0000052.1044710.885696.371690.282755.67226
26270.6620280.6816321.24373881.4174140.711695239993.4825110.010007...1.3948102.74777838.973790.059270.6519632.919864.529879.847170.2751812.74290
27281.0143430.4338840.51711960.7774160.4037641353388.0903490.001873...2.2018401.3507484.624440.000000.0000074.9121618.060960.246890.550751.60479
28290.3203490.8212101.32262961.4714100.733566285845.5114820.017107...1.2201703.03012240.926180.000000.0000023.8785912.799048.514180.3631213.51888
29300.4374950.7281690.90617451.1719430.5959336539956.9694180.002503...0.8697400.46745312.751820.000000.0000058.8872819.079723.366780.000005.91440
..................................................................
5565570.5783560.6772131.06128961.2134030.65399110528315.2430390.003532...0.5751971.11678233.538340.000000.0000045.754862.738152.896420.0429615.02928
5575580.0946610.9411841.43204251.5147770.7611796939473.6842110.002533...1.0136400.83381214.024950.000000.0000031.7707729.6786612.787340.0000011.73828
5585590.5002440.6891810.93254651.1091940.6064492265233.5063010.001533...2.0765900.3707452.965420.000000.0000038.2713149.099826.337520.000003.32593
5595600.6087410.6871691.17178771.3371700.69018711816236.1115890.006159...0.5666501.14734735.576090.051400.0000040.023257.209204.246470.2227312.67087
5605610.4762870.7552371.35395171.4696230.74178211724198.5522230.004132...0.6801451.59902531.170420.045130.0000034.277798.637149.036840.1192716.71341
5615620.4337350.7305050.94103351.1757030.6097751355201.6985140.001812...1.5275300.0000003.480510.000000.0000025.9412555.677079.214850.000005.68631
5625630.4456760.6785130.86263940.9406190.5779531266666.6666670.000375...2.6627200.5592120.137830.000000.000000.0000046.9723443.986030.000008.90379
5635640.4990460.7214771.08012461.2927130.6604479338048.4811290.002628...1.2251000.82091613.453970.034400.0000022.6833651.025183.712670.000009.09041
5645650.5341740.7254891.26857771.4117360.71876828518433.4777830.005425...0.7194281.44041534.220520.122290.0000036.262887.998476.436410.0984914.86095
5655660.5093420.7382501.06446871.4365680.6550892456371.1098060.001774...0.9913710.3392613.339810.000009.925287.6837154.931265.542153.8261714.75162
5665670.2482110.8457781.18924451.3612270.6955496348808.8320740.002049...1.8182101.1096596.259020.000000.0000014.6928745.2048226.230160.000007.61314
5675680.5032260.7413931.29747671.4426840.7267793111951.8072290.008367...0.4275151.74339040.560620.089920.0000026.381027.3771510.391340.0781915.12178
5685690.5343720.6679760.75098851.0750660.5281003279012.3456790.001266...1.5180000.5733024.880110.000000.0000015.0530266.077248.501020.000005.48861
5695700.4238970.7366180.93939851.1855410.609137724933.2146040.004011...2.0808701.4411983.122130.000000.0000014.1844420.2368757.090830.000005.36573
5705710.6675530.6274320.85772761.1242070.5758754460150.3759400.001662...2.2921600.7911011.947360.000000.0000010.5048160.6502320.055240.027296.81508
5715720.2381620.8520221.22800151.3712760.7071232846920.8211140.002131...1.5475801.1451905.491020.000000.0000013.3560335.7686737.026980.000008.35730
5725730.4544850.7463471.17020761.3372740.6896973612654.8905880.007902...0.6431621.79000144.385070.000000.0000028.794326.759764.532510.1131215.41522
5735740.3601310.7762381.08283151.2493070.6613651745242.8476380.002210...1.7379200.6338192.884500.000000.000007.4809042.7167737.542700.000009.37512
5745750.1773580.8898011.33354651.4320800.7364592159871.7034930.001670...1.1942000.9893457.003440.000000.0000034.2435028.5978123.481260.000006.67399
5755760.1133730.9295571.39050651.4960650.7510512331680.4407710.003157...1.5347600.85767815.301120.000000.0000022.8726537.8681714.347440.000009.61062
5765770.1268110.9212081.39832051.4826270.75298818846075.6081120.002170...1.2402200.8791648.502470.000000.0000025.1620233.0679924.309740.000008.95778
5775780.9035920.3481960.34344340.4827030.2906760NaN0.000000...2.7366000.0000000.000000.000000.0057217.1818382.449320.363140.000000.00000
5785790.6778980.6516290.94848371.2680120.6126725310976.4937350.009110...1.7182003.7952328.619050.000002.4954820.5056157.571466.096790.171364.54024
5795800.7220270.6289520.98953671.2238830.6282516512824.9395750.007797...1.9292403.4039659.841550.000000.7713829.2722152.256862.470460.266515.12102
5805810.5718550.4794750.35506630.5267570.2988731270484.5814980.001419...4.0000000.0212911.773530.000000.0000016.068550.000000.0000082.157920.00000
5815820.8151430.4119990.35702140.5711510.3002420NaN0.000000...2.4789600.0000000.000000.000000.000000.0925482.5724912.482010.000004.85297
5825831.0769180.4465740.52057570.8689930.40582115225531.2001340.003917...2.6209703.2417245.576810.000001.8163275.7832212.225950.407240.381483.80897
5835840.9873170.3865460.35222950.6221210.2968811111111.1111110.000900...1.9444400.3949740.019740.000000.000006.8629283.193277.581610.000002.34246
5845851.0899000.3917150.40317260.7018590.3318031384552.8455280.001183...1.6879500.2193730.463140.000000.000004.1939080.932779.482780.015064.91234
5855861.1210300.3034650.27025750.4884080.2368163114285.7142860.000875...1.9470700.5684640.301280.000000.0000010.4157786.717741.271130.000001.29408
\n", - "

586 rows × 117 columns

\n", - "
" - ], - "text/plain": [ - " cat dominance pielou renyi richness shannon simpson \\\n", - "0 1 0.213708 0.867216 1.252613 5 1.395730 0.714243 \n", - "1 2 0.875010 0.511647 0.656834 6 0.916749 0.481510 \n", - "2 3 0.237762 0.867302 1.475816 6 1.553997 0.771408 \n", - "3 4 0.373615 0.659921 0.629171 3 0.724997 0.466967 \n", - "4 5 0.685988 0.375587 0.217895 3 0.412625 0.195790 \n", - "5 6 0.440634 0.682150 0.843472 4 0.945660 0.569786 \n", - "6 7 1.327286 0.259228 0.246013 6 0.464474 0.218088 \n", - "7 8 0.890449 0.189479 0.092429 3 0.208163 0.088286 \n", - "8 9 0.173536 0.874820 1.095909 4 1.212758 0.665764 \n", - "9 10 0.416684 0.767444 1.201512 6 1.375075 0.699261 \n", - "10 11 0.486061 0.298762 0.105668 2 0.207086 0.100277 \n", - "11 12 1.302626 0.330583 0.363104 7 0.643284 0.304486 \n", - "12 13 0.447633 0.769962 1.325055 7 1.498277 0.734212 \n", - "13 14 0.505261 0.686064 0.920011 5 1.104177 0.601485 \n", - "14 15 0.679698 0.620653 0.941395 6 1.112061 0.609917 \n", - "15 16 0.595400 0.630057 0.836454 5 1.014038 0.566756 \n", - "16 17 0.614400 0.657097 1.004676 6 1.177359 0.633837 \n", - "17 18 0.337715 0.811517 1.309070 6 1.454044 0.729929 \n", - "18 19 0.402212 0.775521 1.214702 6 1.389548 0.703202 \n", - "19 20 0.861303 0.585801 0.995894 8 1.218139 0.630607 \n", - "20 21 0.845977 0.527851 0.797247 6 0.945783 0.549432 \n", - "21 22 0.116904 0.927364 1.396801 5 1.492534 0.752613 \n", - "22 23 0.646570 0.598264 0.830907 5 0.962868 0.564346 \n", - "23 24 0.835973 0.570395 0.856043 7 1.109937 0.575160 \n", - "24 25 0.640894 0.642310 0.970165 6 1.150866 0.620979 \n", - "25 26 0.510542 0.715061 1.045408 6 1.281218 0.648452 \n", - "26 27 0.662028 0.681632 1.243738 8 1.417414 0.711695 \n", - "27 28 1.014343 0.433884 0.517119 6 0.777416 0.403764 \n", - "28 29 0.320349 0.821210 1.322629 6 1.471410 0.733566 \n", - "29 30 0.437495 0.728169 0.906174 5 1.171943 0.595933 \n", - ".. ... ... ... ... ... ... ... \n", - "556 557 0.578356 0.677213 1.061289 6 1.213403 0.653991 \n", - "557 558 0.094661 0.941184 1.432042 5 1.514777 0.761179 \n", - "558 559 0.500244 0.689181 0.932546 5 1.109194 0.606449 \n", - "559 560 0.608741 0.687169 1.171787 7 1.337170 0.690187 \n", - "560 561 0.476287 0.755237 1.353951 7 1.469623 0.741782 \n", - "561 562 0.433735 0.730505 0.941033 5 1.175703 0.609775 \n", - "562 563 0.445676 0.678513 0.862639 4 0.940619 0.577953 \n", - "563 564 0.499046 0.721477 1.080124 6 1.292713 0.660447 \n", - "564 565 0.534174 0.725489 1.268577 7 1.411736 0.718768 \n", - "565 566 0.509342 0.738250 1.064468 7 1.436568 0.655089 \n", - "566 567 0.248211 0.845778 1.189244 5 1.361227 0.695549 \n", - "567 568 0.503226 0.741393 1.297476 7 1.442684 0.726779 \n", - "568 569 0.534372 0.667976 0.750988 5 1.075066 0.528100 \n", - "569 570 0.423897 0.736618 0.939398 5 1.185541 0.609137 \n", - "570 571 0.667553 0.627432 0.857727 6 1.124207 0.575875 \n", - "571 572 0.238162 0.852022 1.228001 5 1.371276 0.707123 \n", - "572 573 0.454485 0.746347 1.170207 6 1.337274 0.689697 \n", - "573 574 0.360131 0.776238 1.082831 5 1.249307 0.661365 \n", - "574 575 0.177358 0.889801 1.333546 5 1.432080 0.736459 \n", - "575 576 0.113373 0.929557 1.390506 5 1.496065 0.751051 \n", - "576 577 0.126811 0.921208 1.398320 5 1.482627 0.752988 \n", - "577 578 0.903592 0.348196 0.343443 4 0.482703 0.290676 \n", - "578 579 0.677898 0.651629 0.948483 7 1.268012 0.612672 \n", - "579 580 0.722027 0.628952 0.989536 7 1.223883 0.628251 \n", - "580 581 0.571855 0.479475 0.355066 3 0.526757 0.298873 \n", - "581 582 0.815143 0.411999 0.357021 4 0.571151 0.300242 \n", - "582 583 1.076918 0.446574 0.520575 7 0.868993 0.405821 \n", - "583 584 0.987317 0.386546 0.352229 5 0.622121 0.296881 \n", - "584 585 1.089900 0.391715 0.403172 6 0.701859 0.331803 \n", - "585 586 1.121030 0.303465 0.270257 5 0.488408 0.236816 \n", - "\n", - " cl_11_patchnum cl_11_patchdensity cl_11_mps ... SAR_w7_7_median \\\n", - "0 18 9086.320040 0.011006 ... 1.042240 \n", - "1 22 78501.338091 0.001274 ... 1.643660 \n", - "2 25 14138.272303 0.007073 ... 1.225260 \n", - "3 2 18433.179724 0.005425 ... 0.797337 \n", - "4 0 NaN 0.000000 ... 0.894313 \n", - "5 0 NaN 0.000000 ... 1.309910 \n", - "6 32 60491.493384 0.001653 ... 1.770790 \n", - "7 0 NaN 0.000000 ... 2.252300 \n", - "8 0 NaN 0.000000 ... 0.791420 \n", - "9 14 10588.012857 0.009445 ... 1.319530 \n", - "10 0 NaN 0.000000 ... 3.151870 \n", - "11 69 70951.156812 0.001409 ... 1.353300 \n", - "12 40 13314.471166 0.007511 ... 1.216550 \n", - "13 14 18685.352019 0.005352 ... 0.314760 \n", - "14 18 8412.197687 0.011888 ... 0.448882 \n", - "15 17 38288.288288 0.002612 ... 1.254110 \n", - "16 10 8338.544924 0.011992 ... 0.847633 \n", - "17 29 7805.140627 0.012812 ... 1.164690 \n", - "18 97 30003.093102 0.003333 ... 0.361686 \n", - "19 32 8199.346615 0.012196 ... 0.646450 \n", - "20 11 15907.447578 0.006286 ... 0.839661 \n", - "21 48 10865.259465 0.009204 ... 1.369080 \n", - "22 9 71005.917160 0.001408 ... 2.381000 \n", - "23 58 20043.196544 0.004989 ... 1.056460 \n", - "24 26 15271.659325 0.006548 ... 0.836210 \n", - "25 60 24115.755627 0.004147 ... 0.855276 \n", - "26 23 9993.482511 0.010007 ... 1.394810 \n", - "27 13 53388.090349 0.001873 ... 2.201840 \n", - "28 28 5845.511482 0.017107 ... 1.220170 \n", - "29 65 39956.969418 0.002503 ... 0.869740 \n", - ".. ... ... ... ... ... \n", - "556 105 28315.243039 0.003532 ... 0.575197 \n", - "557 69 39473.684211 0.002533 ... 1.013640 \n", - "558 22 65233.506301 0.001533 ... 2.076590 \n", - "559 118 16236.111589 0.006159 ... 0.566650 \n", - "560 117 24198.552223 0.004132 ... 0.680145 \n", - "561 13 55201.698514 0.001812 ... 1.527530 \n", - "562 1 266666.666667 0.000375 ... 2.662720 \n", - "563 93 38048.481129 0.002628 ... 1.225100 \n", - "564 285 18433.477783 0.005425 ... 0.719428 \n", - "565 24 56371.109806 0.001774 ... 0.991371 \n", - "566 63 48808.832074 0.002049 ... 1.818210 \n", - "567 31 11951.807229 0.008367 ... 0.427515 \n", - "568 32 79012.345679 0.001266 ... 1.518000 \n", - "569 7 24933.214604 0.004011 ... 2.080870 \n", - "570 44 60150.375940 0.001662 ... 2.292160 \n", - "571 28 46920.821114 0.002131 ... 1.547580 \n", - "572 36 12654.890588 0.007902 ... 0.643162 \n", - "573 17 45242.847638 0.002210 ... 1.737920 \n", - "574 21 59871.703493 0.001670 ... 1.194200 \n", - "575 23 31680.440771 0.003157 ... 1.534760 \n", - "576 188 46075.608112 0.002170 ... 1.240220 \n", - "577 0 NaN 0.000000 ... 2.736600 \n", - "578 53 10976.493735 0.009110 ... 1.718200 \n", - "579 65 12824.939575 0.007797 ... 1.929240 \n", - "580 12 70484.581498 0.001419 ... 4.000000 \n", - "581 0 NaN 0.000000 ... 2.478960 \n", - "582 152 25531.200134 0.003917 ... 2.620970 \n", - "583 1 111111.111111 0.000900 ... 1.944440 \n", - "584 13 84552.845528 0.001183 ... 1.687950 \n", - "585 3 114285.714286 0.000875 ... 1.947070 \n", - "\n", - " mean_build_height prop_11 prop_13 prop_14 prop_20 prop_30 \\\n", - "0 2.682045 39.17536 0.00000 0.00000 32.03639 6.38256 \n", - "1 0.596033 1.07294 0.00000 0.00000 24.19338 67.67580 \n", - "2 2.800861 28.72284 0.00000 0.00000 29.11269 18.63147 \n", - "3 1.280882 32.60706 0.00000 0.00000 65.28926 0.00000 \n", - "4 0.000000 0.00000 0.00000 0.00000 5.31915 5.31915 \n", - "5 0.000000 0.00000 0.00000 0.00000 42.46947 49.47987 \n", - "6 0.313403 0.11101 0.00000 0.00000 7.34661 88.02124 \n", - "7 0.000000 0.00000 0.00000 0.00000 95.40603 3.75870 \n", - "8 0.000000 0.00000 0.00000 0.00000 44.48898 33.46693 \n", - "9 2.309613 33.56177 0.31093 0.00000 40.81477 8.93458 \n", - "10 0.000000 0.00000 0.00000 0.00000 94.70588 5.29412 \n", - "11 0.786419 0.99459 0.00000 0.06545 11.62142 82.50050 \n", - "12 2.703586 35.52803 0.00000 2.19075 32.91450 5.73853 \n", - "13 1.393364 33.87206 0.00000 0.00000 52.54295 6.02396 \n", - "14 1.736907 43.87656 0.00000 0.00000 43.77916 5.98759 \n", - "15 0.231378 27.09382 0.00000 0.00000 58.71854 12.17391 \n", - "16 1.367177 43.54180 0.00000 0.00000 40.85504 6.86212 \n", - "17 2.915765 38.95369 0.00000 0.00000 28.86798 12.10128 \n", - "18 1.359388 23.31939 0.00000 0.00000 45.18718 16.43826 \n", - "19 1.681310 45.47202 0.00291 2.31278 39.48327 1.67196 \n", - "20 1.378350 39.35686 0.00000 0.00000 54.24018 1.06716 \n", - "21 2.872126 34.17327 0.00000 0.00000 28.87836 15.39934 \n", - "22 0.380468 1.06609 0.00000 0.00000 47.41678 45.69885 \n", - "23 1.289893 24.31978 0.07144 0.00000 59.51045 9.58294 \n", - "24 1.577875 33.28608 0.00000 0.00000 50.65252 9.15978 \n", - "25 1.076095 24.68315 0.00000 0.00000 52.10447 10.88569 \n", - "26 2.747778 38.97379 0.05927 0.65196 32.91986 4.52987 \n", - "27 1.350748 4.62444 0.00000 0.00000 74.91216 18.06096 \n", - "28 3.030122 40.92618 0.00000 0.00000 23.87859 12.79904 \n", - "29 0.467453 12.75182 0.00000 0.00000 58.88728 19.07972 \n", - ".. ... ... ... ... ... ... \n", - "556 1.116782 33.53834 0.00000 0.00000 45.75486 2.73815 \n", - "557 0.833812 14.02495 0.00000 0.00000 31.77077 29.67866 \n", - "558 0.370745 2.96542 0.00000 0.00000 38.27131 49.09982 \n", - "559 1.147347 35.57609 0.05140 0.00000 40.02325 7.20920 \n", - "560 1.599025 31.17042 0.04513 0.00000 34.27779 8.63714 \n", - "561 0.000000 3.48051 0.00000 0.00000 25.94125 55.67707 \n", - "562 0.559212 0.13783 0.00000 0.00000 0.00000 46.97234 \n", - "563 0.820916 13.45397 0.03440 0.00000 22.68336 51.02518 \n", - "564 1.440415 34.22052 0.12229 0.00000 36.26288 7.99847 \n", - "565 0.339261 3.33981 0.00000 9.92528 7.68371 54.93126 \n", - "566 1.109659 6.25902 0.00000 0.00000 14.69287 45.20482 \n", - "567 1.743390 40.56062 0.08992 0.00000 26.38102 7.37715 \n", - "568 0.573302 4.88011 0.00000 0.00000 15.05302 66.07724 \n", - "569 1.441198 3.12213 0.00000 0.00000 14.18444 20.23687 \n", - "570 0.791101 1.94736 0.00000 0.00000 10.50481 60.65023 \n", - "571 1.145190 5.49102 0.00000 0.00000 13.35603 35.76867 \n", - "572 1.790001 44.38507 0.00000 0.00000 28.79432 6.75976 \n", - "573 0.633819 2.88450 0.00000 0.00000 7.48090 42.71677 \n", - "574 0.989345 7.00344 0.00000 0.00000 34.24350 28.59781 \n", - "575 0.857678 15.30112 0.00000 0.00000 22.87265 37.86817 \n", - "576 0.879164 8.50247 0.00000 0.00000 25.16202 33.06799 \n", - "577 0.000000 0.00000 0.00000 0.00572 17.18183 82.44932 \n", - "578 3.795232 8.61905 0.00000 2.49548 20.50561 57.57146 \n", - "579 3.403965 9.84155 0.00000 0.77138 29.27221 52.25686 \n", - "580 0.021291 1.77353 0.00000 0.00000 16.06855 0.00000 \n", - "581 0.000000 0.00000 0.00000 0.00000 0.09254 82.57249 \n", - "582 3.241724 5.57681 0.00000 1.81632 75.78322 12.22595 \n", - "583 0.394974 0.01974 0.00000 0.00000 6.86292 83.19327 \n", - "584 0.219373 0.46314 0.00000 0.00000 4.19390 80.93277 \n", - "585 0.568464 0.30128 0.00000 0.00000 10.41577 86.71774 \n", - "\n", - " prop_31 prop_41 prop_51 \n", - "0 9.65541 0.00000 12.75028 \n", - "1 3.21404 2.51438 1.32945 \n", - "2 11.31777 0.47107 11.74416 \n", - "3 0.00000 0.00000 2.10368 \n", - "4 89.36170 0.00000 0.00000 \n", - "5 7.01040 0.00000 1.04025 \n", - "6 4.15473 0.00131 0.36509 \n", - "7 0.00000 0.00000 0.83527 \n", - "8 11.22244 0.00000 10.82164 \n", - "9 7.44337 0.00000 8.93458 \n", - "10 0.00000 0.00000 0.00000 \n", - "11 3.40284 0.40832 1.00687 \n", - "12 10.69359 0.28087 12.65374 \n", - "13 1.31103 0.00000 6.25000 \n", - "14 1.54304 0.25632 4.55734 \n", - "15 0.00000 0.64073 1.37300 \n", - "16 1.69738 0.21785 6.82581 \n", - "17 7.72416 0.36432 11.98857 \n", - "18 7.74127 0.09196 7.22194 \n", - "19 3.48082 0.76025 6.81600 \n", - "20 2.61810 0.12806 2.58964 \n", - "21 9.59582 0.00000 11.95320 \n", - "22 3.84171 0.00000 1.97658 \n", - "23 1.50226 0.40971 4.60342 \n", - "24 1.24640 0.07821 5.57701 \n", - "25 6.37169 0.28275 5.67226 \n", - "26 9.84717 0.27518 12.74290 \n", - "27 0.24689 0.55075 1.60479 \n", - "28 8.51418 0.36312 13.51888 \n", - "29 3.36678 0.00000 5.91440 \n", - ".. ... ... ... \n", - "556 2.89642 0.04296 15.02928 \n", - "557 12.78734 0.00000 11.73828 \n", - "558 6.33752 0.00000 3.32593 \n", - "559 4.24647 0.22273 12.67087 \n", - "560 9.03684 0.11927 16.71341 \n", - "561 9.21485 0.00000 5.68631 \n", - "562 43.98603 0.00000 8.90379 \n", - "563 3.71267 0.00000 9.09041 \n", - "564 6.43641 0.09849 14.86095 \n", - "565 5.54215 3.82617 14.75162 \n", - "566 26.23016 0.00000 7.61314 \n", - "567 10.39134 0.07819 15.12178 \n", - "568 8.50102 0.00000 5.48861 \n", - "569 57.09083 0.00000 5.36573 \n", - "570 20.05524 0.02729 6.81508 \n", - "571 37.02698 0.00000 8.35730 \n", - "572 4.53251 0.11312 15.41522 \n", - "573 37.54270 0.00000 9.37512 \n", - "574 23.48126 0.00000 6.67399 \n", - "575 14.34744 0.00000 9.61062 \n", - "576 24.30974 0.00000 8.95778 \n", - "577 0.36314 0.00000 0.00000 \n", - "578 6.09679 0.17136 4.54024 \n", - "579 2.47046 0.26651 5.12102 \n", - "580 0.00000 82.15792 0.00000 \n", - "581 12.48201 0.00000 4.85297 \n", - "582 0.40724 0.38148 3.80897 \n", - "583 7.58161 0.00000 2.34246 \n", - "584 9.48278 0.01506 4.91234 \n", - "585 1.27113 0.00000 1.29408 \n", - "\n", - "[586 rows x 117 columns]" - ] - }, - "execution_count": 78, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "import pandas as pd\n", - "# Load the .csv file in a pandas dataframe\n", - "df=pd.read_csv(outfile_withoutnull, sep=';',header=0)\n", - "# Display the dataframe\n", - "df" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Move files to dedicated folder" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Configuration files**" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the folder where to move the configuration files\n", - "finalfolder='/home/tais/Documents/GRASSDATA_Spie2017subset_Ouaga/Results_spatial_metrics/rli_config'" - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "## Create the folder if does not exists\n", - "if not os.path.exists(finalfolder):\n", - " os.makedirs(finalfolder)\n", - " print \"Folder '\"+finalfolder+\"' created\"\n", - "## Copy the files to the final folder and remove them from the original folder\n", - "for configfile in list_configfile:\n", - " shutil.copy2(configfile,finalfolder)\n", - " os.remove(configfile)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "**Result files**" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Set the folder where to move the configuration files\n", - "finalfolder='/home/tais/Documents/GRASSDATA_Spie2017subset_Ouaga/Results_spatial_metrics/rli_results'" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "## Create the folder if does not exists\n", - "if not os.path.exists(finalfolder):\n", - " os.makedirs(finalfolder)\n", - " print \"Folder '\"+finalfolder+\"' created\"\n", - "## Copy the files to the final folder and remove them from the original folder\n", - "for res_file in resultfiles:\n", - " shutil.copy2(res_file,finalfolder)\n", - " os.remove(res_file)" - ] - }, - { - "cell_type": "code", - "execution_count": 83, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Copy the final csv file with all the results\n", - "shutil.copy2(outfile,finalfolder)\n", - "os.remove(outfile)\n", - "shutil.copy2(outfile+'t',finalfolder)\n", - "os.remove(outfile+'t')" - ] - }, - { - "cell_type": "code", - "execution_count": 84, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Reasign the new path to the variable\n", - "outfile=os.path.join(finalfolder,os.path.split(outfile)[-1])" - ] - }, - { - "cell_type": "code", - "execution_count": 85, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Copy the final csv file with all the results\n", - "shutil.copy2(outfile_withoutnull,finalfolder)\n", - "os.remove(outfile_withoutnull)\n", - "shutil.copy2(outfile_withoutnull+'t',finalfolder)\n", - "os.remove(outfile_withoutnull+'t')" - ] - }, - { - "cell_type": "code", - "execution_count": 86, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Reasign the new path to the variable\n", - "outfile_withoutnull=os.path.join(finalfolder,os.path.split(outfile_withoutnull)[-1])" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Export the landscape polygons (with 'cat' column) as shapefile" - ] - }, - { - "cell_type": "code", - "execution_count": 87, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Define the name of the shapefile with landscape units and the 'cat' column\n", - "outputshp='/home/tais/Documents/GRASSDATA_Spie2017subset_Ouaga/Results_spatial_metrics/shapefile/Ouaga_subset_streetblocks.shp'" - ] - }, - { - "cell_type": "code", - "execution_count": 88, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "## Create the folder if does not exists\n", - "dirname=os.path.dirname(outputshp)\n", - "if not os.path.exists(dirname):\n", - " os.makedirs(dirname)\n", - " print \"Folder '\"+dirname+\"' created\"" - ] - }, - { - "cell_type": "code", - "execution_count": 89, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 89, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Export vector layer as shapefile\n", - "gscript.run_command('v.out.ogr', flags='cem', overwrite=True, \n", - " input=landscape_polygons, output=outputshp, format='ESRI_Shapefile')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Join the .csv file to the landscape unit polygon layer" - ] - }, - { - "cell_type": "code", - "execution_count": 90, - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 90, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Import .csv as new table in GRASS\n", - "csvfile=outfile\n", - "gscript.run_command('db.in.ogr', overwrite=True, quiet=True, input=csvfile, output='spatial_metrics_table')" - ] - }, - { - "cell_type": "code", - "execution_count": 91, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 91, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Join the vector layer with the new table\n", - "gscript.run_command('v.db.join', quiet=True, map=landscape_polygons, column='cat', \n", - " other_table='spatial_metrics_table', other_column='cat_')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Remove temporary raster layer with landscape units" - ] - }, - { - "cell_type": "code", - "execution_count": 92, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "gscript.run_command('g.remove',flags='f',type='raster', name=raster_landscapeunits)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "collapsed": true - }, - "source": [ - "## Fill the NULL values in .csv files with 0" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "If you want to fill NULL values with 0 value, please run the following cell" - ] - }, - { - "cell_type": "code", - "execution_count": 94, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "path,filename=os.path.split(outfile_withoutnull)\n", - "base,ext=os.path.splitext(filename)\n", - "newfilename=\"_\".join(base.split(\"_\")[:-1])+\"_nomissing\"+ext\n", - "outfile_nomissing=os.path.join(path,newfilename)" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Copy the csvt file \n", - "shutil.copy2(outfile_withoutnull+'t',outfile_nomissing+'t')" - ] - }, - { - "cell_type": "code", - "execution_count": 96, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "# Create a copy of the result file with NULL filled by 0 value\n", - "reader=csv.reader(open(outfile_withoutnull,'r'),delimiter=\";\")\n", - "writer=csv.writer(open(outfile_nomissing,'w'),delimiter=\";\")\n", - "content=[]\n", - "content.append(reader.next())\n", - "for row in reader:\n", - " new_row=[]\n", - " for value in row:\n", - " if value in ('NULL',''):\n", - " new_row.append('0')\n", - " else:\n", - " new_row.append(value)\n", - " content.append(new_row)\n", - "writer.writerows(content)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-**" - ] - } - ], - "metadata": { - "anaconda-cloud": {}, - "kernelspec": { - "display_name": "Python 2", - "language": "python", - "name": "python2" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 2 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.12" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -}