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)
endChanging 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
| Prop | Type | Notes |
|---|---|---|
id | string | Required. Referenced by layers' source prop |
data | map or string | Required. A GeoJSON object, or a URL to fetch one from |
cluster | boolean | Group nearby points into clusters |
cluster_radius | number | Cluster radius in pixels, default 50 |
cluster_max_zoom | number | Zoom past which clustering stops |
cluster_min_points | number | Points needed to form a cluster |
cluster_properties | map | Aggregations computed per cluster |
max_zoom | number | Maximum zoom to tile the data at |
buffer | number | Tile buffer in pixels |
tolerance | number | Douglas-Peucker simplification tolerance |
line_metrics | boolean | Required by line_gradient paint |
generate_id | boolean | Auto-assign feature ids, needed for feature state |
promote_id | string or map | Use a property as the feature id instead |
filter | list | Filter applied before tiling |
attribution | string | Attribution text |
incremental | boolean | Send 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
@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
Returns the list of property definitions for the compiled component.