Skip to content

Instantly share code, notes, and snippets.

@Debakel
Last active September 15, 2022 13:48
Show Gist options
  • Save Debakel/36f65aa5fe61023eb9d3b94b6f3e6f76 to your computer and use it in GitHub Desktop.
Save Debakel/36f65aa5fe61023eb9d3b94b6f3e6f76 to your computer and use it in GitHub Desktop.

Simple Features, PostGIS, GeoDjango

Spatial Data Model: Simple-Features

Klassenmodell für geradlinige geometrische Figuren, entwickelt vom OGC:

Simple Features is a set of standards that specify a common storage and access model of geographic feature made of mostly two-dimensional geometries (point, line, polygon, multi-point, multi-line, etc.).

grafik

The Simple-Features spec defines a lot of useful operations on geometries:

grafik

Example operations:

  • Within (anotherGeometry: Geometry) — Returns TRUE if this geometric object is “spatially within” anotherGeometry
  • Envelope() - Returns the minimum bounding box for the Geometry.

See OGC Simple Features or Shapely docs for more info

Implementations

  • GEOS is a C/C++ library for spatial computational geometry implementing the Simple-Feature-Model. Used by PostGIS, QGIS, GDAL, GeoDjango and Shapely.
  • PostGIS is a spatial database extender for PostgreSQL object-relational database. It adds support for geographic objects allowing location queries to be run in SQL.
    • PostGIS uses GEOS as geometry engine.
  • Shapely is a Python package for set-theoretic analysis and manipulation of planar features. With Shapely, geometry operations can be performed outside of PostGIS.
    • Shapely uses GEOS as geometry engine.
  • GeoDjango is
    • a high-level Python wrapper for the GEOS library.
    • a high-level Python interface for some of the capabilities of GDALs OGR, including the reading and coordinate transformation of vector spatial data.
  • django-rest-framework-gis provides DRF fields and serializers for GeoDjango’s Models.
  • GDAL / OCR specializes in reading and writing vector geographic data.

Data exchange formats

GeoJSON

GeoJSON is a standard for structuring JSON when encoding geometry and features. It also standardizes the transport of attribution.

The three key levels of GeoJSON are:

  • Geometry, representation of Points, LineStrings, Polygons, etc.
  • Feature, representation of an object that has a “geometry” and an arbitrary set of other non-geometric “properties”.
  • FeatureCollection, representation of a list of Features.

Example:

{
      "type": "Feature",
      "properties": {
				"area": "1",
				"name": "Schöneberg",
			},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -5.625,
              41.77131167976407
            ],
            [
              -6.6796875,
              34.59704151614417
            ],
            [
              2.109375,
              31.353636941500987
            ],
            [
              2.109375,
              40.97989806962013
            ],
            [
              -5.625,
              41.77131167976407
            ]
          ]
        ]
      }
    }

WKT / WKB

“Well-known text” is a scheme for writing a simple features geometry into a standard text string.

Example

POINT(0 0)
POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))

Since GEOS does not support GeoJSON, WKT must be used to write geospatial queries in PostGIS (?).

Installation

PostGIS

GeoDjango:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment