Post GIS
Post GIS
PostGIS is a Postgres extension for spatial data types like points, lines, and
polygons to be stored inside a database.
PostGIS also has a large set of functions that allow you to work with
geography
QGIS: You will very likely want a desktop app for working with GIS data
POINT (1 2)
LineString
LINESTRING (1 2, 3 4, 5 6)
LinearRing
A LinearRing is a LineString which is both closed and simple. The first and
last points must be equal.
LINEARRING (0 0 0, 4 0 0, 4 4 0, 0 4 0, 0 0 0)
Polygon
A Polygon is a 2-dimensional planar region, delimited by an exterior
boundary and zero or more interior boundaries.
POLYGON ((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0))
MultiPoint
A MultiPoint is a collection of Points.
MULTIPOINT ( (0 0), (1 2) )
CREATE SAMPLE TABLE
We can collect general information about each object using functions that read
the geometry metadata.
ST_GeometryType(geometry) returns the type of the geometry
ST_NDims(geometry) returns the number of dimensions of the geometry
ST_SRID(geometry) returns the spatial reference identifier number of the geometry
Points
SELECT ST_AsText(geom)
FROM geometries
WHERE name = 'Point';
POINT(0 0)
Linestrings
SELECT ST_AsText(geom)
FROM geometries
WHERE name = 'Linestring';
LINESTRING(0 0, 1 1, 2 1, 2 2)
Some of the specific spatial functions for working with linestrings are:
ST_Length(geometry) returns the length of the linestring
ST_StartPoint(geometry) returns the first coordinate as a point
ST_EndPoint(geometry) returns the last coordinate as a point
ST_NPoints(geometry) returns the number of coordinates in the linestring
SELECT ST_Length(geom)
FROM geometries
WHERE name = 'Linestring';
3.41421356237309
Polygons
Polygons are used to represent objects whose size and shape are important. City
limits, parks, building footprints or bodies of water are all commonly represented
as polygons
SELECT ST_AsText(geom)
FROM geometries
WHERE name LIKE 'Polygon%';
POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))
POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))
Some of the specific spatial functions for working with polygons are:
ST_Area(geometry) returns the area of the polygons
ST_NRings(geometry) returns the number of rings (usually 1, more if there are holes)
ST_ExteriorRing(geometry) returns the outer ring as a linestring
ST_InteriorRingN(geometry,n) returns a specified interior ring as a linestring
ST_Perimeter(geometry) returns the length of all the rings
Polygon 1
PolygonWithHole 99
PostGIS provides two tables to track and report on the geometry types
available in a given database.
First table, spatial_ref_sys, defines all the spatial reference systems known to
the database.