My bigger problem is to convert a raster map to polygons. I've found some questions that give info and I'm trying to use GRASS GIS as part of that. However, I'm having some basic problems and I can't find the solution in the manual or tutorials. If someone can point me somewhere helpful that would be great.

I'm using r.in.gdal to import a tiff file (following this post How to convert image of map into vector format?) of this image


It imports the tiff (which I created in Gimp) as red, green and blue bands. Now I'm trying to display them and thin them and here's where the trouble comes in.

When displaying... Sometimes I get the error that the raster map can't be found. Sometimes I get a red box. One time I did get the goblet, but not sure why!

When I thin, I'm not sure whether to thin the 3 bands separately or whether to composite them into one map and thin that. Neither really works.

As you can see I'm a newbie and I appreciate greatly any help. I've been able to do a fair job of this with WinTopo, but am trying to see if GRASS can do a better job.

  • 1
    y cant you use qgis for converting raster into vector?
    – Arun
    Sep 13 '13 at 14:21
  • Hi Arun, no particular reason. My search pointed me to GRASS first. I'll check out QGIS. Is it easier for novices to use? Thanks for the tip.
    – Chuck
    Sep 13 '13 at 20:00
  • Above link looks like a b/w image, hence it should be saved to b/w TIFF prior to import.
    – markusN
    Sep 14 '13 at 11:51
  • Hi markusN, thanks for the suggestion. I'm using a simple b/w image to learn on. Later they could be color. Don't know. I'm using GIMP to create the TIFF from a jpg file. When I export it, I don't get an option for forcing it to b/w.
    – Chuck
    Sep 16 '13 at 19:01

Okay, I used Whitebox GAT (http://www.uoguelph.ca/~hydrogeo/Whitebox/) to solve this problem. Here were my steps:

  1. I imported the jpeg image that you uploaded using the Import Image tool, although if you have a tiff file you can use the Import GeoTIFF tool instead.

  2. The imported image is an RGB colour image, so this needs to be stripped into the individual red, green, blue bands, which I did using the Split Colour Composite tool.

  3. The resulting image is a greyscale image and you really need a Boolean image to perform the line thinning operation, so I thresholded the image in the Raster Calculator using this statement:

    [bool image] = [blue band image]<128

    Importantly, the resulting raster has a zero background and the outline is filled with ones.

  4. I used the line thinning tool to thin lines, resulting in this raster...

    enter image description here

  5. You'll find several tools in the Raster/Vector Conversion toolbox. Unfortunately the Raster to Vector Lines tool is experimental at this time, but the Raster to Vector Polygons tool does work nicely. Here's a screenshot of the final vector... enter image description here

Incidentally, if you're more interested in the area polygons than the lines (see below), then I can do that too. Just let me know and I'll walk you through that process.

enter image description here I hope that helps you out.

EDIT: The following is a Python script that you can use to extract vector polygons, as in the final image above. All it really does is call the series of Whitebox plugin tools in a specific order. It's fairly well documented so you should be able to follow the logic. If you need further explanation, just email me offline. The script assumes that the input file is named 'cup' but I've commented the line where you change this. Otherwise, it should be good to run as is. You simply need to open the Whitebox Scripter, ensure that it is set up for Python (actually Jython) as the scripting language, paste the code, save it and run it. You may have to play around with the white-space depending on how well I can format the code in this text box ;)

# Get the working directory
wd = pluginHost.getWorkingDirectory()

# Split the image into its individual RGB components.
# This will create three outputs. We'll proceed with the
# blue component, but assuming it is a greyscale RGB image,
# each should be more or less equivalent.
outputAlpha = "false"
args = [inputFile, outputAlpha]
pluginHost.runPlugin("SplitColourComposite", args, False, True)

# Threshold one of the image so that 'low' values are 
# assigned a 1, and 'high' values are given a 0.
inputFile1 = wd + "cup_Blue.dep"
constant = "128" # assumes an 8-bit image as in the given example data
outputFile = wd + "thresholded.dep"
args = [inputFile1, constant, outputFile]
pluginHost.runPlugin("LessThan", args, False, True)

# Perform line-thinning on the Boolean image
inputFile = wd + "thresholded.dep"
outputFile = wd + "thinned.dep"
args = [inputFile, outputFile]
pluginHost.runPlugin("LineThinning", args, False, True)

# Clump, or group, the thinned image
inputFile = wd + "thinned.dep"
outputFile = wd + "clumped.dep"
includeDiagonals = "false" # this is important
zeroBackground = "false"
args = [inputFile, outputFile, includeDiagonals, zeroBackground]
pluginHost.runPlugin("Clump", args, False, True)

# We'll have lots of little groups that are associated
# with the edges. We need to remove them.

# Calculate the area of each group in the clumped image
inputFile = wd + "clumped.dep"
outputFile = wd + "area.dep"
outputText = "false"
units = "grid cells"
zeroBackground = "true" # this is important
args = [inputFile, outputFile, outputText, units, zeroBackground]
pluginHost.runPlugin("Area", args, False, True)

# Re-assign the 'background' zero values in the area image
# a 'No Data' value. This is important for the next step.
inputFiles = wd + "area.dep"
backgroundValue = "0.0"
args = [inputFiles, backgroundValue]
pluginHost.runPlugin("SetNoData", args, False, True)

# Pass a 3 x 3 maximum filter over the image. The small edge 
# groups will be assigned the area values of the larger 
# interior groups.
inputFile = wd + "area.dep"
outputFile = wd + "area corrected.dep"
xDim = "3"
yDim = "3"
rounded = "false"
reflectEdges = "true"
args = [inputFile, outputFile, xDim, yDim, rounded, reflectEdges]
pluginHost.runPlugin("FilterMaximum", args, False, True)

# You could optionally run a second clumping operation here 
# if you want to assign each polygon a unique ID.

# Lastly convert this raster to a vector polygon file
# and display the vector.
inputFile = wd + "area corrected.dep"
outputFile = wd + "final polygons.shp"
args = [inputFile, outputFile]
pluginHost.runPlugin("RasterToVectorPolygons", args, False)

print "Operation complete!"
  • Hello Dr. Lindsay, thank you for the post. I downloaded your software and was able to work my way through the instructions. They worked great. I am more interested in the polygons than the lines as input to another piece of software I work with (SAS). That would be great to walk me through the process. Is it much different or just a different tool at the end?
    – Chuck
    Sep 16 '13 at 19:30
  • Hi Chuck, I won't be able to fit the method for extracting the polygon files in this comment, so I'll edit this answer.
    – user21951
    Sep 16 '13 at 22:10
  • This is a very helpful answer and a great software tool. As a new participant to the forum, I can't vote up Whitebox. Would anyone care to do a proxy vote for me at this link? meta.gis.stackexchange.com/questions/1968/… Your help would be appreciated!
    – Chuck
    Sep 18 '13 at 12:59

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.