ManagementAddGeometryColumn DropGeometryColumn DropGeometryTable postgis_full_version postgis_geos_version postgis_lib_version postgis_proj_version postgis_version probe_geometry_columns SetSRID UpdateGeometrySRID Load/Dump Toolsshp2pgsql
pgsql2shp
Meta Tablesspatial_ref_sys
geometry_columns
Geometry CreationBdMPolyFromText BdPolyFromText GeomCollFromText GeomFromText GeometryFromWKB LineFromText LinestringFromText MakeLine MakePolygon MakePoint MLineFromText MPointFromText MPolyFromText PointFromText PolyFromText RelationshipContains* Crosses* Disjoint* Equals* Intersects* Overlaps* Relate* Touches* Within* Spatial AggregatesAccum Collect Extent Extent3D GeomUnion* MakeLine MemCollect MemGeomUnion* Geometry EditorsAddBBOX AddPoint Affine Collect DropBBOX Force_collection Force_2d Force_3d, Force_3dm Force_3dz Force_4d LineMerge Multi RemovePoint Segmentize SetPoint SnapToGrid |
PostGIS ver. 1.2.1 Quick Guide - Cheatsheet PDF Version Official PostGIS Documentation URL: http://postgis.refractions.net/docs/ This list is not comprehensive but tries to cover at least 80%. We also leave out alternative names for functions. *Requires GEOS so in general slower than other functions +Will use GEOS if compiled with GEOSMost commonly used functions and operatorsCode snippet available Measurement functions return in same units geometry SRID except for the *sphere and *spheroid versions which return in meters Geometry Types - WKT RepresentationPOINT(0 0) LINESTRING(0 0,1 1,1 2) POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1)) MULTIPOINT(0 0,1 2) MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4)) MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ..) GEOMETRYCOLLECTION(POINT(2 3),LINESTRING((2 3,3 4))) BBox OperatorsThese operators utilize indexes. They compare bounding boxes of 2 geometriesA &< B (A overlaps or is to the left of B)
A &> B (A overlaps or is to the right of B)
A << B (A is strictly to the left of B)
A >> B (A is strictly to the right of B)
A &<| B (A overlaps B or is below B)
A |&> B (A overlaps or is above B)
A <<| B (A strictly below B)
A |>> B (A strictly above B)
A = B (A bbox same as B bbox)
A @ B (A completely contained by B)
A ~ B (A completely contains B)
A && B (A and B bboxes interact)
A ~= B - true if A and B geometries are binary equal?
Common Use SFSQL Examples --Create a geometry column named the_geom in a
--table called testtable located in schema public
-- to hold point geometries of dimension 2 in WGS84 longlat
SELECT AddGeometryColumn('public', 'testtable', 'the_geom', 4326, 'POINT', 2);
--Insert a record into the new table
INSERT INTO testtable(description, the_geom)
VALUES('center of boston',
GeomFromText('POINT(-71.0891380310059, 42.3123226165771)', 4326));
--Insert a point record into the new table - faster than geomfromtext for points
INSERT INTO testtable(description, the_geom)
VALUES('center of boston',
setsrid(makepoint(-71.0891380310059, 42.3123226165771), 4326));
--Create a spatial index on the new geometry column
CREATE INDEX idx_testtable_the_geom ON testtable USING gist(the_geom);
--Find the neighborhood with the smallest area
SELECT neigh_name, area2d(the_geom)
FROM neighborhoods
ORDER BY area2d(the_geom) limit 1;
--Find the total area of each ward in square feet of wards in Boston,
--the extent (bounding box) of each ward, average sqft per precinct in each ward
SELECT ward, sum(area2d(transform(the_geom,2249))) as totarea,
avg(area2d(transform(the_geom,2249))) as avgarea_precinct,
extent(transform(the_geom,2249)) as wardextent
FROM wardprecincts WHERE city = 'Boston'
GROUP BY ward;
--Find all land parcels within 100 units of a specific parcel.
SELECT l2.parcel_id, l2.st_num, l2.st_name
FROM landparcels l , landparcels l2
WHERE expand(l.the_geom, 100) && l2.the_geom
AND distance(l.the_geom, l2.the_geom) <= 100
AND l.parcel_id = '1234560000';
--Break up multipolygons into individual polygons
SELECT neigh_name,
GeometryN(the_geom, generate_series(1, numgeometries(the_geom))) As polygeom
FROM neighborhoods;
--Take individual polygons and create one multipolygon for each neighborhood
--Note if you have a mixed collection of geometries, will return a geometry collection
SELECT neigh_name, collect(polygeom) as the_geom
FROM neighborhoods
GROUP BY neigh_name;
Using Shape Dumper/Loader Commandline ToolsLoad data into PostgreSQL from ESRI shape file
shp2pgsql -s 2249 neighborhoods public.neighborhoods > neighborhoods.sql
psql -h myserver -d mydb -f neighborhoods.sql
|
AccessorsDimension Dump EndPoint Envelope ExteriorRing GeometryN GeometryType InteriorRingN IsClosed IsEmpty IsRing IsSimple IsValid M NumGeometries NumInteriorRings NumPoints npoints PointN SRID StartPoint X XMin,XMax Y YMin,YMax Z ZMin,ZMax Measurementarea2d azimuth distance distance_sphere distance_spheroid length_spheroid length2d length3d length3d_spheroid max_distance perimeter2d perimeter3d OutputsAsBinary
AsText
AsEWKB
AsEWKT
AsHEXEWKB
AsGML
AsKML
AsSVG
Geometry ProcessorsBoundary* Buffer* BuildArea* Centroid+ ConvexHull* Difference* Expand ForceRHR GeomUnion* Intersection* PointOnSurface* Reverse RotateX RotateY RotateZ Scale Simplify SymDifference* Transform Translate TransScale |