from rest_framework import serializers
from rest_framework_gis import fields
from .models import IndigenousLand, IndigenousVillage, ArchaeologicalPlace
import json
# from django.contrib.gis.geos import GEOSGeometry


class CompactGeometryField(fields.GeometryField):

    def to_representation(self, value):
        if isinstance(value, dict) or value is None:
            return value

        simplify = 0.001

        geometry = value.simplify(tolerance=simplify, preserve_topology=True)

        return fields.GeoJsonDict(geometry)
        # data = {
        #     'type': geometry.geom_type,
        #     'coordinates': geometry.coords,
        # }

        # precision = 8
        # floatrepr = json.encoder.FLOAT_REPR
        # Monkey patch for float precision!
        # json.encoder.FLOAT_REPR = lambda o: format(o, '.%sf' % precision)

        # json_data = json.dumps(data)

        # json.encoder.FLOAT_REPR = floatrepr  # Restore

        # if isinstance(value, dict) or value is None:
        #     return value
        # return json_data


class IndigenousLandSerializer(serializers.ModelSerializer):

    associated_land = serializers.PrimaryKeyRelatedField(read_only=True)
    geometry = CompactGeometryField(source='polygon')
    # polygon = fields.GeometrySerializerMethodField()

    # @staticmethod
    # def get_polygon(obj):
        # import ipdb;ipdb.set_trace()
        # data = (
        #     ('type', obj.polygon.geom_type),
        #     ('coordinates', obj.polygon.coords),
        #  )
        # precision = 2
        # floatrepr = json.encoder.FLOAT_REPR
        # if precision is not None:
        #     # Monkey patch for float precision!
        #     json.encoder.FLOAT_REPR = lambda o: format(o, '.%sf' % precision)
        # json_data = json.dumps(data)
        #
        # json.encoder.FLOAT_REPR = floatrepr  # Restore
        # return json_data
        # simplify = 0.0001
        # simplify = 0

        # return obj.polygon.simplify(tolerance=simplify, preserve_topology=True)

    class Meta:
        model = IndigenousLand
        exclude = ['polygon']
        depth = 1


class IndigenousVillageSerializer(serializers.ModelSerializer):

    geometry = fields.GeometryField(source='position')

    class Meta:
        model = IndigenousVillage
        exclude = ['position']
        depth = 1


class ArchaeologicalPlaceSerializer(serializers.ModelSerializer):

    geometry = fields.GeometryField(source='position')

    class Meta:
        model = ArchaeologicalPlace
        exclude = ['position']
        depth = 1

@@ -36,46 +36,8 @@
 ers%0A
-from rest_framework_gis import fields%0A
 from
@@ -111,1869 +111,137 @@
 ace%0A
-import json%0A# from django.contrib.gis.geos import GEOSGeometry%0A%0A%0Aclass CompactGeometryField(fields.GeometryField):%0A%0A    def to_representation(self, value):%0A        if isinstance(value, dict) or value is None:%0A            return value%0A%0A        simplify = 0.001%0A%0A        geometry = value.simplify(tolerance=simplify, preserve_topology=True)%0A%0A        return fields.GeoJsonDict(geometry)%0A        # data = %7B%0A        #     'type': geometry.geom_type,%0A        #     'coordinates': geometry.coords,%0A        # %7D%0A%0A        # precision = 8%0A        # floatrepr = json.encoder.FLOAT_REPR%0A        # Monkey patch for float precision!%0A        # json.encoder.FLOAT_REPR = lambda o: format(o, '.%25sf' %25 precision)%0A%0A        # json_data = json.dumps(data)%0A%0A        # json.encoder.FLOAT_REPR = floatrepr  # Restore%0A%0A        # if isinstance(value, dict) or value is None:%0A        #     return value%0A        # return json_data%0A%0A%0Aclass IndigenousLandSerializer(serializers.ModelSerializer):%0A%0A    associated_land = serializers.PrimaryKeyRelatedField(read_only=True)%0A    geometry = CompactGeometryField(source='polygon')%0A    # polygon = fields.GeometrySerializerMethodField()%0A%0A    # @staticmethod%0A    # def get_polygon(obj):%0A        # import ipdb;ipdb.set_trace()%0A        # data = (%0A        #     ('type', obj.polygon.geom_type),%0A        #     ('coordinates', obj.polygon.coords),%0A        #  )%0A        # precision = 2%0A        # floatrepr = json.encoder.FLOAT_REPR%0A        # if precision is not None:%0A        #     # Monkey patch for float precision!%0A        #     json.encoder.FLOAT_REPR = lambda o: format(o, '.%25sf' %25 precision)%0A        # json_data = json.dumps(data)%0A        #%0A        # json.encoder.FLOAT_REPR = floatrepr  # Restore%0A        # return json_data%0A        # simplify = 0.0001%0A        # simplify = 0%0A%0A        # return obj.polygon.simplify(tolerance=simplify, preserve_topolog
+%0A%0Aclass IndigenousLandSerializer(serializers.ModelSerializer):%0A%0A    associated_land = serializers.PrimaryKeyRelatedField(read_onl
 y=Tr
@@ -411,64 +411,8 @@
 ):%0A%0A
-    geometry = fields.GeometryField(source='position')%0A%0A
     
@@ -579,64 +579,8 @@
 ):%0A%0A
-    geometry = fields.GeometryField(source='position')%0A%0A
     

remove campos de geometria da API REST