Python - shapely: Iterating with method “difference” return unexpected result











up vote
0
down vote

favorite












I'm actually working with shapely on python.



Here is the things:



I have one big polygon, let say



import matplotlib.pyplot as plt
import shapely
from shapely.geometry import Polygon
LAND = Polygon([(0, 0), (0, 20), (20, 20), (20, 0)])


and I have a list of random polygon generate with



import random
def generate_polygons(box_size=10, amount=15):
"""
A function that generate an amount of polygon randomly
in a square of size = box_size
"""
polygons =
for i in range(amount):
x = random.randint(0, box_size - 2)
y = random.randint(0, box_size - 2)
dx = 2
dy = 2
polygons.append(Polygon([(x, y), (x, y+dy), (x+dx, y+dy), (x+dx, y)]))
return polygons


I want to make the difference between the LAND and the list of polygons



diff = LAND
polygons = generate_polygons(20, 15)

for polygon in polygons:
diff = diff.difference(polygon)


Let's plot the result
Here are the polygons:



FIG, AXS = plt.subplots()

if (isinstance(polygons, shapely.geometry.polygon.Polygon)):
X, Y = polygons.exterior.xy
AXS.fill(X, Y, 'b')
else:
for polygon in polygons:
X, Y = polygon.exterior.xy
AXS.fill(X, Y, 'b')

plt.show()


Here is the diff:



FIG, AXS = plt.subplots()

if (isinstance(diff, shapely.geometry.polygon.Polygon)):
X, Y = diff.exterior.xy
AXS.fill(X, Y, 'r', alpha=0.5)
else:
for polygon in diff:
X, Y = polygon.exterior.xy
AXS.fill(X, Y, 'r', alpha=0.5)

plt.show()


polygon in blue, diff in red



I don't understand why It give me this result, anybody have an idea ?










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm actually working with shapely on python.



    Here is the things:



    I have one big polygon, let say



    import matplotlib.pyplot as plt
    import shapely
    from shapely.geometry import Polygon
    LAND = Polygon([(0, 0), (0, 20), (20, 20), (20, 0)])


    and I have a list of random polygon generate with



    import random
    def generate_polygons(box_size=10, amount=15):
    """
    A function that generate an amount of polygon randomly
    in a square of size = box_size
    """
    polygons =
    for i in range(amount):
    x = random.randint(0, box_size - 2)
    y = random.randint(0, box_size - 2)
    dx = 2
    dy = 2
    polygons.append(Polygon([(x, y), (x, y+dy), (x+dx, y+dy), (x+dx, y)]))
    return polygons


    I want to make the difference between the LAND and the list of polygons



    diff = LAND
    polygons = generate_polygons(20, 15)

    for polygon in polygons:
    diff = diff.difference(polygon)


    Let's plot the result
    Here are the polygons:



    FIG, AXS = plt.subplots()

    if (isinstance(polygons, shapely.geometry.polygon.Polygon)):
    X, Y = polygons.exterior.xy
    AXS.fill(X, Y, 'b')
    else:
    for polygon in polygons:
    X, Y = polygon.exterior.xy
    AXS.fill(X, Y, 'b')

    plt.show()


    Here is the diff:



    FIG, AXS = plt.subplots()

    if (isinstance(diff, shapely.geometry.polygon.Polygon)):
    X, Y = diff.exterior.xy
    AXS.fill(X, Y, 'r', alpha=0.5)
    else:
    for polygon in diff:
    X, Y = polygon.exterior.xy
    AXS.fill(X, Y, 'r', alpha=0.5)

    plt.show()


    polygon in blue, diff in red



    I don't understand why It give me this result, anybody have an idea ?










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm actually working with shapely on python.



      Here is the things:



      I have one big polygon, let say



      import matplotlib.pyplot as plt
      import shapely
      from shapely.geometry import Polygon
      LAND = Polygon([(0, 0), (0, 20), (20, 20), (20, 0)])


      and I have a list of random polygon generate with



      import random
      def generate_polygons(box_size=10, amount=15):
      """
      A function that generate an amount of polygon randomly
      in a square of size = box_size
      """
      polygons =
      for i in range(amount):
      x = random.randint(0, box_size - 2)
      y = random.randint(0, box_size - 2)
      dx = 2
      dy = 2
      polygons.append(Polygon([(x, y), (x, y+dy), (x+dx, y+dy), (x+dx, y)]))
      return polygons


      I want to make the difference between the LAND and the list of polygons



      diff = LAND
      polygons = generate_polygons(20, 15)

      for polygon in polygons:
      diff = diff.difference(polygon)


      Let's plot the result
      Here are the polygons:



      FIG, AXS = plt.subplots()

      if (isinstance(polygons, shapely.geometry.polygon.Polygon)):
      X, Y = polygons.exterior.xy
      AXS.fill(X, Y, 'b')
      else:
      for polygon in polygons:
      X, Y = polygon.exterior.xy
      AXS.fill(X, Y, 'b')

      plt.show()


      Here is the diff:



      FIG, AXS = plt.subplots()

      if (isinstance(diff, shapely.geometry.polygon.Polygon)):
      X, Y = diff.exterior.xy
      AXS.fill(X, Y, 'r', alpha=0.5)
      else:
      for polygon in diff:
      X, Y = polygon.exterior.xy
      AXS.fill(X, Y, 'r', alpha=0.5)

      plt.show()


      polygon in blue, diff in red



      I don't understand why It give me this result, anybody have an idea ?










      share|improve this question















      I'm actually working with shapely on python.



      Here is the things:



      I have one big polygon, let say



      import matplotlib.pyplot as plt
      import shapely
      from shapely.geometry import Polygon
      LAND = Polygon([(0, 0), (0, 20), (20, 20), (20, 0)])


      and I have a list of random polygon generate with



      import random
      def generate_polygons(box_size=10, amount=15):
      """
      A function that generate an amount of polygon randomly
      in a square of size = box_size
      """
      polygons =
      for i in range(amount):
      x = random.randint(0, box_size - 2)
      y = random.randint(0, box_size - 2)
      dx = 2
      dy = 2
      polygons.append(Polygon([(x, y), (x, y+dy), (x+dx, y+dy), (x+dx, y)]))
      return polygons


      I want to make the difference between the LAND and the list of polygons



      diff = LAND
      polygons = generate_polygons(20, 15)

      for polygon in polygons:
      diff = diff.difference(polygon)


      Let's plot the result
      Here are the polygons:



      FIG, AXS = plt.subplots()

      if (isinstance(polygons, shapely.geometry.polygon.Polygon)):
      X, Y = polygons.exterior.xy
      AXS.fill(X, Y, 'b')
      else:
      for polygon in polygons:
      X, Y = polygon.exterior.xy
      AXS.fill(X, Y, 'b')

      plt.show()


      Here is the diff:



      FIG, AXS = plt.subplots()

      if (isinstance(diff, shapely.geometry.polygon.Polygon)):
      X, Y = diff.exterior.xy
      AXS.fill(X, Y, 'r', alpha=0.5)
      else:
      for polygon in diff:
      X, Y = polygon.exterior.xy
      AXS.fill(X, Y, 'r', alpha=0.5)

      plt.show()


      polygon in blue, diff in red



      I don't understand why It give me this result, anybody have an idea ?







      python polygon difference shapely






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 9:15

























      asked Nov 7 at 9:08









      Bourhis Nicolas

      94




      94
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          The final answer is that Shpely DOES the operation but pyplot can't plot it !






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186359%2fpython-shapely-iterating-with-method-difference-return-unexpected-result%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            The final answer is that Shpely DOES the operation but pyplot can't plot it !






            share|improve this answer



























              up vote
              0
              down vote













              The final answer is that Shpely DOES the operation but pyplot can't plot it !






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                The final answer is that Shpely DOES the operation but pyplot can't plot it !






                share|improve this answer














                The final answer is that Shpely DOES the operation but pyplot can't plot it !







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 at 11:04

























                answered Nov 7 at 9:36









                Bourhis Nicolas

                94




                94






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186359%2fpython-shapely-iterating-with-method-difference-return-unexpected-result%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini