[MUSIC] 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 GEO's library. Shapely allows Python programmers to perform geometric operations. It is based on the same technology, which is behind PostGIS is and is largely guided by OGC s 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 not different cartesian planes. That would give you a wrong results. So 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're doing in shapely. So what is the point? Point in point class, point has exactly one interior point, which is itself, and it has no points in its boundary set and no points. And all the other points except that point are in its exterior set. A point has a topological dimension of zero, a curve in shapely, essentially represented by a line string or a line ring. Class has an imperious set of points, which are basically infinitely as many along the length of the line. The boundary points and 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 what 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. So 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. And 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 multiply in string class. And a collection of surfaces are implemented by a MultiPolygon class. The relationship which are there in shapely, consists 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. So what's the point class and how do you use it within shapely? It's very simple. You want to import shapely. That's the first line you do, and then you define a point. Basically, you can say such _point= shapely.geometry.point and the coordinates for the point. And then when you say sh.point within your Jupyter environment, it will show you that point, and 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 a shapely.geometry.Linestring, and it has exactly 2 0 0 and 1 1 that defines a line from 0 0 to 1 1 a line segment. And if you see the type of the line segment, it is a strictly.geometry.Line string, plus defining a polygon shapely.geometry.polygon. And then, whatever are the coordinates associated with the vertex of the polygon. The polygon will close itself, so in this case, 001110 and then it automatically close from the last to the first point, which is which closes the polygon. And then you can see the polygon there displayed in jupyter. A multi polygon essentially is taking defined by shapely.geometry.multiple a guard, and it takes in assets names such as multiple polygons as inputs. So if you see a multi polygon as 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. 00111000 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 at sh_ polygons.wkb shows you the well known binary, which something a computer can understand. 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 a .wkt after it. Similarly, you can find area laying boundary bounds or envelope just by doing this. So 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 polygon 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 so you can see you can get both the exterior and the interior sets of points from the multi polygon in this example. So shapely you're able to do and evaluate topological relationship. For example, if you define a line segment with the coordinates 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 took through. Similarly, if you say the same point, which in this case is 0.55 is it within the line segment, the same line segment. That's also true because 0.55 is if you remember, our definitions is in the interior of the line segment, so that's why it's contained within a line segment. Now, if you 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're 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 dust line segment a touchline segment b. In this case, this returns true because they are touching, they are sharing a common point, which is the boundary point, and so touches returns true. But and intersect will also return true because they intersect at the boundary. But overlap will return false because they are sharing the boundary point and not a set of internal points. So that is kind of why you need to understand the definitions of those boundary interior and exterior sets of points. So with this example, you have a and b as two line segments which when we say a touches b .touches b. It returns true a.intersex b it returns true, but a.overlaps b returns false. Similarly, a.within(b) is false because a it's not contained within b or b is not contained within a. So it contains b or a is within b is both returning false. So what you can see from this is you can use shapely if you have multiple geospatial objects or vector data that you want to evaluate some topological relationships of these data sets. 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.