{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Shapely and geometric objects\n", "\n", "In this lesson, you will learn how to create and manipulate geometries in Python using the [Shapely Python Package](https://shapely.readthedocs.io/en/stable/manual.html).\n", "\n", "**Sources:**\n", "\n", "These materials are partly based on [Shapely-documentation](https://shapely.readthedocs.io/en/stable/manual.html) and [Westra\n", "E. (2013), Chapter 3](https://www.packtpub.com/application-development/python-geospatial-development-second-edition).\n", "\n", "## Spatial data model\n", "\n", "![Spatial data model](img/SpatialDataModel.PNG)\n", "\n", "*Fundamental geometric objects that can be used in Python with* [Shapely](https://shapely.readthedocs.io/en/stable/manual.html).\n", "\n", "The most fundamental geometric objects are `Points`, `Lines` and `Polygons` which are the basic ingredients when working with spatial data in vector format. \n", "Python has a specific module called [Shapely](https://shapely.readthedocs.io/en/stable/manual.html) for doing various geometric operations. Basic knowledge of using Shapely is fundamental for understanding how geometries are stored and handled in GeoPandas.\n", "\n", "**Geometric objects consist of coordinate tuples where:**\n", "\n", "- `Point` -object represents a single point in space. Points can be either two-dimensional (x, y) or three dimensional (x, y, z).\n", "- `LineString` -object (i.e. a line) represents a sequence of points joined together to form a line. Hence, a line consist of a list of at least two coordinate tuples\n", "- `Polygon` -object represents a filled area that consists of a list of at least three coordinate tuples that forms the outerior ring and a (possible) list of hole polygons.\n", "\n", "**It is also possible to have a collection of geometric objects (e.g. Polygons with multiple parts):**\n", "\n", "- `MultiPoint` -object represents a collection of points and consists of a list of coordinate-tuples\n", "- `MultiLineString` -object represents a collection of lines and consists of a list of line-like sequences\n", "- `MultiPolygon` -object represents a collection of polygons that consists of a list of polygon-like sequences that construct from exterior ring and (possible) hole list tuples\n", "\n", "**Useful attributes and methods in Shapely include:**\n", "\n", "- Creating lines and polygons based on a collection of point objects.\n", "- Calculating areas/length/bounds etc. of input geometries\n", "- Conducting geometric operations based on the input geometries such as `union`, `difference`, `distance` etc.\n", "- Conducting spatial queries between geometries such as `intersects`, `touches`, `crosses`, `within` etc.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "**Tuple**\n", "\n", "[Tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences) is a Python data structure that consists of a number of values separated by commas. Coordinate pairs are often represented as a tuple. For example:\n", "\n", "```\n", "(60.192059, 24.945831)\n", "``` \n", "\n", "Tuples belong to [sequence data types](https://docs.python.org/3/library/stdtypes.html#typesseq) in Python. Other sequence data types are lists and ranges. Tuples have many similarities with lists and ranges, but they are often used for different purposes. The main difference between tuples and lists is that tuples are [immutable](https://docs.python.org/3/glossary.html#term-immutable), which means that the contents of a tuple cannot be altered (while lists are mutable; you can, for example, add and remove values from lists).\n", "