Thursday, December 6, 2012

Automating TileMill in Windows

I have a need to live update TileMill projects similar to this poster's question.  I thought I had a solution when I found projectmill (a node.js project) but then I ran into a few problems.  Did I mention I am running on Windows 7?

(I discuss the challenges first, so if you are just looking for an answer, jump to the end of the post)

node-sqlite3 build problems

projectmill is a node.js project that has some dependencies.  npm install tried to install and build projectmill's node-sqlite3 dependency.  It turns out that I didn't have the correct Visual Studio build environment on my box and therefore the build didn't work.  Looking at projectmill's use of sqlite3, I discovered that it only used sqlite to update some metadata in the MBTiles file.  So instead of taking the time to learn all I needed to about Visual Studio to solve a relatively small part of projectmill, I cloned projectmill and tweaked my version to not require node-sqlite3.  Then I refactored the code to use the "shell command" approach (click the images to see full size)...



So that worked, but then I ran into the next problem.

projectmill not compatible with windows

Projectmill was likely never meant for Windows OS, but I found out the hard way.  The application does things like copying TileMill project files.  Unfortunately, it is expecting Unix-style pathnames and Windows lettered drive names cause it to break.  I could code around that, but then the next problem was that projectmill tried to launch tilemill with the Unix nice wrapper.  Again, this is not available in Windows, so that broke, too.  I could code around that, too, but it seemed that I was modifying too much of projectmill.

So I decided to take another approach.

Calling TileMill directly from Windows command line

I discovered that you can call TileMill directly from the Windows command line.  The important thing to remember is that you need to use the node.exe found in <TileMill Program Directory>\tilemill and not the node.exe that you might have installed for your other node projects.

This Windows bat code snippet gives an example of how you can construct calls to TileMill to automate rendering and upload processes.

The projectmill application does basically the same thing the bat code does, but wraps it in a node.js framework.  It probably wouldn't take much to create a Windows-friendly node.js or python wrapper for TileMill automation.  The command line parameters for uploading to MapBox require a --syncAccount and --syncAccessToken  While the syncAccount is easy to know, it is not very easy to determine your syncAccessToken.  In a subsequent post I will show how to tweak TileMill to show that information more clearly to you.


Automating Tile Generation with TileMill over at the Fuzzy Tolerance blog gives additional details.