Basic geometric operations

Here are some basic geometric operations we can perform on our polygon. We create the area, centroid, boundary, convex hull, buffer, and check if a polygon contains a certain point:

# 1 create area
In: print("The area of our polygon is %d" % polygon.Area())
Out: The area of our polygon is 16

# 2 calculate centroid of polygon
In: cen = polygon.Centroid()
print(cen)
Out: POINT (3 3)

# 3 Get the boundary
In: b = polygon.GetBoundary()
print(b)
Out: LINESTRING (1 1,5 1,5 5,1 5,1 1)

# 4 convex hull does the same in this case as boundary, as our polygon is a square:
In: ch = polygon.ConvexHull()
print(ch)
Out: POLYGON ((1 1,1 5,5 5,5 1,1 1))

# 5 buffer. A buffer value of 0 (zero) returns the same values as boundary and convex hull in this example:
In: buffer = polygon.Buffer(0)
print(buffer)
Out: POLYGON ((1 1,1 5,5 5,5 1,1 1))

# 6 check if a point is inside our polygon
In: point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10, 10)
polygon.Contains(point)
Out: False
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset