This is probably a long shot, but is there a way to automatically select colours for neighbouring regions using the four colour theorem, in PostGIS? The data I'm using is suburb boundaries in OpenStreetMap (boundary=administrative, admin_level=10).

1 Answer 1


I had a similar question from an Esri customer once. The answer was to create a field of a numerical type, which could hold a value from 1-4. The colour was then set from the attribute of that field. The issue was to get the values. We created a script that took the first polygon in a dataset gave it the number one, then took the next polygon. If it touched the first then it got a 2, if it didn't it became a 1. Then you iterate through each one checking to see what other values are touching it, always defaulting to the lowest number. In the end I had to use a number 5, but very occassionally - it got to the point where writing the code was exceeding the value of getting to 4.

Depends how many polygons, you've got, but if not many then you could do it manually?

  • Thanks - I missed this response for some reason. This approach seems reasonable, and highlights a dumb assumption I made - using slightly more than 4 colours would actually be fine :) Wonder if I can find a script to do this in PostGIS. Commented Aug 6, 2013 at 5:43

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.