Geometric Objects - Spatial Data Model¶
These materials are partly based on Shapely -documentation and Westra E. (2013), Chapter 3.
Overview of geometric objects and Shapely -module¶
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
- Creating point is easy, you pass x and y coordinates into Point() -object (+ possibly also z -coordinate) :
# Import necessary geometric objects from shapely module In : from shapely.geometry import Point, LineString, Polygon # Create Point geometric object(s) with coordinates In : point1 = Point(2.2, 4.2) In : point2 = Point(7.2, -25.1) In : point3 = Point(9.26, -2.456) In : point3D = Point(9.26, -2.456, 0.57) # What is the type of the point? In : point_type = type(point1)
- Let’s see what the variables look like
In : print(point1) POINT (2.2 4.2) In : print(point3D)