What would a minimal example for a choropleth map in Mathematica look like?
I can read in a ESRI Shapefile using Import
, but do not know how to work with th
Because I cannot resist a Code Golf competition with belisarius:
Graphics[{Hue[i~#~"Area"/10^7],i~#~"Polygon"}~Table~{i,#[]}&@CountryData]
(for the same result)
Graphics[
{
ColorData["ThermometerColors"][
Rescale[CountryData[#, "GDPPerCapita"], {100, 50000}]
] /. HoldPattern[Blend[___]] -> Yellow,
CountryData[#, "Polygon"]
} & /@
CountryData[]
]
And why the replacement? If there are no data of the required type for a given country CountryData returns Missing["NotAvailable"], causing ColorData
, and its underlying Blend
function not to return a specific RGB value. I replace this unevaluated Blend
with the color Yellow.
Just for reference, here some tips for working with ESRI Shapefiles. CountryData
does not provide county-level data for Germany (the administrative unit is called "Kreis"), which is why I wrote my own KreisData
function. The shape file I used can be downloaded for free, however there are terms of use to consider.
The KreisData
function is then created as follows:
shp = Import["C:/TEMP/map/VG2500/vg2500_krs.shp", "Data"];
polys = "Geometry" /. First[shp];
ags = "RS" /. ("LabeledData" /. First[shp]);
names = "GEN" /. ("LabeledData" /. First[shp]);
area = "SHAPE_AREA" /. ("LabeledData" /. First[shp]);
KreisDataRules =
Dispatch[MapThread[
Rule[#1, #2] &, {ags, Transpose[{polys, area, names}]}]];
KreisData[tag_String, "Polygon"] := First[tag /. KreisDataRules];
KreisData[tag_String, "Area"] := Part[tag /. KreisDataRules, 2];
KreisData[tag_String, "Name"] := Last[tag /. KreisDataRules];
KreisData[] := ags;
With this function, and the example code by Sjoerd C. de Vries, a map of Germany is created thus:
renderMap[scheme_String] :=
Graphics[{ColorData[scheme][
Rescale[KreisData[#, "Area"], {3.63067036816521*10^7,
3.08469540395003*10^9}]] /.
HoldPattern[Blend[___]] -> Yellow, KreisData[#, "Polygon"]} & /@
KreisData[]];
Manipulate[renderMap[s], {s, ColorData["Gradients"]}]
A throw on Minimal in the code golf sense:
Graphics@Function[f,{Hue[f[#,"Area"]/10^7],f[#,"Polygon"]} &/@ f[]]@CountryData
@Karsten W.: unfortunately your shp file is not longer available. I tried a similar one (vg250_0101.utm32s.shape.ebenen\vg250_ebenen\vg250_krs.shp) from the same source but I got the error message "Transpose: the first two levels can not be transposed" from your function KreisDataRules. And since I did not really understand what your code does, maybe you can help. I try to produce a thematic map on a Kreis level, where the colouring shows the number of tourists in our city, if necessary also 0. I would be very grateful for any help.