Search code examples
pythonpython-3.xmapboxputmapbox-gl-js

Mapbox: Programatically update mapbox dataset from .geojson file


I have a .geojson file (call it data.geojson) which I use to manually update a dataset on mapbox.

Suppose that my data.geojson file is structured as follows:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "suburb": "A",
        "unemployed": 10
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          0,
          0
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "suburb": "B",
        "unemployed": 20
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          1,
          1
        ]
      }

data.geojson is stored locally, and every 12 hours the 'unemployed' property of each feature is updated using another python script that scrapes data from the web.

Currently, in order to update these properties within the online dataset (stored at mapbox.com) I am manually navigating to the Mapbox website and reuploading the data.geojson file. I am looking for a way to accomplish this task pythonically.

Any help would be greatly appreciated!


Solution

  • My solution, in the end, was simply to point the source of the Mapbox layer to the locally stored dataset.geojson file rather than the corresponding dataset stored online at mapbox.com.

    I was able to edit the locally stored dataset.geojson using the 'json' python package. Since the Mapbox layer source was pointing directly to the local dataset, all updates to this local file would then be reflected in the Mapbox layer. This way, there is no need to upload any data to Mapbox.

    @David also posted a helpful solution if you wish to go down that route.