adding condition to avoid self-comparison in neighbor queries

This commit is contained in:
Andy Eschbacher 2016-03-30 15:37:51 -04:00
parent 22b6aed7c1
commit ea02f36235
2 changed files with 21 additions and 15 deletions

View File

@ -89,9 +89,11 @@ def knn(params):
"%(attr_select)s" \ "%(attr_select)s" \
"(SELECT ARRAY(SELECT j.\"{id_col}\" " \ "(SELECT ARRAY(SELECT j.\"{id_col}\" " \
"FROM ({subquery}) As j " \ "FROM ({subquery}) As j " \
"WHERE %(attr_where_j)s AND " \ "WHERE " \
"i.\"{id_col}\" <> j.\"{id_col}\" " \ "i.\"{id_col}\" <> j.\"{id_col}\" AND " \
"ORDER BY j.\"{geom_col}\" <-> i.\"{geom_col}\" ASC " \ "%(attr_where_j)s " \
"ORDER BY " \
"j.\"{geom_col}\" <-> i.\"{geom_col}\" ASC " \
"LIMIT {num_ngbrs})" \ "LIMIT {num_ngbrs})" \
") As neighbors " \ ") As neighbors " \
"FROM ({subquery}) As i " \ "FROM ({subquery}) As i " \
@ -118,8 +120,9 @@ def queen(params):
"%(attr_select)s" \ "%(attr_select)s" \
"(SELECT ARRAY(SELECT j.\"{id_col}\" " \ "(SELECT ARRAY(SELECT j.\"{id_col}\" " \
"FROM ({subquery}) As j " \ "FROM ({subquery}) As j " \
"WHERE ST_Touches(i.\"{geom_col}\", j.\"{geom_col}\") AND " \ "WHERE i.\"{id_col}\" <> j.\"{id_col}\" AND " \
"%(attr_where_j)s)" \ "ST_Touches(i.\"{geom_col}\", j.\"{geom_col}\") AND " \
"%(attr_where_j)s)" \
") As neighbors " \ ") As neighbors " \
"FROM ({subquery}) As i " \ "FROM ({subquery}) As i " \
"WHERE " \ "WHERE " \

View File

@ -40,12 +40,13 @@ class PysalUtilsTest(unittest.TestCase):
"i.\"jay_z\"::numeric As attr2, " \ "i.\"jay_z\"::numeric As attr2, " \
"(SELECT ARRAY(SELECT j.\"cartodb_id\" " \ "(SELECT ARRAY(SELECT j.\"cartodb_id\" " \
"FROM (SELECT * FROM a_list) As j " \ "FROM (SELECT * FROM a_list) As j " \
"WHERE j.\"andy\" IS NOT NULL AND " \ "WHERE " \
"j.\"jay_z\" IS NOT NULL AND " \ "i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \
"j.\"jay_z\" <> 0 AND " \ "j.\"andy\" IS NOT NULL AND " \
"i.\"cartodb_id\" <> j.\"cartodb_id\" " \ "j.\"jay_z\" IS NOT NULL AND " \
"j.\"jay_z\" <> 0 " \
"ORDER BY " \ "ORDER BY " \
"j.\"the_geom\" <-> i.\"the_geom\" ASC " \ "j.\"the_geom\" <-> i.\"the_geom\" ASC " \
"LIMIT 321)) As neighbors " \ "LIMIT 321)) As neighbors " \
"FROM (SELECT * FROM a_list) As i " \ "FROM (SELECT * FROM a_list) As i " \
"WHERE i.\"andy\" IS NOT NULL AND " \ "WHERE i.\"andy\" IS NOT NULL AND " \
@ -63,11 +64,13 @@ class PysalUtilsTest(unittest.TestCase):
"i.\"jay_z\"::numeric As attr2, " \ "i.\"jay_z\"::numeric As attr2, " \
"(SELECT ARRAY(SELECT j.\"cartodb_id\" " \ "(SELECT ARRAY(SELECT j.\"cartodb_id\" " \
"FROM (SELECT * FROM a_list) As j " \ "FROM (SELECT * FROM a_list) As j " \
"WHERE ST_Touches(i.\"the_geom\", " \ "WHERE " \
"j.\"the_geom\") AND " \ "i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \
"j.\"andy\" IS NOT NULL AND " \ "ST_Touches(i.\"the_geom\", " \
"j.\"jay_z\" IS NOT NULL AND " \ "j.\"the_geom\") AND " \
"j.\"jay_z\" <> 0)" \ "j.\"andy\" IS NOT NULL AND " \
"j.\"jay_z\" IS NOT NULL AND " \
"j.\"jay_z\" <> 0)" \
") As neighbors " \ ") As neighbors " \
"FROM (SELECT * FROM a_list) As i " \ "FROM (SELECT * FROM a_list) As i " \
"WHERE i.\"andy\" IS NOT NULL AND " \ "WHERE i.\"andy\" IS NOT NULL AND " \