I can plot the elevation data by this code

entity = Entity["AdministrativeDivision", {"Berlin", "Germany"}];
latlon = Transpose[GeoBounds[entity]];
elevation = Reverse[QuantityMagnitude[GeoElevationData[latlon]]];
ListPlot3D[elevation, Boxed -> False, Mesh -> None, Axes -> False, 
 Background -> Black, BoxRatios -> {1, 1, 0.3}, Lighting -> "Neutral",
  RotationAction -> "Clip"]

But as we know, the Berlin is not a rectangle shape, it is like this:


I hope those elevation data inside the shape keep the origin and those elevation data outside the shape become 0. Is it possible?


1 Answer 1


We can do this with a bit of image processing with a mask. For example, use this function to apply a mask to a data matrix:

applyMask[data_, pol_] := ImageData@ ImageApply[0 &, Image[data], Masking -> Graphics[pol, PlotRangePadding -> None]]

It basically constructs an image with the data, then apply the mask, and finally extracts the clipped data back. Now we can insert it in your elevation call:

elevation = Reverse[applyMask[QuantityMagnitude[GeoElevationData[latlon]], entity["Polygon"]]];

and use your same ListPlot3D command:

enter image description here


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.