mirror of
https://github.com/CartoDB/crankshaft.git
synced 2024-11-01 10:20:48 +08:00
catch empty return values and error on them
This commit is contained in:
parent
34161fd8a4
commit
d679975f72
@ -2,18 +2,25 @@
|
|||||||
import plpy
|
import plpy
|
||||||
import pysal_utils as pu
|
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:
|
class AnalysisDataProvider:
|
||||||
def get_getis(self, w_type, params):
|
def get_getis(self, w_type, params):
|
||||||
"""fetch data for getis ord's g"""
|
"""fetch data for getis ord's g"""
|
||||||
try:
|
try:
|
||||||
query = pu.construct_neighbor_query(w_type, params)
|
query = pu.construct_neighbor_query(w_type, params)
|
||||||
result = plpy.execute(query)
|
data = plpy.execute(query)
|
||||||
# if there are no neighbors, exit
|
|
||||||
if len(result) == 0:
|
# if there are no neighbors or all nulls, exit
|
||||||
return pu.empty_zipped_array(4)
|
verify_data(len(data))
|
||||||
else:
|
return data
|
||||||
return result
|
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, err:
|
||||||
plpy.error('Analysis failed: %s' % err)
|
plpy.error('Analysis failed: %s' % err)
|
||||||
|
|
||||||
@ -23,9 +30,7 @@ class AnalysisDataProvider:
|
|||||||
query = pu.construct_neighbor_query(w_type, params)
|
query = pu.construct_neighbor_query(w_type, params)
|
||||||
data = plpy.execute(query)
|
data = plpy.execute(query)
|
||||||
|
|
||||||
if len(data) == 0:
|
verify_data(len(data))
|
||||||
return pu.empty_zipped_array(4)
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, err:
|
||||||
plpy.error('Analysis failed: %s' % err)
|
plpy.error('Analysis failed: %s' % err)
|
||||||
@ -37,8 +42,7 @@ class AnalysisDataProvider:
|
|||||||
data = plpy.execute(query)
|
data = plpy.execute(query)
|
||||||
|
|
||||||
# if there are no neighbors, exit
|
# if there are no neighbors, exit
|
||||||
if len(data) == 0:
|
verify_data(len(data))
|
||||||
return pu.empty_zipped_array(2)
|
|
||||||
return data
|
return data
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, err:
|
||||||
plpy.error('Analysis failed: %s' % e)
|
plpy.error('Analysis failed: %s' % e)
|
||||||
@ -48,6 +52,7 @@ class AnalysisDataProvider:
|
|||||||
"""fetch data for non-spatial kmeans"""
|
"""fetch data for non-spatial kmeans"""
|
||||||
try:
|
try:
|
||||||
data = plpy.execute(query)
|
data = plpy.execute(query)
|
||||||
|
verify_data(len(data))
|
||||||
return data
|
return data
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, err:
|
||||||
plpy.error('Analysis failed: %s' % err)
|
plpy.error('Analysis failed: %s' % err)
|
||||||
@ -55,13 +60,14 @@ class AnalysisDataProvider:
|
|||||||
def get_spatial_kmeans(self, params):
|
def get_spatial_kmeans(self, params):
|
||||||
"""fetch data for spatial kmeans"""
|
"""fetch data for spatial kmeans"""
|
||||||
query = ("SELECT "
|
query = ("SELECT "
|
||||||
"array_agg({id_col} ORDER BY {id_col}) as ids,"
|
"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_X(\"{geom_col}\") ORDER BY \"{id_col}\") As xs,"
|
||||||
"array_agg(ST_Y({geom_col}) ORDER BY {id_col}) As ys "
|
"array_agg(ST_Y(\"{geom_col}\") ORDER BY \"{id_col}\") As ys "
|
||||||
"FROM ({subquery}) As a "
|
"FROM ({subquery}) As a "
|
||||||
"WHERE {geom_col} IS NOT NULL").format(**params)
|
"WHERE \"{geom_col}\" IS NOT NULL").format(**params)
|
||||||
try:
|
try:
|
||||||
data = plpy.execute(query)
|
data = plpy.execute(query)
|
||||||
|
verify_data(len(data))
|
||||||
return data
|
return data
|
||||||
except plpy.SPIError, err:
|
except plpy.SPIError, err:
|
||||||
plpy.error('Analysis failed: %s' % err)
|
plpy.error('Analysis failed: %s' % err)
|
||||||
|
Loading…
Reference in New Issue
Block a user