Geometric Objects - Spatial Data Model

Sources:

These materials are partly based on Shapely -documentation and Westra E. (2013), Chapter 3.

Overview of geometric objects and Shapely -module

img/SpatialDataModel.PNG

Fundamental geometric objects that can be used in Python with Shapely module

The most fundamental geometric objects are Points, Lines and Polygons which are the basic ingredients when working with spatial data in vector format. Python has a specific module called Shapely that can be used to create and work with Geometric Objects. There are many useful functionalities that you can do with Shapely such as:

  • Create a Line or Polygon from a Collection of Point geometries
  • Calculate areas/length/bounds etc. of input geometries
  • Make geometric operations based on the input geometries such as Union, Difference, Distance etc.
  • Make spatial queries between geometries such Intersects, Touches, Crosses, Within etc.

Geometric Objects consist of coordinate tuples where:

  • Point -object represents a single point in space. Points can be either two-dimensional (x, y) or three dimensional (x, y, z).
  • 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
  • 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.

It is also possible to have a collection of geometric objects (e.g. Polygons with multiple parts):

  • MultiPoint -object represents a collection of points and consists of a list of coordinate-tuples
  • MultiLineString -object represents a collection of lines and consists of a list of line-like sequences
  • 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

Point

  • Creating point is easy, you pass x and y coordinates into Point() -object (+ possibly also z -coordinate) :
  • Let’s see what the variables look like

We can see that the type of the point is shapely’s Point which is represented in a specific format that is based on GEOS C++ library that is one of the standard libraries in GIS. It runs under the hood e.g. in Quantum GIS. 3D-point can be recognized from the capital Z -letter in front of the coordinates.

Point attributes and functions

Point -object has some built-in attributes that can be accessed and also some useful functionalities. One of the most useful ones are the ability to extract the coordinates of a Point and calculate the Euclidian distance between points.

  • Extracting the coordinates of a Point can be done in a couple of different ways

Ok, we can see that the output is a Shapely CoordinateSequence. Let’s see how we can get out the actual coordinates:

  • What is inside?

Okey, so we can see that the our xy variable contains a tuple where x and y are stored inside of a numpy arrays. However, our x and y variables are plain decimal numbers.

  • It is also possible to calculate the distance between points which can be useful in many applications
  • the returned distance is based on the projection of the points (degrees in WGS84, meters in UTM)

LineString

  • Creating a LineString -object is fairly similar to how Point is created. Now instead using a single coordinate-tuple we can construct the line using either a list of shapely Point -objects or pass coordinate-tuples:
  • Let’s see how our LineString looks like

Ok, now we can see that variable line constitutes of multiple coordinate-pairs and the type of the data is shapely LineString.

LineString attributes and functions

LineString -object has many useful built-in attributes and functionalities. It is for instance possible to extract the coordinates or the length of a LineString (line), calculate the centroid of the line, create points along the line at specific distance, calculate the closest distance from a line to specified Point and simplify the geometry. See full list of functionalities from Shapely documentation. Here, we go through a few of them.

  • We can extract the coordinates of a LineString similarly as with Point

Okey, we can see that the coordinates are again stored as a numpy arrays where first array includes all x-coordinates and the second all the y-coordinates respectively.

  • We can extract only x or y coordinates by referring to those arrays as follows
  • We can get specific attributes such as lenght of the line and center of the line (centroid) straight from the LineString object itself

Okey, so these are already fairly useful information for many different GIS tasks, and we didn’t even calculate anything yet! These attributes are built-in in every LineString object that is created. Notice that the centroid that is returned is Point -object that has its own functions as was described earlier.

Polygon

  • Creating a Polygon -object continues the same logic of how Point and LineString were created but Polygon object only accepts coordinate-tuples as input. Polygon needs at least three coordinate-tuples:

Notice that Polygon has double parentheses around the coordinates. This is because Polygon can also have holes inside of it. As the help of Polygon -object tells, a Polygon can be constructed using exterior coordinates and interior coordinates (optional) where the interior coordinates creates a hole inside the Polygon:

Help on Polygon in module shapely.geometry.polygon object:
class Polygon(shapely.geometry.base.BaseGeometry)
 |  A two-dimensional figure bounded by a linear ring
 |
 |  A polygon has a non-zero area. It may have one or more negative-space
 |  "holes" which are also bounded by linear rings. If any rings cross each
 |  other, the feature is invalid and operations on it may fail.
 |
 |  Attributes
 |  ----------
 |  exterior : LinearRing
 |      The ring which bounds the positive space of the polygon.
 |  interiors : sequence
 |      A sequence of rings which bound all existing holes.
  • Let’s create a Polygon with a hole inside
  • Let’s see what we have now:

Now we can see that the polygon has two different tuples of coordinates. The first one represents the outerior and the second one represents the hole inside of the Polygon.

Polygon attributes and functions

  • We can again access different attributes that are really useful such as area, centroid, bounding box, exterior, and exterior-length of the Polygon
  • Let’s see what we have now

Pro -tips (optional)

This part is not obligatory but it contains some useful information related to construction and usage of geometry collections and some special geometric objects -such as bounding box.

Geometry collections

In some occassions it is useful to store e.g. multiple lines or polygons under a single feature (i.e. a single row in a Shapefile represents more than one line or polygon object). Collections of points are implemented by using a MultiPoint -object, collections of curves by using a MultiLineString -object, and collections of surfaces by a MultiPolygon -object. These collections are not computationally significant, but are useful for modeling certain kinds of features. A Y-shaped line feature (such as road), or multiple polygons (e.g. islands on a like), can be presented nicely as a whole by a using MultiLineString or MultiPolygon accordingly. Creating and visualizing a minimum bounding box e.g. around your data points is a really useful function for many purposes (e.g. trying to understand the extent of your data), here we demonstrate how to create one using Shapely.

  • Geometry collections can be constructed in a following manner:
  • Let’s see what do we have:

We can see that the outputs are similar to the basic geometric objects that we created previously but now these objects contain multiple features of those points, lines or polygons.

Geometry collection -objects’ attributes and functions

  • We can also get many useful attributes from those objects:
  • Let’s see what do we have:

From the above we can see that MultiPolygons have exactly the same attributes available as single geometric objects but now the information such as area calculates the area of ALL of the individual -objects combined. There are also some extra features available such as is_valid attribute that tells if the polygons or lines intersect with each other.