Opened 3 years ago

Closed 3 years ago

#33742 closed New feature (fixed)

GeoJSON serializer doesn't provide a mechanism to include the ID of a feature

Reported by: Samir Shah Owned by: Samir Shah
Component: GIS Version: dev
Severity: Normal Keywords:
Cc: Claude Paroz Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The GeoJSON specification says:

If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of the Feature object with the name "id", and the value of this member is either a JSON string or number.

Thus it should be possible to generate a GeoJSON object which looks like this:

{
    "type": "Feature",
    "id": 24  
    "properties": {},
    "geometry": {
        "type": "Point",
        "coordinates": [8.11, 52.11]
    }
}

django.contrib.gis.serializers.geojson.Serializer doesn't support this - it's not possible to add an id key to a feature without overriding the get_dump_object() method of the class. If you specify pk in the fields, then this gets added to the properties of the feature, but not a top level id field. Having an ID is important for a number of use cases - particularly when GeoJSON data is made editable via an API, for which many client libraries use the id to uniquely identify features.

Given that the specification says an id SHOULD be present if available, I would like to propose one of the following:

  1. Django defaults to using the object PK as the id for the feature.
  1. Or, if (1) is considered a backwards-incompatible change, then a mechanism is provided to instruct the class to include an ID (which I guess may not always be the the primary key)

I'm happy to contribute a PR for this if you can give me guidance on what the preferred approach would be. Personally I think Django should include the ID by default.

Change History (5)

comment:1 by Mariusz Felisiak, 3 years ago

Cc: Claude Paroz added
Type: BugNew feature

comment:2 by Claude Paroz, 3 years ago

Triage Stage: UnreviewedAccepted

Sure, a patch would be welcome!

comment:3 by Samir Shah, 3 years ago

Has patch: set
Owner: changed from nobody to Samir Shah
Status: newassigned

comment:4 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 6f73eb9:

Fixed #33742 -- Added id to GeoJSON serializer.

Note: See TracTickets for help on using tickets.
Back to Top