mapbox / simplespec-to-gl-style

Converts GeoJSON styled with simplestyle-spec to a GL Style
ISC License
31 stars 6 forks source link

Allow single geojson features #14

Closed bsudekum closed 8 years ago

bsudekum commented 8 years ago

Closes: https://github.com/mapbox/simplespec-to-gl-style/issues/1

Accounts for a single geojson feature and also featureCollections.

/cc @jfirebaugh for :eyes:

jfirebaugh commented 8 years ago

Instead of duplicating code for handling various geometry types, write a recursive function.

function addLayers(geojson, layers) {
    switch (geojson.type) {
        case 'FeatureCollection':
            geojson.features.forEach(function (feature) { addLayers(feature, layers); });
        case 'Feature':
            switch (geojson.geometry.type) {
                case 'LineString':
                    layers.push(makeLayer(geojson, 'LineString'));
                    break;
                case 'Polygon': ... 
                    layers.push(makeLayer(geojson, 'LineString'));
                    layers.push(makeLayer(geojson, 'Polygon'));
                    break;
                default: throw new Error("unknown or unsupported GeoJSON geometry type");
            }
        default: throw new Error("unknown or unsupported GeoJSON type");
    }
}

This will also handle nested FeatureCollections.

bsudekum commented 8 years ago

@jfirebaugh Updated with a cleaner code path