diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index cbc27bc..373c100 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -2,18 +2,25 @@ import plpy import pysal_utils as pu +NULL_VALUE_ERROR = ('No usable data passed to analysis. Check your input rows ' + 'for null values and fill in appropriately.') + + +def verify_data(n_rows): + if n_rows == 0: + plpy.error(NULL_VALUE_ERROR) + class AnalysisDataProvider: def get_getis(self, w_type, params): """fetch data for getis ord's g""" try: query = pu.construct_neighbor_query(w_type, params) - result = plpy.execute(query) - # if there are no neighbors, exit - if len(result) == 0: - return pu.empty_zipped_array(4) - else: - return result + data = plpy.execute(query) + + # if there are no neighbors or all nulls, exit + verify_data(len(data)) + return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) @@ -23,9 +30,7 @@ class AnalysisDataProvider: query = pu.construct_neighbor_query(w_type, params) data = plpy.execute(query) - if len(data) == 0: - return pu.empty_zipped_array(4) - + verify_data(len(data)) return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) @@ -37,8 +42,7 @@ class AnalysisDataProvider: data = plpy.execute(query) # if there are no neighbors, exit - if len(data) == 0: - return pu.empty_zipped_array(2) + verify_data(len(data)) return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % e) @@ -48,6 +52,7 @@ class AnalysisDataProvider: """fetch data for non-spatial kmeans""" try: data = plpy.execute(query) + verify_data(len(data)) return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) @@ -55,13 +60,14 @@ class AnalysisDataProvider: 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 " + "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) + "WHERE \"{geom_col}\" IS NOT NULL").format(**params) try: data = plpy.execute(query) + verify_data(len(data)) return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err)