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["http://gadm.org/data/shp/FRA_adm.zip", "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?


3 Answers 3


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

mapdata = Import["http://gadm.org/data/shp/FRA_adm.zip", "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

  • $\begingroup$ 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}]}? $\endgroup$
    – wxffles
    Jun 5, 2013 at 2:46
  • 1
    $\begingroup$ @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. $\endgroup$
    – Silvia
    Jun 5, 2013 at 3:44
  • $\begingroup$ @VitaliyKaurov Thanks:) $\endgroup$
    – Silvia
    Jun 5, 2013 at 5:01
  • $\begingroup$ @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. $\endgroup$ Jun 5, 2013 at 12:20
  • $\begingroup$ @bobthechemist You're welcome. Glad I can help. $\endgroup$
    – Silvia
    Jun 5, 2013 at 12:23

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

  • 1
    $\begingroup$ 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. $\endgroup$ Jun 4, 2013 at 23:58

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

france = Import["http://gadm.org/data/shp/FRA_adm.zip", "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.


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.