Smathermather's Weblog

Remote Sensing, GIS, Ecology, and Oddball Techniques

CartoDB, Leaflet, and a little anti-generalization

Posted by smathermather on November 28, 2011

CartoDB is one of two hosted (read: cloud) PostGIS database implementations.  It has a maps API, an SQL API, and is some fun to use.  The other hosted PostGIS implementation is SpacialDB which has a Restful API, but can also take SQL.  I just got my key for the free version of that, so hopefully I will be reviewing use of that in the future too.

I can’t beat Bill Dollins’ post CartoDB + Leaflet = Easy, and I hadn’t played with Leaflet before, but am often up to my eyeballs in OpenLayers, so I thought I’d build off Bill’s post.  I haven’t, well, not substantially anyway.  But I did start to and found some strange behavior in displaying GeoJSON in Leaflet, under the right conditions anyway.  This is what I got:

Difference between properly displayed GeoJSON, and GeoJSON with missing point.

The green line shows the correct line, the dark gray line shows how it’s being displayed noticeably incorrectly by Leaflet.  I was about to post to the CartoDB website when I dug a little further:

The problem goes away as you zoom in...

The problem goes away as you zoom in…

and also as you zoom out...

and also as you zoom out… leading me to conclude it’s not a CartoDB issue, but a leaflet issue, as the GeoJSON geometry is not re-requested based on zoom level.  But is it a bug?  I suspect not.  I suspect it’s a feature– I suspect leaflet is auto-generalizing the geometry of the GeoJSON by dropping the occasional node.  Since we have so few nodes along the southern boundary, it matters here.  Maybe they should use a Douglas-Peucker or similar algorithm, but that may not be practical as it’s difficult to know a priori what the tolerance value should be (of course, the Leaflet user might know what value to use, however…).  I’ll post to the leaflet forum tomorrow to find out.  In the mean time, my fix is simple.  Instead of a simple load of the CartoDB geometries:,%20the_geom%20FROM%20boundary&format=geojson&callback=?

We’ll use the SQL API to complicate the geometry on the fly with ST_Segmentize, which will add extra nodes in anticipation of Leaflet simplifying it:,%20ST_Segmentize%28the_geom,%200.0001%29%20AS%20the_geom%20FROM%20boundary&format=geojson&callback=?

Yup.  It’s a hack.  I’ll have to find out if there are any alternative (less hackish) solutions.

BTW, rockin’ the CartoDB, but hate thinking in geographic to do segmentization.  In market fairness, I’ll try something similar with SpacialDB soon.

5 Responses to “CartoDB, Leaflet, and a little anti-generalization”

  1. Hi Stephen,

    Thanks for reporting the findings about CartoDB. Already hear about it some time ago but it slipped way from my radar. I’ll putted on my todo list for one of my academic works.

    Tell me, did they take too long to provide you with an access?

    • No, not too long. I think I requested right after FOSS4G in Denver, and they sent me access 2-3 weeks later– but that was when they were still releasing slowly and testing capacity.

  2. The Leaflet folks have found and fixed this bug. Nice job folks.

  3. […] I posted this post about an apparent problem in rendering of GeoJSON in Leaflet, and now it’s fixed a week […]

  4. […] an earlier post, I demonstrated the use of Leaflet in conjunction with CartoDB. What’s cartodb? From their […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: