Specifying Precision and Using Simplify

34 views
Skip to first unread message

Taylor Johnson

unread,
Aug 27, 2018, 2:14:16 PM8/27/18
to Django REST Framework GIS
I am relatively new to Python so I am hoping the answer is pretty straightforward, but I can't figure out how to simplify the geometry before adjusting the precision. 

I have:


lines = GeometrySerializerMethodField()
lines2 = GeometryField(2, True, source='lines')

def get_lines(self, obj):
# return obj.lines
tolerance = float(self.context['request'].query_params.get('simplify_tolerance', 0.1))
preserve_topology = self.context['request'].query_params.get('simplify_preserve_topology', 'false').lower() == 'true'
return obj.lines.simplify(tolerance, preserve_topology) if obj.lines is not None else None


Lines returns simplified geometry but with all decimal places. Lines2 returns all geometry with 2 decimal places. How would I combine these 2 so I can have simplified geometry with fewer decimal places?

Thanks,
Taylor Johnson

Taylor Johnson

unread,
Aug 27, 2018, 2:40:56 PM8/27/18
to Django REST Framework GIS
I think I figured it out, if nobody has a better suggestion. I needed to use SerializerMethodField instead of GeometrySerializerMethodField and then call GeometryField from within that. Like this:

lines = serializers.SerializerMethodField()

def get_lines_test(self, obj):

tolerance = float(self.context['request'].query_params.get('simplify_tolerance', 0.1))
preserve_topology = self.context['request'].query_params.get('simplify_preserve_topology',
'false').lower() == 'true'
    simplified = obj.lines.simplify(tolerance, preserve_topology) if obj.lines is not None else None
return GeometryField(precision=2, remove_duplicates=True).to_representation(simplified)
Reply all
Reply to author
Forward
0 new messages