COALESCE(Array_to_json(Array_agg(f)), '[]'::json) AS features
FROM (
SELECT
'Feature' AS type,
St_asgeojson(ST_Transform(lg.${query.geom_column}, 4326))::json AS geometry,
${query.columns?`
Row_to_json(
(
SELECT
l
FROM
(SELECT ${query.columns}) AS l
)
) AS properties
`:`'{}'::json AS properties`}
FROM
${params.table} AS lg
${bounds?`, (SELECT ST_SRID(${query.geom_column}) as srid FROM ${params.table} LIMIT 1) sq`:''}
-- Optional Filter
${query.filter||bounds?'WHERE':''}
${query.filter?`${query.filter}`:''}
${query.filter&&bounds?'AND':''}
${bounds?`
${query.geom_column} &&
ST_Transform(
ST_MakeEnvelope(${bounds.join()}, 4326),
srid
)
`:''}
) AS f
) AS fc;
`
}
module.exports=function(app,pool){
/**
* @swagger
*
* /data/geojson/{table}:
* get:
* description: return table as geojson
* tags: ['geodata']
* produces:
* - application/json
* parameters:
* - name: table
* description: name of table or view
* in: path
* required: true
* type: string
* - name: geom_column
* description: name of geometry column (default 'geom')
* in: query
* required: false
* - name: columns
* description: optional comma seperated list of attribute columns to be added to the mvt geometries
* in: query
* required: false
* type: string
* - name: filter
* description: 'Optional filter parameters for a SQL WHERE statement.'
* in: query
* type: string
* required: false
* - name: bounds
* description: 'Optionally limit output to features that intersect bounding box. Can be expressed as a bounding box (sw.lng, sw.lat, ne.lng, ne.lat) or a Z/X/Y tile (0,0,0).'
WHERE s.consrc ~~* '%srid(% = %'::text) sr ON sr.connamespace = n.oid AND sr.conrelid = c.oid AND (a.attnum = ANY (sr.conkey))
WHERE (c.relkind = ANY (ARRAY['r'::"char", 'v'::"char", 'm'::"char", 'f'::"char", 'p'::"char"])) AND NOT c.relname = 'raster_columns'::name AND t.typname = 'geometry'::name AND NOT pg_is_other_temp_schema(c.relnamespace) AND has_table_privilege(c.oid, 'SELECT'::text);