[ncl-talk] Labeling Shape File Segments Individually

Rick Brownrigg brownrig at ucar.edu
Fri Sep 9 14:48:28 MDT 2016


Hi Sean,

I see the issue -- gsn_add_shapefile_polylines is drawin all the lines, not
leaving any opportunity to change the value of gsLineLabelString.  (BTW:  I
couldn't run your script -- NCL is complaining the shapefile is corrupt?
GDAL seems to be able to at least peak into it however. Not sure what's
going on here).

One thing that comes to mind is to draw the lines yourself, much the way
gsn_add_shapefile_polylines does under the hood. The code for that looks
something like this (UNTESTED, but comes straight out of the 1st example at
http://ncl.ucar.edu/Applications/shapefiles.shtml):

  ; get needed info out of shapefile
  shapefile = "mp143-mpua5-contour.shp"
  f = addfile(shapefile,"r")

  segments = f->segments
  geometry = f->geometry
  segsDims = dimsizes(segments)
  geomDims = dimsizes(geometry)
;
; Read global attributes
;
  geom_segIndex = f at geom_segIndex
  geom_numSegs  = f at geom_numSegs
  segs_xyzIndex = f at segs_xyzIndex
  segs_numPnts  = f at segs_numPnts

  lines       = new(segsDims(0),graphic)   ; Array to hold polygons
  numFeatures = geomDims(0)

  lon   = f->x
  lat   = f->y

; ******** your code here ******************

  ; draw the lines...

  segNum = 0
  do i=0, numFeatures-1
     pres at gsLineLabelString = labels(i)  ;; <<========
     startSegment = geometry(i, geom_segIndex)
     numSegments  = geometry(i, geom_numSegs)
     do seg=startSegment, startSegment+numSegments-1
        startPT = segments(seg, segs_xyzIndex)
        endPT = startPT + segments(seg, segs_numPnts) - 1
        lines(segNum) = gsn_add_polygon(wks, map, lon(startPT:endPT),  \
                                                   lat(startPT:endPT), pres)
        segNum = segNum + 1
     end do
  end do




I hope this helps...
Rick


On Fri, Sep 9, 2016 at 11:58 AM, Sean Egan <sdegan at alaska.edu> wrote:

> Dear NCL Community,
>
> I have a shape file containing 3 polyline segments. Each segment is a
> "contour" level and these level values are stored in the CONTOUR variable.
> See the description of the shape file below.
>
> ncl 0> f = addfile("mp143-mpua5-contour.shp","r")
> ncl 1> print(f)
>
> Variable: f
> Type: file
> filename:    mp143-mpua5-contour
> path:    mp143-mpua5-contour.shp
>    file global attributes:
>       layer_name : mp143-mpua5-contour
>       geometry_type : polyline
>       geom_segIndex : 0
>       geom_numSegs : 1
>       segs_xyzIndex : 0
>       segs_numPnts : 1
>    dimensions:
>       geometry = 2
>       segments = 2
>       num_features = 3  // unlimited
>       num_segments = 3
>       num_points = 149
>    variables:
>       integer geometry ( num_features, geometry )
>
>       integer segments ( num_segments, segments )
>
>       double x ( num_points )
>
>       double y ( num_points )
>
>       string CONTOUR ( num_features )
>
> When I plot these shape files, I want to label each segment with the
> contour level. I am trying to do this by setting labels=f->CONTOUR and then
> setting the gsLineLabelString resource equal to "labels". This, however,
> labels each segment with the first entry in labels (labels(0)), instead of
> each segment with each value. See the description of labels below.
>
> ncl 5> print(labels)
>
>
> Variable: labels
> Type: string
> Total Size: 24 bytes
>             3 values
> Number of Dimensions: 1
> Dimensions and sizes:    [num_features | 3]
> Coordinates:
> Number Of Attributes: 0
> (0)    1,000
> (1)    10
> (2)    100
>
> So in the output, each segment is given a value of 1,000.
>
> Is there a way that I can either 1) label each segment with the values in
> labels or 2) convert the polylines to polygons and shade each a different
> color?
>
> The NCL script is below (and is attached) as well as the shape files.
>
> Thanks again for your help!
> Sean
>
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/wrf/WRFUserARW.ncl"
> load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
>
> begin
>
>   shapefile = "mp143-mpua5-contour.shp"
>   f = addfile(shapefile,"r")
>
>   b = addfile("wrf_redoubt_domain"+".nc","r")
>
>   wks = gsn_open_wks("x11","testing")
>
>   lat = f->y
>   lon = f->x
>
>   gsn_define_colormap(wks,"WhBlGrYeRe")
>   res                         = True
>   res at gsnMaximize             = False
>   res at gsnDraw                 = False        ; Turn off for now.
>   res at gsnFrame                = False        ; Will draw later
>
>   res at tiMainOn                = False
>
>   mpres                              = res
>   mpres                              = wrf_map_resources(b,mpres)
>   mpres at mpOutlineBoundarySets        = "GeophysicalAndUSStates"
>   mpres at mpFillOn                     = False
>   mpres at mpOutlineOn                  = True
>   mpres at mpCountyLineThicknessF       = 2.5
>   mpres at mpGeophysicalLineThicknessF  = 2.5
>   mpres at mpNationalLineThicknessF     = 2.5
>   mpres at mpProvincialLineThicknessF   = 2.5
>   mpres at mpUSStateLineThicknessF      = 2.5
>   mpres at mpCountyLineColor            = "Black"
>   mpres at mpGeophysicalLineColor       = "Black"
>   mpres at mpNationalLineColor          = "Black"
>   mpres at mpUSStateLineColor           = "Black"
>
>   mpres at mpRightCornerLonF = 215.0
>   mpres at mpRightCornerLatF = 64.5
>   mpres at mpLeftCornerLonF  = 200.0
>   mpres at mpLeftCornerLatF  = 55.5
>
>   print(mpres)
>
>   map = gsn_csm_map(wks, mpres)
>
>   labels=f->CONTOUR
>
>   pres                               = res
>   pres at gsLineColor                   = "Red"
>   pres at gsMarkerIndex                 = 16
>   pres at gsMarkerColor                 = "Red"
>   pres at gsLineLabelString             = labels
>
>   poly = gsn_add_shapefile_polylines(wks,map,shapefile,pres)
>
>   draw(map)
>   frame(wks)
>
> end
>
>
> --
> PO1 Sean D. Egan, US Navy
> PhD Candidate
> Geophysical Institute
> University of Alaska Fairbanks
> 903 Koyukuk Drive
> Fairbanks, AK 99775
> sdegan at alaska.edu | 907 474-5483
> IARC Office 334 | www.gi.alaska.edu/~sdegan
> <http://www.gi.alaska.edu/%7Esdegan>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20160909/9add5991/attachment.html 


More information about the ncl-talk mailing list