0
votes

mapbox-gl-js version: >=0.27.0 <=0.38.0

(1) I have a Feature geojson type of a single polygon adding to my map from a url source.

(2) I also have a FeatureCollection geojson type of ~4k polygons adding to the map also from a url source.


I use turf.intersect between (1) and (2) to select the intersecting polygons of (2).

I then store the intersecting polygons [a sub-set of (2)] to an array.

I then manipulate the values of some properties of those intersecting polygons with simple math.


I now want to update (2) with the resulting values of the array.

I would love to use something like setData to update (2), but that is not possible for a subset which also is not (a) a geojson data object or (b) a url to one as specified in the docs.

Hopefully I have explained this sufficiently. If so, what is the suggested workflow in this case?

1
Your data set (2) should, in the end, be an intersection of the original set (2) and the updated subset?Scarysize
Yes, exactly Scarysize. The updated values of the properties of each object in the subset should replace the original values.Shawn Goulet

1 Answers

1
votes

Mapbox is not good at sharing if you let it load the GeoJSON; see https://github.com/mapbox/mapbox-gl-js/issues/1762 If you want to manipulate the data yourself, you should load it yourself, then pass it to Mapbox. For example:

// load your data via ajax however you want
const promises = {
  polygon: $.get('.../geojson.json'),
  collection: $.get('.../feature-collection.json'),
};

// setup your map
const map = new mapboxgl.Map({...});
map.on('load', () => {
  RSVP.hash(promises).then((data) => {
    map.addSource('my-data', {
      type: 'geojson',
      // transform does intersection and updates,
      // then outputs a GeoJSON formatted object
      data: transform(data.polygon, data.collection),
    });
  });
});