Loadjson

Markup: {{loadjson}}

Quick start

The example below, running in the Mappia calculator - click it to run it live. Full-size picture

  1. Run it

    Click the picture: the map opens right here and runs the example. Nothing is saved, and nothing to install.

  2. Try it
    • Read the panel: each geological era in the JSON is listed with its area, largest first.
    • In afterCalc, change the sort to areas[a] - areas[b] and run it again: the smallest era comes first.
    • Point url at your own JSON of names and numbers and run it again: the list shows your data.
  3. Make it yours

    Copy the query, change it - the key parameters are below - and run it again in the playground, or paste it into the Mappia editor to save it as your map.

Key parameters

ParameterExampleWhat it does
url/theme/app/data/geologia_area.jsonWhere the JSON is downloaded from; relative paths resolve on the Mappia server. Write = inside a query string as \=.
idareasNames the result: your callbacks read the parsed object as inputs.id["areas"].
corstrueDownloads through the Mappia proxy, for servers that do not send CORS headers.
valueinputs.id["areas"]What the callbacks receive: the parsed JSON, an object or an array. An empty response gives [].
content<div id="area_list"></div>HTML next to the tag. Give an element an id so your callback can write the result into it.

Every parameter, with its type and default, is in the reference at the end of this page.

Complete example

The query 26 lines · runs as is
// LoadJson: downloads JSON before the layer calculates; its input value is the parsed object.
// Here, the area of each geological era, listed in the panel.
[
  {
    title: "Geology - area by era",
    name: "CSR:geologia",
    source: "calculate",
    opacity: 0.8,
    visibility: true,
    paramsButtonConfig: [{ type: "query", pressed: true }],
    descriptionHtml:
      "{{loadjson|id=areas|url=/theme/app/data/geologia_area.json}}" +
      '<div id="area_list" style="font-size:12px"></div>',
    // afterCalc runs once the expression's values are computed, so the layer needs one
    expression: function (layersVals) {
      return layersVals[0];
    },
    afterCalc: function (inputs) {
      var areas = inputs.id["areas"]; // { "Cenozóico": 4984826.16, ... } in km2
      var rows = Object.keys(areas).sort(function (a, b) { return areas[b] - areas[a]; });
      document.getElementById("area_list").innerHTML = rows.map(function (era) {
        return "<b>" + era + "</b>: " + ExtjsUtils.NUMBER.abbreviateNumber(areas[era]) + " km&sup2;";
      }).join("<br>");
    },
  },
];

Customize it

Where you can read the object

The parsed JSON is available as inputs.id["areas"] (the id you chose) in beforeCalc, onInputsReady and afterCalc, and as this.getInputs().id["areas"] in the layer’s functions. The layer waits for the download before it calculates, so the object is always complete when you read it.

afterCalc runs only for a layer that has an expression. Without one nothing is calculated and afterCalc never runs - that is why the example has an expression that just returns layersVals[0].

What arrives

Exactly what JSON.parse makes of the file: an object or an array. JSON carries only plain data, so a date arrives as a string and there are no functions. If the server answers with an empty body, the value is [].

Write the result into the panel

Put an empty element with an id in descriptionHtml, next to the tag, and fill it from your callback. afterCalc runs again after every calculation round, so replace the content (innerHTML = ...) instead of appending to it, as the example does.

Use the data inside the calculation

expression runs in a worker, where inputs.id does not exist. The inputs reach it as a plain array in markup order, and {{loadjson}} takes a position like any other input:

descriptionHtml: "{{loadjson|id=weights|url=/data/weights.json}}",  // { "1": 0.5, "2": 2 }
expression: function (layersVals, inputs) {
  var weights = inputs[0];               // the parsed JSON, read by position
  return weights[layersVals[0]];
},

This works because parsed JSON is plain data; keep such tables small, since they are copied into the worker together with the pixel values. A {{loadcsv}} table, by contrast, is an object with methods and cannot be used there. The rules are in the execution model.

Files on other servers

If the server does not send CORS headers, add cors=true. Inside a URL, write every = as \= (\\= inside a JavaScript string), or the markup reads it as a new parameter. For a table rather than an object, use Loadcsv.

Real maps that use it

Remote Javascript Object Transfer

Remote Javascript Object Transfer

Remote Javascript Object Transfer

Panorama of Brazil's Forest Code

Panorama of Brazil's Forest Code

Reference: every parameter

Generated from the platform source. Every entry, searchable, is in the API reference; the raw data is api.json. Open an entry for its description, parameters and example; # links to it.

loadjson · load JSON (input)

LoadJson4 entries

Written in a layer's descriptionHtml as {{loadjson|parameter=value|...}}

Tool to load a remote JSON and use it with map, this object can store string, values and functions.
Usage: {{loadjson}}

cors

Boolean= false# Loads the JSON through the Mappia CORS proxy (for servers without CORS headers).

Loads the JSON through the Mappia CORS proxy (for servers without CORS headers).

|cors=true|

id

String# Defines an id for the stored info on layerInputs.

Defines an id for the stored info on layerInputs.

|id=window-div-id|
on javascript: layer.getInputs().id["NAME_DEFINED_HERE"]

url

String# Defines the url to load the json from.

Defines the url to load the json from.

|url=https://maps.csr.ufmg.br/theme/app/data/conab/limites_municipios_conab.geojson|

value

Object|Array# Value stored in inputs.id[ID]: the parsed JSON (JSON.parse of the response body — an object or an array; an empty response gives []).

Value stored in inputs.id[ID]: the parsed JSON (JSON.parse of the response body — an object or an array; an empty response gives []). It is undefined until the download finishes: the layer waits for the resource and recalculates on its waitend event, so beforeCalc/expression can rely on it being loaded.

{{loadjson|id=limits|url=https://maps.csr.ufmg.br/theme/app/data/example.geojson}}
beforeCalc: function(inputs) {
    var geojson = inputs.id['limits'];
    console.log(geojson.features.length);
}

Free HTML

HTML1 entry
Any HTML is possible to be added to the layer description.

content

String# Free HTML inside a layer's descriptionHtml: everything that is not a {{tag}} is passed through to the panel untouched, so a description can carry headings, images, links, tables and the container …

Free HTML inside a layer's descriptionHtml: everything that is not a {{tag}} is passed through to the panel untouched, so a description can carry headings, images, links, tables and the container elements a chart or a custom control needs. Widgets and HTML mix freely in the same string, and the HTML around a widget is rendered before the widget is created, so an element declared here can already be referenced by an id.

Two things to keep in mind. Inside a {{tag|...}} the pipe separates parameters, so HTML that must live in a parameter goes in html= (verbatim to the end of the value) or has its = escaped as \\=. And the description is written inside a JavaScript string in the query, so quotes have to be escaped or alternated as usual.

descriptionHtml:
  '<h3>Deforestation</h3>' +
  '<p>Pick the minimum area to highlight.</p>' +
  '{{slider|id=threshold|minValue=0|maxValue=100|value=20}}' +
  '<div id="chart_area" style="height:220px"></div>'