jcarlos
jcarlos

Reputation: 435

How to store the colors of SpatialPolygons in R

The code below is part of a small routine to store the LEGEND of colors used on a map.

The main function is to not use the PLOT function or the LEGEND function, but generate the polygons that form the LEGENDs and save them a shapefile.

contour <- bbox(mapa)
polBorder <- polygonBox(contour, scale=0.0002)
boxBorder <- bbox(polBorder)

polLeg <- list()

xmin <- boxBorder["x", "min"] + 0.0005
ymin <- boxBorder["y", "min"] + 0.0005
sizex = abs(boxBorder["x", "max"]-boxBorder["x", "min"])/20
sizey = abs(boxBorder["y", "max"]-boxBorder["y", "min"])/20

for (i in 1:7)
{
  polygon <- polygonBoxPos2(xmin, ymin, +sizex, +sizey, paste0("p",i))

  polLeg <- append(polygon, polLeg)

  ymin <- ymin + sizey

}

SdP = SpatialPolygons(polLeg)

plot(polBorder, border="black" )
plot(SdP, add=TRUE )
plot(mapa, col=colPalette,  add=TRUE)

polygonBox <- function (bbox, scale=0.0)
{

  bbox["x", "min"] <- bbox["x", "min"] + scale*bbox["x","min"]
  bbox["x", "max"] <- bbox["x", "max"] - scale*bbox["x","max"]

  bbox["y", "min"] <- bbox["y", "min"] + scale*bbox["y","min"]
  bbox["y", "max"] <- bbox["y", "max"] - scale*bbox["y","max"]

  Sr1 = Polygon(cbind(c(bbox["x","min"],bbox["x","max"],bbox["x","max"],bbox["x","min"],bbox["x","min"]),
                      c(bbox["y","min"],bbox["y","min"],bbox["y","max"],bbox["y","max"],bbox["y","min"]))
                , hole=TRUE)
  Srs1 = Polygons(list(Sr1), "s1")
  SdP = SpatialPolygons(list(Srs1))
  return(SdP)
}

polygonBoxPos2 <- function (xmin, ymin, sizex, sizey,id)
{

  xmax <- xmin + sizex
  ymax <- ymin + sizey

  Sr1 = Polygon(cbind(c(xmin , xmax , xmax , xmin, xmin ),c(ymin, ymin, ymax, ymax, ymin)), hole=TRUE)
  Srs1 = Polygons(list(Sr1), id)

  return(Srs1)
}

You can see them at the bottom left.

All the examples I´ve searching set the polygons colors during the PLOT operation, but I want to store the color with the Polygon, how to achieve that?

enter image description here

Upvotes: 1

Views: 449

Answers (1)

Spacedman
Spacedman

Reputation: 94277

Create a SpatialPolygonsDataFrame from your spatial polygons and a data frame constructed from your colours:

spdf = SpatialPolygonsDataFrame(Sr=SdP,
                                data=data.frame(
                                  id=1:length(colPalette),
                                  colour=colPalette,
                                  stringsAsFactors=FALSE),
                                match.ID=FALSE)

then you can plot this into your map by doing:

plot(spdf,col=spdf$colour, add=TRUE)

and then if you save it sing writeOGR to a Shapefile you might be able to use the colour attribute to shade it in whatever package you are loading these things into. I say "might" because its possible your GIS doesn't allow direct specification of colours from attributes, in which case you probably have to build a palette in your GIS, which is why I've also put an id attribute in there too. But anyway, that's beyond the scope of your question now.

Upvotes: 1

Related Questions