Skip to content

Instantly share code, notes, and snippets.

@mrstif
Last active August 9, 2018 14:28
Show Gist options
  • Save mrstif/b34e31ef49473a48b02bfa10cadce61b to your computer and use it in GitHub Desktop.
Save mrstif/b34e31ef49473a48b02bfa10cadce61b to your computer and use it in GitHub Desktop.
PostGis

https://postgis.net/docs/

SRIDs

3857 - Simple Spherical Mercator projection coordinate system
4326 - Geographic coordinate system

FUNCTIONS

  • ST_Transform: Return a new geometry with its coordinates transformed to a different spatial reference.

ST_Transform(geometry, 4326)

  • ST_SetSRID: Set the SRID on a geometry to a particular integer value.

ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)

  • ST_AsText: Return the Well-Known Text (WKT) representation of the geometry/geography without SRID metadata.

ST_AsText(geometry)

ST_MakePoint(longitude, latitude)

  • ST_GeomFromText: Return a specified ST_Geometry value from Well-Known Text representation (WKT).  

ST_GeomFromText('POINT(-115.993052 43.645232)', 4326)

  • <->: Returns the 2D distance between A and B  

ST_Transform(geometry, 4326) <-> ST_GeomFromText('POINT(-115.993052 43.645232)', 4326)

  • ST_Distance: For geometry type Returns the 2D Cartesian distance between two geometries in projected units (based on spatial ref). For geography type defaults to return minimum geodesic distance between two geographies in meters.

ST_Distance(geometry, ST_Transform(ST_GeomFromText('POINT(-115.993052 43.645232)',4326), 3857))

  • ST_Distance_Sphere: Returns minimum distance in meters between two lon/lat geometries. Uses a spherical earth and radius of 6370986 meters. Faster than ST_Distance_Spheroid ST_Distance_Spheroid, but less accurate. PostGIS versions prior to 1.5 only implemented for points.

ST_Distance_Sphere(geometry, ST_GeomFromText('POINT(-115.993052 43.645232)',4326))

  • ST_DWithin: Returns true if the geometries are within the specified distance of one another. For geometry units are in those of spatial reference and For geography units are in meters and measurement is defaulted to use_spheroid=true (measure around spheroid), for faster check, use_spheroid=false to measure along sphere.

ST_DWithin(geography, ST_GeomFromText('POINT(-115.993052 43.645232)',4326)::geography, 1000)

  • ST_X: Return the X coordinate of the point, or NULL if not available. Input must be a point.

  • ST_Y: Return the Y coordinate of the point, or NULL if not available. Input must be a point.

EXAMPLES

  • Extract latitude and longitude from point
SELECT ST_X(ST_TRANSFORM(geometry, 4326)) AS longitude,
       ST_Y(ST_TRANSFORM(geometry, 4326)) AS latitude
  • Print geometry point as geographical coordinate

ST_AsText(ST_Transform(geometry, 4326))

  • Transform coordinates into simple mercator projection

ST_Transform(ST_GeomFromText('POINT(longitude latitude)', 4326), 3857)

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