Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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



share|improve this question
How big are your datasets? Does the process finish or does it appear to get stuck? –  underdark May 17 '13 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. –  Nathalie Verlinden May 17 '13 at 11:14
I have the same problem and have not managed to resolve it yet. My dataset only has 4 points. –  user18588 May 28 '13 at 13:51

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

share|improve this answer

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

To prevent this answer from turning useless when the links break, please explain the general idea here. –  underdark Jun 27 '13 at 20:25
Thanks for the comment to better improve my answer. –  Zach Jul 3 '13 at 20:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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