Thursday, April 13, 2017

ArcGIS tile + dynamic layer with automatic switchover

This was another interesting one to puzzle out, and turned out to be surprisingly elegant once I had wrapped my head around it.

  • The client has an ArcGIS Server with a tile service. We set up a Leaflet map for them showing these tiles, a couple of years back.
  • They now have dynamic services. They are not fast enough for production use at far-out zoom levels, but they are available at deeper scales than the tiles.
  • Can we have the map detect that they've zoomed in far enough, and silently switch between the two? Tiles when far-out and dynamic when close-in?
The answer was a comparatively simple wrapper class:

This uses the ESRI-Leaflet adapter, and creates within itself both a L.TileLayer and a L.ESRI.DynamicMapLayer. It then watches the map's zoomend event and does what it should do.

Our needs here were pretty specific, so there are a few things it does and does not support. If needs change, or someone else is interested, maybe this could be developed further.
  • It has a setOpacity() method which will apply to both sub-layers. During this I discovered and had to work around some behaviors of the dynamicMapLayer class, e.g. one cannot set its opacity if it is not in the map at that time (one can with L.TileLayer).
  • Options are passed to both layers as-given, e.g. the *attribution* is the same for both. In this case that's perfect.
Maybe this will fit a need of yours, and maybe it won't. But for me, it was a fun and interesting puzzle, and the clients couldn't be happier.

Monday, December 19, 2016

Scroll-Animated Image-Based Movies

Last week I had an interesting request that was fun to figure out. The GIS folks had put together a series of images forming an animation, as one zooms in on the California coast. Inspired by parallax image effects, they wanted to know if the image animation could be tied to scrolling the page, so the animation plays a frame at a time as you scroll.

We found a few existing toolkits that did this such as ScrollMagic, but found that the animation was not at all smooth, and that it didn't work so well for some of the cases the web designers specifically had in mind. A few of them didn't work on mobile at all.

So I ended up creating my own, and came up with two prototypes you may find interesting.

Scroll-Animated Header

This first prototype, has a top-and-center banner image on the page as with a lot of sites. But the page content locks to the height of your screen and forces scrolling, and the banner animates as you scroll.

The effect is pretty neat, right?

This one was meant as a proof of concept and a demonstration of a technique, and not a redistributable library. Still, if you're interested you can View Source above or hit up the repo:


Turns out that what the web folks really wanted, was for multiple, smaller image-animations to be sprinkled and embedded throughout the page content. The same way you'd normally add some float:right and float:left images throughout your text, they wanted image-animations.

So here's a look at a new jQuery plugin I created: jQuery.Reanimator.

 Unlike the previous one, this is specially designed to work with multiple such animations, and to not make assumptions about the page flow (e.g. no fixed header and no fixed scrolling zone). And it's implemented as a jQuery plugin, so it's almost copy-paste easy to apply to any one or any several such animations.

jQuery.Reanimator sports some neat features as well.
  • Animations can have mousemove and click effects, to play their animations. This provides alternate ways of triggering the animation should scrolling be insufficient.
  • Ability to add padding to the top and bottom of the scrollable area for an image.
  • These options can be set in the constructor, and also in data-reanimator-XXX attributes to override that global setting. Makes it easy to customize individual animations.
  • A "debug mode" to show the scrollable areas, etc. to give insight into testing and tuning the animation for your website.
For more info and to download it for your site, hit up the repo:

Wednesday, November 23, 2016

Many things brewing

Happy harvest festival!

I realized the other day that it's been 5 months since my last posting. Things have been happening, but many of them are still in progress so I've kept quiet. But here's a sampling of some of what I've been working on.

AccordionLegend control for Leaflet


This was actually my prior blog post, back in June. It's a Leaflet control that displays a spiffy collapsing legend, with layers broken into accordion sections, with legend swatches and opacity sliders and all.

Just today, someone reported a bug with it and I fixed that bug, then went on to improve the demo, and add a few more features.

Early Warning System v2

The EWS scrapes project disclosures from several international funding banks such as the World Bank, International Funding Corporation, African Development Bank, Asian Development Bank, and others. The material is reviewed by the International Accountability Project, looking projects that could threaten human rights, e.g. involuntary relocation of a whole town, and connecting with local activists.

I did the original EWS system back in 2011, but they want to clean things up, expand on the system, add support for other folks editing it, etc. Internal goals included some code cleanup, a nicer model structure, better error handling, a nicer search UI, and more. And, this project is in Django, which I just love, so there's a bonus.

The EWSv2 is coming along well, and preliminary feedback is quite positive. We should have the thing launched to the public in a month or three.

Tobacco retailer visualizations, next-generation system


A long-time client of GreenInfo Network, CounterTools has us make interactive web maps of tobacco retailers, inspections and undercover purchases of tobacco, and derivative stats such as "percentage of undercover sales which sold, by county" Our oldest maps with them go back to late 2011 or early 2012, with the latest of the current generation having been set up only a few weeks ago.

I'm working on the next generation of both the data management and mapping components. The project involves 3 agencies for 3 sections: back-end, front-end, and GIS/mapping. The mapping fits in the middle, firmly in both front-end and back-end: data models and tables, file uploads and data processing, data searching and reporting, and front-end cartography.

Unlike the current generation, this one will work with a living dataset, with new retailers and retailer inspections being added daily, so there's a whole new dimension of intelligence in the upload capabilities and error handling, in the calculations and rendering, and so on. Also unlike the current generation, the front-end is in AngularJS, and the back-end is in Django. (yay!)

I've been learning a lot from how the other teams do their job: AngularJS and Sails for the front-end, Django REST Framework for writing new APIs, their particular tactics for layout out the sections of the Django app, and so on. It's been a learning curve there, but I'm appreciating the education.

Coder Commando

I've also been handling the usual flow of miscellaneous requests that aren't large projects, but "my coder quit and I launch in two weeks!" situations. This happens a lot more often than you would imagine; four projects in the last five months, come immediately to mind.

I don't usually blog about these since they're not usually the lengthy and involved projects, but they do keep me busy. Some of my recent ones have involved some neat moving parts:
  • A tool to draw a polygon on a Google Maps map, and derive statistics from census blockgroup data, then download a PDF.
  • Learning about Expression Engine, a CMS from the folks who made CodeIgniter, and writing a bunch of fixes in various pages, some of them maps and some not.
  • A map of farms and farmers' markets statewide, with a nice CMS on the back end where one manages five pages of details about their farm. Notably, the part that lets you upload photos of your farm, has a UI for zooming and cropping the photo, so your 200x100 thumbnail looks the way you want it.

Happy Harvest


So yeah, I've been silent but not idle. I'm told of at least 2 new projects waiting for signatures, and some these projects in progress will likely start going live over the next few months. Once the lid is off, I can describe them in more detail.

Have a nice holiday!

Tuesday, June 14, 2016

New Leaflet control: L.Control.AccordionLegend

Some time back I came up with a custom legend control for Leaflet. Unlike the basic L.Layers control, we wanted it to be really snazzy:
  • The layer list broken into sections, not by basemap/overlay, but thematic similarity: Parks in a section, Admin Boundaries as a section, Health Statistics as a section, etc. And the sections should have accordion-style behavior: clicking a section title should expand and collapse, show only one section at a time so as not to overload the screen, and so on.
  • Each layer has an opacity slider.
  • Each layer has a legend.
So, here it is:


Friday, June 3, 2016

New Leaflet control: L.Control.CartographicScale

A request we get now and then, is to add a readout of the current map scale denominator, e.g. a readout that changes from "1:24K" to "1:12K" as you zoom in a notch. These are handy for cartographers who still think in terms of scale, instead of zoom levels.

OpenLayers had the handy-dandy OpenLayers.Control.Scale but I couldn't find a similar such control for Leaflet. So I wrote it.

I should point out that the accuracy of this sort of thing, has always been dubious. Between the projection itself (a "square degree" in Lagos is not the same area as in Barrow, and Greenland is not that big in real life) and the realities of screen resolution (96dpi is for mid-90s CRTs) these scale readouts have always had very poor accuracy and always will. It's the mathematics of a world that isn't flat, you know?

Still, the control is accurate enough for cartographers to get the sense of "2K or 20K?" and that's what was really needed here.

Friday, May 20, 2016

In-Browser filtering of raster pixels in Leaflet, Part 3

In my last posting, I mentioned described the process of creating a three-band TIFF and then slicing it into three-band PNG tiles for display in Leaflet. The tiles are not visually impressive, being almost wholly black, but they have the proper R, G, and B codes corresponding to the location's PHZ, PCP, and GEZ. (if you don't know what those are, go back and read that post from a few days ago)

So now the last step: transforming these tiles based on a set of arbitrary RGB codes determined by the user. Let's go through the working source code for the L.TileLayer.PixelFilter and I'll describe how it works.

First off, we need two RGBA codes so we can color the pixels as hit or miss, and we need the list of [r,g,b] trios that would be considered a match. The initialize method accepts these as constructor params, and the various set functions allow this to be set later as well.

(The decision of what list of RGB codes should be passed into setPixelCodes() is a matter of business logic: selectors, clicks, lists... outside the scope of this discussion. See the demo code for a trivial example.)

Next, we need to intercept the map tile when it's in the browser. Thus the
tileload event handler set up in the constructor, which runs the tile through
applyFiltersToTile() to do the real work.

applyFiltersToTile() is the interesting part, which only took me a couple of hours sitting down with some HTML5 Canvas tutorials. Let's dissect it one piece at a time:
  • "copy the image data onto a canvas" The first paragraph creates a Canvas of the same width and height as the image, and copies the data into it. The IMG object itself won't let us access raw pixel data, but once it's in a Canvas we can.
  • "target imagedata" We then use createImageData() to construct a new and empty byte sequence, which can later be assigned back into a Canvas using putImageData() This is effectively a straight list of bytes, and as we write to it, we need to always write 4 bytes at a time for every pixel: R, G, B, and A.
  • "generate the list of integers" Before we start looping over pixels, a performance optimization. Arrays have the indexOf() method so we can determine whether a given value is in an array, but it only works on primitive values and not three-element arrays. The lodash library has findIndex() which would work, but it means a new dependency... and also the performance was not so great (256 x 256 = 65536 pixels per tile). So I cheat, and translate the list of desired
    pixelCodes into a simple list of integers so that indexOf() can work after all.
  • "iterate over the pixels" Now we start looping over the pixels in our Canvas and doing the actual substitution. Again, we step over the bytes in fours (R, G, B, A) and we would assign them into the output imagedata in fours as well. Each pixel is translated into an integer, and if that integer appears in the pixelcodes list of integers, it's a hit. Since indexOf() is a native function it's pretty swift, and since our pixelcodes list tends to be very short (10-50 values) the usually-avoided loop-in-loop is actually quite fast.
  • "push a R, a G, and a B" Whatever the results, we push a new R, G, B, A set of 4 bytes onto the output imagedata.
  •  "write the image back" And here we go: write the imagedata sequence back into the Canvas to replace the old data, then reassign the img.src to this Canvas's base64 representation of the newly-created imagedata. Now the visible tile isn't based on a HTTP file at all, but from a base64-encoded image in memory.
The only gotcha I found, was that upon calling setPixelCodes() the tiles would flash to their proper color and then instantly all turn into the no-match color. It worked... then would un-work itself?

The tileload event wraps the IMG element's load event. This means that when I assigned the img.src to replace the tile's visible representation... this was itself a tileload event! It would call applyFiltersToTile() again, and this time the RGB codes didn't match anything on my filters so all pixels were no-match. Worse, the assignment of img.src was again a tileload event, so it was in an infinite loop of processing the tile.

Thus the already_pixel_swapped flag. After processing the IMG element, this flag is set and
applyFiltersToTile() will skip out on subsequent runs on that IMG element. If we need to change pixel codes via setPixelCodes() that calls layer.redraw() which empties out all these old IMG elements anyway, replacing them with fresh new ones that do not have already_pixel_swapped set.

So yeah, it was an educational day. Not only did we achieve what the client asked for (the more complex version, not the simplified case I presented last week) and as a neat reusable package, but the performance is near instant.

Tuesday, May 17, 2016

In-Browser filtering of raster pixels in Leaflet, Part 2

A few days back I posted a link to L.TileLayer.PixelFilter This is a Leaflet TileLayer extension which rewrites the tiles after they have loaded, comparing each pixel against a set of pixel-codes and replacing the pixel with either a "matched" or "not matched" color. It's pretty useful for generating dynamic masks and highlights, if your back-end data is a raster and not vector data.

I had said that the client's request was to display plant hardiness zones, and that I was using the Unique Values colors as the RGB codes to match against zone codes. I lied. The reality was much more complicated than that, but would have been distracting from the end and the result. So here's the longer story.

The Rasters and the Requirement

The client has 3 rasters: Plant Hardiness Zones (PHZ), Precipitation classifications (PCP), Global Ecoregions (GEZ). Each of these is a worldwide dataset, and each has about 20 discrete integer values numbered 10 to 30ish.

The user selects multiple combination of these three factors, e.g. these two:
  • Zone 7 (PHZ=7)
  • Rainfall 20-30 inches per year (PCP=3)
  • Temperate oceanic forest (GEZ=31)

  • Zone 7 (PHZ=7)
  • Rainfall 20-30 inches per year (PCP=3)
  • Temperate continental forest (GEZ=32)
The user would then see any areas which match any of these "criteria trios" highlighted on the map. The idea is that a plant that's invasive in these areas, would also be invasive in the other areas highlighted on the map.

Fortunately, the rasters are not intended to be visible and do not need to be visually pleasing. They need to be either colored (the pixel matches any of the trios) or else transparent (not a match).

Raster To Tiles To Leaflet

Three variables and a need for one raster... sounds like we could have a three-band raster! I used ArcMap's Composite Bands tool to merge the three rasters into a three-band raster. Voila, one TIFF with three-part pixels such as (7, 3, 31) and (7, 3, 32) I just have to keep straight that R is PHZ, G is PCP, and B is GEZ.

Second, I needed to slice this up into map tiles. But it's very important that:
  • The tiles be in a format that preserves R, G, and B exactly. Something GIF or JPEG would be entirely unsuitable since GIF picks a 256-color palette, and JPEG is lossy and fudges together colors. TIFF on the other hand is not viewable in browsers as a map tile. But PNG is just perfect: RGB by default, and viewable in browsers.
  • The tile-slicing process (I picked must also preserve exact RGB values. The default average resampler uses interpolation, so a pixel in between a PCP=3 and PCP=2 would get PCP=2.5 and that's no good! Use the nearest neighbor resampling algorithm, which guarantees to use an existing pixel value.
Slicing up the TIFF into web-read PNGs was pretty straightforward: -z 0-7 -r near -t_srs epsg:3857 threeband.tif tiles

Point Leaflet at it, and I get some nearly-black tiles loading and displaying just as I expected. Well, after I remembered that generates tiles in TMS numbering scheme, so I had to set tms:true in my L.TileLayer constructor.

I downloaded a few of the tiles and opened them up in GIMP and sure enough, that shade of black is actually (7,3,31) The tiles are not meant to be visually attractive... but those preserved RGB codes are beautiful to me.

Client-Side Pixel Detection and Swapping

That's the topic of my next posting: now that I have RGB tiles, how can I intercept them, compare them against a set of RGB codes, and appropriately color/clear pixels...? Turns out it was easier and more elegant than I had imagined.