patch_python_snippets_cookbook.txt

Load of MySQL geometries and Graduated symbol renderer - gcarrillo -, 2011-03-01 06:30 PM

Download (4.25 KB)

 
1
Index: source/loadlayer.rst
2
===================================================================
3
--- source/loadlayer.rst	(revisión: 15306)
4
+++ source/loadlayer.rst	(copia de trabajo)
5
@@ -25,6 +25,11 @@
6
 
7
     vlayer = QgsVectorLayer("/path/to/shapefile/file.shp", "layer_name_you_like", "ogr")
8
 
9
+* MySQL WKB-based geometries, through OGR - data source is the connection string to the table::
10
+    
11
+    uri = "MySQL:dbname,host=localhost,port=3306,user=root,password=xxx|layername=my_table"
12
+    vlayer = QgsVectorLayer( uri, "my_table", "ogr" )
13
+
14
 * PostGIS database - data source is a string with all information needed to create a connection to PostgreSQL database. :class:`QgsDataSourceURI` class can generate this string for you. 
15
   Note that QGIS has to be compiled with Postgres support, otherwise this provider isn't available.
16
   ::
17
Index: source/vector.rst
18
===================================================================
19
--- source/vector.rst	(revisión: 15306)
20
+++ source/vector.rst	(copia de trabajo)
21
@@ -575,7 +575,7 @@
22
 
23
 * single symbol renderer (:class:`QgsSingleSymbolRenderer`) --- all features are rendererd with the same symbol.
24
 * unique value renderer (:class:`QgsUniqueValueRenderer`) --- symbol for each feature is choosen from attribute value.
25
-* graduated symbol renderer (:class:`QgsGraduatedSymbolRenderer`)
26
+* graduated symbol renderer (:class:`QgsGraduatedSymbolRenderer`) --- a symbol is applied to a subgroup (class) of features, which is calculated on a numeric field
27
 * continuous color renderer (:class:`QgsContinuousSymbolRenderer`)
28
 
29
 How to create a point symbol::
30
@@ -624,5 +624,65 @@
31
 
32
 Create graduated symbol renderer::
33
 
34
-  TODO
35
+    # Set the numeric field and the number of classes to be generated
36
+    fieldName = "My_Field"
37
+    numberOfClasses = 5
38
+    
39
+    # Get the field index based on the field name
40
+    mFieldMap = {}
41
+    fields = layer.pendingFields()
42
+    for ( key, field ) in fields.iteritems():
43
+        if field.type() == QVariant.Int or field.type() == QVariant.Double:        
44
+            mFieldMap[ str( field.name() ) ] = key
45
+    fieldIndex = mFieldMap[ fieldName ]
46
 
47
+    # Create the renderer object to be associated to the layer later
48
+    renderer = QgsGraduatedSymbolRenderer( layer.geometryType() )
49
+
50
+    # Here you may choose the renderer mode from EqualInterval/Quantile/Empty
51
+    renderer.setMode( QgsGraduatedSymbolRenderer.EqualInterval ) 
52
+
53
+    # Prepare the required symbol objects
54
+    symbolList = []
55
+    geom = layer.geometryType()
56
+    provider = layer.dataProvider()
57
+    for i in range( numberOfClasses ):
58
+        symbol = QgsSymbol( geom )
59
+        brush = QBrush()        
60
+        brush.setColor( self.randomColor() )
61
+        brush.setStyle( Qt.SolidPattern )
62
+        symbol.setBrush( brush )
63
+        symbolList.append( symbol )
64
+
65
+    # Define classes (lower and upper value as well as a label for each class)
66
+    minimum = provider.minimumValue( fieldIndex ).toDouble()[ 0 ]
67
+    maximum = provider.maximumValue( fieldIndex ).toDouble()[ 0 ]
68
+    for i in range( numberOfClasses ):
69
+        # Switch if attribute is int or double
70
+        lower = ('%.*f' % (2, minimum + ( maximum - minimum ) / numberOfClasses * i ) )
71
+        upper = ('%.*f' % (2, minimum + ( maximum - minimum ) / numberOfClasses * ( i + 1 ) ) )
72
+
73
+        lowerString = QVariant( lower ).toString()
74
+        upperString = QVariant( upper ).toString()
75
+        symbolList[ i ].setLowerValue( lowerString )
76
+        symbolList[ i ].setUpperValue( upperString )
77
+        symbolList[ i ].setLabel( lowerString + " - " + upperString )
78
+
79
+    # Define a symbol object based on each class definition
80
+    for i in range( numberOfClasses ):
81
+        symbol = symbolList[ i ]
82
+        lower_bound = symbol.lowerValue()
83
+        upper_bound = symbol.upperValue()
84
+        label = symbol.label()
85
+
86
+        sy = QgsSymbol( layer.geometryType(), lower_bound, upper_bound, label )
87
+        sy.setFillColor( symbol.brush().color() )
88
+        sy.setFillStyle( symbol.brush().style() )
89
+
90
+        renderer.addSymbol( sy )
91
+
92
+    # Set the field index to classify and set the created renderer object to the layer
93
+    renderer.setClassificationField( fieldIndex )
94
+    layer.setRenderer( renderer )
95
+
96
+