How do I create an emptry LineString in GeoDjango

I am having a problem with the latest version of the GeoDjango extensions in Django version 4.2.6 when I create an empty LineString. The same code is behaving differently than it did in version 4.0.10. The default seems to have switched to creating a Geos object with 3-dimensions rather than the previous 2-dimensions.

The following code returns a different WKT object depending on the version of Django I am using.

from django.contrib.gis.geos import LineString
ls = LineString()
ls.wkt

This is returning a 3-dimensional object “LINESTRING Z EMPTY”. Running exactly the same code on Django 4.0.10 creates a 2-dimension object “LINESTRING EMPTY”.

Is there anything obvious I am doing wrong? I am also running a slightly newer version of geos-3.12.0 versus 3.11.2 but I cannot see anything is both sets of release notes that describe this change in behaviour.

In GeoDjango,starting from version 3.1, 3D geometries are supported by default.
If you want to create a 2D LineString explicitly, you can use the dim parameter when creating the LineString object. Set dim=2 to create a 2D LineString.

from django.contrib.gis.geos import LineString
ls = LineString(dim=2)
print(ls.wkt)

Leave a Comment