We have a table "Shapes" in PostGIS that consists of (among other things) a geometry column (the_geom) and an ID column (type_id) which is a foreign key to another table called "ShapeTypes".

A given row in the Shapes table may have a Type ID value or it may be NULL. If it does have a Type ID, then we want to style the feature based on that. The issue is that we don't know how many different values there may be for Type ID, so we can't just add a fixed number of CLASS entries into the mapfile. Our goal for these shapes (polygons) is to set the fill color... and we can estimate that there will be between 3 and 10 – a color range would be fine. We can also do a SQL JOIN to get the name for each Type, so our query results are essentially the geometry, the type ID, and the corresponding type name. The name is important to the end-user who don't see the IDs elsewhere in the application.

Is it possible to color the shapes based on that Type ID and have the legend display the type name - even though we don't know in advance how many types there will be?

1 Answer 1


No matter how many types have, Is not needed make CLASS entries for each color based on Type ID. Since mapserver 5.0 you can specify color from dataset using hard brackets [].

From doc: For example, if your data set has an attribute named “MYPAINT” that holds color values for each record, use: object for might contain: COLOR [MYPAINT]

  1. Create view that contains: features with id, geom and your color based on Type ID.
  2. Create a single class entry in mapfile like: LABELITEM "type_name" CLASS STYLE COLOR [rgb_color_based_type_id] WIDTH 1 OPACITY 50 END LABEL COLOR 255 255 255 TYPE TRUETYPE FONT "tahoma" SIZE 8 END END

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.