0% found this document useful (0 votes)
12 views

Post GIS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Post GIS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is PostGIS?

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

PostGIS: You just run inside PostgreSQL to get going.


CREATE EXTENSION postgis;

QGIS: You will very likely want a desktop app for working with GIS data

PostGIS add two additional data types geometry and geography.

Geometry is an abstract type. Sub Types


Point, LineString, LinearRing and Polygon and
collection types : MultiPoint, MultiLineString
POINT
Point is a 0-dimensional geometry that represents a single location in
coordinate space

POINT (1 2)
LineString

A LineString is a 1-dimensional line segment is defined by two points

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

CREATE TABLE geometries (name varchar, geom geometry);

INSERT INTO geometries VALUES


('Point', 'POINT(0 0)'),
('Linestring', 'LINESTRING(0 0, 1 1, 2 1, 2 2)'),
('Polygon', 'POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))'),
('PolygonWithHole', 'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(1 1, 1 2, 2 2, 2 1, 1 1))'),
('Collection', 'GEOMETRYCOLLECTION(POINT(2 0),POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))');

SELECT name, ST_AsText(geom) FROM geometries;

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

SELECT name, ST_GeometryType(geom), ST_NDims(geom), ST_SRID(geom)


FROM geometries;

name | st_geometrytype | st_ndims | st_srid


-----------------+-----------------------+----------+---------
Point | ST_Point | 2 | 0
Polygon | ST_Polygon | 2 | 0
PolygonWithHole | ST_Polygon | 2 | 0
Collection | ST_GeometryCollection | 2 | 0
Linestring | ST_LineString | 2 |

Points

SELECT ST_AsText(geom)
FROM geometries
WHERE name = 'Point';

POINT(0 0)

A spatial point represents a single location on the Earth.


Some of the specific spatial functions for working with points are:
 ST_X(geometry) returns the X ordinate
 ST_Y(geometry) returns the Y ordinate

So, we can read the ordinates from a point like this:

SELECT ST_X(geom), ST_Y(geom)


FROM geometries
WHERE name = 'Point';

Linestrings

A linestring is a path between locations.


A linestring is said to be closed if it starts and ends on the same point.

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

A polygon is a representation of an area.

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

SELECT name, ST_Area(geom)


FROM geometries
WHERE name LIKE 'Polygon%';

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.

Second table (actually, a view), geometry_columns, provides a listing of all


“features”

SELECT * FROM geometry_columns;

You might also like