From 000a4404174ac91895ac095eaebdc39e0600be43 Mon Sep 17 00:00:00 2001 From: John Krauss Date: Tue, 3 Jan 2017 16:37:16 +0000 Subject: [PATCH] resolve issues in build and with code, now returning geometries and data as expected from obs_getoverpass --- Makefile | 5 +++ src/pg/sql/41_observatory_augmentation.sql | 2 + src/pg/sql/60_observatory_providers.sql | 43 ++++++++++++++++++++-- src/python/Makefile | 7 +++- src/python/observatory/observatory/osm.py | 19 ++++++++-- 5 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 1f613ad..a7b9715 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ include ./Makefile.global EXT_DIR = src/pg +PYP_DIR = src/python .PHONY: install .PHONY: run_tests @@ -12,14 +13,17 @@ EXT_DIR = src/pg # It requires sudo. install: ## Generate and install development version of the extension; requires sudo. $(MAKE) -C $(EXT_DIR) install + $(MAKE) -C $(PYP_DIR) install # Run the tests for the installed development extension. test: ## Run the tests for the development version of the extension $(MAKE) -C $(EXT_DIR) test + $(MAKE) -C $(PYP_DIR) test # Generate a new release into release release: ## Generate a new release of the extension. Only for release manager $(MAKE) -C $(EXT_DIR) release + $(MAKE) -C $(PYP_DIR) release # Install the current release. # It requires sudo. @@ -27,6 +31,7 @@ release: ## Generate a new release of the extension. Only for release manager # sudo make deploy RELEASE_VERSION=1.0.0 deploy: ## Deploy a released extension. Only for release manager. Requires sudo. $(MAKE) -C $(EXT_DIR) deploy + $(MAKE) -C $(PYP_DIR) deploy # Cleanup development extension script files clean-dev: ## clean up development extension script files diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index b7227ba..c732cc3 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -684,11 +684,13 @@ BEGIN Array_Cat(Array_Agg(geom_tablename), Array_Agg(denom_tablename) FILTER (WHERE denom_tablename IS NOT NULL))) )) tablename) bar) tablenames, + String_Agg(numer_tablename || '.' || numer_geomref_colname || ' = ' || geom_tablename || '.' || geom_geomref_colname || Coalesce(' AND ' || numer_tablename || '.' || numer_geomref_colname || ' = ' || denom_tablename || '.' || denom_geomref_colname, ''), ' AND ') AS obs_wheres, + String_Agg('ST_Intersects(' || geom_tablename || '.' || geom_colname || ', _geoms.geom)', ' AND ') AS user_wheres diff --git a/src/pg/sql/60_observatory_providers.sql b/src/pg/sql/60_observatory_providers.sql index d5cf6dc..ed9f55e 100644 --- a/src/pg/sql/60_observatory_providers.sql +++ b/src/pg/sql/60_observatory_providers.sql @@ -1,7 +1,44 @@ -CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetOverpass(query text) -RETURNS table (lat Numeric, lon Numeric, "type" TEXT, id Numeric, tags TEXT) as $$ +CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetOverpass( + query text +) RETURNS TABLE ( + geom TEXT, + "type" TEXT, + id TEXT, + properties TEXT +) AS $$ from observatory.osm import get_overpass - return get_overpass(query) + import plpy + import json + + result = get_overpass(query) + + return [{ + 'geom': json.dumps(feature['geometry']), + 'type': feature['type'], + 'id': str(feature['id']), + 'properties': json.dumps(feature['properties']) + } for feature in result] $$ LANGUAGE plpythonu; + +CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetOverpass( + query text +) RETURNS TABLE ( + geom Geometry(Geometry, 4326), + "type" TEXT, + id TEXT, + properties JSON +) AS $$ +BEGIN + RETURN QUERY + EXECUTE $string$ + SELECT ST_GeomFromGeojson(geom) geom, + "type", + id, + properties::JSON + FROM cdb_observatory._OBS_GetOverPass($1) + $string$ USING query + RETURN; +END; +$$ LANGUAGE plpgsql; diff --git a/src/python/Makefile b/src/python/Makefile index d4953ed..77bfcb7 100644 --- a/src/python/Makefile +++ b/src/python/Makefile @@ -1,4 +1,9 @@ +include ../../Makefile.global + # Install the package locally for development install: - pip install --upgrade ./crankshaft + pip install --upgrade ./observatory + +test: + #TODO noop diff --git a/src/python/observatory/observatory/osm.py b/src/python/observatory/observatory/osm.py index fb4ede2..091861e 100644 --- a/src/python/observatory/observatory/osm.py +++ b/src/python/observatory/observatory/osm.py @@ -18,7 +18,20 @@ way 40.704301, -73.936658)) ''' - api = API() - response = api.Get(query, responseformat='json') + ''' +(node [amenity] (around:400, 40.704301, -73.936658); way [amenity] (around:400, 40.704301, -73.936658)) + ''' - return [(el['lat'], el['lon'], el['type'], el['id'], json.dumps(el['tags']), ) for el in response['elements'] if 'lat' in el] + api = API() + response = api.Get(query) + + return response['features'] + #return [(el['lat'], el['lon'], el['type'], el['id'], json.dumps(el['tags']) ) + # for el in response['elements'] if 'lat' in el][0] + + +def get_overpass_poi(within_geom, geom, filters=None, name=None): + ''' + Simplified access to overpass API + ''' + pass