I would like to put background border - with negative values - in my squere landscape in order to use in fragstats. I'm trying to do it with pyqgis. Exemple what I would like to do (figure 3).

So, I have a land use map (vector) and a grid (vector) (fig 1). I would like to clip land use map with this grid (one by one square grid) through pyqgis script. If possible I set this result in memory. In my code I set to save into temporary folder. I have to do it because I need to do many operations with a clipped land use separately in order to achieve the example result(figure 3) for my case. I try to do it with pyqgis but I dind't. There was a problem with my script. When I have run it, the land use didn't clip completely. What is the problem? The clip was running however it was only do in few polygons (fig 2). Figure 1: Vector Land use and grid

Figure 2: Clipped land use but only few polygons were clipped (red polygons)

Figure 3:Example of fragstats background with negative values around border square.

Here is my code:

# coding: utf-8
import processing 
from qgis.analysis import *
from PyQt4.QtGui import QInputDialog

#Defining vector land use patch
path_map = QgsVectorLayer("/media/dogosousa/56A22ED6A22EBA7F/
Geoprocessamento/Projetos_2015/Sisbiota/Vetor/Dados_Brutos/mapeamento_total.shp", "landscape use", "ogr")
path_grid = QgsVectorLayer("/media/dogosousa/56A22ED6A22EBA7F/Geoprocessamento/Projetos_2015/Sisbiota/Vetor/buffer_square_point/square_3700m/square_3700m_join_sirgas200024S.shp", 
"grid", "ogr")

#Load layer grid into canvas

#Doing loop in grid vector rows
canvas_layer = iface.activeLayer()

features = canvas_layer.getFeatures()

for feat in features:
    attrs = feat.attributes()
    print feat.id()
    print attrs[4]
    overlaylayer = path_grid.setSelectedFeatures([feat.id()])

    clip = processing.runalg("qgis:clip", path_grid,  path_map, "/tmp/"+"clip_"+str(attrs[4])+".shp")

    #Conferir se a layer foi criada como esperado
    if not path_grid.isValid():
        print "Layer %s did not load" % path_grid.name()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.