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:
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…
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:
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:
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.