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,7 +120,8 @@ 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 " \
"ST_Touches(i.\"{geom_col}\", j.\"{geom_col}\") AND " \
"%(attr_where_j)s)" \ "%(attr_where_j)s)" \
") As neighbors " \ ") As neighbors " \
"FROM ({subquery}) As i " \ "FROM ({subquery}) As i " \

View File

@ -40,10 +40,11 @@ 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 " \
"i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \
"j.\"andy\" IS NOT NULL AND " \
"j.\"jay_z\" IS NOT NULL AND " \ "j.\"jay_z\" IS NOT NULL AND " \
"j.\"jay_z\" <> 0 AND " \ "j.\"jay_z\" <> 0 " \
"i.\"cartodb_id\" <> j.\"cartodb_id\" " \
"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 " \
@ -63,7 +64,9 @@ 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 " \
"i.\"cartodb_id\" <> j.\"cartodb_id\" AND " \
"ST_Touches(i.\"the_geom\", " \
"j.\"the_geom\") AND " \ "j.\"the_geom\") AND " \
"j.\"andy\" IS NOT NULL AND " \ "j.\"andy\" IS NOT NULL AND " \
"j.\"jay_z\" IS NOT NULL AND " \ "j.\"jay_z\" IS NOT NULL AND " \