Category Archives: Solutions

Change SP Object Polygon Rendering Order in R

The Problem

I have a geoJSON file that was made by combining many (as in ~250) geoJSON files each containing a single polygon in R.  The polygons are of varying size and can overlap in space.  When I open the file in a GIS, often the smaller polygons are hidden under the larger ones, making displaying the data challenging, if not impossible.

Map showing few polygons when there should be many

I know there are more polygons than are visible in this area, so they must be hiding under the larger polygons.

Map showing larger polygons obscuring smaller polygons

With transparency set to 60% for each polygon (see the Draw Effects dialog in QGIS for the fill symbol layer), you can see that smaller polygons are hiding under larger ones.

The Goal

I would prefer that the polygons stack up so that the largest is on the bottom and the smallest is on the top.  This requires that I change the rendering order based on the area of each polygon.

The Quick Fix

QGIS has an option to control the rendering order.  Open the Layer Properties; go to the Style tab; check the box for “Control feature rendering order”; click the button on the right with the A Z and an arrow to enter the expression you want (I would order by area for example).

Why isn’t this the right solution for me?  I’m sharing a user-contributed dataset.  One of the goals is that anyone can use it.  When polygons are obscured, it makes the dataset just a little harder to use and understand, which means people won’t like using it.  Another goal is that anyone with a reasonable understanding of GIS can contribute.  If I have to write a bunch of instructions on how to visualize the data before they can add to the dataset, people are less likely to contribute.

Map showing all the polygons expected.

Now I can see all of the polygons because the larger ones are on the bottom and the smaller ones are on top.

My Solution

Hat tip to Alex Mandel and Ani Ghosh for spending a couple of hours with me hammering out a solution.  We used R because I already had a script that takes all of the polygons made by contributors and combines them into one file.  It made sense in this case to add a few lines to this post-processing code to re-order the polygons before sending the results to the GitHub repository.

What you need to know about rendering order & SP Objects

The order in which items in an SP object are rendered is controlled by the object’s ID value.  The ID value is hidden in the ID slot nested inside the polygons slot.  If you change these values, you change the order items are rendered.  ID = 1 goes first, ID =2 goes on top of 1, 3 goes on top of 2, and so on.  So for my case, assigning the IDs based on the area will get me the solution.

How

This Stack Exchange Post on re-ording spatial data was a big help in the process.  Note that every SP object should have the slots and general structure I used here.  There’s nothing special about this dataset.  If you’d like the dataset and another copy of the R code, however, it is in the UC Davis Library’s AVA repository.

#load the libraries you'll need
library(raster)
library(geojsonio)
library(rgdal)

### FIRST: Some context about how I made my dataset in the first place

# search in my working directory for the files inside the folders 
# called "avas" and "tbd"
avas <- list.files(path="./avas", pattern = "*json$", full.names = "TRUE")
tbd <- list.files(path="./tbd", pattern = "*json$", full.names = "TRUE")

#put the two lists into one list
gj <- c(avas, tbd)

#read all the geojson files & create an SP object
vects <- lapply(gj, geojson_read, what="sp")

#combine all the vectors together. bind() is from the raster package.
#probably could just rbind geojson lists too, but thats harder to plot
all <- do.call(bind, vects)

#Change any "N/A" data to nulls
all@data[all@data=="N/A"]<- NA


### SECOND: How I did the sorting

#Calculate area of polygons - needed for sorting purposes
# the function returns the value in the area slot of each row
all@data$area<-sapply(slot(all, "polygons"), function(i){slot(i, "area")})

#add the row names in a column - needed for sorting purposes
all$rows<-row.names(all)

#Order by area - row names & area are needed here
# decreasing = TRUE means we list the large polygons first
all<-all[match(all[order(all$area, decreasing = TRUE),]$rows, row.names(all@data)),]

#add new ID column - essentially you are numbering the rows 
# from 1 to the number of rows you have but in the order of 
# largest to smallest area
all$newid<-1:nrow(all)

#assign the new id to the ID field of each polygon
for (i in 1:nrow(all@data)){
 all@polygons[[i]]@ID<-as.character(all$newid[i])}

#drop the 3 columns we added for sorting purposes (optional)
all@data<-all@data[,1:(ncol(all@data)-3)]

#write the data to a file in my working directory
geojson_write(all, file="avas.geojson", overwrite=TRUE, convert_wgs84 = TRUE)
Advertisements

Growing Cyclamen from Seed

The Backstory

A couple of years ago I bought a cyclamen.  I was having a bunch of people over for Thanksgiving and the patio off the dining room looked like death.  The large volume of red flowers on the Cyclamens at the hardware store was an easy fix.  I’ve heard they are hard to grow and that they usually die after the winter, but I was willing to give it a try.  My new plant went into a large pot visible from the back door in a fairly shady spot.  In the spring, as the flowers died back, I noticed that a couple of them had made seed pods.  Being a plant person, I couldn’t just leave them.  This was an opportunity! So I did a bunch of research online on how to start them from seed.

How NOT to Do It

It’s very difficult, according to the internet, to grow a Cyclamen from seed.  You need cold, and darkness, and it takes a very long time.  I did my best to provide the prescribed conditions, and hoping that since Cyclamens come from a Mediterranean climate like my own, any mistakes would be mitigated.  I was delighted to get 5 sprouts!  I was so careful with them, but only one survived.  I thought perhaps the internet was right and this WAS hard.  It didn’t help that the clamshell plastic container that I used (because the internet said I needed a lid) was shallow and prone to drying out.

IMG_4125

My one surviving plant from my first try. It’s grown a second leaf which makes me very happy.

The Epiphany

I consoled myself with the fact that I could buy more of these festive winter flowerers and the next winter bought myself a cheerful pink and white one.  Like the one before it (which is still going strong, unlike what the internet predicted), it did very well. Both plants had masses of flowers and ended up making heaps of seeds.  I collected them and decided to give it another try.  The seeds I collected were waiting inside for the weather to get cold.  After all, the internet says 104 degree days are not the time to grow a Cyclamen.  Around September, I was examining plant #2 for new leaves.  This summer’s heat hit it hard but it was coming back.  I noticed some small leaves that didn’t look like the big curled new growth on the plant I bought.  They looked like the sprouts I got last year!

IMG_4121

Three of the 5 sprouts that grew themselves in the pot with their mother plant.

You’re kidding me.  All they need is the conditions around their mother plant.  I transplanted the sprouts to their own pot.  A windstorm killed one, but the rest are going strong.

IMG_4130

My cyclamen collection… and a mass of oxallis in the big pot.

Success

I grabbed a glazed pot, filled it with quality potting soil, and sprinkled on some seeds.  The established plants had a layer of leaf litter from the last year’s spent leaves that seemed to provide good protection from the sun and hold in moisture, so I covered the pot with some spent Cyclamen and Oxallis leaves because that’s what I had on hand.

IMG_4129

Leaf litter covering freshly planted seeds.

It took several weeks but I started to see the seeds swell and form small bulbs.

IMG_4123

There! Right in the middle of the photo! It’s a tiny cyclamen bulb.

I knew I had the formula right when I saw that I had a leaf climbing out from the leaf mulch.

IMG_4115

Yes, that’s a leaf, although it kind of looks more like a mung bean sprout…

The key really seems to be keeping them moist.  I let my pots sit in a shallow pool of water so they can wick up what they need.

IMG_4119

Sprouts in progress. In the square pots are attempt #1 and the volunteers. The larger yellow round pot has attempt #2. The small round pots have the last of the seeds planted today.

I’m crossing my fingers that they keep growing.  The second leaf appearing on my plant from attempt #1 is encouraging.  I’m really curious to find out what color flowers they have.  All their leaves are different from their parent plants, so I really can’t say what color flowers I’ll get, if they decide to flower at all.  I imagine I have at least another year, maybe two, to wait.

So the Internet is Wrong…

I guess you can’t trust all the garden know-how posted on the internet.  Here’s a recap of things I’ve learned:

  • Cyclamens aren’t hard to grow, but they do need the right conditions.  Direct summer sun will nuke their leaves, but they can recover. Bright shade seems to be best.
  • Don’t keep them indoors either.  They aren’t house plants.
  • They are not difficult to grow from seed, but again, you need the right conditions.
  • Temperature isn’t really an issue.
  • Moisture seems to be key.  Covering the seeds with leaf litter really helps with this and keeps the light off (if that even matters).
  • A deep, glazed or plastic pot will prevent the pot from drying out.
  • Keep some water in the saucer to provide constant moisture to the seedlings.
  • You don’t need to grow them in a dark cold closet.  It sounded insane when I read it but enough sites said it, so I thought it might be true.

Hiding Laptop Cords with a Serving Tray

Recently I started a new job and with each new job comes a new work space and new equipment.  At my new (to me… 1960s standard university issues Steelcase) desk, I’ve got a sleek new laptop and a heap of cords plugged into a hub that they tell me is called a docking station, which just looks like a heap of cords to me.  It not only looks cluttered, it’s taking up space.  So here’s my solution…

1. Get yourself a serving tray.  Make sure you like the look of the sides and bottom because you’re going to turn it over.  Also, make sure there are handle holes and that the whole edge of the tray is even. Some have handles that stick up and you’re not going to want that.  Finally, make sure it’s deep enough to accommodate the height of your cord mess (I mean docking station).  Mine’s less than an inch tall, so this 2 inch deep tray is great.

img_20170113_1315311

2. Stick your “docking station” (or cord mess) in the middle of that tray and run the wires out of the holes.  I’ve got the mouse and keyboard cords running into the hub from the handle that’s near the center of the desk and the power cords through the handle on the edge of the desk.  The cords that plug into each side of the laptop come out on their respective sides.

img_20170113_122535742_hdr1

3. Now, turn that tray over and hide that mess underneath the tray.  So much better.  Now I only see small lengths of cords and the computer can sit over the cord jumble.

img_20170113_122635078_hdr1

Ok, I need to bring more decorations, but now I can put a potted plant on the corner of the tray with the computer and it won’t be in the way.

img_20170113_131831716_hdr1