Creating Vector Layers in QGIS
Credit: Youtube Channel “Statistics Canada”
Getting Started with Vector Layers in QGIS
Vector layers allow you to work with geographic data represented by points, lines, or polygons. In QGIS you can create new vector layers from scratch or by importing data from a file. The following guide walks you through the most common methods for adding vector layers to a QGIS project.
Prerequisites
- QGIS installed (any recent release – QGIS 3.x is recommended).
- Basic familiarity with the QGIS user interface.
- Optional: a sample data file (shapefile, GeoJSON, CSV with coordinates, or a PostGIS database).
1. Creating a New Vector Layer (Empty)
Use this method to start a clean layer that you will later populate with features.
- Open QGIS.
- Go to Layer > Create Layer > New Shapefile Layer…. For QGIS 4 this goes under Layer > Create Layer > Create Layer….
- In the New Vector Layer dialog:
- Geometry type: Choose
Point
,Line
, orPolygon
. - Coordinate reference system (CRS): Click Set CRS and pick the appropriate system (e.g., EPSG:4326 – WGS 84).
- New Field: Add attribute fields by clicking Add…. Define field name, type, and length.
- Click OK to create the layer.
- Geometry type: Choose
- The new layer appears in the Layers panel and is in edit mode, ready for drawing.
2. Importing an Existing Vector File
QGIS supports many vector formats such as ESRI Shapefile, GeoJSON, KML, and others.
- From the main menu choose Layer > Add Layer > Add Vector Layer… (or press Ctrl+Shift+V).
- Browse to and select your file. If it’s a folder of shapefiles, tick Add selected vector data to import all layers in one go.
- Click Open. The layer(s) load in the Layers panel.
3. Loading Vector Data from a Database
QGIS can directly connect to PostgreSQL/PostGIS and other spatial databases.
- Open the Database Manager from Database > DB Manager.
- In the DB Manager panel, select the database type (e.g., PostgreSQL) and click New connection….
- Fill in host, port, database name, username, password, and set the SSL mode if necessary.
- After connecting, navigate to Tables and check the tables you want.
- Click Add Selected to load the layers into your QGIS project.
4. Adding Layers via Drag‑and‑Drop
For a quick import, simply drag a vector file from your file manager into the Layers panel.
This works for supported formats and automatically creates a new layer with the file’s CRS.
5. Editing and Saving Your Vector Layer
- Select the layer and toggle Edit mode by clicking the pencil icon.
- Use the Digitizing Toolbar (point, line, polygon tools) to create features.
- To save edits, click the disk icon or right‑click the layer and choose Save Edits.
- When finished, toggle edit mode off to lock the layer.
6. Exporting the Layer
Export a layer to a new file or copy it to another format.
- Right‑click the layer and select Export > Save Features As….
- Choose the Format (e.g., ESRI Shapefile, GeoJSON).
- Specify the output File name, CRS, and any other desired options.
- Click OK to write the file.
Troubleshooting Common Issues
- Layer not visible: Ensure the scale range and symbology are set correctly in Layer Properties > General.
- Data not loading from PostGIS: Verify the connection string, username permissions, and that the spatialite drivers are installed.
- CRS mismatch: Use the Coordinate Transform tool or re‑project the layer via Vector > Data Management Tools > Reproject Layer….
Example: Creating a Polygon Layer with Attributes
# Using Python Console (PyQGIS)
layer = QgsVectorLayer('Polygon?crs=EPSG:4326', 'parks', 'memory')
layer.dataProvider().addAttributes([
QgsField('name', QVariant.String),
QgsField('area_km2', QVariant.Double)
])
layer.updateFields()
# Add a feature (example coordinates)
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPolygonXY([[
QgsPointXY(-122.42, 37.78),
QgsPointXY(-122.41, 37.78),
QgsPointXY(-122.41, 37.77),
QgsPointXY(-122.42, 37.77),
QgsPointXY(-122.42, 37.78)
]]))
feat.setAttributes(['Golden Gate Park', 39.5])
layer.dataProvider().addFeatures([feat])
QgsProject.instance().addMapLayer(layer)
Next Steps
- Experiment with styling and labeling via Layer Properties > Style.
- Try joining a raster layer with your vector data to analyze spatial relationships.
- Explore QGIS expressions for dynamic labeling and styling.
Creating and managing vector layers is the foundation of any GIS workflow in QGIS. With these tools at your disposal, you can quickly build robust spatial datasets and harness the full power of the QGIS platform.