Since the CRS stored in a vector layer may not be the same as the one which the current project using.
It must convert the coordinates of features first for correct drawing.
Below is an example to do it.
from qgis.core import *
from qgis.gui import *
mapCanvas = iface.mapCanvas
RubberBand = QgsRubberBand(mapCanvas, False)
DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand)
self.mapCanvas.refresh()
def DrawVectorsInCandList(mapCanvas, CandList, VL, color, RubberBand_l):
if len(CandList) <= 0: #to cater for -1 or +1 frequency
RubberBand_l.reset(False)
return
str_find = ""
for item in CandList:
str_find = str_find + 'or CandID=\'' + item + '\''
str_find = str_find.lstrip('or ')
VL.setSubsetString(str_find)
VL.invertSelection()
VL.setSubsetString("")
featlist = VL.selectedFeatures()
if VL.selectedFeatureCount == 0:
return
fNIdx_CandID = VL.fieldNameIndex("CandID")
Qgs_MPL = QgsGeometry().asMultiPolyline()
VL_crs = QgsCoordinateReferenceSystem()
VL_crs.createFromEpsg(VL.crs().epsg())
mapCanvas_crs = mapCanvas.mapRenderer().destinationCrs()
for feature in featlist:
QgsPoint_O = feature.geometry().vertexAt(0)
QgsPoint_O = CoorTransform(QgsPoint_O, VL_crs, mapCanvas_crs)
QgsPoint_D = feature.geometry().vertexAt(1)
QgsPoint_D = CoorTransform(QgsPoint_D, VL_crs, mapCanvas_crs)
Qgs_MPL.append([QgsPoint_O, QgsPoint_D])
RubberBand_l.reset(False)
RubberBand_l.setColor(color)
RubberBand_l.setWidth(2)
RubberBand_l.setToGeometry(QgsGeometry.fromMultiPolyline(Qgs_MPL), None)
RubberBand_l.show()
沒有留言:
張貼留言