HoloMap.Source.GeoJSON (HoloMap v0.4.0)

Copy Markdown View Source

GeoJSON features, given inline or fetched from a URL.

<HoloMap.Source.GeoJSON id="parcels" data={@feature_collection} />
<HoloMap.Source.GeoJSON id="stops" data="/api/stops.geojson" cluster cluster_radius={60} />

This is the source to reach for when the data comes from your own application. data is a plain Elixir map. Build it in a command, put it in state, and the map follows:

def command(:load_parcels, params, server) do
  put_action(server, :parcels_loaded, parcels: Repo.geojson(params.bbox))
end

def action(:parcels_loaded, params, component) do
  put_state(component, :parcels, params.parcels)
end

Changing data costs nothing structurally. MapLibre's setData swaps the features in place, leaving every layer built on top of it untouched, and with incremental only the features that changed are sent (see "Changing a few features"). So does a change to cluster, cluster_radius or cluster_max_zoom, which setClusterOptions applies, building the clusters again. Any other prop, or a cluster_radius or cluster_max_zoom set back to nil, rebuilds the source (see "Updating a source" in HoloMap.Source).

Props

PropTypeNotes
idstringRequired. Referenced by layers' source prop
datamap or stringRequired. A GeoJSON object, or a URL to fetch one from
clusterbooleanGroup nearby points into clusters
cluster_radiusnumberCluster radius in pixels, default 50
cluster_max_zoomnumberZoom past which clustering stops
cluster_min_pointsnumberPoints needed to form a cluster
cluster_propertiesmapAggregations computed per cluster
max_zoomnumberMaximum zoom to tile the data at
buffernumberTile buffer in pixels
tolerancenumberDouglas-Peucker simplification tolerance
line_metricsbooleanRequired by line_gradient paint
generate_idbooleanAuto-assign feature ids, needed for feature state
promote_idstring or mapUse a property as the feature id instead
filterlistFilter applied before tiling
attributionstringAttribution text
incrementalbooleanSend a data change as the features that changed

Clustering

Clustered sources emit two kinds of feature, and they usually want two layers: one for clusters, one for the leftover single points.

<HoloMap.Source.GeoJSON id="stops" data={@stops} cluster cluster_radius={60} />

<HoloMap.Layer.Circle
  id="clusters"
  source="stops"
  filter={["has", "point_count"]}
  paint={%{circle_color: "#51bbd6", circle_radius: 18}}
  expand_cluster_on_click
/>

<HoloMap.Layer.Circle
  id="unclustered"
  source="stops"
  filter={["!", ["has", "point_count"]]}
  paint={%{circle_color: "#11b4da", circle_radius: 6}}
/>

expand_cluster_on_click zooms in to where a clicked cluster breaks apart, without a round trip to Elixir (see "Expanding clusters" in HoloMap.Layer).

The cluster options can follow state, and each change re-clusters the points with no layer taken off the map:

<HoloMap.Source.GeoJSON id="stops" data={@stops} cluster={@clustered} cluster_radius={@radius} />

MapLibre's getStyle goes on reporting the cluster options the source was added with after such a change, so HoloMap.API.request/4 with "getStyle" reads them from there; with :source and "getClusterOptions" it reads the ones in force.

Changing a few features

setData hands MapLibre the whole collection, and its worker tiles every feature again, however few changed. For live data, such as vehicles or sensors, where a few features move between renders, set incremental:

<HoloMap.Source.GeoJSON id="fleet" data={@vehicles} promote_id="vehicle_id" incremental />

On each data change the browser runtime compares the new features with the ones the source holds, by id, and hands MapLibre's updateData the difference: the features removed, those added, and for each that changed its new geometry and the properties taken away or set. A feature whose other members change, such as a bbox, or whose geometry goes, is sent whole. Nothing is sent when no feature changed. MapLibre then loads again only the tiles the changes touch, or every tile of a clustered source, whose points its worker clusters again. A change to the cluster props in the same render applies first, so the new features are clustered with the new options. Changing incremental alone changes nothing on the map.

The diff names each feature by its id: the property promote_id names, or the feature's own id without one. So every feature needs one, a number or a string, and no two the same. When a feature of the new data has none, or shares one, the runtime sets the whole data with setData, as without incremental, and logs one console warning for the source. It does the same, with one warning, for a source with generate_id, whose ids MapLibre's worker numbers itself. Data that is not a FeatureCollection given inline, such as a URL, is always set whole.

The worker draws the features a diff added or changed after the others, so in a layer that does not sort its features (circle_sort_key, symbol_sort_key and the like) they may draw above features they used to be under, and the order of the features MapLibre's getStyle reports need not follow data either: a feature added comes last. getStyle reports a diff once the worker has applied it. The data stays what data says through a style change, a style built from scratch and a lost WebGL context.

An application that already knows which features changed can send them itself with HoloMap.API.update_geojson/3, which leaves data as it was, with or without incremental. The source keeps that change until data changes, and then holds exactly the new data.

Feature state

HoloMap.API.set_feature_state/3 needs every feature to have an id. GeoJSON features often do not carry one, so set generate_id or point promote_id at a property that is already unique.

Summary

Functions

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Returns the list of property definitions for the compiled component.

Functions

__is_hologram_component__()

@spec __is_hologram_component__() :: boolean()

Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).

Examples

iex> __is_hologram_component__()
true

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.