I'm trying to do a nearest neighbour analysis on fish shoals distribution. I go to Vector > Analysis Tools > Nearest neighbour analysis. I choose the appropriate layer and when I hit ok the black and white wheel is on, and nothing happens; I think it IS doing the analysis because the blue bar is 100%; but just not showing anything.

Has anyone had this problem and know how to solve it?

I'm using: QGIS 1.8.0-Lisboa GDAL 1.9.1 With Mac OS X 10.7.5

  • 1
    How big are your datasets? Does the process finish or does it appear to get stuck?
    – underdark
    May 17, 2013 at 11:10
  • It's fairly small, I've tried it with just 6 points and it seems to get stuck. But QGIS doesn't freeze and I can just close the analysis to get back to the main page. May 17, 2013 at 11:14

1 Answer 1


This a Python solution (comes installed with QGIS):

Make sure to have shapely installed.

Here's the Python script, neighbors.py and run this script in the Python Console (Plugins -> Python Console):

from PyQt4.QtCore import *
from shapely.wkb import loads

# Replace the below value with the field containing name or id of the feature
# For example, if your field is called name then change the line below to
# name_field = 'name'
name_field = 'NAME'

# Replace the below value with the field name that you want to sum up
sum_field = 'POP_EST'

layer = qgis.utils.iface.activeLayer()
provider = layer.dataProvider()

# We add 2 attributes to the current layer
provider.addAttributes( [QgsField("Neighbors", QVariant.String),
                    QgsField("Sum", QVariant.Int)])
neighbor_name_index = provider.fieldNameIndex("Neighbors")
neighbor_sum_index = provider.fieldNameIndex("Sum")
allAttrs = provider.attributeIndexes()

# Select all features along with their attributes
feat = QgsFeature()
polygon_dict = {}

# Loop through all features and store their geometry and relevant attributes in
# a dictionary
while provider.nextFeature(feat):
feature_id = feat.id()
attrmap = feat.attributeMap()
name = attrmap[provider.fieldNameIndex(name_field)].toString()
sum_value = attrmap[provider.fieldNameIndex(sum_field)].toInt()[0]
print 'Reading Geometry for %s' % name
geom = feat.geometry()
wkb = geom.asWkb()
polygon = loads(wkb)
polygon_dict[feature_id] = [ polygon, name, sum_value ]

# Now one-by-one take a feature and find all other features that touch its
# geometry
all_polygons = polygon_dict.keys()
attribute_dict = {}

for polygon_id in all_polygons:
this_polygon, this_name, this_sum = polygon_dict[polygon_id]
neighbor_list = []
sum_of_neighbors = 0

for polygon_id_compare in all_polygons:
compare_polygon, compare_name, compare_sum = polygon_dict[polygon_id_compare]
if this_polygon.touches(compare_polygon):
sum_of_neighbors += compare_sum

# Make a list of all neighbors' names
neighbor_string = ','.join(neighbor_list)
attribute_dict[polygon_id] = {neighbor_name_index: QVariant(neighbor_string),
                           neighbor_sum_index: QVariant(sum_of_neighbors)}

# Update the attribute table

Try using this suggestion and this tutorial.

Vector -> Analysis -> Distance Matrix


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.