6

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.

Edit:

Example

GR_
  • 1,005
  • 9
  • 20

1 Answers1

4

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

EDIT:

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

Jens
  • 6,219
  • 2
  • 30
  • 43
  • 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 '12 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 '12 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 '12 at 14:43
  • 2
    Give MMQGIS plugins hub distance tool a try. I think it does point-to-point. – underdark Dec 20 '12 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 '12 at 17:15
  • Gandalf, with the sequence you described (A1 C2 B3 and I7 H8 G9) I would try the described approach to convert the polygons to centroids. In most cases this will provide the desired result. But there will be some problems too: With strongly curved polygon or with strongly curved border-line, the sequence may be shifted slightly. Polygons like D,E,F will get the same IDs. If the border-line not has enough vertices some polygons will get the same ID (try to add vertices with the “densify”-function). – Jens Dec 21 '12 at 08:54
  • If I convert line vertices to points the order of the points is not randomly. I do not understand why your order is randomly. Perhaps you can try to reorder the vertices with one of these aproaches: Export/Import shapefile, add vertices with densify, use ogr2ogr to convert the shape file to a new shapefile – Jens Dec 21 '12 at 09:00
  • I am currently trying to reorder the vertices with mixed results. I have gotten so far that vertices that origin from the same polygon now lie next to each other. But im proving on that approach will have to wait until after the holidays since for the next couple of days I have no access to the data. – GR_ Dec 21 '12 at 15:28
  • @Gandalf. Do I understand your last comment correctly? You are trying to reorder the vertices of the POLYGONS? You do not have to use the vertices of the POLYGONS (= "polygons in question"). You have to convert the vertices of the LINE (= "western border") to points. – Jens Jan 03 '13 at 13:06
  • I do mean lines... should have mentioned that, my bad. – GR_ Jan 03 '13 at 15:27