Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to beautify a map by using Colorize as demonstrated in the Mathematica documentation. First, I import a shapefile from DIVA-GIS for France which contains polygons for the country, its regions and departments.

mapdata = Import["", "Data"];

I then extract the region data and (poorly) place a bounding region around the map:

Graphics[Rectangle[{-6, 41}, {9.7, 51.5}]];
Graphics@{EdgeForm[Thick], FaceForm[White], ("Geometry" /. mapdata[[2]])};
t1 = Show[%%, %]

I can then Colorize

t2 = Colorize[MorphologicalComponents[t1, CornerNeighbors -> False], ColorFunction -> "LightTerrain"]

which gives me the following Image (note, it is not a Graphic)

France in terrestrial colors

I'd now like to take the department information and lay it over the colored regional map

t3 = Show[Graphics@{Black, EdgeForm[Thin], FaceForm[], ("Geometry" /. mapdata[[1]])}]

enter image description here

However the MorphologicalComponents function rescaled the regional map, preventing me from overlapping t2 and t3 above.

Is there a way to get the same or similar effect as obtained through MorphologicalComponents and Colorize without losing the x,y coordinate data?

share|improve this question
up vote 5 down vote accepted

The problem comes from rasterization by MorphologicalComponents. Maybe you could avoid it at the first place.

mapdata = Import["", "Data"];

regiondata = "Geometry" /. mapdata[[2]];
departdata = "Geometry" /. mapdata[[1]];

regionNum = Length@regiondata

t1 = Graphics@{
   Rectangle[{-6, 41}, {9.7, 51.5}],
       Rescale[#2[[1]], {1, regionNum}]], #1} &, regiondata]

t3 = Graphics@{Black, EdgeForm[Thin], FaceForm[], departdata}

Show[{t1, t3}]

enter image description here

share|improve this answer
Why not {RGBColor[0, 0.33, 0.643], Rectangle[{-6, 41}, {-0.77, 51.5}], White, Rectangle[{-0.77, 41}, {4.47, 51.5}], RGBColor[0.98, 0.235, 0.196], Rectangle[{4.47, 41}, {9.7, 51.5}]}? – wxffles Jun 5 '13 at 2:46
@wxffles That is a good call. But the background is not the topic concerned in the question, nor it is a hard job, so I'll just let it be what it's in the question. ps. I just found some interesting historical evolution about the proportion of the 3 bars's widths. – Silvia Jun 5 '13 at 3:44
+1 nicely done Silvia – Vitaliy Kaurov Jun 5 '13 at 4:57
@VitaliyKaurov Thanks:) – Silvia Jun 5 '13 at 5:01
@wxffles I concur - the Black background is from my attempts to use MorphologicalComponents and I'm happy to have a solution that avoids that problem. Thanks Silvia. – bobthechemist Jun 5 '13 at 12:20

The problem is using rectangle at the beginning. Use Background -> Black instead:

t1 = Graphics[{EdgeForm[Thick], 
   FaceForm[White], ("Geometry" /. mapdata[[2]])}, Background -> Black, ImageSize -> 600];

t2 = Colorize[MorphologicalComponents[t1, CornerNeighbors -> False], 
  ColorFunction -> "LightTerrain"];

t3 = Show[Graphics@{Black, EdgeForm[Thin], 
    FaceForm[], ("Geometry" /. mapdata[[1]])}, ImageSize -> 600];

Overlay[{t2, t3}]

enter image description here

share|improve this answer
This is a nice solution however the final object rendered isn't in the original coordinate system. The next step of my project is to overlay the GPS coordinates of a trip log, so I'm hoping to find a solution that maintains the original (lat-long) data. – bobthechemist Jun 4 '13 at 23:58

The following solution proceeds similarly to Vitaliy's, but preserves the coordinates of France's departments:

france = Import["", "Data"];
provinces = Colorize[MorphologicalComponents[
                     Graphics[{EdgeForm[Thick], FaceForm[White],
                               "Geometry" /. france[[2]]}, Background -> Black], 
                     CornerNeighbors -> False], ColorFunction -> "LightTerrain"];

Graphics[{Thin, ("Geometry" /. france[[1]]) /. Polygon -> Line}, 
         Prolog -> {Texture[provinces], 
                    Polygon[ImageScaled /@ {{0, 0}, {1, 0}, {1, 1}, {0, 1}}, 
                            VertexTextureCoordinates -> {{0, 0}, {1, 0}, {1, 1}, {0, 1}}]}]

the departments of France

The black background is a bit unseemly to me, though. I don't know how to replace it with a different color.

share|improve this answer
+1 neat idea to use textures ;) – Vitaliy Kaurov Jun 5 '13 at 4:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.