Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniel-j-h/c7106a75984dd97908fc to your computer and use it in GitHub Desktop.
Save daniel-j-h/c7106a75984dd97908fc to your computer and use it in GitHub Desktop.
Geospatial Analysis, KSIT workshop on 2015-2-11

Introduction To PostGIS

Point Geometry From Coordinates

http://postgis.refractions.net/docs/ST_MakePoint.html

SELECT ST_MakePoint(8.4137603, 49.0091767);
                st_makepoint                
--------------------------------------------
 01010000002CC4D963D8D320404E31BDB32C814840

See the SRID note in the documention's example.

Note: For primitives in general, there is ST_Make*, e.g. ST_Make{Point, Polygon, Box2d, ...}

GeoJSON From Geometry

http://postgis.org/docs/ST_AsGeoJSON.html

SELECT ST_AsGeoJSON(ST_MakePoint(8.4137603, 49.0091767));
                     st_asgeojson                      
-------------------------------------------------------
 {"type":"Point","coordinates":[8.4137603,49.0091767]}

Geometry Vs Geography

Geometry

Patterns And Casting

SELECT name,
       population
FROM planet_osm_point
WHERE place='city'
  AND population ~ '^[0-9]+$'
ORDER BY population::int DESC LIMIT 5;
         name         | population 
----------------------+------------
 Stuttgart            | 613392
 Mannheim             | 307640
 Karlsruhe            | 283959
 Freiburg im Breisgau | 214716
 Heidelberg           | 142889

Exploring Map Features

http://wiki.openstreetmap.org/wiki/Map_Features

SELECT distinct(amenity)
FROM planet_osm_point;

Note: do this with amenity, shop, tourism, "natural", etc. to get a feel for what is there.

Indexed K-Nearest-Neighbor

SELECT name,
       ST_AsGeoJSON(way)
FROM planet_osm_point
WHERE place = 'suburb'
ORDER BY way <-> ST_MakePoint(8.4137603, 49.0091767) LIMIT 5;
    name     |                     st_asgeojson                      
-------------+-------------------------------------------------------
 Südstadt    | {"type":"Point","coordinates":[8.4060496,49.0021606]}
 Oststadt    | {"type":"Point","coordinates":[8.4267919,49.0118481]}
 Rintheim    | {"type":"Point","coordinates":[8.4407597,49.0154961]}
 Dammerstock | {"type":"Point","coordinates":[8.3999556,48.9850514]}
 Beiertheim  | {"type":"Point","coordinates":[8.3889974,48.993349]}

Boundaries And Polygons

Filter on Polygon admin_level

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