﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31142	"MakeValid() polygon to multi-polygon raises error in GEOS C function ""GEOSGetNumInteriorRings_r""."	Eran Keydar	Sergey Fedoseev	"I have model with Polygon field. 
Using MakeValid on the polygon field returns MultiPolygon, but the resulting geom is broken.

This worked on 1.11 and failed on 2.2

A simple solution is to make the fix on MultiPolygon field (with single polygon).


This is the model (app1/models.py):

{{{
from django.contrib.gis.db import models
class WithPolygon(models.Model):
    geom = models.PolygonField()

}}}

Then I run the following code:


{{{
def fix_polygon():
    import django
    from django.contrib.gis.db.models.functions import MakeValid
    from django.contrib.gis.geos import MultiPolygon, Polygon
    from app1.models import WithPolygon
    print('version = {}'.format(django.__version__))
   # create disjoint rings
    r1 = [[0,0],[100,0],[100,100],[0, 100], [0,0]]
    r2 = [[x+1000,y+1000] for x,y in r1]
   #  create polygon with two rings
    p1 = Polygon(r1, r2)
    print('p1.valid  = {}'.format(p1.valid))  # False
    print('p1.valid_reason =  {}'.format(p1.valid_reason)) # Hole lies outside shell[1000 1000]
    wp1 = WithPolygon.objects.create(geom=p1)
    wp1_with_fix = WithPolygon.objects.annotate(v=MakeValid('geom')).filter(id=wp1.id)[0]
    mp = wp1_with_fix.v
    print('mp.geom_type = {}'.format(mp.geom_type)) # MultiPolygon
    print('mp.__class__ = {}'.format(mp.__class__)) # returns Polygon in 2.2 MultiPolygon in 1.11
    print(len(mp))  # GESOEXception in 2.2, (In 1.11 prints 2)
}}}


Outputs are attached.
"	Bug	closed	GIS	dev	Normal	fixed		Sergey Fedoseev	Ready for checkin	1	0	0	0	0	0
