Skip to content

Commit

Permalink
Fixing doctests in gh workflow (#9152)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5b1857e)
  • Loading branch information
DelazJ committed Feb 19, 2025
1 parent 42653f5 commit 90aefb0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 34 deletions.
4 changes: 4 additions & 0 deletions docs/pyqgis_developer_cookbook/canvas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ layers for the canvas.
# set the map canvas layer set
canvas.setLayers([vlayer])

.. testoutput:: canvas
:hide:

(2): Using non-preferred coordinate operation between EPSG:2964 and EPSG:4326. Using +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=push +v_3 +step +proj=cart +ellps=clrk66 +step +proj=helmert +x=-5 +y=135 +z=172 +step +inv +proj=cart +ellps=WGS84 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg, preferred +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=hgridshift +grids=us_noaa_alaska.tif +step +proj=unitconvert +xy_in=rad +xy_out=deg.

After executing these commands, the canvas should show the layer you have
loaded.
Expand Down
2 changes: 1 addition & 1 deletion docs/pyqgis_developer_cookbook/crs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Output:
Ellipsoid Acronym: EPSG:7030
Proj String: +proj=longlat +datum=WGS84 +no_defs
Is geographic: True
Map units: DistanceUnit.Degrees
Map units: 6

.. index:: Projections

Expand Down
14 changes: 7 additions & 7 deletions docs/pyqgis_developer_cookbook/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ order to compute new field values:

.. testcode:: expr

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

# create a vector layer
vl = QgsVectorLayer("Point", "Companies", "memory")
pr = vl.dataProvider()
pr.addAttributes([QgsField("Name", QVariant.String),
QgsField("Employees", QVariant.Int),
QgsField("Revenue", QVariant.Double),
QgsField("Rev. per employee", QVariant.Double),
QgsField("Sum", QVariant.Double),
QgsField("Fun", QVariant.Double)])
pr.addAttributes([QgsField("Name", QMetaType.Type.QString),
QgsField("Employees", QMetaType.Type.Int),
QgsField("Revenue", QMetaType.Type.Double),
QgsField("Rev. per employee", QMetaType.Type.Double),
QgsField("Sum", QMetaType.Type.Double),
QgsField("Fun", QMetaType.Type.Double)])
vl.updateFields()

# add data to the first three fields
Expand Down
18 changes: 9 additions & 9 deletions docs/pyqgis_developer_cookbook/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,18 @@ enumeration.
.. testcode:: geometry

print(gPnt.wkbType())
# output: 'WkbType.Point'
# output: 1
print(gLine.wkbType())
# output: 'WkbType.LineString'
# output: 2
print(gPolygon.wkbType())
# output: 'WkbType.Polygon'
# output: 3

.. testoutput:: geometry
:hide:

WkbType.Point
WkbType.LineString
WkbType.Polygon
1
2
3

As an alternative, one can use the :meth:`type() <qgis.core.QgsGeometry.type>`
method which returns a value from the :meth:`QgsWkbTypes.GeometryType <qgis.core.QgsWkbTypes.geometryType>`
Expand All @@ -168,12 +168,12 @@ enumeration.
.. testcode:: geometry

print(gLine.type())
# output: 'GeometryType.Line'
# output: 1

.. testoutput:: geometry
:hide:

GeometryType.Line
1

You can use the :meth:`displayString() <qgis.core.QgsWkbTypes.displayString>`
function to get a human readable geometry type.
Expand Down Expand Up @@ -279,7 +279,7 @@ It's also possible to modify each part of the geometry using

.. testoutput:: geometry

MultiPoint ((-10334728.12541878595948219 -5360106.25905461423099041),(-10462135.16126426123082638 -5217485.4735023295506835),(-10589399.84444035589694977 -5072021.45942386891692877))
MultiPoint ((-10334726.79314758814871311 -5360105.10101194866001606),(-10462133.82917747274041176 -5217484.34365733992308378),(-10589398.51346861757338047 -5072020.35880533326417208))

.. index:: Geometry; Predicates and operations

Expand Down
9 changes: 9 additions & 0 deletions docs/pyqgis_developer_cookbook/loadlayer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ identifier, name for the layer and provider's name:
else:
QgsProject.instance().addMapLayer(vlayer)

.. .. testoutput:: loadlayer
:hide:
(2): Using non-preferred coordinate operation between EPSG:2964 and EPSG:4326. Using +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=push +v_3 +step +proj=cart +ellps=clrk66 +step +proj=helmert +x=-5 +y=135 +z=172 +step +inv +proj=cart +ellps=WGS84 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg, preferred +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=hgridshift +grids=us_noaa_alaska.tif +step +proj=unitconvert +xy_in=rad +xy_out=deg.
The data source identifier is a string and it is specific to each vector data
provider. Layer's name is used in the layer list widget. It is important to
check whether the layer has been loaded successfully. If it was not, an invalid
Expand Down Expand Up @@ -225,6 +230,10 @@ providers:
uri = "https://demo.mapserver.org/cgi-bin/wfs?service=WFS&version=2.0.0&request=GetFeature&typename=ms:cities"
vlayer = QgsVectorLayer(uri, "my wfs layer", "WFS")

.. .. testoutput:: loadlayer
:hide:
(2): Using non-preferred coordinate operation between EPSG:2964 and EPSG:4326. Using +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=push +v_3 +step +proj=cart +ellps=clrk66 +step +proj=helmert +x=-5 +y=135 +z=172 +step +inv +proj=cart +ellps=WGS84 +step +proj=pop +v_3 +step +proj=unitconvert +xy_in=rad +xy_out=deg, preferred +proj=pipeline +step +proj=unitconvert +xy_in=us-ft +xy_out=m +step +inv +proj=aea +lat_0=50 +lon_0=-154 +lat_1=55 +lat_2=65 +x_0=0 +y_0=0 +ellps=clrk66 +step +proj=hgridshift +grids=us_noaa_alaska.tif +step +proj=unitconvert +xy_in=rad +xy_out=deg.
The uri can be created using the standard ``urllib`` library:

Expand Down
2 changes: 1 addition & 1 deletion docs/pyqgis_developer_cookbook/raster.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The following code assumes ``rlayer`` is a

.. testoutput:: raster

RasterLayerType.GrayOrUndefined
0

.. testcode:: raster

Expand Down
34 changes: 18 additions & 16 deletions docs/pyqgis_developer_cookbook/vector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ Here you have some examples that demonstrate how to use these editing methods.

.. testcode:: vectors

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

feat1 = feat2 = QgsFeature(layer.fields())
fid = 99
Expand All @@ -522,7 +522,7 @@ Here you have some examples that demonstrate how to use these editing methods.
layer.changeAttributeValue(fid, fieldIndex, value)

# add new field
layer.addAttribute(QgsField("mytext", QVariant.String))
layer.addAttribute(QgsField("mytext", QMetaType.Type.QString))
# remove a field
layer.deleteAttribute(fieldIndex)

Expand Down Expand Up @@ -579,12 +579,12 @@ For deletion of fields just provide a list of field indexes.

.. testcode:: vectors

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

if caps & QgsVectorDataProvider.AddAttributes:
res = layer.dataProvider().addAttributes(
[QgsField("mytext", QVariant.String),
QgsField("myint", QVariant.Int)])
[QgsField("mytext", QMetaType.Type.QString),
QgsField("myint", QMetaType.Type.Int)])

if caps & QgsVectorDataProvider.DeleteAttributes:
res = layer.dataProvider().deleteAttributes([0])
Expand All @@ -593,7 +593,9 @@ For deletion of fields just provide a list of field indexes.

# Alternate methods for removing fields
# first create temporary fields to be removed (f1-3)
layer.dataProvider().addAttributes([QgsField("f1",QVariant.Int),QgsField("f2",QVariant.Int),QgsField("f3",QVariant.Int)])
layer.dataProvider().addAttributes([QgsField("f1", QMetaType.Type.Int),
QgsField("f2", QMetaType.Type.Int),
QgsField("f3", QMetaType.Type.Int)])
layer.updateFields()
count=layer.fields().count() # count of layer fields
ind_list=list((count-3, count-2)) # create list
Expand Down Expand Up @@ -839,7 +841,7 @@ you can do the following:
def fieldDefinition(self, field):
idx = self.layer.fields().indexFromName(field.name())
if idx == self.list_field_idx:
return QgsField(LIST_FIELD_NAME, QVariant.String)
return QgsField(LIST_FIELD_NAME, QMetaType.Type.QString)
else:
return self.layer.fields()[idx]

Expand Down Expand Up @@ -867,12 +869,12 @@ Directly from features

.. testcode:: vectors

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

# define fields for feature attributes. A QgsFields object is needed
fields = QgsFields()
fields.append(QgsField("first", QVariant.Int))
fields.append(QgsField("second", QVariant.String))
fields.append(QgsField("first", QMetaType.Type.Int))
fields.append(QgsField("second", QMetaType.Type.QString))

""" create an instance of vector file writer, which will create the vector file.
Arguments:
Expand Down Expand Up @@ -966,22 +968,22 @@ The following example code illustrates creating and populating a memory provider

.. testcode:: vectors

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QMetaType

# create layer
vl = QgsVectorLayer("Point", "temporary_points", "memory")
pr = vl.dataProvider()

# add fields
pr.addAttributes([QgsField("name", QVariant.String),
QgsField("age", QVariant.Int),
QgsField("size", QVariant.Double)])
pr.addAttributes([QgsField("name", QMetaType.Type.QString),
QgsField("age", QMetaType.Type.Int),
QgsField("size", QMetaType.Type.Double)])
vl.updateFields() # tell the vector layer to fetch changes from the provider

# add a feature
fet = QgsFeature()
fet.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(10,10)))
fet.setAttributes(["Johny", 2, 0.3])
fet.setAttributes(["Johnny", 2, 0.3])
pr.addFeatures([fet])

# update layer's extent when new features have been added
Expand All @@ -1008,7 +1010,7 @@ Finally, let's check whether everything went well
fields: 3
features: 1
extent: 10.0 10.0 10.0 10.0
F: 1 ['Johny', 2, 0.3] <QgsPointXY: POINT(10 10)>
F: 1 ['Johnny', 2, 0.3] <QgsPointXY: POINT(10 10)>

.. index:: Vector layers; Symbology

Expand Down

0 comments on commit 90aefb0

Please sign in to comment.