rasterListContourPlot With PlotLegends in Mathematica 9

Jens Nöckel has write functions about (List) Contour Plots with rasterized shading in Mathematica:

http://pages.uoregon.edu/noeckel/computernotes/Mathematica/rasterContourPlot.html

It works well. But in Mathematica 9 it can not work with PlotLegends. How to modify? I can use Grid to combine a result of rasterListContourPlot with a legend, but this lost the link of the graphic with the legend.

rasgrap = rasterListContourPlot[RandomReal[1, {10, 10}], InterpolationOrder -> 3]
mylegend = BarLegend["LakeColors"];

Grid[{{rasgrap, mylegend}}]


rasterListContourPlot can be found in the above link.

-
–  belisarius May 5 '13 at 15:20
The reason my functions don't work with version 9 legends is of course that they were written before version 9. And unfortunately the new legends cause the output of plots to have different Head (Legended instead of Graphics) when you specify a legend. I didn't anticipate this new invention (nor do I think it's a good change). But I'll try to modify my functions eventually. This kind of thing is exactly why I've mainly kept working with version 8 so far. It's too much work to update all my notebooks... –  Jens May 5 '13 at 17:30
I've changed the code at the URL linked above, so the problem mentioned in the question no longer occurs. If the changes work as I hope, it may be best to close this question as too localized. But I'd suggest waiting a little so I can get feedback. –  Jens May 5 '13 at 19:31

The question refers to a function rasterListContourPlot whose purpose was to replace the polygon-based filling of ListContourPlot with a rasterized image while maintaining all line-bases primitives as vector art. The output is a combination of Graphics objects, and there was no provision for Legended wrappers.

Here is a quick fix to the particular function mentioned in the question. It works for the example you're using, and I'll test it on some other examples. Please let me know if this works for you:

rasterListContourPlot[pList_, opts : OptionsPattern[]] :=
Module[{img, cont, contL, plotRangeRule, contourOptions,
frameOptions, rangeCoords},
contourOptions =
Join[FilterRules[{opts},
FilterRules[Options[ListContourPlot],
Except[{Background, Frame, Axes}]]], {Frame -> None,
Axes -> None}];
contL =
ListContourPlot[pList, Evaluate@Apply[Sequence, contourOptions]];
cont = First[Cases[{contL}, Graphics[__], Infinity]];
img = Rasterize[
Graphics[GraphicsComplex[cont[[1, 1]], cont[[1, 2, 1]]],
Options[cont, PlotRange]], "Image",
ImageSize ->
With[{size =
Total[{2, 0} (ImageSize /. {opts}) /. {ImageSize ->
CurrentValue[ImageSize]}]},
If[NumericQ[size], size,
First[WindowSize /. Options[EvaluationNotebook[]]]]]];
plotRangeRule = FilterRules[Quiet@AbsoluteOptions[cont], PlotRange];
rangeCoords = Transpose[PlotRange /. plotRangeRule];
frameOptions =
Join[FilterRules[{opts},
FilterRules[Options[Graphics],
Except[{PlotRangeClipping, PlotRange}]]], {plotRangeRule,
Frame -> True, PlotRangeClipping -> True}];
If[Head[contL] === Legended, Legended[#, contL[[2]]], #] &@
Show[Graphics[{Inset[
Show[SetAlphaChannel[img,
AspectRatio -> Full], rangeCoords[[1]], {0, 0},
rangeCoords[[2]] - rangeCoords[[1]]]},

What I did is to put in a Cases that filters out the actual Graphics on which I then do the manipulations as before, without any changes. In the end, an If statement is used to add the Legended wrapper back if it existed beforehand.