Coder Social home page Coder Social logo

vladimirkalachikhin / polycolorrenderer Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 44 KB

Color-code a Leaflet polyline and geojson objects based on additional values, e.g. altitude, speed, depth, etc..

License: GNU General Public License v3.0

JavaScript 100.00%
colored geojson gradient leaflet leafletjs polyline

polycolorrenderer's Introduction

polycolorRenderer License: GPLv3

v. 0.2

The Leaflet L.Renderer upon the idea leaflet-polycolor project.
Suitable for color-coding a Leaflet polyline based on additional values, e.g. altitude, speed, depth, etc... Main goal the renderer is a gradient color of segments to represent continuous values.
The color code maybe created by value2color function.
Additionally, it is possible to set the weight option of each L.Polyline segment.

It can be used both for L.Polyline and L.GeoJSON LineString features.

Usage

<script src="polycolorRenderer.js"></script>

As the renderer has a narrow purpose, only the following options of L.Polyline are supported:

  • color
  • opacity
  • weight
  • lineCap
  • lineJoin
  • dashArray
  • dashOffset

Polyline

let polyline = L.polyline(latLngs, {
	noClip: true,
	smoothFactor: 0,
	renderer: new polycolorRenderer(),
	colors: [......], // array of html colors
	useGradient: false,
	weights: [......] // array of segment's width
}).addTo(map);

GeoJSON

It makes no sense to use the renderer for coloring GeoJSON LineString to solid colors. This is done naturally through style option. But it is impossible to make a gradient color in this way. Therefore, a renderer is needed:

var polyline = L.geoJSON(geoJSONdata,{
	style: function(feature){
		return {
			noClip: true,
			smoothFactor: 0,
			renderer: new polycolorRenderer(),
			colors: [......], // array of html colors
			weights: [......] // array of segment's width
		}
	}
}).addTo(map);

GeoJSON MultiLineString

The function L.geoJSON create "MultiPolyline" L.Polyline object. In this case, the colors array must contain arrays of colors similar in structure to GeoJSON MultiLineString "coordinates" object.
However, any of these color arrays can be empty. Then all segments of the corresponding section will be colored in the color specified by the parameter color. Therefore, if you have a line of thousands of segments, and there are only a few additional values, for example, depths, there is no need to create a color array of thousands of null values. It is enough to make an GeoJSON MultiLineString object and a color array of three arrays:

const colors = [
	[],
	[few of color values...],
	[]
];

The same for weights array.

Suppression of Leaflet's line optimization

The next two L.Polyline options suppress simplification and smoothing of the line created by Leaflet:

noClip: true,
smoothFactor: 0,

As a result, all segments defined when creating a polyline will be present in the resulting line, and they can be colored correctly. BUT! The empty GeoJSON MultiLineString segments, such as:

geometry: { type: "MultiLineString",
	coordinates: Array [ 
		[],
		[…]
	]
}

will be ignored by the Leaflet regardless of any options. The procedure for creating of colors array should take this into account.

Options

  • colors <Array>: An array of html colors strings rgb(x,y,z), rgba(x,y,z,t)or #3388ff, or array of arrays of colors. Any of these arrays can be empty.
  • useGradient <Boolean>: Use or not gradient to smooth colors. Defaults to true.
  • weights <Array>: An array of segments stroke width in pixels. The number of values of this array is equal to the number of segments, that is a number of colors - 1.

If colors or weight are undefined, the default coloror weight parameter is used. To leave default value, use null in array:
['rgb(0, 0, 0)', null, 'rgb(0, 45, 120)']
An empty array has the same as null: applying the default value.
No gradient is made between the default and the specified values.

Efficiency

Using the renderer requires resources and will work slower. In addition, the Leaflet calls the renderer every time the map is moved. For dynamic applications, this can result in a heavy load.

Demo

The examples directory has a complete example of using the polycolorRenderer with GeoJSON data.

polycolorrenderer's People

Contributors

vladimirkalachikhin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.