From ea02f36235420e1ec4afc621b728f3dfd87fc7f5 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Wed, 30 Mar 2016 15:37:51 -0400 Subject: [PATCH] adding condition to avoid self-comparison in neighbor queries --- .../crankshaft/pysal_utils/pysal_utils.py | 13 +++++++---- src/py/crankshaft/test/test_pysal_utils.py | 23 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py b/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py index 9fdbfb3..fa06e26 100644 --- a/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py +++ b/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py @@ -89,9 +89,11 @@ def knn(params): "%(attr_select)s" \ "(SELECT ARRAY(SELECT j.\"{id_col}\" " \ "FROM ({subquery}) As j " \ - "WHERE %(attr_where_j)s AND " \ - "i.\"{id_col}\" <> j.\"{id_col}\" " \ - "ORDER BY j.\"{geom_col}\" <-> i.\"{geom_col}\" ASC " \ + "WHERE " \ + "i.\"{id_col}\" <> j.\"{id_col}\" AND " \ + "%(attr_where_j)s " \ + "ORDER BY " \ + "j.\"{geom_col}\" <-> i.\"{geom_col}\" ASC " \ "LIMIT {num_ngbrs})" \ ") As neighbors " \ "FROM ({subquery}) As i " \ @@ -118,8 +120,9 @@ def queen(params): "%(attr_select)s" \ "(SELECT ARRAY(SELECT j.\"{id_col}\" " \ "FROM ({subquery}) As j " \ - "WHERE ST_Touches(i.\"{geom_col}\", j.\"{geom_col}\") AND " \ - "%(attr_where_j)s)" \ + "WHERE i.\"{id_col}\" <> j.\"{id_col}\" AND " \ + "ST_Touches(i.\"{geom_col}\", j.\"{geom_col}\") AND " \ + "%(attr_where_j)s)" \ ") As neighbors " \ "FROM ({subquery}) As i " \ "WHERE " \ diff --git a/src/py/crankshaft/test/test_pysal_utils.py b/src/py/crankshaft/test/test_pysal_utils.py index 8e9b7da..4ea0d9b 100644 --- a/src/py/crankshaft/test/test_pysal_utils.py +++ b/src/py/crankshaft/test/test_pysal_utils.py @@ -40,12 +40,13 @@ class PysalUtilsTest(unittest.TestCase): "i.\"jay_z\"::numeric As attr2, " \ "(SELECT ARRAY(SELECT j.\"cartodb_id\" " \ "FROM (SELECT * FROM a_list) As j " \ - "WHERE j.\"andy\" IS NOT NULL AND " \ - "j.\"jay_z\" IS NOT NULL AND " \ - "j.\"jay_z\" <> 0 AND " \ - "i.\"cartodb_id\" <> j.\"cartodb_id\" " \ + "WHERE " \ + "i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \ + "j.\"andy\" IS NOT NULL AND " \ + "j.\"jay_z\" IS NOT NULL AND " \ + "j.\"jay_z\" <> 0 " \ "ORDER BY " \ - "j.\"the_geom\" <-> i.\"the_geom\" ASC " \ + "j.\"the_geom\" <-> i.\"the_geom\" ASC " \ "LIMIT 321)) As neighbors " \ "FROM (SELECT * FROM a_list) As i " \ "WHERE i.\"andy\" IS NOT NULL AND " \ @@ -63,11 +64,13 @@ class PysalUtilsTest(unittest.TestCase): "i.\"jay_z\"::numeric As attr2, " \ "(SELECT ARRAY(SELECT j.\"cartodb_id\" " \ "FROM (SELECT * FROM a_list) As j " \ - "WHERE ST_Touches(i.\"the_geom\", " \ - "j.\"the_geom\") AND " \ - "j.\"andy\" IS NOT NULL AND " \ - "j.\"jay_z\" IS NOT NULL AND " \ - "j.\"jay_z\" <> 0)" \ + "WHERE " \ + "i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \ + "ST_Touches(i.\"the_geom\", " \ + "j.\"the_geom\") AND " \ + "j.\"andy\" IS NOT NULL AND " \ + "j.\"jay_z\" IS NOT NULL AND " \ + "j.\"jay_z\" <> 0)" \ ") As neighbors " \ "FROM (SELECT * FROM a_list) As i " \ "WHERE i.\"andy\" IS NOT NULL AND " \