openwisp / django-rest-framework-gis

Geographic add-ons for Django REST Framework. Maintained by the OpenWISP Project.
http://openwisp.org
MIT License
1.07k stars 200 forks source link

[feat] Bounding Box support for Point, Line, Polygon, ... #271

Closed Debakel closed 2 years ago

Debakel commented 2 years ago

The GeoJSON specification allows GeoJSON objects of all types (Feature, Point, Polygon, ...) to contain a bounding box.

https://github.com/openwisp/django-rest-framework-gis/pull/57 already added support for bounding boxes for Features.

This patch adds bounding box support for all remaining types and introduces a new argument auto_bbox on the GeometryField.

Example

Model:

class Location(models.Model):
    name = models.CharField(...)
    geometry = geomodels.PolygonField()

Serializer:

class LocationSerializer(serializers.ModelSerializer):
    geometry = GeometryField(auto_bbox=True)

    class Meta:
        model = Location
        fields = ["name", "geometry"]

Serialization:

{
   "name": "lorem ipsum",
   "geometry": {
            "type": "Polygon",
            "bbox": [8.0, 39.0, 8.02, 39.02]
            "coordinates": [[[8.0, 39.0], [8.02, 39.01], [8.01, 39.02], [8.0, 39.0]]],
        }
}
devkapilbansal commented 2 years ago

@Debakel qa checks are failing, can you please enclose commit type in square brackets

Please check commit message guidelines

Debakel commented 2 years ago

@devkapilbansal @auvipy Thanks for reviewing! I just updated the commit message. QA checks should succeed now. Could you approve the workflow run?