support a bbox for the postgres provider

This commit is contained in:
Andrew Yan
2019-10-22 13:58:57 -05:00
parent c8d0f1b512
commit 89d8905c8d
2 changed files with 18 additions and 6 deletions
+12 -5
View File
@@ -45,7 +45,7 @@
import logging
import json
import psycopg2
from psycopg2.sql import SQL, Identifier
from psycopg2.sql import SQL, Identifier, Placeholder
from pygeoapi.provider.base import BaseProvider, \
ProviderConnectionError, ProviderQueryError
@@ -102,7 +102,7 @@ class DatabaseConnection(object):
self.conn = psycopg2.connect(**self.conn_dic)
except psycopg2.OperationalError:
LOGGER.error('Couldnt connect to Postgis using:{}'.format(
LOGGER.error("Couldn't connect to Postgis using:{}".format(
str(self.conn_dic)))
raise ProviderConnectionError()
@@ -200,16 +200,23 @@ class PostgreSQLProvider(BaseProvider):
with DatabaseConnection(self.conn_dic, self.table) as db:
cursor = db.conn.cursor(cursor_factory=RealDictCursor)
sql_query = SQL("DECLARE \"geo_cursor\" CURSOR FOR \
SELECT {0},ST_AsGeoJSON({1}) FROM {2}").\
SELECT {0},ST_AsGeoJSON({1}) FROM {2} WHERE geom && ST_MakeEnvelope({3}, {4}, {5}, {6})").\
format(db.columns,
Identifier('geom'),
Identifier(self.table))
Identifier(self.table),
Placeholder(),
Placeholder(),
Placeholder(),
Placeholder()
)
if not bbox:
bbox = [-180, -90, 180, 90]
LOGGER.debug('SQL Query: {}'.format(sql_query))
LOGGER.debug('Start Index: {}'.format(startindex))
LOGGER.debug('End Index: {}'.format(end_index))
try:
cursor.execute(sql_query)
cursor.execute(sql_query, bbox)
for index in [startindex, limit]:
cursor.execute("fetch forward {} from geo_cursor"
.format(index))
+6 -1
View File
@@ -30,7 +30,6 @@
# =================================================================
# Needs to be run like: python3 -m pytest
import pytest
from pygeoapi.provider.postgresql import PostgreSQLProvider
@@ -64,6 +63,12 @@ def test_query(config):
assert geometry is not None
def test_query_bbox(config):
psp = PostgreSQLProvider(config)
boxed_feature_collection = psp.query(bbox=[29.3373, -3.4099, 29.3761, -3.3924])
assert len(boxed_feature_collection['features']) == 5
def test_get(config):
"""Testing query for a specific object"""
p = PostgreSQLProvider(config)