Invalid multipolygon of valid individual polygons

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












Could anyone please explain this to me? The polygons all have valid geometry but the resulting multipolygon somehow doesn't. The following script:



from shapely.geometry import Polygon, MultiPolygon

polygon1 = Polygon([(0,0), (1,0), (1,1), (0,1), (0,0)])
polygon2 = Polygon([(1,1), (2,1), (2,2), (1,2), (1,1)])
polygon3 = Polygon([(1,0), (2,0), (2,1), (1,1), (1,0)])
polygon_list = [polygon1,polygon2, polygon3]

mpolygon = MultiPolygon(polygon_list)

for item in polygon_list:
print(item.is_valid)
for item in mpolygon:
print(item.is_valid)

print(mpolygon.is_valid)


returns the following:



True
True
True
True
True
True
False


I believe this is causing me problems as I need to intersect a multilinestring with a multipolygon, and the intersection method requires for all the polygon geometries to be valid.



Why are the individual polygons valid while the full multipolygon is not?










share|improve this question



























    up vote
    1
    down vote

    favorite












    Could anyone please explain this to me? The polygons all have valid geometry but the resulting multipolygon somehow doesn't. The following script:



    from shapely.geometry import Polygon, MultiPolygon

    polygon1 = Polygon([(0,0), (1,0), (1,1), (0,1), (0,0)])
    polygon2 = Polygon([(1,1), (2,1), (2,2), (1,2), (1,1)])
    polygon3 = Polygon([(1,0), (2,0), (2,1), (1,1), (1,0)])
    polygon_list = [polygon1,polygon2, polygon3]

    mpolygon = MultiPolygon(polygon_list)

    for item in polygon_list:
    print(item.is_valid)
    for item in mpolygon:
    print(item.is_valid)

    print(mpolygon.is_valid)


    returns the following:



    True
    True
    True
    True
    True
    True
    False


    I believe this is causing me problems as I need to intersect a multilinestring with a multipolygon, and the intersection method requires for all the polygon geometries to be valid.



    Why are the individual polygons valid while the full multipolygon is not?










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Could anyone please explain this to me? The polygons all have valid geometry but the resulting multipolygon somehow doesn't. The following script:



      from shapely.geometry import Polygon, MultiPolygon

      polygon1 = Polygon([(0,0), (1,0), (1,1), (0,1), (0,0)])
      polygon2 = Polygon([(1,1), (2,1), (2,2), (1,2), (1,1)])
      polygon3 = Polygon([(1,0), (2,0), (2,1), (1,1), (1,0)])
      polygon_list = [polygon1,polygon2, polygon3]

      mpolygon = MultiPolygon(polygon_list)

      for item in polygon_list:
      print(item.is_valid)
      for item in mpolygon:
      print(item.is_valid)

      print(mpolygon.is_valid)


      returns the following:



      True
      True
      True
      True
      True
      True
      False


      I believe this is causing me problems as I need to intersect a multilinestring with a multipolygon, and the intersection method requires for all the polygon geometries to be valid.



      Why are the individual polygons valid while the full multipolygon is not?










      share|improve this question















      Could anyone please explain this to me? The polygons all have valid geometry but the resulting multipolygon somehow doesn't. The following script:



      from shapely.geometry import Polygon, MultiPolygon

      polygon1 = Polygon([(0,0), (1,0), (1,1), (0,1), (0,0)])
      polygon2 = Polygon([(1,1), (2,1), (2,2), (1,2), (1,1)])
      polygon3 = Polygon([(1,0), (2,0), (2,1), (1,1), (1,0)])
      polygon_list = [polygon1,polygon2, polygon3]

      mpolygon = MultiPolygon(polygon_list)

      for item in polygon_list:
      print(item.is_valid)
      for item in mpolygon:
      print(item.is_valid)

      print(mpolygon.is_valid)


      returns the following:



      True
      True
      True
      True
      True
      True
      False


      I believe this is causing me problems as I need to intersect a multilinestring with a multipolygon, and the intersection method requires for all the polygon geometries to be valid.



      Why are the individual polygons valid while the full multipolygon is not?







      python shapely






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 43 mins ago









      Kadir

      3,88011226




      3,88011226










      asked 1 hour ago









      user32882

      99911124




      99911124




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:



          enter image description here
          ...On the right, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link



          To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).



          Your polygons plotted:



          enter image description here






          share|improve this answer





























            up vote
            1
            down vote













            In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.



            mpolygon = mpolygon.buffer(0)
            print(mpolygon.is_valid)
            print(type(mpolygon))
            # OUT:
            # True
            # <class 'shapely.geometry.polygon.Polygon'>


            enter image description here



            If you need multipolygon, you have to convert polygon to multipolygon.



            if isinstance(mpolygon, Polygon):
            mpolygon = Multipolygon([mpolygon.buffer(0)])





            share|improve this answer






















            • I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
              – user32882
              8 mins ago











            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f298307%2finvalid-multipolygon-of-valid-individual-polygons%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:



            enter image description here
            ...On the right, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link



            To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).



            Your polygons plotted:



            enter image description here






            share|improve this answer


























              up vote
              2
              down vote



              accepted










              It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:



              enter image description here
              ...On the right, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link



              To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).



              Your polygons plotted:



              enter image description here






              share|improve this answer
























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:



                enter image description here
                ...On the right, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link



                To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).



                Your polygons plotted:



                enter image description here






                share|improve this answer














                It's because polygon1 and polygon3 that you created intersect along an infinite amount of points. Taken here from the Shapely documentation:



                enter image description here
                ...On the right, a valid MultiPolygon with 2 members, and on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line). Link



                To fix this, you need to shift polygon3 so that it will have a maximum of one intersecting point with the other polygons (or none at all).



                Your polygons plotted:



                enter image description here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 38 mins ago

























                answered 44 mins ago









                15Step

                1,1911217




                1,1911217






















                    up vote
                    1
                    down vote













                    In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.



                    mpolygon = mpolygon.buffer(0)
                    print(mpolygon.is_valid)
                    print(type(mpolygon))
                    # OUT:
                    # True
                    # <class 'shapely.geometry.polygon.Polygon'>


                    enter image description here



                    If you need multipolygon, you have to convert polygon to multipolygon.



                    if isinstance(mpolygon, Polygon):
                    mpolygon = Multipolygon([mpolygon.buffer(0)])





                    share|improve this answer






















                    • I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                      – user32882
                      8 mins ago















                    up vote
                    1
                    down vote













                    In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.



                    mpolygon = mpolygon.buffer(0)
                    print(mpolygon.is_valid)
                    print(type(mpolygon))
                    # OUT:
                    # True
                    # <class 'shapely.geometry.polygon.Polygon'>


                    enter image description here



                    If you need multipolygon, you have to convert polygon to multipolygon.



                    if isinstance(mpolygon, Polygon):
                    mpolygon = Multipolygon([mpolygon.buffer(0)])





                    share|improve this answer






















                    • I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                      – user32882
                      8 mins ago













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.



                    mpolygon = mpolygon.buffer(0)
                    print(mpolygon.is_valid)
                    print(type(mpolygon))
                    # OUT:
                    # True
                    # <class 'shapely.geometry.polygon.Polygon'>


                    enter image description here



                    If you need multipolygon, you have to convert polygon to multipolygon.



                    if isinstance(mpolygon, Polygon):
                    mpolygon = Multipolygon([mpolygon.buffer(0)])





                    share|improve this answer














                    In addition to @15Step's answer, If you want to fix the invalidity, use buffer method. But if polygons are adjacent, you get polygon instead of multipolygon.



                    mpolygon = mpolygon.buffer(0)
                    print(mpolygon.is_valid)
                    print(type(mpolygon))
                    # OUT:
                    # True
                    # <class 'shapely.geometry.polygon.Polygon'>


                    enter image description here



                    If you need multipolygon, you have to convert polygon to multipolygon.



                    if isinstance(mpolygon, Polygon):
                    mpolygon = Multipolygon([mpolygon.buffer(0)])






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 3 mins ago

























                    answered 15 mins ago









                    Kadir

                    3,88011226




                    3,88011226











                    • I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                      – user32882
                      8 mins ago

















                    • I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                      – user32882
                      8 mins ago
















                    I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                    – user32882
                    8 mins ago





                    I need an mpolygon, because I wish to do mpolygon.intersection(multilinestring) where mpolygon has many polygons and multilinestring has many LineStrings which may or may not share vertices
                    – user32882
                    8 mins ago


















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f298307%2finvalid-multipolygon-of-valid-individual-polygons%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery