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 ?
python polygon difference shapely
add a comment |
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 ?
python polygon difference shapely
add a comment |
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 ?
python polygon difference shapely
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
python polygon difference shapely
edited Nov 7 at 9:15
asked Nov 7 at 9:08
Bourhis Nicolas
94
94
add a comment |
add a comment |
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 !
add a comment |
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 !
add a comment |
up vote
0
down vote
The final answer is that Shpely DOES the operation but pyplot can't plot it !
add a comment |
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 !
The final answer is that Shpely DOES the operation but pyplot can't plot it !
edited Nov 9 at 11:04
answered Nov 7 at 9:36
Bourhis Nicolas
94
94
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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