In this lecture, we will be seeing how to do simple vector manipulation using a library called Shapely. Shapely is essentially a Python library for set-theoretic analysis, and manipulation of planar features. It relies on GEOS library. Shapely allows Python programmers to perform geometric operations. It is based on the same technology which is behind PostGIS, and is largely guided by OGCs, Simple Feature Access Specification. Shapely however, does not have support for coordinate reference system. It assumes the features are on the same coordinate plane, same Cartesian plane, that is an important point for you to keep in mind. We should not try to do vector operations using Shapely on features which are on different Cartesian planes, that would give you a wrong results. The spatial data model associated with Shapely, is quite simple. It has fundamental geometric objects implemented which are points, curves, and surfaces. Each of these are associated with three sets of points in the plane, just interior point, boundary point, and exterior point, each of which are mutually exclusive. This might seem a bit esoteric, but understanding this fundamental knowledge and theory behind it will allow you to use Shapely correctly and understand what's happening in the operations which you are doing in Shapely. What is a point? Point in point class, point has exactly one interior point, which is itself and it has no points in its boundary set and all the other points except that point ordinates exterior set. A point has a topological dimension of zero. A curve in Shapely essentially represented by a LineString or a LinearRing Class has an interior set of points which are basically infinitely as many along the length of the line. The boundary points in that set are basically the two end points of the line and the exterior set is all the other points in the plane. A curve has a topological dimension of one, a surface represented by a polygon class, has an interior set consisting of infinitely many points. The boundary essentially is one or more curves and the exterior set is all the set of points, including those within the hole that might exist in the surface. A surface has a topological dimension of two. Exterior set of points, is all other points basically, that also includes holes that might exist within the surface. The surface has a topological dimension of two. The collection of points are implemented by a multipoint class. Collection of curves are implemented by a multiline string class, and a collection of surfaces are implemented by a multi polygon class. The relationship, which are there in Shapely, consist of natural language relationship between objects like contains, intersects, overlaps, touches and it's basically based off of the dimensionally extended nine intersection model that forms the theoretical basis of how Shapely deals with vector objects. We are not going into details of the nine-intersection model in this class, but that is something which forms the basis of Shapely. What's the point class and how do you use it within Shapely? It' very simple, you want to import Shapely, that's the first line you do and then you define a point. Basically, you can say sh_point is equal to shapely.geometry.point, and the coordinates for the point. Then when you say sh.point within your Jupyter environment, it will show you that point. If you see the type of that point it will show you it's a Shapely point. A line, a line is basically defined with the shapely.geometry.LineString, and it has exactly two points, 0, 0 and 1, 1, that defines a line from 0, 0 to 1, 1 a line segment. If you see the type of the line segment, it is a shapely.geometry.LineString class. Defining a polygon, shapely.geometry.polygon and then whatever are the coordinates associated with the vertex of the polygon. The polygon will close itself, in this case, 0, 0, 1,1, 1, 0, and then it automatically goes from the last to the first point which closes the polygon, and then you can see the polygon there displayed in Jupyter. A multi-polygon essentially is defined by shapely.geometry.multi-polygon, and it takes in, as its name suggests multiple polygons as inputs. If you see a multi polygon, sh_polygons, you will see those two polygons displayed there. Next, we'll see how we can display the coordinates of these polygons or multi-polygon. There are two basic formats, well-known text and well-known binary, so when you say sh.polygons.wkt, well-known text, you will see the multi-polygons coordinates being displayed as a text which you can understand. In this case you see two polygons there, 0 0, 1 1, 1 0, 0 0, that's forms the first polygon and the second polygon, which is starting from 2,2. As you see, these polygons are closed. Sh_polygons.wkb shows you the well-known binary, which something a computer can understand and it's not something you can easily understand. Similarly, Shapely has many geometric attributes and methods which you can use. For example to find centroid, you can use sh polygons.centroid, and to get the well-known text, you can add.wkt after it. Similarly, you can find area, length, boundary, bounds or envelope just by doing this. All these functions are directly implemented by Shapely and you don't need to implement them. You can directly use them within your program. Next we'll look at a polygons with holes example. Here, the first line we are defining a polygon, and then we are defining some holes in it. If we want to get the coordinates of the exterior point, we could just do this line and that will give us the list of exterior points. To get a list of interior points, you could do this. You can see, you can get both the exterior and the interior sets of points from the multi polygon in this example. With shapely, you are able to do and evaluate topological relationship. For example, if you define a line segment with the coordinate 0,0 and 1,1, which is basically a straight line, and if you want to see if it contains a point, so the line segment.contains and a point which is 0.5, 0.5 and it returns basically a true or false. In this case, the line segment has the point within it, which is why it returns true. Similarly, if you say the same point, which in this case is point 5,5, is it within the same line segment? That's also true because point 5,5 if you remember our definition is in the interior of the line segment. That's why it's contained within a line segment. Now, if we see, do say, does shapely.geometry.linesegment, the same line segment 0, 0 to 1, 1 contain the point 1, 1? In this case it returns false because if you remember the boundaries of the points, which is in this case 0, 0 and 1, 1 are in the boundary set, and not in the interior set. Which is why you are seeing that it returns false because the line segment does not contain the point 1, 1, it is in its boundary. Here are a few more examples. You are defining two line segments, 0, 0, 1, 1, and 1, 1, 2, 2. Now if we say, does line segment a touch line segment b? In this case, this returns true because they're sharing a common point which is the boundary point, so touches returns true, but an intersect will also return true because they intersect at the boundary. But overlap will return false because they're sharing the boundary point and not a set of internal points. That is why you need to understand the definitions of those boundary, interior and exterior sets of points. With this example, you have a and b as two line segments, which when we say a.touches b, it returns true, a.intersects b, it returns true, but a.overlaps b, returns false. Similarly, here.within b is false because is not contained within b or b is not contained within a. A contains b or is within b is both returning false. What you can see from this is, you can use Shapely if you have multiple geo-spatial objects or vector data that you want to evaluate some topological relationships of these datasets, you can easily use Shapely libraries, and a number of functions it provides, in order to evaluate that topological relationship of the vector data. Thank you.