Quick start
The example below, running in the Mappia calculator - click it to run it live. Full-size picture
- Run it
Click the picture: the map opens right here and runs the example. Nothing is saved, and nothing to install.
- Try it
- Click the Shapefile field and pick a .shp file in latitude/longitude (EPSG:4326): its shapes are drawn on the map.
- 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
| Parameter | Example | What it does |
|---|---|---|
id | loadshp | Names the field: inputs.id['loadshp']() returns the chosen File, or undefined before the user picks one. |
eventNames | _onChange=onSelectFile | Runs a function on a field event: write _on plus the event name. _onChange fires when the user picks a file. |
fieldLabel | Shapefile (.shp) | Label shown at the left of the file picker. |
ignoreUpdate | true | Stops a new file from recalculating the layer. Your _onChange function still runs. |
value | inputs.id['loadshp']() | What the input holds: a function that returns the file, not the file itself. Call it on the page only. |
Every parameter, with its type and default, is in the reference at the end of this page.
Complete example
The query 22 lines · runs as is
// FileField: a local file picker. Its input value is a getter: inputs.id[ID]() returns the File.
[
{
title: "Upload a shapefile",
name: "uploaded_shapefile",
source: "file",
type: "empty",
visibility: true,
paramsButtonConfig: [{ type: "query", pressed: true }],
descriptionHtml:
"{{label|text=Load a shapefile (.shp) to draw it on the map}}" +
"{{filefield|id=loadshp|fieldLabel=Shapefile (.shp)|_onChange=onSelectFile}}",
functions: {
onSelectFile: function () {
var file = this.getInputs().id["loadshp"]();
if (file) {
ExtjsUtils.GEOJSON.shapefile2GeojsonAsync(file, { layer: this, fromProj: "EPSG:4326" });
}
},
},
},
];Customize it
Read the file
Call the input: inputs.id["loadshp"]() in beforeCalc, or this.getInputs().id["loadshp"]() in a function of the layer (an _onChange callback, a button handler). this is the layer in both. You get the browser’s own File object, the first one selected, or undefined.
Draw a shapefile
ExtjsUtils.GEOJSON.shapefile2GeojsonAsync(file, options) reads the file and adds its shapes to the layer given as options.layer, as in the example. Things to know:
- It reads a bare
.shpfile. It does not open a.zip. - The coordinates must be in EPSG:4326 (latitude/longitude) or EPSG:900913, and
fromProjsays which. A.shpfile does not carry its projection; a wrongfromProjputs the shapes in the wrong place. - To keep the attributes, let the user pick the
.dbfin a second file field and pass that file asdbf. limitCountcaps how many shapes are read.
Other kinds of file
For a CSV, a JSON file or an image, read the File yourself with the browser’s file API in the callback, for example file.text().then(function (text) { ... }). To load a CSV or JSON from a URL instead, use LoadCsv or LoadJson.
Pitfalls
inputs.id["loadshp"]without()is the function, not the file.- The file never reaches
expression. The field still takes a position ininputs, so count it when you read other inputs by position there. _on<event>names ignore case:_onchangeand_onChangeare the same. Any event of the field works, for example_onFocus.