I'm working on a nature conservation GIS using a combination of QGIS and PostgreSQL. To manage measures, I need point, line, and area objects, ideally in a single layer. In PostgreSQL/PostGIS, it seems possible to manage different geometry objects in one table if I don't define a geometry type for the geometry column. However, in QGIS, the table is either not displayed or is displayed as a non-mappable layer. Is there a solution or alternative?
I'm working on a nature conservation GIS using a combination of QGIS and PostgreSQL. To manage measures, I need point, line, and area objects, ideally in a single layer. In PostgreSQL/PostGIS, it seems possible to manage different geometry objects in one table if I don't define a geometry type for the geometry column. However, in QGIS, the table is either not displayed or is displayed as a non-mappable layer. Is there a solution or alternative?
Share Improve this question asked Mar 28 at 11:57 Detlev FinkeDetlev Finke 112 bronze badges2 Answers
Reset to default 1It's generally bad practice to store multiple types of geometry in a single table, but if you must then you should make the type of the column GEOMETRY
rather than NULL
then QGIS should be able to recognise as a spatial table. Styling it will be difficult since the renderer needs to know what type of geometry to decide how to style it. You can work around this by creating 3 (or more) views for the different geometry types and use those in QGIS.
Step 1: Create a Table to Hold Different Geometries
First, you need to create a table in PostGIS with a column that can hold different geometry types (point, line, polygon).
PostGIS provides the geometry type that allows you to store any kind of geometry (points, lines, or polygons) in the same column.
CREATE TABLE spatial_data (
id SERIAL PRIMARY KEY,
geom geometry(Geometry, 4326) -- Geometry column that can hold various geometry types
);
Step 2: Insert Different Geometries into the Table
- You can insert points, lines, and polygons into the same table using the
ST_GeomFromText()
function or by directly inserting geometry objects.
-- Inserting a Point
INSERT INTO spatial_data (geom)
VALUES (ST_GeomFromText('POINT(30 10)', 4326));
-- Inserting a LineString
INSERT INTO spatial_data (geom)
VALUES (ST_GeomFromText('LINESTRING(30 10, 10 30, 40 40)', 4326));
-- Inserting a Polygon
INSERT INTO spatial_data (geom)
VALUES (ST_GeomFromText('POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))', 4326));
Step 3: Query Different Geometries
- You can query the table and select geometries based on their type using
ST_GeometryType()
.
-- Query to select all geometries
SELECT id, ST_AsText(geom) FROM spatial_data;
-- Query to select only Points
SELECT id, ST_AsText(geom) FROM spatial_data
WHERE ST_GeometryType(geom) = 'ST_Point';
Step 4: Ensure Correct Projection
Make sure that all geometries are in the same spatial reference system (SRS), such as EPSG:4326, to avoid projection issues when displaying them.
2. QGIS:
Step 1: Create a Layer with Mixed Geometries
In QGIS, you can directly create a shapefile or GeoPackage to store different geometry types in a single layer.
When creating a new vector layer in QGIS, choose Point/Line/Polygon geometries and define your attribute fields. You can later modify the geometry type if necessary.
Step 2: Add Points, Lines, and Polygons to the Layer
You can add different geometries (points, lines, polygons) manually through the QGIS interface by using the editing tools.
To add a point, click the Add Feature button and choose the Point tool.
To add a line, select the Line tool.
To add a polygon, select the Polygon tool.
Step 3: View and Manage Different Geometry Types in QGIS
QGIS allows you to work with layers containing multiple geometry types. In the Attribute Table, the geometry types will be recognized automatically.
If you need to filter or manage them, you can use the Field Calculator or SQL-based expressions in QGIS to query based on geometry types.
Step 4: Export to a New Layer with Mixed Geometries
After adding the data, you can export it to a new layer that contains mixed geometries. This is particularly useful when combining point, line, and polygon data for a unified analysis.
Step 5: Styling and Visualization
In QGIS, you can apply different symbologies and styles to each geometry type. For example, you can color points, lines, and polygons differently within the same layer.
Go to the Layer Styling Panel and apply styles based on the geometry type or other attributes.