I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile. but I've looked in the pyqgis cookbook and API and can't figure out how to call it.
infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.
Thank you
How do I change the geometry of this shapefile - or is it a case of recreating and re-saving the original shapefile? Right-click the layer and select Save As... . In the Layer Options section, there is a dropdown menu for SHPT . Select Polygon, this should re-save the polygon layer without the z-dimension.
Geometries can be of different types like Point/ Multi-Point, Line/Multi-Line, and Polygon/Multi-Polygon. QGIS is a free geographical information system application that supports viewing, editing, and analysis of geospatial data.
Geometry Construction Polyline (Linestring) is represented by a list of points. Polygon is represented by a list of linear rings (i.e. closed linestrings).
The QGIS Python Console is an interactive shell for the python command executions. It also has a python file editor that allows you to edit and save your python scripts. Both console and editor are based on PyQScintilla2 package. To open the console go to Plugins ‣ Python Console ( Ctrl+Alt+P ).
The command is simple:
layer=qgis.utils.iface.mapCanvas().currentLayer()
if layer.wkbType()==QGis.WKBPoint:
print 'Layer is a point layer'
if layer.wkbType()==QGis.WKBLineString:
print 'Layer is a line layer'
if layer.wkbType()==QGis.WKBPolygon:
print 'Layer is a polygon layer'
if layer.wkbType()==QGis.WKBMultiPolygon:
print 'Layer is a multi-polygon layer'
if layer.wkbType()==100:
print 'Layer is a data-only layer'
You can use numbers (1,2,3,4) instead of the QGis.WKB****
* syntax, but the way described above yields a more readable code.
The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html
Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :
geomTypeString=qgis.core.QgsWkbTypes.displayString(int(layer.wkbType()))
that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.
For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)
geomFlatTypeString=qgis.core.QgsWkbTypes.displayString(int(
qgis.core.QgsWkbTypes.flatType(int(in_layer.wkbType()))))
For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:
def copyLayer(in_layer,condition=None):
#condition=function to test features and return True or False______
if condition==None:
def condition(f):
return True
typeGeom=qgis.core.QgsWkbTypes.displayString(int(
qgis.core.QgsWkbTypes.flatType(int(in_layer.wkbType()))))
crsId=in_layer.crs().authid()
out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
in_layer.name()+"_copie",
"memory")
fields=in_layer.dataProvider().fields().toList()
out_layer.dataProvider().addAttributes(fields)
out_layer.updateFields()
features=[f for f in in_layer.getFeatures() if condition(f)]
out_layer.dataProvider().addFeatures(features)
return out_layer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With