UNPKG

rlayers

Version:

React Components for OpenLayers

56 lines (54 loc) 22 kB
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="keywords" content="openlayers, react, gis, maps, cartography, typescript, javascript"><title>rlayers examples - React Components for OpenLayers 6+</title><script defer="defer" src="bundle.js"></script></head><body><div id="fullscreen" class="fullscreen"><div id="root"><h1 id="rlayers---react-components-for-openlayers-6">rlayers - React Components for OpenLayers 6+</h1><p><strong>Update: since Oct 24 I am homeless and living in my van. I lost access to most of my computer hardware. The eviction from my home has been timed for maximum effect as I was not present when it happened. I have only a single set of clothes and no means to get anything else. Please, if you use my software, consider asking everyone in the geospatial community if they are taking part in this extortion and why.</strong></p><p><img src="https://raw.githubusercontent.com/mmomtchev/rlayers/master/rlayers-logo.svg" alt="logo"></p><p><a href="https://github.com/mmomtchev/rlayers/blob/master/LICENSE"><img src="https://img.shields.io/github/license/mmomtchev/rlayers" alt="License: ISC"></a><a href="https://www.npmjs.com/package/rlayers"><img src="https://img.shields.io/npm/v/rlayers" alt="npm version"></a><a href="https://github.com/mmomtchev/rlayers/actions?query=workflow%3A%22Node.js+CI%22"><img src="https://github.com/mmomtchev/rlayers/workflows/Node.js%20CI/badge.svg" alt="Node.js CI"></a><a href="https://codecov.io/gh/mmomtchev/rlayers"><img src="https://codecov.io/gh/mmomtchev/rlayers/branch/master/graph/badge.svg" alt="codecov"></a><a href="https://www.npmjs.com/package/rlayers"><img src="https://img.shields.io/npm/dm/rlayers" alt="downloads"></a></p><p><img src="https://gist.githubusercontent.com/mmomtchev/e789dfa545b97c7ae97770f1b5606172/raw/79b486bcc8c45b069e3a8f552c82360de80febff/ref1.png" alt="© OpenStreetMap contributors"> <img src="https://gist.githubusercontent.com/mmomtchev/e789dfa545b97c7ae97770f1b5606172/raw/79b486bcc8c45b069e3a8f552c82360de80febff/ref2.png" alt="Kartendaten: © OpenStreetMap-Mitwirkende, SRTM | Kartendarstellung: © OpenTopoMap (CC-BY-SA)"></p><p><em>rlayers</em> is an opinionated set of <em>React</em> components for <em>OpenLayers</em>.</p><p>It&#39;s design policy is:</p><ul><li>Fully Typescript-typed</li><li>Do everything that faces the user the <strong><em>React</em> way</strong> and not the <em>OpenLayers</em> way - <code>onClick</code> and <code>onPointerEnter</code>/<code>onPoinerLeave</code> handlers are typical examples</li><li>If it does not face the user, it does not need to be <em>React</em> way - internally it uses inheritance, following the <em>OpenLayers</em> classes, over composition</li><li>Simple things should be simple to do, performance optimizations should not get in the way unless needed</li><li>If taking shortcuts when updating the components, always err on the safe side but do provide an override method that allows to come close to the raw OpenLayers performance</li><li>Expose all the advanced <em>OpenLayers</em> features</li><li>Try to be as much SSR-friendly as possible (this feature is currently in POC stage, see below)</li><li>The current target is <em>OpenLayers</em> 6+</li><li>Avoid dependencies when built except for <em>React</em> and OpenLayers (the examples have some dependencies) - currently the single one is <a href="https://www.npmjs.com/package/lru-cache"><code>lru-cache</code></a> at 8Kbytes</li></ul><h2 id="long-term-support-of-this-project">Long term support of this project</h2><p>The birth of this project is related to a huge extortion in the geography community linked to a sexual harassment affair covered up by the French Judiciary. It is maintained as a free service to the geography community so that it can remain as a remainder to a number of companies - including Camptocamp, ESRI, Mapbox and Makina Corpus - that the most noble way to claim size bragging rights is to produce good software. You can safely use this framework in your projects, be assured that it will be maintained very well and for many years to come. It&#39;s companion project on the server-side is <a href="https://github.com/mmomtchev/node-gdal-async"><code>gdal-async</code></a>.</p><h2 id="alternatives">Alternatives</h2><p><strong>&lt;- Light-Weight --- Feature-Rich -&gt;</strong></p><p><a href="https://pigeon-maps.js.org/">pigeon-maps</a> - <a href="https://react-leaflet.js.org/">react-leaflet</a> - <a href="https://mmomtchev.github.io/rlayers/">rlayers</a></p><p>Among the completely free and open source alternatives for creating maps with <em>React</em>, on a scale going from the most light-weight to the most feature-rich solution, <em>rlayers</em> is the right-most one.</p><p>It offers the full power <em>OpenLayers</em> - dynamic reprojections, comprehensive event handlers, a very rich set of supported formats, interfaces and layer types and a very good performance for very complex maps. This comes at the price of a quite significant total bundle size.</p><h2 id="installation">Installation</h2><pre><code class="language-bash">npm --save install rlayers ol react react-dom </code></pre><h3 id="compatibility">Compatibility</h3><p><em>React</em> is a peer dependency and should be installed separately.</p><p><em>React</em> is supported from version 16.8.0 with <code>rlayers@2.x</code> and from version 18 with <code>rlayers@3.x</code>.</p><p>Starting with version 3.0, <code>rlayers</code> always comes with a fixed OpenLayers version which is usually the latest one at the time of the release.</p><hr><p>When using dynamic styles with React 18, you may get a warning in the console in debug mode: <a href="https://github.com/mmomtchev/rlayers/issues/40">https://github.com/mmomtchev/rlayers/issues/40</a>. You can safely ignore it as has no functional consequences - React 18, including the concurrent renderer, is fully supported.</p><h2 id="usage">Usage</h2><p><em>rlayers</em> is a set of reusable <em>React</em> components that can be nested in various ways to create map applications for the web through <em>React</em> composition in the true <strong>spirit of <em>React</em></strong>. The components are based on a simplified model of the <em>OpenLayers</em> classes: for example the layers and the sources abstraction levels have been fused into one single level and the map and the view are also represented by a single component.</p><p>In order to avoid confusion between the <em>OpenLayers</em> classes and the <em>rlayers</em> classes which sometimes have the same names - all <em>rlayers</em> classes are prefixed with <strong>R</strong>. If a class begins with <strong>R</strong>, it is from <em>rlayers</em>, otherwise it is an <em>OpenLayers</em> class.</p><p>The most important element is the <code>&lt;RMap&gt;</code>. Every other element, except <code>&lt;RStyle&gt;</code>, requires a parent to function - an <code>&lt;RLayer&gt;</code> must be part of a map, an <code>&lt;RFeature&gt;</code> must be part of an <code>&lt;RLayerVector&gt;</code>, an <code>&lt;RControl&gt;</code> must also be part of a map.</p><h3 id="simple-step-by-step-example">Simple step-by-step example</h3><p>This is the simple overlay example - <a href="https://mmomtchev.github.io/rlayers/#/overlays">https://mmomtchev.github.io/rlayers/#/overlays</a></p><pre><code class="language-jsx">import React from &#39;react&#39;; import {fromLonLat} from &#39;ol/proj&#39;; import {Point} from &#39;ol/geom&#39;; import &#39;ol/ol.css&#39;; import {RMap, ROSM, RLayerVector, RFeature, ROverlay, RStyle} from &#39;rlayers&#39;; import locationIcon from &#39;./svg/location.svg&#39;; // Create a map, its size is set in the CSS class example-map &lt;RMap className=&#39;example-map&#39; initial={{center: fromLonLat([2.364, 48.82]), zoom: 11}}&gt; {/* Use an OpenStreetMap background */} &lt;ROSM /&gt; {/* Create a single layer for holding vector features */} &lt;RLayerVector zIndex={10}&gt; {/* Create a style for rendering the features */} &lt;RStyle.RStyle&gt; {/* Consisting of a single icon, that is slightly offset * so that its center falls over the center of the feature */} &lt;RStyle.RIcon src={locationIcon} anchor={[0.5, 0.8]} /&gt; &lt;/RStyle.RStyle&gt; {/* Create a single feature in the vector layer */} &lt;RFeature {/* Its geometry is a point geometry over the monument */} geometry={new Point(fromLonLat([2.295, 48.8737]))} {/* Bind an onClick handler */} onClick={(e) =&gt; {/* e.map is the underlying OpenLayers map - we call getView().fit() to pan/zoom the map over the monument with a small animation */} e.map.getView().fit(e.target.getGeometry().getExtent(), { duration: 250, maxZoom: 15 }) } &gt; {/* The icon is an SVG image that represents the feature on the map while an overlay allows us to add a normal HTML element over the feature */} &lt;ROverlay className=&#39;example-overlay&#39;&gt; Arc de Triomphe &lt;br /&gt; &lt;em&gt;&amp;#11017; click to zoom&lt;/em&gt; &lt;/ROverlay&gt; &lt;/RFeature&gt; &lt;/RLayerVector&gt; &lt;/RMap&gt; </code></pre><p>Check <a href="https://github.com/mmomtchev/rlayers/tree/master/examples/static_page.html">examples/static_pages.html</a> for a fully self-contained static HTML page using <em>rlayers</em>.</p><p>You can also check the GPLed <a href="https://github.com/mmomtchev/xc-db.git">XC-DB</a> for a larger and more complex project entirely implemented using React, Redux and rlayers.</p><h4 id="contexts">Contexts</h4><p>Composition works by using <em>React</em> Contexts. Every nested element uses the context of its nearest parent.</p><p>The underlying OpenLayers objects can be accessed using the <code>useOL()</code> hook - check the <a href="https://mmomtchev.github.io/rlayers/#/geolocation"><code>Geolocation</code></a> example to see how.</p><p>Currently <code>useOL()</code> has an <code>RContextType</code> and can contain the following elements:</p><ul><li><code>map</code> provided by a map, every other element, except an <code>RStyle</code> must have a map parent</li><li><code>layer</code> and <code>source</code> provided by all layers - not required for anything at the moment, but can be used to access the underlying <em>OpenLayers</em> objects</li><li><code>vectorlayer</code> and <code>vectorsource</code> provided by vector layers only - required for <code>&lt;RFeature&gt;</code></li><li><code>vectortilelayer</code> provided by vector tile layers only</li><li><code>location</code> and <code>feature</code> provided by a map feature - required for <code>&lt;ROverlay&gt;</code> and <code>&lt;RPopup&gt;</code></li><li><code>style</code> provided by a style definition - the only one which can be outside of a map</li></ul><p>Additionally, <code>useRLayersComponent()</code> allows retrieving the containing <em>rlayers</em> component.</p><h4 id="accessing-the-underlying-openlayers-objects-and-api">Accessing the underlying <em>OpenLayers</em> objects and API</h4><p>The underlying <em>OpenLayers</em> objects can be accessed in a number of different ways:</p><ul><li>Through the context objects by using <code>React.Context.Consumer</code></li><li>In an event handler that is a normal function and not an arrow lambda, <code>this</code> will contain the <em>rlayers</em> component and <code>this.context</code> will contain the context - this is an alternative to using <code>useOL()</code></li><li>In all event handlers, <em>OpenLayers</em> will pass the target object in <code>event.target</code> and the map in <code>event.map</code> - <a href="https://mmomtchev.github.io/rlayers/#/popups">the popups example</a> uses this method</li><li>And finally, accessing arbitrary elements, even outside their contexts, is possible by using React references - <code>React.RefObject</code>s. <a href="https://mmomtchev.github.io/rlayers/#/igc">The high performance example</a> contains an example of this. The underlying <em>OpenLayers</em> objects can be accessed through the <code>ol</code> property of every component. Additionaly, for <code>layer</code> objects, the underlying <em>OpenLayers</em> source can be accessed through <code>source</code>:<pre><code class="language-ts">const layerRef = React.createRef() as React.RefObject&lt;RLayerVector&gt;; </code></pre>Then after rendering:<pre><code class="language-jsx">&lt;RLayerVector ref={layerRef} /&gt; </code></pre><code>layerRef.current.ol</code> will contain the <em>OpenLayers</em> layer and <code>layerRef.current.source</code> will contain the source. This is the only way of accessing the object outside its context.</li></ul><h3 id="styles">Styles</h3><p>Style definitions can be placed anywhere inside the DOM and can be referenced with a <em>React</em> reference. rlayers includes two special types for dealing with styles:</p><ul><li><code>RStyleRef</code> which is an alias to <code>React.RefObject&lt;RStyle&gt;</code> is a <em>React</em> reference to an <code>&lt;RStyle&gt;</code> element. It can be transparently used everywhere where a classical <em>OpenLayers</em> <code>StyleLike</code> is required</li><li><code>RStyleLike</code> is the new union type that allows for <code>StyleLike</code> or a <code>RStyleRef</code></li></ul><p>A style placed inside a vector layer will be automatically applied to that vector layer.</p><p>A style can either be static or dynamic. A static style depends only on its properties. A dynamic style is a function that takes an <em>OpenLayers</em> <code>Feature</code> object as its input and returns a <code>Style</code>. A dynamic style creates a new object for every rendered feature, so this must be taken into account. A simple caching mechanism is also provided, based on a user-supplied hash function. It can greatly improve performance when the majority of the features use relatively few different styles.</p><p>You can refer to</p><ul><li><a href="https://mmomtchev.github.io/rlayers/#/features">https://mmomtchev.github.io/rlayers/#/features</a> for a basic example with static styles;</li><li><a href="https://mmomtchev.github.io/rlayers/#/vectortiles">https://mmomtchev.github.io/rlayers/#/vectortiles</a> for a more complete example with dynamic styles;</li><li><a href="https://mmomtchev.github.io/rlayers/#/cluster">https://mmomtchev.github.io/rlayers/#/cluster</a> for the use of caching.</li></ul><p>Classical <em>OpenLayers</em> <code>StyleLike</code> objects are supported too, but this is not the <strong><em>React</em> way</strong>. Still, if you need every last bit of performance, writing an optimized <em>OpenLayers</em> style function is the best solution.</p><h3 id="performance">Performance</h3><p>React is a wonderful framework that makes it very easy to write complex web applications without having to manually handle all the interdependencies between the various components. This is the reason why it is called <em>React</em> - components automatically <em>React</em> to changes in other components. In the true spirit of <em>React</em>, <em>rlayers</em> prefers to err on the safe side - always updating when there is a chance that the component needs updating - making it easy on the beginner who wants simple interface while still allowing the experienced engineer to achieve the performance he needs.</p><p>When high performance is required, particular care must be taken that the component properties do not change without a reason. This is especially true when the <code>pointermove</code> event is used. In these cases one should avoid using anonymous objects, arrays or functions as properties.</p><p>Take for example this:</p><pre><code class="language-jsx">&lt;RFeature geometry={new Point(fromLonLat([2.295, 48.858])} onClick={(e: MapBrowserEvent) =&gt; process(e.target)} /&gt; </code></pre><p>This is a feature that will be re-evaluated at every frame. Its geometry appears to be a constant, but it is in fact an anonymous object that is created at every frame - even if it always holds the same value. Passing a constant is one way around this, but the true <em>React way</em> is using the two tools <em>React</em> provides: <code>React.useMemo</code> and <code>React.useCallback</code>. They memoize the value and take care to always return a reference to the same object unless one of the listed dependencies is modified.</p><p>This is a much better performing code that won&#39;t rerender the feature component:</p><pre><code class="language-jsx">&lt;RFeature geometry={React.useMemo(new Point(fromLonLat([2.295, 48.858]), [/* no deps */])} onClick={React.useCallback((e: MapBrowserEvent) =&gt; process(e.target), [/* no deps */])} /&gt; </code></pre><p>Anonymous objects, arrays and <strong>especially lambdas</strong> in the properties of a component are prime candidates for memoization. Sometimes, you can also memoize whole components or groups of components - for a very significant performance boost.</p><p>Generally, if you are binding code to the <code>pointermove</code> event and your performance is not good enough, this is the first thing you should be looking at - which components update at every <code>pointermove</code> and why.</p><p>These 3 examples run code on various high-frequency events, take a look at them:</p><ul><li><a href="https://mmomtchev.github.io/rlayers/#/cluster">Clustering</a> runs the styling function every time the map is panned or zoomed</li><li><a href="https://mmomtchev.github.io/rlayers/#/pindrop">Drop a pin</a> runs code on every <code>pointermove</code> that carefully avoids rerendering</li><li><a href="https://mmomtchev.github.io/rlayers/#/geodata">Geo data</a> updates components at every <code>pointerenter</code>/<code>pointerleave</code></li><li><a href="https://mmomtchev.github.io/rlayers/#/igc">The high performance example</a> is a complex example that runs lots of code and updates components at every <code>pointermove</code></li></ul><p>Also, when searching for features listening on <code>pointermove</code>/<code>pointerenter</code>/<code>pointerleave</code> events, <em>rlayers</em> 2.0.0 and later, is able to eliminate very early feature layers that do not contain features listenening for those events. If your map contains a large number of features, and only a handful of these use <code>pointermove</code> events - try to group them in a separate layer.</p><h2 id="examples">Examples</h2><p>The examples can be found here: <a href="https://mmomtchev.github.io/rlayers/">https://mmomtchev.github.io/rlayers/</a></p><h3 id="server-side-rendering">Server-Side Rendering</h3><p>Server-side rendering of map components is difficult - there is still no comprehensive solution. Besides the obvious complexities of rendering on canvas outside the browser, one of the major issues is that server-side rendering runs before the browser layout flowing - and thus must work independent of layout and resolution.</p><p>The best solution is to use a WMS-compatible server (such as Geoserver) and to serve prerendered maps over WMS - eventually replacing the initial image by a canvas.</p><p>An intermediate solution, which does not require extensive server-side investment (such as Geoserver), but is limited to static layout(s), is to prerender one (or one per screen size) image to be used as a temporary place-holder until the map is loading. In this case, at least some devices, will get an ugly looking map for the first few seconds.</p><p>Pushing the initial tiles is also an option:</p><ul><li>when combined with a WMS-server it could deliver pixel-perfect maps with on the first HTTP request</li><li>without a WMS-server it could still avoid doing a large number of HTTP requests on the first load</li></ul><p>Currently, server-side rendering of raster layers on fixed map sizes has reached POC status and an online demo is accessible at <a href="https://rlayers-ssr.meteo.guru/">https://rlayers-ssr.meteo.guru/</a>. The code can be found in the <code>ssr</code> branch of this project. The <code>next.js</code> project can be found at <a href="https://github.com/mmomtchev/rlayers-ssr-demo.git">https://github.com/mmomtchev/rlayers-ssr-demo.git</a>. <em>This is still not a user-friendly, install-and-run project.</em> Take a look at <code>pages/index.js</code> if you want see how it is meant to be used.</p><p>As of March 2022, SSR support is stale and I am not working on it anymore. Should there be any interest in this feature, I could consider adding it.</p><h3 id="nextjs">Next.js</h3><p>As currently <code>rlayers</code> is an entirely client-side component without SSR support, using it with Next.js requires some special configuration that has tendency to change from version to version.</p><p><code>rlayers</code> has been found to work with all recent Next.js versions, including Next.js 14 in <code>app</code> mode.</p><p>Check <a href="https://github.com/mmomtchev/rlayers-npm-tests"><code>rlayers-npm-tests</code></a> for working examples.</p><h3 id="google-maps-api-support">Google Maps API Support</h3><p>The Google Maps API is not open and although it is now supported out-of-the-box by OpenLayers starting from version 9.0, it requires a paid subscription (with an eventual trial period). Adding Google Maps support is not possible unless someone is willing to sponsor it.</p><h2 id="api">API</h2><p>You can browse the full documentation at <a href="https://mmomtchev.github.io/rlayers/api">https://mmomtchev.github.io/rlayers/api</a>.</p><h2 id="license">License</h2><p><a href="https://choosealicense.com/licenses/isc/">ISC</a></p></div></div></body></html>