GeoDjango model field for MultiPoint with z dimension

I’m working on a project using GeoDjango to set up sort of GIS web app.

I have declared the following model for my app :

from django.contrib.gis.db import models

class Profile(models.Model):
    index = models.IntegerField()
    geom = models.MultiPointField(blank=True, null=True)

In my view, I process an uploaded GeoJSON file containing points with X,Y,Z coordinates. But I get the following error : Geometry has Z dimension but column does not.

Simplified view of my function:

def handle_uploaded_file(geo_file):
    
        ds = DataSource(geo_file)
        profiles = get_profiles_from_source(ds)
        for profile in profiles:
            # profile is a list of Point from GEOS API
            new_profile = Profile(index=pid, geom=MultiPoint(profile))
            new_profile.save()

I’ve seen some posts on stackoverflow regarding the ability to remove the z-dimension, but this information is relevant to me, so I’d rather like to store it.

Any idea ?

Try:

geom = models.MultiPointField(blank=True, null=True, dim=3)

If it works for you, you can then thank us by contributing a documentation patch to GeoDjango!

Brilliant, it works for me indeed, thank you :slightly_smiling_face: ! Yet, it seems to break the display in admin panel (it displays the field info as raw text instead of using the OpenStreetMap default widget), but I probably need to start another thread for this ?

Also, I’d love to contribute with a documentation patch but I’m not familiar with the best way to do it. The related documentation page is https://docs.djangoproject.com/en/3.0/ref/contrib/gis/model-api/. The dim option is mentioned at the end of the page (which I didn’t see btw), so should I just add some example ?

The default map widget in the admin doesn’t support 3D, that’s why it’s deactivated on purpose with data with more than 2 dimensions, otherwise there’s a risk the 3rd dimension be stripped on save. You should set your own map widget. If you find one that keeps the 3rd dimension on edition, I’d be curious to know.

That makes sense. In fact, I don’t really need to edit the geometry within the web admin panel (at least not the Z dimension), so a slight modification (fixing Z and editing X,Y) of the widget would probably do the trick.

I’ll do some research on possible 3D map widgets, but I think I will change the models to have a Point class with 2D geometry (X,Y don’t change over time), being associated to another class holding the Z dimension. I feel like this is probably a better approach to me.