2238 Rate this article:
No rating

Updates to the MAPGRID function

Mark Alonzo
Prior to IDL 8.2, the labels used on the graticule produced by MAPGRID were hardcoded (e.g., 30oE or 15oS). Now, with the LABEL_FORMAT property, you can provide a callback function to create labels with custom formatting. Also, through the LATITUDES and LONGITUDES properties, you can get references for the individual lines of latitude/longitude and change properties on them. These three properties are described in the IDL Help, but I want to elaborate on the documentation with an example. Start by defining a callback function that can be used by MAPGRID:
function make_mapgrid_labels, orientation, location, fractional, default_label
   compile_opt idl2

   if location eq 0 then $
      return, orientation ? ' Equator ' : ' Prime Meridian '

   label = strtrim(round(abs(location)),2)
   prefix = location gt 0 ? '+' : '-'

   return, prefix + label
end
This routine is what MAPGRID will use to define its labels when specified by the LABEL_FORMAT property. It has four required arguments. Orientation is 0 for a line of longitude and 1 for a line of latitude. Location gives the value of the line of longitude or latitude. Fractional and default_label are not used here, but are described in the Help. This callback function assigns eponymous labels for the equator and the prime meridian, while other lines of latitude and longitude are marked with plus and minus signs; e.g., 30oE becomes +30 while 15oS becomes -15. Compile this routine before continuing:
IDL> .compile make_mapgrid_labels
Now call MAP, specifying the callback routine above with the LABEL_FORMAT property, to set up a projection:
IDL> m = map('Cylindrical Equal Area', standard_parallel=-30, $ ; Behrmann
>   center_longitude=15, $
>   limit=[-40,-30,40,60], $
>   label_position=0, $                   ; labels bottom and left
>   label_format='make_mapgrid_labels', $ ; callback for formatting
>   background_color='medium sea green', $
>   title='Behrmann Projection')
With LABEL_POSITION, I chose to position the grid labels on the left and bottom of the map, but I wasn't happy with the appearance of the label for the prime meridian. With the LONGITUDES property, I can get an array of references for all the lines of longitude:
IDL> glon = m.mapgrid.longitudes
IDL> help, glon
GLON            OBJREF    = Array[7]
and change the properties of the line representing the prime meridian. Getting the index of this line from this array of references was a little harder than I'd anticipated. I wrote a quick helper routine to pick it (it has a LOCATION of 0) out of the array of longitudes:
function get_zero_index, line_array
   compile_opt idl2

   vals = list()
   foreach line, line_array do vals.add, fix(line.location)
   return, where(vals.toarray() eq 0, /null)
end
Compile this routine:
IDL> .compile get_zero_index
and use it to get the index of the prime meridian; also change the color of the prime meridan as well as the alignment of its label:
IDL> prime_meridian = glon[get_zero_index(glon)]
IDL> prime_meridian.label_align = 0.0
IDL> prime_meridian.color = 'navy'
For completeness, I performed the same steps to highlight the equator:
IDL> glat = m.mapgrid.latitudes
IDL> equator = glat[get_zero_index(glat)]
IDL> equator.label_align = 0.0
IDL> equator.color = 'navy'
Finish by displaying the continents:
IDL> c = mapcontinents(fill_color='wheat')
Here's my result: An example of customizing map grid labels Please feel free to grab my example code here.

Please login or register to post comments.