Translate

顯示具有 QGIS 標籤的文章。 顯示所有文章
顯示具有 QGIS 標籤的文章。 顯示所有文章

2013年12月10日 星期二

[QGIS][2.x] Example of iterating layers, attributes and features

layermap = QgsMapLayerRegistry.instance().mapLayers()
   
for (name,layer) in layermap.iteritems():
    if layer.type() != QgsVectorLayer.VectorLayer:
        continue
     
    if "SampleLayer" in layer.name():
        dP = layer.dataProvider()
     
        for attr in dP.fields():
            if (attr.name() == "SampleAttr"):
                SampleAttrId = dP.fieldNameIndex(attr.name())
                break
 
 
    layer.select(dP.attributeIndexes())
     
    FetSet = layer.getFeatures()
 
    for feat in FetSet:
        SampleAttrStr = str(feat[SampleAttrId])
        SampleAttrValue = float(str(feat[SampleAttrId]))

2013年8月29日 星期四

[QGIS] Draw vector layer features correctly on the mapCanvas

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()

[QGIS] Coordinate Transformation

https://github.com/walter426/QgisUtilites/blob/master/QgsUtilities.py

It is a very useful and frequently used light tool to do coordinate transformation from a source CRS(Coordinate Reference System) to a destination CRS.

Code:

from qgis.core import *

def CoorTransformByCrsId(point, crs_id_src, crs_id_des):
    crs_src = QgsCoordinateReferenceSystem()
    crs_src.createFromSrid(crs_id_src)

    crs_des = QgsCoordinateReferenceSystem()
    crs_des.createFromSrid(crs_id_des)

    transformer = QgsCoordinateTransform(crs_src, crs_des)
    pt = transformer.transform(point)
    
    return pt


def CoorTransform(point, crs_src, crs_des):
    transformer = QgsCoordinateTransform(crs_src, crs_des)
    pt = transformer.transform(point)
    
    return pt

2013年7月11日 星期四

[QGIS] QGIS Plugin - GeoSearch

A QGIS Plugin Tool for searching locations by address or position(latitude, longtitude).  Although 'GeoSearch' has similar function with 'GeoCode', but 'GeoSearch' has a easier GUI and zoom in feature.

Features:
Search location(with elevation) by words or point like google map; 
Calculate Distance between two points on mapCanvas.; 
Draw Route with multi ways points by google maps service.

https://github.com/walter426/QgisPlugin_GeoSearch/

2013年6月13日 星期四

[QGIS] QgisUtilites

https://github.com/walter426/QgisUtilites/

Description:
[Python QGIS]: Collection of Utilites frequently used

SQLiteTool:
- Initialize SQLite Db
- Exampls to create SQLite Tables
- Create a table Joining Two SQLite points into a Line string
- Delete Spatialite Geometry Column
- Recover Spatialite Geometry Column
- Add Spatialite Geometry Column
- Data Type Mapping from xlrd to SQLite