color-coding data of SpatialPolygonsDataFrame by ranges

182 Views Asked by At

I have a SpatialPolygonsDataFrame object (polUTM) that contains values at each lat/lon location. I want to color-code on a plot the values per a color scheme. Here is my object and code so far:

polUTM is...
class : SpatialPolygonsDataFrame 
features : 6278 
extent   : 709.9431, 2785.146, 3068.771, 4119.979  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=14 +datum=WGS84 +units=km +no_defs +ellps=WGS84 +towgs84=0,0,0 
variables: 1
names    : layer 
min values  : 35 
max values  : 45 

and

minDbzValue = 35    #yellow; 40 - gold; 45 - orange; 50 - red; 55 - med red; 60 - dk red
ncolors = 7
colors = brewer.pal(n = ncolors, name="Spectral")
colorRanges = 65 - cumsum(rep_len(1,ncolors))*5
plot( polUTM )

which places the data from polUTM on the map in the correct places which have values v = f( lon, lat ) of 35, 40, 45 dBZ as color-coded dots.

Added code for this increment (FWIW).

xmnValue = pixelToLonFxn( ext.rr@xmin, coeff ) 
xmxValue = pixelToLonFxn( ext.rr@xmax, coeff ) 
ymnValue = pixelToLatFxn( ext.rr@ymax, coeff )
ymxValue = pixelToLatFxn( ext.rr@ymin, coeff )
rr2 = raster(nrows=nrow(imageData ), ncols=ncol(imageData ), xmn=xmnValue, xmx=xmxValue, ymn=ymnValue, ymx=ymxValue )
values( rr2 ) = (values( imageData ) - 7 ) * 5
rr2[rr2<minDbzValue]  <- NA
pol < - rasterToPolygons(rr2, fun=function(x){ x >= minDbzValue } )
pol2 = spTransform( pol, "+proj=utm +zone=14 +datum=WGS84 +units=km" )
ptsUTM = SpatialPoints( pol2 )
coords = ptsUTM@coords
n = length( pol2@data$layer )
wxData = data.frame( x = numeric( n ), y = numeric( n ), z = numeric( n ),  c = character( n ), stringsAsFactors = FALSE )
wxData$x = coords[,1]
wxData$y = coords[,2]
wxData$z = pol2@data$layer
wxData$c = colors[ match( wxData$z, colorRanges ) ]
xmnValue = pixelToLonFxn( ext.rr@xmin, coeff ) 
xmxValue = pixelToLonFxn( ext.rr@xmax, coeff ) 
ymnValue = pixelToLatFxn( ext.rr@ymax, coeff )
ymxValue = pixelToLatFxn( ext.rr@ymin, coeff )
rr2 = raster(nrows=nrow(imageData ), ncols=ncol(imageData ), xmn=xmnValue, xmx=xmxValue, ymn=ymnValue, ymx=ymxValue )
values( rr2 ) = (values( imageData ) - 7 ) * 5
rr2[rr2<minDbzValue]  <- NA 
pol <- rasterToPolygons(rr2, fun=function(x){ x >= minDbzValue } )
pol2 = spTransform( pol, "+proj=utm +zone=14 +datum=WGS84 +units=km" )
ptsUTM = SpatialPoints( pol2 )
coords = ptsUTM@coords
n = length( pol2@data$layer )
wxData = data.frame( x = numeric( n ), y = numeric( n ), z = numeric( n ), c = character( n ), stringsAsFactors = FALSE )
wxData$x = coords[,1]
wxData$y = coords[,2]
wxData$z = pol2@data$layer
wxData$c = colors[ match( wxData$z, colorRanges ) ]

CONUS honey-comb, Doppler wx radar (35-45 dBZ), flight (BOS to ATL)

0

There are 0 best solutions below