1

I am looking for a way to automatically cluster the monthly rainfall data that I have like the Koppen Classification Map. I have 12 raster files of precipitation (months of Jan-Dec) of a country and I want to make a map of the precipitation pattern (eg. Cluster 1 are regions with Jan-Dry, Feb-Dry, March-Wet ..... while Cluster 2 are regions with Jan-Wet, Feb-Dry .... Cluster 3 are regions with Jan-Wet, Feb-Wet and so forth....). Can I do that on QGIS 2.18?

3
  • Is it possible for you to update to QGIS 3? It has a cluster-renderer symbology.
    – csk
    Dec 5, 2019 at 17:20
  • I recently found out that I can use the K-mean feature on version 2.18 but I do not know how many clusters should I put? Is there a way to easily determine this? Thank you :) Dec 6, 2019 at 4:14
  • Actually after re-reading your question I think it's more complicated than just making clusters. Clustering is about combining features that are physically close together. Your goal is about grouping regions by their raster values. There are probably a lot of different ways to achieve your end goal. I'll post an outline of one approach.
    – csk
    Dec 6, 2019 at 16:55

2 Answers 2

1

After I posted my other answer, I realized there's a much faster way to do it.

  1. Use the Raster Pixels to points tool on one of your rasters. This creates a point layer with one point per pixel.

  2. Use the Sample Raster Values processing algorithm or the Point Sampling Tool plugin to copy the raster values from each of the other rasters to the point layer created in step 1.

  3. (This step 3 is the same as step 3 in my other answer)

    Write an expression that combines all of the raster values, like this:

    field1 || field2 || field3 || field4 || field5 || field6 || field7 || field8 || field9 || field10 || field11 || field12
    

    Substitute the actual field names in your vector layer from step 2 (where field1 is the value from raster1, field2 is the value from raster2, etc).

    You can use this expression in several places:

    • In the Field Calculator, to create a new field.
    • In the Categorized style renderer, to display each category as a distinct color.

If you want a field that says 'Jan-Wet, Feb-Dry, Mar-Wet, etc', for field1, substitute: 'Jan-' || if(field1 = 1, 'Wet, ', 'Dry, '), where 1 is the raster value that means 'wet'

0

Here's one way to approach this problem. This method converts the rasters to vector data at the beginning. If your data is very high-resolution, this won't work because vector data takes up more memory and storage space ("raster is faster but vector is corrector") - in that case you'll want an approach that stays in raster format.

  1. Vectorize the raster layers. If they're very small (ie each raster doesn't have very many cells) you may be able to get away with polygonizing (using the GDAL tool Polygonize (raster to vector)). If you're not sure, try the polygonize tool; if QGIS doesn't crash, proceed with polygonizing the rest of the layers (save often).

    Otherwise convert them to points with the raster pixels to points tool. Be sure to give each layer a unique name that tells you which raster it came from.

    • If you converted them to points, the points will be arranged in a grid, where each point is the center of the raster pixel it represents. Make sure the points from one layer are in the same location as points from another layer. Ideally the points will exactly overlap, so they can be unambiguously joined. It should look like this:

    enter image description here

    It's okay if they are slightly offset, as long as each point unambiguously corresponds to an adjacent point, like this: enter image description here

    But it's not good if they points are offset so far from each other that it's not clear which point in raster A belongs to which point in raster B. If you proceed from here, the final analysis will have some spatial error in it. enter image description here

  2. Use the intersection tool to combine two of the layers. If they're polygons, this step should work perfectly. If they're points, this tool only works if the points are in exactly the same place. Open the attribute table - hopefully each feature now has one value in each of two attribute columns. The two attributes are the values from the two input rasters.

    • If it worked, repeat the step with this layer and the next of your point or polygon layers.
    • If it didn't work, instead use the NNJoin plugin to do a nearest-neighbor join. Repeat until the data from all the layers is combined into a single layer.
  3. Write an expression that combines all of the raster values, like this:

    field1 || field2 || field3 || field4 || field5 || field6 || field7 || field8 || field9 || field10 || field11 || field12
    

    Substitute the actual field names in your vector layer from step 2 (where field1 is the value from raster1, field2 is the value from raster2, etc).

    You can use this expression in several places:

    • In the Field Calculator, to create a new field.
    • In the Categorized style renderer, to display each category as a distinct color.

If you want a field that says 'Jan-Wet, Feb-Dry, Mar-Wet, etc', for field1, substitute: 'Jan-' || if(field1 = 1, 'Wet, ', 'Dry, '), where 1 is the raster value that means 'wet'

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.