TileStache.Goodies.Providers.PostGeoJSON
index

Provider that returns GeoJSON data responses from PostGIS queries.
 
Note:
 
The built-in TileStache Vector provider (new in version 1.9.0) offers a more
complete method of generating vector tiles, and supports many kinds of data
sources not avilable in PostGeoJSON such as shapefiles. PostGeoJSON will
continue to be provided and supported in TileStache, but future development
of vector support will be contentrated on the mainline Vector provider, not
this one.
 
More information:
  http://tilestache.org/doc/TileStache.Vector.html
 
Anyway.
 
This is an example of a provider that does not return an image, but rather
queries a database for raw data and replies with a string of GeoJSON. For
example, it's possible to retrieve data for locations of OpenStreetMap points
of interest based on a query with a bounding box intersection.
 
Read more about the GeoJSON spec at: http://geojson.org/geojson-spec.html
 
Many Polymaps (http://polymaps.org) examples use GeoJSON vector data tiles,
which can be effectively created using this provider.
 
Keyword arguments:
 
  dsn:
    Database connection string suitable for use in psycopg2.connect().
    See http://initd.org/psycopg/docs/module.html#psycopg2.connect for more.
  
  query:
    PostGIS query with a "!bbox!" placeholder for the tile bounding box.
    Note that the table *must* use the web spherical mercaotr projection
    900913. Query should return an id column, a geometry column, and other
    columns to be placed in the GeoJSON "properties" dictionary.
    See below for more on 900913.
  
  clipping:
    Boolean flag for optionally clipping the output geometries to the bounds
    of the enclosing tile. Defaults to fales. This results in incomplete
    geometries, dramatically smaller file sizes, and improves performance
    and compatibility with Polymaps (http://polymaps.org).
  
  id_column:
    Name of id column in output, detaults to "id". This determines which query
    result column is placed in the GeoJSON "id" field.
  
  geometry_column:
    Name of geometry column in output, defaults to "geometry". This determines
    which query result column is reprojected to lat/lon and output as a list
    of geographic coordinates.
  
  indent:
    Number of spaces to indent output GeoJSON response. Defaults to 2.
    Skip all indenting with a value of zero.
  
  precision:
    Number of decimal places of precision for output geometry. Defaults to 6.
    Default should be appropriate for almost all street-mapping situations.
    A smaller value can help cut down on output file size for lower-zoom maps.
 
Example TileStache provider configuration:
 
  "points-of-interest":
  {
    "provider":
    {
      "class": "TileStache.Goodies.Providers.PostGeoJSON.Provider",
      "kwargs":
      {
        "dsn": "dbname=geodata user=postgres",
        "query": "SELECT osm_id, name, way FROM planet_osm_point WHERE way && !bbox! AND name IS NOT NULL",
        "id_column": "osm_id", "geometry_column": "way",
        "indent": 2
      }
    }
  }
 
Caveats:
 
Currently only databases in the 900913 (google) projection are usable,
though this is the default setting for OpenStreetMap imports from osm2pgsql.
The "!bbox!" query placeholder (see example below) must be lowercase, and
expands to:
    
    ST_SetSRID(ST_MakeBox2D(ST_MakePoint(ulx, uly), ST_MakePoint(lrx, lry)), 900913)
    
You must support the "900913" SRID in your PostGIS database for now.
For populating the internal PostGIS spatial_ref_sys table of projections,
this seems to work:
 
  INSERT INTO spatial_ref_sys
    (srid, auth_name, auth_srid, srtext, proj4text)
    VALUES
    (
      900913, 'spatialreference.org', 900913,
      'PROJCS["Popular Visualisation CRS / Mercator",GEOGCS["Popular Visualisation CRS",DATUM["Popular_Visualisation_Datum",SPHEROID["Popular Visualisation Sphere",6378137,0,AUTHORITY["EPSG","7059"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6055"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4055"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],AUTHORITY["EPSG","3785"],AXIS["X",EAST],AXIS["Y",NORTH]]',
      '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over'
    );

 
Classes
       
Provider
SaveableResponse

 
class Provider
     Methods defined here:
__init__(self, layer, dsn, query, clipping=False, id_column='id', geometry_column='geometry', indent=2, precision=6)
getTypeByExtension(self, extension)
Get mime-type and format by file extension.
 
This only accepts "json".
renderTile(self, width, height, srs, coord)
Render a single tile, return a SaveableResponse instance.

 
class SaveableResponse
    Wrapper class for JSON response that makes it behave like a PIL.Image object.
 
TileStache.getTile() expects to be able to save one of these to a buffer.
 
  Methods defined here:
__init__(self, content, indent=2, precision=2)
save(self, out, format)

 
Functions
       
row2feature(row, id_field, geometry_field)
Convert a database row dict to a feature dict.
shape2geometry(shape, projection, clip)
Convert a Shapely geometry object to a GeoJSON-suitable geometry dict.