timwis/leaflet-choropleth

extend for topojson functionality

Closed this issue · 6 comments

is it possible to extend this nice plugin to handle as well topojson?

Hi @TrantorM! Good question. I haven't used topojson in leaflet before, and I can't seem to find an example in the docs where it's done. Can you help me find an example?

An example is described here, where a snippet from Rayn Clark is used to extend leaflet for topojson. Note this edited version with return this to allow the same method chaining used in L.GeoJSON.

Hey @TrantorM thanks so much for submitting the pull request! (#16) I think it's a great idea, but I'm a little concerned about having leaflet-choropleth be the place adds TopoJSON support to Leaflet -- seems like either Leaflet ought to support TopoJSON out of the box, or someone should make a plugin for it to add support. I asked some of the maintainers in the leaflet gitter, and it doesn't sound like there are plans to make it part of core, but they agreed that it probably wouldn't make sense to put it into leaflet-choropleth (if for no other reason than the added file size for all the users who don't need topojson).

Instead, I wonder if there is a way to get leaflet-choropleth to work with topojson by converting the topojson to geojson and then passing it to choropleth? If that's the case, we could document how to do it and hopefully it's just a few more lines of code. What do you think?

const topojson = require('topojson')
const topojsonData = require('./examples/basic_topo/crimes_by_district.json')

const geojsonData = topojson.feature(topojsonData, topojsonData.objects.crimes_by_district)

L.choropleth(geojsonData, {
    valueProperty: 'incidents', // which property in the features to use
    scale: ['white', 'red'], // chroma.js scale - include as many as you like
    steps: 5, // number of breaks or steps in range
    mode: 'q', // q for quantile, e for equidistant, k for k-means
    style: {
        color: '#fff', // border color
        weight: 2,
        fillOpacity: 0.8
    },
    onEachFeature: function(feature, layer) {
        layer.bindPopup(feature.properties.value)
    }
}).addTo(map)

Hi @timwis thank you for the reply. You are completely right, the topojson.feature() conversion outside the L.choropleth() function does the job.

However I would like to use your plugin within R. @bhaskarvk brought it a few weeks ago as a htmlwidget through the leaflet.extra R-package to the R-community. Unfortunatly I couldn't figure out how to do the topojson.feature() conversion within R in a efficient way while calling up the addGeoJSONChoropleth(). So I extended the leaflet.extra R-package with the addTopoJSONChoropleth() where the topojson.feature() conversion is done under the hood in javascript within the function. The topojsonData object in R is always a simple character-string and no parsing has to be done, which is efficient. See the RPubs example.

What do you think?

@TrantorM I would be happy to merge your fork into my leaflet.extras package. Please create a PR if you think it's worthwhile.

Hey folks, going to close this for now, but feel free to re-open if you think warrants further discussion.