{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "1jgYvotJf4Lf"
},
"source": [
"# Session 1: Geometric Objects\n",
"*Written by Men Vuthy, 2021*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "V_yTvAm0f4Lm"
},
"source": [
"### Overview:\n",
"\n",
"In this session, we will learn the most fundamental geometric objects (i.e. Points, Lines and Polygons) which are important vector elements in spatial data analysis. In order to read or write these objects in Python, we use a package called [Shapely](https://shapely.readthedocs.io/en/stable/manual.html). Shapely is a Python package for the manipulation and analysis of two-dimensional geospatial geometries."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HJAU0IlFf4Lo"
},
"source": [
"Let's learn how to create Point, Line and Polygon with Shapely module by passing coordinates (x, y [, z]) into `Point()`, `LineString()`, and `Polygon()`\n",
"\n",
"**1. Point**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fld_xNeif4Lp"
},
"source": [
"Creating point is easy, you just pass x and y coordinates into `Point()` -object as follows:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"executionInfo": {
"elapsed": 25,
"status": "ok",
"timestamp": 1627099029483,
"user": {
"displayName": "Sardor Erjigitov",
"photoUrl": "",
"userId": "01264172018502842517"
},
"user_tz": -300
},
"id": "BmTfQQPPf4Lr"
},
"outputs": [],
"source": [
"# Import shapely module for Point\n",
"from shapely.geometry import Point"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oBlM5s_qf4Lw"
},
"source": [
"Now we can see how each point looks like:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 120
},
"executionInfo": {
"elapsed": 388,
"status": "ok",
"timestamp": 1627099232181,
"user": {
"displayName": "Sardor Erjigitov",
"photoUrl": "",
"userId": "01264172018502842517"
},
"user_tz": -300
},
"id": "FAWNxjPRf4Ly",
"outputId": "9edd3948-1d4a-454b-9bc2-ff0773ca5a6d"
},
"outputs": [],
"source": [
"# Create Point geometric object(s) with coordinates\n",
"point1 = Point(2.2, 4.2)\n",
"point2 = Point(7.2, -25.1)\n",
"point3 = Point(9.26, -2.456)\n",
"point3D = Point(9.26, -2.456, 0.57)\n",
"point4 = Point(10.26, -3.456)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z8SloNfPf4L1"
},
"source": [
"To check the information of each point, we use the `Print()` statement as follows:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 306,
"status": "ok",
"timestamp": 1627099240373,
"user": {
"displayName": "Sardor Erjigitov",
"photoUrl": "",
"userId": "01264172018502842517"
},
"user_tz": -300
},
"id": "zc3W3BAaf4L2",
"outputId": "a8a61bf5-697e-4d82-8499-5bd44065dc23"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POINT (2.2 4.2)\n",
"POINT Z (9.26 -2.456 0.57)\n"
]
}
],
"source": [
"print(point1)\n",
"print(point3D)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "za9iSuxKf4L4"
},
"source": [
"When passing 3-dimension point, it will result in letter `Z` in front of object definition. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3N6kMAsPf4L4"
},
"source": [
"So let's check the type of a point"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "P1QaETgRf4L5",
"outputId": "fac0061f-37aa-4a00-cbbb-7d613142b944"
},
"outputs": [
{
"data": {
"text/plain": [
"shapely.geometry.point.Point"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(point1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Vg6l_ed0f4L7"
},
"source": [
"We can check the geometry type of the object by using `Point.geom_type`:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "mlVv1OqGf4L8",
"outputId": "a6e1b760-3d0f-488a-f40e-0419e1f31bc3"
},
"outputs": [
{
"data": {
"text/plain": [
"'Point'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point1.geom_type"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "y_vezm3df4MA"
},
"source": [
"**Point attributes and functions**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VNvr-qnOf4MC"
},
"source": [
"In shapely Point, there are some useful built-in attributes and fuctions for data analysis such as the fuction to extract coordinates x or y, and the fuction to calculate Euclidian distance between two points."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zudum8yuf4MC"
},
"source": [
"There are few ways to extract the coordinates of a Point:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "RO0JTTdbf4MD",
"outputId": "1ed06fbd-7c27-4f82-ccf0-a162aa331e67"
},
"outputs": [
{
"data": {
"text/plain": [
"[(2.2, 4.2)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get xy coordinate tuple\n",
"list(point1.coords)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ei-YSIgMf4ME"
},
"source": [
"In a list, there are tuples which can be extracted by using the attributes `x` and `y`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "utgyQq_rf4MF",
"outputId": "72400786-a89f-4492-f89a-7a2c36633b77"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.2\n",
"4.2\n",
"2.2 4.2\n"
]
}
],
"source": [
"# Read x and y coordinates separately\n",
"x = point1.x\n",
"y = point1.y\n",
"print(x)\n",
"print(y)\n",
"print( x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JB_x0GDJf4MG"
},
"source": [
"Let's step into calculating distance between two points based on the cartesian coordinate system. When working with real GIS data, determine the coordinate reference system (CRS) is very important. Besides, you should always check the unit of measurement (Ex: meters) in the CRS you're using."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bnw_sDmSf4MH"
},
"source": [
"To calculate the distance between 2 points or objects, we use `.distance` method in Shapely. Let's calculate distance between point3 and point 4:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "9fsYIPMpf4MH",
"outputId": "3cce114c-6c24-4d0b-f8e0-ee611e7c49ad"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POINT (9.26 -2.456)\n",
"POINT (10.26 -3.456)\n"
]
}
],
"source": [
"# Check input data\n",
"print(point3)\n",
"print(point4)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "vyKW0aVGf4MI",
"outputId": "2fc363c9-ec2b-4ef2-b3d4-c11c494ae126"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Distance between the points is 1.414 units (m or km).\n"
]
}
],
"source": [
"# Calculate the distance between point3 and point4\n",
"dist = point3.distance(point4)\n",
"\n",
"# Print out result\n",
"print(\"Distance between the points is {0:.3f} units (m or km).\".format(dist))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6c-jEIBef4MJ"
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-ERiD38mf4MJ"
},
"source": [
"**2.LineString**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PHw-Ymqvf4MJ"
},
"source": [
"So far you know how to create Point and check its geometric information. Now, let's create LingString by using `LineString()` statement as follows:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "lK5zJG3kf4MK"
},
"outputs": [],
"source": [
"# Import shapely module for LineString\n",
"from shapely.geometry import LineString"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"id": "2NjRboxCf4ML",
"outputId": "6ec9a9e4-0f09-4b61-b089-6852949ac1a5"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create linestring from two points with tuple\n",
"line = LineString([(0, 0), (1, 1)])\n",
"line"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "YHPs7WShf4MO",
"outputId": "5eb3352e-36ce-4b88-f1ff-8fdd6efd80bb"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create linestring using more than 2 points from Point objects we defined above.\n",
"line_ptobj = LineString([point1, point2, point3]) # Point object\n",
"line_tuple = LineString([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456)])\n",
"\n",
"line_ptobj"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"id": "tDshK1wQf4MQ",
"outputId": "733354ce-e60d-4a26-8dc3-ef60eab3300b"
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if lines are identical\n",
"line_ptobj == line_tuple "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "nRLCFj5Lf4MS",
"outputId": "4e6e48a1-200a-4eaf-d4e6-47c45c167af6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LINESTRING (2.2 4.2, 7.2 -25.1, 9.26 -2.456)\n"
]
}
],
"source": [
"print(line_ptobj)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rttZnx9Sf4MT"
},
"source": [
"We can see that LingString is a combination of multiple points' coordinate.\n",
"\n",
"Let's then check the type of geometry."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"id": "TrBgD1Yxf4MU",
"outputId": "0fa45b5a-f522-49d9-875c-90cecf643de5"
},
"outputs": [
{
"data": {
"text/plain": [
"shapely.geometry.linestring.LineString"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(line_ptobj)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"id": "J1Kjw291f4MV",
"outputId": "f9609321-900d-4432-9e1f-5d01c3c37eee"
},
"outputs": [
{
"data": {
"text/plain": [
"'LineString'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"line_ptobj.geom_type"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8orFUDHyf4MW"
},
"source": [
"**LineString attributes and functions**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CIIt3CYcf4MW"
},
"source": [
"In shapely LineString, there are also some useful built-in attributes and fuctions. With these fuctions, we can calculate length or centroid, buffer the line, create points along the line at specific distance, calculate the distance from points, etc. Check more fuctions and attributes in [Shapely document](https://shapely.readthedocs.io/en/stable/manual.html)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"id": "Y-oqihA6f4MX",
"outputId": "3797b12e-4a13-45b8-8580-d30dd49e1c90"
},
"outputs": [
{
"data": {
"text/plain": [
"[(0.0, 0.0), (1.0, 1.0)]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get x and y coordinates of the line\n",
"list(line.coords)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"id": "B79TfUkaf4MX",
"outputId": "c3e0ac8e-7bcb-461f-b93e-84caad9d1825"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of our line: 1.41 units\n"
]
}
],
"source": [
"# Get length of the line\n",
"length = line.length\n",
"\n",
"print(\"Length of our line: {0:.2f} units\".format(length))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"id": "COBVX3Wtf4MY",
"outputId": "ef2b4a83-48a7-491e-f74e-3b2b4d84e36a"
},
"outputs": [
{
"data": {
"text/plain": [
"(0.0, 0.0, 1.0, 1.0)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get x-y bounding box is a (minx, miny, maxx, maxy) tuple\n",
"line.bounds"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"id": "1lCt2CeUf4MY",
"outputId": "346923e3-b443-472d-fb46-46aab783ef41"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POINT (0.5 0.5)\n"
]
}
],
"source": [
"# Get the centroid of the line\n",
"print(line.centroid)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Xt7Azo1ff4MZ"
},
"source": [
"Notice: the centroid of the line is a Shapely Point object."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jBY2RF8hf4Ma"
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Wbpnue9Rf4Ma"
},
"source": [
"**3. Polygon**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "O-qfIJ_uf4Ma"
},
"source": [
"Creating a `Polygon` object is not so different from creating `Point` and `LineString` object; however, it takes the input as a sequence of coordinates.\n",
"\n",
"We can create a polygon by using `Polygon()` as follows:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"id": "0G9XK-w9f4Mb"
},
"outputs": [],
"source": [
"# Import shapely module for Polygon\n",
"from shapely.geometry import Polygon"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"id": "k5lcAueGf4Me",
"outputId": "a0f2aaef-9899-48c4-a9ef-0adeda7d4330"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a Polygon from the coordinates\n",
"polygon = Polygon([(0, 0), (1, 1), (1, 0)])\n",
"polygon"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"id": "gnCif5jdf4Mf",
"outputId": "8f7def83-2171-48b4-d7df-b4a1b33fece8"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create a Polygon from the points we created above\n",
"polygon2 = Polygon([(pt.x, pt.y) for pt in (point1, point2, point3, point4)])\n",
"polygon2"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"id": "w2tRrjh1f4Mi",
"outputId": "b05c10e0-2ffd-40be-bf62-85c2abd878b4"
},
"outputs": [
{
"data": {
"text/plain": [
"[(2.2, 4.2), (7.2, -25.1), (9.26, -2.456), (10.26, -3.456)]"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's see what just happened in the point list\n",
"[(pt.x, pt.y) for pt in (point1, point2, point3, point4)]"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"id": "gPMrXsS2f4Mj",
"outputId": "4fc62f10-eb4b-4b71-9b6c-1a065836030e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POLYGON ((2.2 4.2, 7.2 -25.1, 9.26 -2.456, 10.26 -3.456, 2.2 4.2))\n",
"\n",
"Polygon\n"
]
}
],
"source": [
"# Let's see the geometric type when we print it\n",
"print(polygon2)\n",
"print(type(polygon2))\n",
"print(polygon2.geom_type)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "77m5Geayf4Ml"
},
"source": [
"We can also create a polygon with a hole. \n",
"So, let's learn how to create this kind of polygon with `Polygon(shell, holes)`. The type of\n",
"`shell` and `holes` should be a list of points."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"id": "KcYMj8Rwf4Ms"
},
"outputs": [],
"source": [
"# Define exterior boundary (shell) and interior boundary (holes)\n",
"shell = [(0, 0), (0, 2), (2, 2), (2, 0)]\n",
"hole = [[(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5)]]\n",
"\n",
"# Create polygon with holes\n",
"polygon = Polygon(shell, hole) # or Polygon(shell = shell, holes = hole)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"id": "Xodwri9Ff4Mt",
"outputId": "3b4a6f10-f8a3-4c1d-c5f4-faab50b6aba3"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's see how polygon looks like\n",
"polygon"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"id": "48pKG7Ngf4Mu",
"outputId": "51b1d322-103a-41af-c014-7203973c06a4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n"
]
}
],
"source": [
"# Check the type of shell and holes\n",
"print(type(shell))\n",
"print(type(hole))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HYmxtdOJf4Mv"
},
"source": [
"**Polygon attributes and functions**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5ZG7czHdf4Mw"
},
"source": [
"We can again access different attributes directly from the Polygon object itself that can be really useful for many analyses, such as area, centroid, bounding box, exterior, and exterior-length. "
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"id": "DHi_IEDRf4Mx",
"outputId": "48efbf81-965f-4903-ed6c-a177636cb20a"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Define polygon again\n",
"polygon = Polygon(shell)\n",
"polygon"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"id": "DHurOm8Hf4Mz",
"outputId": "2e23b400-c436-4905-9c43-515907dd4e87"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Polygon centroid: POINT (1 1)\n",
"Polygon Area: 4.0\n",
"Polygon Bounding Box: (0.0, 0.0, 2.0, 2.0)\n",
"Polygon Exterior: LINEARRING (0 0, 0 2, 2 2, 2 0, 0 0)\n",
"Polygon Exterior Length: 8.0\n"
]
}
],
"source": [
"# Print the outputs\n",
"print(\"Polygon centroid: \", polygon.centroid)\n",
"print(\"Polygon Area: \", polygon.area)\n",
"print(\"Polygon Bounding Box: \", polygon.bounds)\n",
"print(\"Polygon Exterior: \", polygon.exterior)\n",
"print(\"Polygon Exterior Length: \", polygon.exterior.length)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jWeWpxadf4M1"
},
"source": [
"Now we see that it is easy to access the polygon's attributes with the method above. See a full list of methods in the [Shapely User Manual](https://shapely.readthedocs.io/en/stable/manual.html)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8qmybkZyf4M1"
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "J-yYB76Gf4M2"
},
"source": [
"### Exercises"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "p0g-wJACf4M2"
},
"source": [
"Plot these shapes using Shapely with the given coordinates:"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DDVq7Bv4f4M2"
},
"source": [
"1. Create Point: (31.91, 0.62), and extract `x` and `y` coordinates\n",
"2. Create Line: (31.18, -1.63), (28.82, -1.63), (28.09, 0.62), and calculate the length of line.\n",
"3. Create Polygon: (30, 2.01), (31.91, 0.62), (31.18, -1.63), (28.82, -1.63), (28.09, 0.62), and calculate the polygon area."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cjVL73-6f4M3"
},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cPdiXM9uf4M3"
},
"source": [
"**4. Geometry collections**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BCd-tCy2f4M3"
},
"source": [
"In real practice, you will need to store multiplt points, linestrings, or polygons in a collection.\n",
"Therefore, it is important to know how to create or analyze more than a single feature using Shapely. \n",
"\n",
"In Shapely:\n",
"* collections of points are created by using a MultiPoint -object\n",
"* collections of lines by using a MultiLineString -object\n",
"* and collections of polygon by a MultiPolygon -object."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"id": "0kg40fTwf4M4"
},
"outputs": [],
"source": [
"# Import module for creating geometry collections\n",
"from shapely.geometry import MultiPoint, MultiLineString, MultiPolygon"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"id": "dd4lsHZWf4M4"
},
"outputs": [],
"source": [
"# Create a MultiPoint object of our points 1,2,3 and 4\n",
"multi_point = MultiPoint([point1, point2, point3, point4])\n",
"\n",
"# It is also possible to pass coordinate tuples inside\n",
"multi_point2 = MultiPoint([(2.2, 4.2), (7.2, -25.1), (9.26, -2.456), (7.2, -2.456)])"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"id": "eefpXFmGf4M5",
"outputId": "40c0227f-11fc-4fb2-f3d4-7aa364cb3efa"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MULTIPOINT (2.2 4.2, 7.2 -25.1, 9.26 -2.456, 10.26 -3.456)\n"
]
},
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print object definitions\n",
"print(multi_point)\n",
"multi_point"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"id": "pHI4MTmMf4M6"
},
"outputs": [],
"source": [
"# We can also create a MultiLineString with two lines\n",
"line1 = LineString([point1, point2])\n",
"line2 = LineString([point2, point3])\n",
"line3 = LineString([point3, point4])\n",
"multi_line = MultiLineString([line1, line2, line3])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"id": "thBv0_Wff4M6",
"outputId": "e1755b86-7a34-4adf-a827-1b687260a258"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MULTILINESTRING ((2.2 4.2, 7.2 -25.1), (7.2 -25.1, 9.26 -2.456), (9.26 -2.456, 10.26 -3.456))\n"
]
},
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print object definitions\n",
"print(multi_line)\n",
"multi_line"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"id": "6cUMG8SEf4M7"
},
"outputs": [],
"source": [
"\n",
"# Circle (using a buffer around a point)\n",
"point = Point((0,1))\n",
"circle = point.buffer(1)\n",
"\n",
"# Triangle\n",
"triangle = Polygon([(10,0), (12,4), (14,0)])\n",
"\n",
"# square\n",
"square = Polygon([(2,0), (2,6), (8,6), (8,0)])\n",
"\n",
"multi_poly = MultiPolygon([circle, square, triangle])"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"id": "qaGhNC0Xf4M8",
"outputId": "2a07531c-70c8-4788-e12b-19ff20f4da59"
},
"outputs": [
{
"data": {
"image/svg+xml": "",
"text/plain": [
""
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Print object definitions\n",
"multi_poly"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eLNZPqeyf4M9"
},
"source": [
"**Some useful attributes for geometry collection:**"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"id": "QkaevxvEf4M-",
"outputId": "5b52ab7c-16e9-4ce3-db73-eaaf82720ae2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of objects in our MultiPoints: 4\n",
"Number of objects in our MultiLine: 3\n",
"Number of objects in our MultiPolygon: 3\n"
]
}
],
"source": [
"# Attributes for number of objects and length\n",
"print(\"Number of objects in our MultiPoints:\", len(multi_point))\n",
"print(\"Number of objects in our MultiLine:\", len(multi_line))\n",
"print(\"Number of objects in our MultiPolygon:\", len(multi_poly))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"id": "xeGBBjYhf4NA",
"outputId": "2b2f3aba-ac39-48d8-cdaa-dff7d5ff056e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of objects in our MultiLineString 53.87528269176866\n"
]
}
],
"source": [
"# Attributes for number of objects and length\n",
"print(\"Length of objects in our MultiLineString\", (multi_line.length))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"id": "bJ-9Z002f4NC",
"outputId": "6800f010-44e6-4e68-ec0f-3afaaafb1d6f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Area of our MultiPolygon: 47.13654849054594\n",
"Area of our circle: 3.1365484905459384\n"
]
}
],
"source": [
"# Attributes for area\n",
"print(\"Area of our MultiPolygon:\", multi_poly.area)\n",
"print(\"Area of our circle:\", multi_poly[0].area)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FZdw1fmef4NE"
},
"source": [
"---"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"y_vezm3df4MA",
"8orFUDHyf4MW",
"HYmxtdOJf4Mv",
"J-yYB76Gf4M2",
"eLNZPqeyf4M9"
],
"name": "01-Geometric-objects.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}