layer = QgsVectorLayer("Point?crs=EPSG:4326", "my_points", "memory") # creates a new vector layer with point geometry type, a WGS84 CRS, and in-memory data storage
path = "/path/to/my/shapefile.shp" layer = QgsVectorLayer(path, "my_shapefile", "ogr") # loads a shapefile from disk and creates a new vector layer with the given name
layer = QgsProject.instance().mapLayersByName("my_layer")[0] # get a reference to an existing layer print("Layer name:", layer.name()) # prints the layer's name print("Layer extent:", layer.extent().toString()) # prints the layer's extent in a human-readable format print("Layer CRS:", layer.crs().authid()) # prints the layer's spatial reference system identifier
layer = QgsProject.instance().mapLayersByName("my_layer")[0] # get a reference to an existing layer features = layer.getFeatures() # get an iterator over the layer's features for feature in features: # do something with the feature, like print its attributes or geometry print("Attributes:", feature.attributes()) print("Geometry:", feature.geometry().asPoint()) new_feature = QgsFeature() # create a new feature new_feature.setAttributes([1, "foo", 3.14]) # set the feature's attributes new_feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(0, 0))) # set the feature's geometry layer.startEditing() # start editing the layer layer.addFeature(new_feature) # add the new feature to the layer layer.commitChanges() # save the changes to the layerNote: These examples assume that QGIS is already installed and configured properly on the system.