21

What exactly does a layer represent in the Leaflet Mapping Library?

Conceptually, to me a layer would represent a single tier of some type of feature or object; for example all image tiles representing the base level map would be represented on a single layer, a set of polygons representing states in the US may be on their own separate layer.

Specifically looking at L.GeoJSON.addGeoJSON(geojson), it reads that each new polygon created is placed in it's own layer (and then maybe merged with the layer you're calling the method on?). My use case is that I need to add many geoJSON objects one at a time and want to ensure I'm not creating many unnecessary layers (or if I am, if this is actually a bad thing).

Thank you.

meetar
  • 7,443
  • 8
  • 42
  • 73
oli
  • 3,541
  • 1
  • 27
  • 34

2 Answers2

32

In Leaflet anything that can be added to the map is a layer. So polygons, circles, markers, popups, tiles are all layers. You can combine layers in a L.LayerGroup (or FeatureGroup), if you for example want to treat a set of polygons as a single layer. So maybe your interpretation of layers matches better with what is modelled by L.LayerGroup in Leaflet.

L.GeoJSON is a LayerGroup (specifically a FeatureGroup) that is initialized from GeoJSON. Each new polygon is added to the L.GeoJSON LayerGroup using addLayer, which is the method for adding anything (that is a layer) to a LayerGroup. It does not create a new layer for each polygon (other than the L.Polygon which is already considered a layer). It only creates new FeatureGroups (LayerGroups) for a GeometryCollection and MultiPoints, which (I assume) is in order to preserve the structure from the GeoJSON.

If you want to add geoJSON objects to the same LayerGroup one at a time, you can just call L.GeoJSON.geometryToLayer to convert your GeoJSON object, and then add it to your LayerGroup using L.LayerGroup.addLayer.

Asbjørn
  • 1,212
  • 1
  • 11
  • 15
  • 1
    This is a great answer, thanks. Just to be clear, `L.GeoJSON` is actually a layer group (a group of other layers) as opposed to a layer in itself? Nit picky,but just for complete clarity if others read this. – oli May 06 '12 at 04:27
  • @oli thanks and you are right, L.GeoJSON is a LayerGroup, but I guess a LayerGroup could also be considered a layer (depending on definition). Anyway, I updated the answer for clarity. – Asbjørn May 07 '12 at 12:58
0

As you mentioned, "layer" is a concept coming from a wider scope than simply the Leaflet implementation.

It is an abstract concept of "collection" in the context of Geospatial data.
A tier is also an alternative name, but I see "layer" being used more, in several standards & technologies.

The first lines here describe it simply enough:
https://doc.arcgis.com/en/arcgis-online/reference/layers.htm


In the context of Leaflet you can have as many layers as you want and it is not necessary to "spare" them.
More than thinking to optimization of the technical implementation, I'd put effort more in trying to identify "layers" (according to your business domain) as logical-groups of geospatial data that belongs together.

Specifically looking at L.GeoJSON.addGeoJSON(geojson), it reads that each new polygon created is placed in its own layer (and then maybe merged with the layer you're calling the method on?).

  • one Leaflet layer => one GeoJSON Feature (or set of Feature, given that FeatureCollection extends Feature).
  • there will be no merge: Leaflet will replace the whole layer with newly generated geospatial data, when you add GeoJSON data. Merging might be possible with custom implementation, but I don't know if it is advisable.

My use case is that I need to add many geoJSON objects one at a time and want to ensure I'm not creating many unnecessary layers (or if I am, if this is actually a bad thing).

It is not a bad thing per-sé, to have many layers; as long as concrete performance concerns don't arise.
If you want to reduce the number of layers, then put effort in modelling the structure of your GeoJSON so that most of the related objects are contained in a single GeoJSON/Layer (grouping by domain), instead of having a layer for each individual object (grouping by technical implementation).

E.g. a layer related to "risk areas"

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "title": "virus spread area",
        "risk": "high"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              114.521484375,
              30.89279747750818
            ],
            [
              113.89251708984374,
              30.64972717137329
            ],
            [
              114.28253173828124,
              30.21635515266855
            ],
            [
              114.521484375,
              30.89279747750818
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "Wuhan Institute of Virology",
        "risk": "high"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          114.35462951660156,
          30.543338954230222
        ]
      }
    }
  ]
}

instead of having one layer for the Polygon:

{
      "type": "Feature",
      "properties": {
        "title": "virus spread area",
        "risk": "high"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              114.521484375,
              30.89279747750818
            ],
            [
              113.89251708984374,
              30.64972717137329
            ],
            [
              114.28253173828124,
              30.21635515266855
            ],
            [
              114.521484375,
              30.89279747750818
            ]
          ]
        ]
      }
    }

and a separated (but actually related) one for the Point:

{
    "type": "Feature",
    "properties": {
        "name": "Wuhan Institute of Virology",
        "risk": "high"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            114.35462951660156,
            30.543338954230222
        ]
    }
}

(imho) both features conceptually belong to the same Layer.

Kamafeather
  • 8,663
  • 14
  • 69
  • 99