14
$\begingroup$

I would like to plot the world map without the countries' borders and just the coastlines (in Mathematica 8). I need to overlap a temperature contour plot on the world map.

When I plot country data with lines, it is so crowded because of the lines related to the borders. Plotting with polygons does not help, since I cannot overlap the second plot on top of it.

For example in the following command how can I add an option to exclude the borders?

    Graphics[CountryData[#, "Polygon"] & /@ CountryData["Continents"], 
             ImageSize -> 400] /. Polygon -> Line

In other words, I need something nice like this but not with polygons and only the coastlines:

    map = Graphics[{GrayLevel[0.5], AbsoluteThickness[1.6], 
   CountryData[#, {"FullPolygon", {"Equirectangular", {0, 
       180}}}]} & /@ {"Antarctica", "World"} /. Polygon -> Line, 
   Frame -> True, FrameStyle -> Directive[Thick], 
   PlotLabel -> "Precipitation", 
   FrameLabel -> {"Longitude (°E)", "Latitude (°N)"}, 
  Axes -> True, AxesStyle -> Directive[Gray], 
  LabelStyle -> Directive[Black, 18, Bold], PlotRangePadding -> None,
  ImageSize -> 600, 
  GridLinesStyle -> Directive[AbsoluteThickness[2], GrayLevel[0.25]],
  GridLines -> {Automatic, Automatic}, 
  Method -> {"GridLinesInFront" -> True}];
$\endgroup$
5
  • $\begingroup$ It may be easier to just import the outlines from some other source. I found this but Mma won't read the SHP file from there. Maybe you can search around more. There's very detailed data here: ngdc.noaa.gov/mgg/shorelines/gshhs.html $\endgroup$
    – Szabolcs
    Commented Jan 24, 2013 at 19:22
  • $\begingroup$ @Szabolcs I don't know if this way would make it easier. I do not need a very detailed map, just a correct shape of the coastline would be enough. Thanks anyway $\endgroup$
    – Karami
    Commented Jan 24, 2013 at 19:36
  • 1
    $\begingroup$ @Szabolcs The correct Import statement is Import["http://pubs.usgs.gov/of/2006/1187/basemaps/continents/continents.zip", "SHP"]. It works. $\endgroup$ Commented Jan 24, 2013 at 19:40
  • $\begingroup$ @Sjoerd Are you familiar with SHP files (I'm not)? Why do you need to import the ZIP and not the SHP? $\endgroup$
    – Szabolcs
    Commented Jan 24, 2013 at 19:41
  • $\begingroup$ @Szabolcs It's in the SHP page. I read up on that yesterday, because we had a question with SHP data. $\endgroup$ Commented Jan 24, 2013 at 19:51

4 Answers 4

15
$\begingroup$

You can easily do it using CountryData and there's no need to import anything. For this, we'll need an undocumented function, Graphics`Mesh`PolygonCombine, which will be used to "combine" all the countries so that there are no internal borders. Evaluating Graphics`Mesh`MeshInit[] will load the undocumented functions into your context path.

You can then create a map of the world with only the coastline as:

Graphics`Mesh`MeshInit[];
world = CountryData[#, "FullPolygon"] & /@ CountryData["Continents"] // Flatten;
Graphics[{Gray, PolygonCombine@world} /. Polygon -> Line]

$\endgroup$
3
  • 1
    $\begingroup$ great,thanks. I do not understand why there are such functions but not documented. $\endgroup$
    – Karami
    Commented Jan 25, 2013 at 10:19
  • 1
    $\begingroup$ @Karami see here mathematica.stackexchange.com/questions/5940/… $\endgroup$
    – Ajasja
    Commented Jan 25, 2013 at 15:09
  • 2
    $\begingroup$ @Karami Indeed, the link that Ajasja provided is a very good one that lists the pros and cons of using undocumented functions. In this particular case, I don't think these functions will be removed, as they're used a lot internally for generating mesh polygons in the plotting routines. You can also just do this once and save the coast map (the line data) and simply reuse it when you need it instead of building it each time. $\endgroup$
    – rm -rf
    Commented Jan 25, 2013 at 15:27
10
$\begingroup$

One option is to use a different dataset than the one provided by CountryData.

I found this detailed shoreline dataset.

After downloading and extracting the dataset, navigate to the GSHHS_shp/c folder. This contains the lowest resolution data, then

map = Import["GSHHS_c_L1.shp"]

enter image description here

If you are lazy like me, you can just drag the SHP file into the notebook with the mouse.

This gives you a graphics object which you can then manipulate any way you like, for example

Graphics[{EdgeForm[Black], FaceForm[None], Cases[map, _Polygon, Infinity]}]

EDIT Per @Sjoerd's comment you can also use

Import["http://pubs.usgs.gov/of/2006/1187/basemaps/continents/continents.zip", "SHP"]

This is a smaller (much faster to download) dataset.

$\endgroup$
1
  • $\begingroup$ Thank you...useful $\endgroup$
    – Karami
    Commented Jan 25, 2013 at 10:09
3
$\begingroup$

Using those weird longitude values:

Graphics[{Lighter@Orange, 
  Translate[CountryData[#, "SchematicPolygon"], {180, 0}] & /@ 
   CountryData[]}, Axes -> True, Frame -> True,
 Epilog -> {Gray,
   {Style[Text["look, no borders!", {80, -20}], 10],
    Arrow[{{90, -25}, {120, -25}}]},
   {Style[Text["lots of coastline!", {330, 15}], 10],
    Arrow[{{340, 5}, {310, 5}}]}
   }]

map

I always think Antarctica looks daft spread along the bottom. But yes, it's gone missing, melted, or something, don't know why... :)

$\endgroup$
4
  • $\begingroup$ The reason is that Antarctica isn't a country! $\endgroup$ Commented Jan 25, 2013 at 14:16
  • 1
    $\begingroup$ You need to mention Antarctica in your code otherwise it will not be plotted. About your code, sorry, I do not really understand your point.It does not answer the question,does it? $\endgroup$
    – Karami
    Commented Jan 25, 2013 at 14:19
  • $\begingroup$ You can downvote it! :) $\endgroup$
    – cormullion
    Commented Jan 25, 2013 at 14:20
  • $\begingroup$ You are welcome :) $\endgroup$
    – Karami
    Commented Jan 25, 2013 at 14:48
2
$\begingroup$

Using the method suggested in answer 2 by @rm -rf, and another post about how to define the Longitude from 0-360 (define the range of Longitude from 0-360 in the World plot from CountryData), I wrote the following code to plot the world map:

    map1 = CountryData[#, {"FullPolygon", {"Equirectangular", {0, 
    180}}}] & /@ {"Antarctica", "World"};

rm -rf

    Graphics`Mesh`MeshInit[];

    map2 = Graphics[{Gray, PolygonCombine@map1} /. Polygon -> Line, 
    Frame -> True];

<

Chris Degnen

    linepos = First[Position[map2, Line]];
    line = Extract[map2, Most[linepos]];
    newline = line /. {a_Real, b_} :> {a + 180, b};

<

    map3 = ReplacePart[map2, Most[linepos] -> newline]
$\endgroup$

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.