&

With Martin

Up

Running

@rhysallister

rhys@victorious-wanyamaha:~/dev/martin-0.3.0/target/release$ ./martin --help
Martin - PostGIS Mapbox Vector Tiles server.

Usage:
  martin [options] [<connection>]
  martin -h | --help
  martin -v | --version

Options:
  -h --help               Show this screen.
  -v --version            Show version.
  --config=<path>         Path to config file.
  --keep_alive=<n>        Connection keep alive timeout [default: 75].
  --listen_addresses=<n>  The socket address to bind [default: 0.0.0.0:3000].
  --pool_size=<n>         Maximum connections pool size [default: 20].
  --watch                 Scan for new sources on sources list requests
  --workers=<n>           Number of web server workers.
rhys@victorious-wanyamaha:~/dev/martin-0.3.0/target/release$ 
# Database connection string
connection_string: 'postgres://postgres@localhost/db'

# Maximum connections pool size [default: 20]
pool_size: 20

# Connection keep alive timeout [default: 75]
keep_alive: 75

# Number of web server workers
worker_processes: 8

# The socket address to bind [default: 0.0.0.0:3000]
listen_addresses: '0.0.0.0:3000'

# Enable watch mode
watch: true

# associative arrays of table sources
table_sources:
  public.table_source:
    # table source id
    id: public.table_source

    # table schema
    schema: public

    # table name
    table: table_source

    # geometry column name
    geometry_column: geom

    # geometry srid
    srid: 4326

    # tile extent in tile coordinate space
    extent: 4096

    # buffer distance in tile coordinate space to optionally clip geometries
    buffer: 64

    # boolean to control if geometries should be clipped or encoded as is
    clip_geom: true

    # geometry type
    geometry_type: GEOMETRY

    # list of columns, that should be encoded as a tile properties
    properties:
      gid: int4
CREATE OR REPLACE FUNCTION keepmesafe.keepmesafe(
	z integer,
	x integer,
	y integer,
	query_params json)
    RETURNS bytea
    LANGUAGE 'plpgsql'

    COST 100
    IMMUTABLE STRICT PARALLEL SAFE
AS $BODY$
DECLARE
  bounds geometry;
  mvt bytea;
BEGIN
-- RAISE EXCEPTION '****';
  -- INSERT into keepmesafe.qp (qp, qpt) VALUES (query_params, query_params);
  perform pg_notify('pop', query_params::text);
  
  IF query_params::jsonb ?& array['start_date','end_date','incidents'] THEN 
   mvt := keepmesafe.keepmesafe_date_crime(z,x,y,query_params);
   RETURN mvt;
 ELSIF query_params::jsonb ?& array['start_date','end_date'] AND not (query_params::jsonb ? 'incidents')  THEN 
   mvt := keepmesafe.keepmesafe_date_only(z,x,y,query_params);
  RETURN mvt;
 END IF;

  
  SELECT INTO bounds keepmesafe.TileBBox(z, x, y, 4326);
  raise notice '%', query_params;
  if z in (1,2,3,4,5) THEN
   SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
      distinct ST_AsMVTGeom(st_centroid(st_geomfromgeohash(st_geohash(geom,4))), bounds, 4096, 64, true) AS geom
    FROM keepmesafe.geohash_groupings
    WHERE geom && bounds
  ) as tile WHERE geom IS NOT NULL;
  
  elsif z in (6,7,8) THEN

    SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
      SELECT
        ST_AsMVTGeom(st_centroid(geom), bounds, 4096, 64, true) AS geom
      FROM keepmesafe.geohash_groupings
      WHERE geom && bounds
    ) as tile WHERE geom IS NOT NULL;
  
  elsif z in (9,10,11,12) THEN

   SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
       ST_AsMVTGeom(st_centroid(st_geomfromgeohash(ghash)), bounds, 4096, 64, true) AS geom
    FROM 
		(SELECT distinct st_geohash(geom,7) ghash from keepmesafe.incidents
    	WHERE g && bounds 
		) as myset
	group by 1										   
  
  ) as tile WHERE geom IS NOT NULL;
  

  ELSE
  SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
     ST_AsMVTGeom(geom, bounds, 4096, 64, true) AS geom
    FROM keepmesafe.incidents
    WHERE g && bounds 
    limit 5500
  ) as tile WHERE geom IS NOT NULL;
  
  
END IF;
  RETURN mvt;
END;
$BODY$;

ALTER FUNCTION keepmesafe.keepmesafe(integer, integer, integer, json)
    OWNER TO keepmesafe;

Function Sources

CREATE OR REPLACE FUNCTION keepmesafe.keepmesafe(
	z integer,
	x integer,
	y integer,
	query_params json)
    RETURNS bytea
    LANGUAGE 'plpgsql'

    COST 100
    IMMUTABLE STRICT PARALLEL SAFE
AS $BODY$
DECLARE
  bounds geometry;
  mvt bytea;
BEGIN
-- RAISE EXCEPTION '****';
  -- INSERT into keepmesafe.qp (qp, qpt) VALUES (query_params, query_params);
  perform pg_notify('pop', query_params::text);
  
  IF query_params::jsonb ?& array['start_date','end_date','incidents'] THEN 
   mvt := keepmesafe.keepmesafe_date_crime(z,x,y,query_params);
   RETURN mvt;
 ELSIF query_params::jsonb ?& array['start_date','end_date'] AND not (query_params::jsonb ? 'incidents')  THEN 
   mvt := keepmesafe.keepmesafe_date_only(z,x,y,query_params);
  RETURN mvt;
 END IF;

  
  SELECT INTO bounds keepmesafe.TileBBox(z, x, y, 4326);
  raise notice '%', query_params;
  if z in (1,2,3,4,5) THEN
   SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
      distinct ST_AsMVTGeom(st_centroid(st_geomfromgeohash(st_geohash(geom,4))), bounds, 4096, 64, true) AS geom
    FROM keepmesafe.geohash_groupings
    WHERE geom && bounds
  ) as tile WHERE geom IS NOT NULL;
  
  elsif z in (6,7,8) THEN

    SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
      SELECT
        ST_AsMVTGeom(st_centroid(geom), bounds, 4096, 64, true) AS geom
      FROM keepmesafe.geohash_groupings
      WHERE geom && bounds
    ) as tile WHERE geom IS NOT NULL;
  
  elsif z in (9,10,11,12) THEN

   SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
       ST_AsMVTGeom(st_centroid(st_geomfromgeohash(ghash)), bounds, 4096, 64, true) AS geom
    FROM 
		(SELECT distinct st_geohash(geom,7) ghash from keepmesafe.incidents
    	WHERE g && bounds 
		) as myset
	group by 1										   
  
  ) as tile WHERE geom IS NOT NULL;
  

  ELSE
  SELECT INTO mvt ST_AsMVT(tile, 'geohash_groupings', 4096, 'geom') FROM (
    SELECT
     ST_AsMVTGeom(geom, bounds, 4096, 64, true) AS geom
    FROM keepmesafe.incidents
    WHERE g && bounds 
    limit 5500
  ) as tile WHERE geom IS NOT NULL;
  
  
END IF;
  RETURN mvt;
END;
$BODY$;

ALTER FUNCTION keepmesafe.keepmesafe(integer, integer, integer, json)
    OWNER TO keepmesafe;

Function Sources

Questions?

Made with Slides.com