places query gen in kmeans data provider

This commit is contained in:
Andy Eschbacher
2016-11-30 10:08:36 -05:00
parent b22f79b0cc
commit 6ab1c285d9
2 changed files with 11 additions and 8 deletions
@@ -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
@@ -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']