1

I'm developing my plugin and I want after selected some feature highlight them on mapCanvas. My code is below but it does not work correctly, I don't get an error but I cannot view the highlighted features: ZoomTo recall addHighlight method that highlight feature selected. Feature Extent use getFeatures for take feature and append (layer,feature) on self.feature[].

The code is:

def featureExtent(self, key, element_type,x,y, highlight=False):
        r = QgsRectangle()
        r.setMinimal()
        #d = self.searchRadiusMU(self.canvas)
        #p = QgsPoint(float(x),float(y))

        f = QgsFeature()
        for l in self.plugin.elementLayers(element_type):
            f2=l.getFeatures(QgsFeatureRequest().setFilterExpression(u"ELECTRICAL_LINE_ID={}".format(key)))# tutte le linee che hanno quell attributo = alle line selezionate
            idIdx = l.fieldNameIndex(u"ELECTRICAL_LINE_ID") 
            if(idIdx>-1):
              while f2.nextFeature(f):
                #attrs = f.attributes()
                #id=attrs[idIdx]   
                #if(id==key):
                  if highlight:
                     self.features.append([l, f])
                  fe = f.geometry().boundingBox()
                  r.combineExtentWith(fe)
                  #break

        return r

def addHighlight(self, l, f):
    s = QSettings()
    #logMessage("CURRENT COLOR {}".format(self.currentColor))
    if(self.currentColor is None):
        c = QColor(s.value("/Map/highlight/color", QGis.DEFAULT_HIGHLIGHT_COLOR.name()))
    else:
        c = self.currentColor 
    h = QgsHighlight(self.iface.mapCanvas(), f.geometry(), l)
    h.setWidth(8)
    h.setColor(c)
    c.setAlpha(int(s.value("/Map/highlight/colorAlpha", QGis.DEFAULT_HIGHLIGHT_COLOR.alpha())))
    h.setFillColor(c)
    #ADD FOR TEST
    h.setBuffer(float(s.value("/Map/highlight/buffer", QGis.DEFAULT_HIGHLIGHT_BUFFER_MM)))
    h.setMinWidth(float(s.value("/Map/highlight/minWidth", QGis.DEFAULT_HIGHLIGHT_MIN_WIDTH_MM)))        
    #
    h.show()

    return h 

  def zoomTo(self, r):
    if r.isNull():
        return

    r.scale(1.1)

    c = self.iface.mapCanvas()
    if c.hasCrsTransformEnabled():
        t = QgsCoordinateTransform(QgsCoordinateReferenceSystem(GEOSRID, QgsCoordinateReferenceSystem.PostgisCrsId), c.mapSettings().destinationCrs())
        r = t.transform(r)

    self.iface.mapCanvas().setExtent(r)

     for f in self.features:
        self.highlights.append(self.addHighlight(f[0], f[1]))
        #logMessage("FEATURES: {}".format(repr(self.features)))
        #f[0].setSelectedFeatures(f[1].id())
    self.features = []

    self.iface.mapCanvas().refresh() 
  • What you import at first is missing, right? – DirkB. Jul 6 '17 at 6:36

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.