diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index 6af37f5..1d1cf2b 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -38,8 +38,14 @@ class AnalysisDataProvider: except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) - def get_spatial_kmeans(self, query): + def get_spatial_kmeans(self, params): """fetch data for spatial kmeans""" + query = ("SELECT " + "array_agg({id_col} ORDER BY {id_col}) as ids," + "array_agg(ST_X({geom_col}) ORDER BY {id_col}) As xs," + "array_agg(ST_Y({geom_col}) ORDER BY {id_col}) As ys " + "FROM ({subquery}) As a " + "WHERE {geom_col} IS NOT NULL").format(**params) try: data = plpy.execute(query) return data diff --git a/src/py/crankshaft/crankshaft/clustering/kmeans.py b/src/py/crankshaft/crankshaft/clustering/kmeans.py index e59c25b..1e49115 100644 --- a/src/py/crankshaft/crankshaft/clustering/kmeans.py +++ b/src/py/crankshaft/crankshaft/clustering/kmeans.py @@ -16,14 +16,11 @@ class Kmeans: find centers based on clusters of latitude/longitude pairs query: SQL query that has a WGS84 geometry (the_geom) """ - full_query = ("SELECT " - "array_agg(cartodb_id ORDER BY cartodb_id) as ids," - "array_agg(ST_X(the_geom) ORDER BY cartodb_id) xs," - "array_agg(ST_Y(the_geom) ORDER BY cartodb_id) ys " - "FROM ({query}) As a " - "WHERE the_geom IS NOT NULL").format(query=query) + params = {"subquery": query, + "geom_col": "the_geom", + "id_col": "cartodb_id"} - data = self.data_provider.get_spatial_kmeans(full_query) + data = self.data_provider.get_spatial_kmeans(params) # Unpack query response xs = data[0]['xs']