Commit 8a2befdc authored by Luis García's avatar Luis García
Browse files

Add center to map API

parent bb59d6b8
......@@ -6,6 +6,10 @@ from django.utils.translation import gettext as _
from rest_framework import serializers
from drf_extra_fields.geo_fields import PointField
from core.models import Profile, Map
import json
from django.contrib.gis.geos import Polygon
from rest_framework.serializers import SerializerMethodField
from pymbtiles import MBtiles
class UserSerializer(serializers.ModelSerializer):
class Meta:
......@@ -72,9 +76,19 @@ class OwnerSerializer(serializers.ModelSerializer):
exclude = ('id', 'username', 'password', 'email', 'first_name', 'last_name', 'is_active', 'is_staff', 'is_superuser', 'date_joined', 'groups', 'user_permissions', 'last_login')
class MapSerializer(serializers.ModelSerializer):
center = SerializerMethodField('get_center_mbtiles')
def get_center_mbtiles(self, object):
with MBtiles(object.file.path) as src:
bounds = src.meta['bounds'].split(',')
pol = Polygon.from_bbox(bounds)
return json.loads(pol.centroid.json)
return ''
class Meta:
model = Map
fields = ('id', 'name', 'file')
fields = ('id', 'name', 'file', 'center')
class PasswordResetSerializer(serializers.Serializer):
email = serializers.EmailField()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment