Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tuple for geo_field property #98

Open
daredevil82 opened this issue Feb 5, 2016 · 2 comments
Open

Allow tuple for geo_field property #98

daredevil82 opened this issue Feb 5, 2016 · 2 comments

Comments

@daredevil82
Copy link

I've been asked to create a Django app where users can manage their existing library of KML documents along with other GIS formats. Unfortunately, it seems that about half of the documents have 2D geometry while the rest are 3D. This causes issues when saving the geometry model because GeoDjango's GeometryField has a default dim value of 2, which results in errors when uploading a 3D file.

My conundrum is similar to the question here at GIS Stackexchange, just inverted. However, I would not want to not force a 2D transform, as the height values are a big part of the data.

A short term solution is to include two geometry fields within the model, one 2D and the other 3D. Depending on the dimension, the geometry will be saved to the appropriate field in the model. In order to use this within the serializer, a GeometrySerializerMethodField needs to be used, much like

class SegmentSerializer(gis_serializer.GeoFeatureModelSerializer):
    trail = TrailSerializer()
    geometry = gis_serializer.GeometrySerializerMethodField()

    def get_geometry(self, obj):
        print(obj)
        if obj.two_d_geom is None:
            return obj.three_d_geom
        else:
            return obj.two_d_geom

    class Meta:
        model = TrailSegment
        geo_field = 'geometry'

If geo_field were to accept a tuple as a value, then the above could be functionally identical to

class SegmentSerializer(gis_serializer.GeoFeatureModelSerializer):
    trail = TrailSerializer()

    class Meta:
        model = TrailSegment
        geo_field = ('two_d_geom', 'three_d_geom', )

This would also benefit devs who find themselves doing far too many joins that impact performance and use denormalization as a solution.

@nemesifier
Copy link
Member

I don't see the benefit of including such a feature in drf-gis right now, you should be able to extend drf-gis serializers and implement what you need. You might as well publish your solution somewhere so we can better understand the use case in more detail.

@tahv0
Copy link

tahv0 commented May 6, 2020

This would be awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants