I have a shapefile (which can be downloaded from this dropbox link) that I'd like to rasterize so I can merge it with some non-geographic data (I have the code to do this already). Unfortunately, the shapefile does not have a unique ID assigned to each delimited area, as shown below. The region_id attribute isn't actually unique. (Look at the areas with 10, 14, and 15 as region IDs).

![Shapefile with non-unique "region ID"

How can I assign a unique attribute to each of these delimited, closed regions so that I can rasterize the shapefile into the proper format? (In my case, an XYZ format with latitude, longitude, and a specific value).

The attribute table looks like this:

Attribute table

  • Try dissolving based on region_id, then use split multiparts to singleparts to split apart separate regions that have the same region_id.
    – csk
    Nov 5, 2018 at 16:12
  • It looks as though you have multiple straight line segments defined by two points. csk's suggestions above will help you merge these segments together so that you only have one unique ID per line segment, but from the looks of the data this will often still fail to constitute a closed boundary which can be converted to a polygon. You could try using something like this (desktop.arcgis.com/en/arcmap/10.3/manage-data/…) or some manual processes to form closed polygon features. Nov 6, 2018 at 0:44
  • the .shp file is only part of what you need to share for others to help on this. You need all the other files with names conductivity_m3.* At a minimum these other files have extensions .dbf and .shx, but there may be others as well, such as .prj, .sbn, .sbx, and .xml. Zip all these files and place that in your dropbox.
    – Llaves
    Nov 6, 2018 at 15:12
  • @Llaves Sorry, you're right. I fixed the link to point to the archive instead of just the shapefile.
    – Michael A
    Nov 7, 2018 at 15:54

2 Answers 2


Now that we understand that the file contains lines and not polygons -

Select Processing-> Toolbox
Expand QGIS algorithms
Expand Vector Geometry Tools under QGIS algorithms
Select the Polygonize Tool (not the Lines from Polygons Tool, that won't work).

Apply the Polygonize tool to your layer. You now have a polygon layer. You can now apply the field calculator solution to add a row-number column, which is unique to region.

Note - when I applied this to your data set, several regions did not properly close. I suspect this is because some endpoints of line segments do not match exactly. However, is it not obvious where these gaps are.

Here's the result. For clarity I colored the regions by area to make clear where the different regions are.

Map with Regions

  • I had to fix a few of the gaps manually, but otherwise, this was the perfect solution. Thank you!
    – Michael A
    Nov 13, 2018 at 16:05

Underdark answered this in her blog years ago. You can read it here. It still works at least as far as 2.18. (For those who don't want to follow the link -

open the attribute table of the layer
enable editing
create new column
expression is @rownumber
  • 1
    Each of the closed regions in the shapefile is defined by many rows in the attribute table, though. How does creating a field with the row number create a value that is unique across (but constant within) each closed region?
    – Michael A
    Nov 4, 2018 at 15:16
  • 1
    Also, the expression is "$rownumber".
    – Michael A
    Nov 4, 2018 at 15:27
  • so you do have a shapefile with several regions. But "one" region is spread over several "lines/datasets" in your attribute table? Maybe it helps if you can post a screenshot of your attribute table so we can get an idea about how your data is structured. Also give an example in this screenshot which "lines/datasets" belong together.
    – MrXsquared
    Nov 5, 2018 at 0:11
  • What is your figure showing us? Does your shapefile contain polygons? If so, then each polygon corresponds to a single row in the attribute table. If that's the case, what does the region_id mean? If the shapefile contains something other than polygons, what does it contain? Is each label in your figure associated with a line?
    – Llaves
    Nov 5, 2018 at 19:20
  • If your file is lines, maybe the solution from gis.stackexchange.com/questions/207463/… is what you want
    – Llaves
    Nov 5, 2018 at 19:29

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.