Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import itertools
import qgis
from qgis.gui import QgsMessageBar
from qgis.core import QgsMapLayer
def layer_info(layer):
print "Layer name: %s" % layer.name()
print "CRS: %s" % layer.crs().authid()
print "Extent: %s" % layer.extent().asWktCoordinates()
print "Feature count: %s" % layer.featureCount()
print "Geometry type: %s" % qgis.core.QgsWKBTypes.displayString(int(layer.wkbType()))
field_names = [field.name() for field in layer.fields().toList()]
print "Fields: %s" % field_names
num_features = min(2, layer.featureCount())
print "First %d features..." % num_features
features = itertools.islice(layer.getFeatures(), num_features)
for feature in features:
print "Feature ID %d: " % feature.id()
print "Attributes: ", zip(field_names, feature.attributes())
geom = feature.geometry().geometry()
print "Geometry: ", geom.asJSON()
layer = qgis.utils.iface.activeLayer()
if layer and layer.type() == QgsMapLayer.VectorLayer:
layer_info(layer)
else:
qgis.utils.iface.messageBar().pushMessage("Please select a vector layer in the layer panel", QgsMessageBar.INFO, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.