I have a shapefile (polygon) with a large number of polygons (roughly 10,000) that form a narrow but very long belt that windes through half a country and another shapefile (polyline) that represents the western border of the belt. Of those 10,000 polygons only about one third touches the border. So simply cutting the line into smaller lines using intersect, number those and then join back does not work. I need to gibe each polygon a unique ID-number that consecutively increases from the first polygon in the north to the last polygon in the south. My first idea, to simply create centroids, calculate the y-value for them and sort decreasingly, does not work either because the southern end is not the most southern point of the belt.

My question now is how to get this done with an automated action and without leaving the polygons behind that do not touch the border.



  • 2
    Could you post an illustration?
    – whuber
    Dec 20, 2012 at 15:03
  • Please elaborate on the logic of the polygon numbering. i.e. if the southern most point is not the same as the "southern end" then what?
    – Willy
    Dec 22, 2012 at 3:49

1 Answer 1


Try this approach:

  1. Convert line to points (extract vertices)
  2. calculate number ($id) for each point
  3. use distance matrix to find the nearest point for each centroid
  4. transfer point-number to polygon-centroids


Gandalf, how do you want to increases the IDs in detail? May you specify the IDs of these polygons: enter image description here

  • Unfortunately this does not work. If I calculate the IDs for the points, the spatial distribution is kind of random, which means ID-2 is not next to ID-1 and so forth. The next problem is that I could only use this approach on those polygons that touch the line.
    – GR_
    Dec 20, 2012 at 13:17
  • If I calculate the IDs for the points, the spatial distribution is NOT random. Do you have 1 line or more lines?
    – Jens
    Dec 20, 2012 at 14:39
  • You're right: QGIS is not able to make a spatial join from a point-layer to another point-layer (step 3). For step 3 distance matrix may help.
    – Jens
    Dec 20, 2012 at 14:43
  • 2
    Give MMQGIS plugins hub distance tool a try. I think it does point-to-point.
    – underdark
    Dec 20, 2012 at 15:59
  • 1
    @ Jens: I would like to start with the north-western polygon and go reading style to the south-eastern polygon. Using your illustration: A1 C2 B3 F4 E5 D6 I7 H8 G9.
    – GR_
    Dec 20, 2012 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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