5

I am trying to write a GeoDataFrame's GeometryCollection geometry to a GPKG file and open the result in QGIS. This is the way I create the GPKG file:

import shapely
import geopandas as gpd

gpd.GeoSeries(
    shapely.geometry.GeometryCollection(
        [
            shapely.geometry.LineString([[0, 0], [0, 1]]),
            shapely.geometry.LineString([[1, 0], [1, 1]]),
        ]
    )
).set_crs(3857).to_file(
    "geomcolltest.gpkg", engine="pyogrio", geometry_type="GeometryCollection"
)

However, when I try opening geomcolltest.gpkg in QGIS, I get this error message:

enter image description here

At other times, no error message, no shown geometries, just a warning next to the layer title:

enter image description here

Yet other times, I get:

enter image description here

(Interestingly, using shapely.geometry.MultiLineString instead of shapely.geometry.GeometryCollection seems to consistently lead to this last error message. The problem is not isolated to GeometryCollections.)

Conversely, ogrinfo does seem to indicate the geometry type is not Unknown Type but it is Geometry Collection, as ogrinfo geomcolltest.gpkg returns:

INFO: Open of `geomcolltest.gpkg'
      using driver `GPKG' successful.
1: geomcolltest (Geometry Collection)

I check that my GeoSeries is valid this way:

gpd.GeoSeries(
    shapely.geometry.GeometryCollection(
        [
            shapely.geometry.LineString([[0, 0], [0, 1]]),
            shapely.geometry.LineString([[1, 0], [1, 1]]),
        ]
    )).is_valid

Output:

0    True
dtype: bool

To investigate further, I convert the GPKG file in question to a GeoJSON via ogr2ogr geomcolltest.geojson geomcolltest.gpkg. I end up with geomcolltest.geojson:

{
"type": "FeatureCollection",
"name": "geomcolltest",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "GeometryCollection", "geometries": [ { "type": "LineString", "coordinates": [ [ 0.0, 0.0 ], [ 0.0, 1.0 ] ] }, { "type": "LineString", "coordinates": [ [ 1.0, 0.0 ], [ 1.0, 1.0 ] ] } ] } }
]
}

When I drag-and-drop this file to QGIS, I again get the latest screenshot posted above, just as in the GPKG case. geojson.io seems to have no problem processing & visualizing the file:

enter image description here

However, another GeoJSON visualizing tool, geojson.tools has the following to say when I try to visualize this GeoJSON:

1 feature(s) added successfully and 1 feature(s) failed to load.

while not displaying the correct geometries:

enter image description here

These few lines from the comment section summarizes the situation very well:

We can say that at this moment: ogr recognizes de geometry collection geometry, so geopandas (through pyogrio) is writing a valid geopackage; QGIS doesn't visualize geometry collections from both geopackage and geojson; neither geojson.tools but geojson.io does.

To return to the premise: I am trying to write a GeoDataFrame's GeometryCollection geometry to a GPKG file and open the result in QGIS.

How to write a GeometryCollection to a GPKG file and open it in QGIS?

3
  • We can say that at this moment: ogr recognizes de geometry collection geometry, so geopandas (through pyogrio) is writing a valid geopackage; QGIS doesn't visualize geometry collections from both geopackage and geojson; neither geojson.tools but geojson.io does. Commented Mar 21 at 2:07
  • Good summary. I'm about to edit the question to reflect this.
    – zabop
    Commented Mar 21 at 8:15
  • 1
    FYI, link to issue for Support of geometry collection with GeoJSON in QGIS github.com/qgis/QGIS/issues/32747 (not implemented at the moment) Command line approach GeoJSON to GPKG ogr2ogr -f GPKG demo.gpkg demo.geojson -nln demo (work for all GeoJSON Feature types, not only GeometryCollection)
    – ThomasG77
    Commented Mar 25 at 22:11

2 Answers 2

2

The purpose of a GeometryCollection is typically to combine different geometry types (e.g. a line and/or a point and/or a polygon) in a single geometry. However, the use cases for this are limited, and this creates a lot of complexities e.g. for symbologies,... so QGIS doesn't really support them.

Not sure what your use case to use it is, but QGIS does support tables in e.g. geopackage files that contain multiple geometry types. To keep everything technically simple, QGIS will treat each geometry type as a seperate layer.

To create a valid Geopackage file like that, the geometry column type specified should be "Unknown":

import shapely
import geopandas as gpd

gpd.GeoSeries(
    [
        shapely.geometry.MultiLineString(
            [
                shapely.geometry.LineString([[0, 0], [0, 1]]),
                shapely.geometry.LineString([[1, 0], [1, 1]]),
            ]
        ),
        shapely.geometry.box(5, 5, 6, 6),
        shapely.geometry.Point(10, 10),
    ]
).set_crs(3857).to_file(
    "multi_type_test.gpkg", engine="pyogrio", geometry_type="Unknown"
)
1

One possible way around the problem is to use geometry_type="MultiLineString" instead of geometry_type="GeometryCollection" in the to_file() call:

import geopandas as gpd
import shapely

gpd.GeoSeries(
    shapely.geometry.MultiLineString(
        [
            shapely.geometry.LineString([[0, 0], [0, 1]]),
            shapely.geometry.LineString([[1, 0], [1, 1]]),
        ]
    )
).set_crs(3857).to_file(
    "gpdMultiLineString.gpkg", engine="pyogrio", geometry_type="MultiLineString"
)
# -----------------------------------------------^^^^^^^^^^^ KEY BIT ^^^^^^^^^^^

The file produced this way can be opened in QGIS, and the lines are treated as multilines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.