From 5f3669c4e0556da59684d50261e3a0d93a31a044 Mon Sep 17 00:00:00 2001 From: Adrian Wilkins Date: Tue, 2 Apr 2019 15:22:10 +0100 Subject: [PATCH] Polygon parameters are a bit more flexible It's not really reflected in the Shapely API docs, but it's a natural thing to try in code, and hey, it works. Reading the source code reveals that Polygon can cope with a variety of inputs. --- source/lessons/L1/Geometric-Objects.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/source/lessons/L1/Geometric-Objects.rst b/source/lessons/L1/Geometric-Objects.rst index d55daa2..a92024f 100644 --- a/source/lessons/L1/Geometric-Objects.rst +++ b/source/lessons/L1/Geometric-Objects.rst @@ -243,18 +243,22 @@ Polygon ------- - Creating a Polygon -object continues the same logic of how Point and - LineString were created but Polygon object only accepts - coordinate-tuples as input. Polygon needs at least three - coordinate-tuples: + LineString were created. Polygon objects accept a list of coordinate tuples + as constructor arguments, but can also use other Shapely objects that + encapsulate coordinates, like a list of Point, or a LineString. + + Polygon needs at least three coordinates : .. ipython:: python # Create a Polygon from the coordinates poly = Polygon([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)]) - # We can also use our previously created Point objects (same outcome) - # --> notice that Polygon object requires x,y coordinates as input - poly2 = Polygon([[p.x, p.y] for p in [point1, point2, point3]]) + # We can also use our previously created Point objects + poly2 = Polygon([point1, point2, point3]) + + # Or we can use the LineString we made from them + poly3 = Polygon(line) # Geometry type can be accessed as a String poly_type = poly.geom_type @@ -265,6 +269,7 @@ Polygon # Let's see how our Polygon looks like print(poly) print(poly2) + print(poly3) print("Geometry type as text:", poly_type) print("Geometry how Python shows it:", poly_type2)