@acalcutt/maplibre-gl-native-test 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +515 -0
- package/LICENSE.mbgl-core.md +661 -0
- package/LICENSE.md +30 -0
- package/README.md +145 -0
- package/lib/node-v102/mbgl.node +0 -0
- package/lib/node-v108/mbgl.node +0 -0
- package/lib/node-v57/mbgl.node +0 -0
- package/lib/node-v59/mbgl.node +0 -0
- package/lib/node-v64/mbgl.node +0 -0
- package/lib/node-v67/mbgl.node +0 -0
- package/lib/node-v72/mbgl.node +0 -0
- package/lib/node-v79/mbgl.node +0 -0
- package/lib/node-v83/mbgl.node +0 -0
- package/lib/node-v88/mbgl.node +0 -0
- package/lib/node-v93/mbgl.node +0 -0
- package/package.json +73 -0
- package/platform/node/CHANGELOG.md +284 -0
- package/platform/node/README.md +204 -0
- package/platform/node/index.js +51 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @maplibre/maplibre-gl-native
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/@maplibre/maplibre-gl-native)
|
|
4
|
+
[](https://github.com/maplibre/maplibre-gl-native/actions/workflows/node-ci.yml)
|
|
5
|
+
|
|
6
|
+
## Installing
|
|
7
|
+
|
|
8
|
+
#### :warn: This package isn't live yet.
|
|
9
|
+
|
|
10
|
+
Requires a modern C++ runtime that supports C++14.
|
|
11
|
+
|
|
12
|
+
By default, installs binaries. On these platforms no additional dependencies are needed.
|
|
13
|
+
|
|
14
|
+
- 64 bit macOS or 64 bit Linux
|
|
15
|
+
- Node.js v10.x
|
|
16
|
+
|
|
17
|
+
Run:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
npm install @maplibre/maplibre-gl-native
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Other platforms will fall back to a source compile with `make node`; see [DEVELOPING.md](DEVELOPING.md) for details on
|
|
24
|
+
building from source.
|
|
25
|
+
|
|
26
|
+
## Testing
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
npm test
|
|
30
|
+
npm run test-suite
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Rendering a map tile
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
var fs = require('fs');
|
|
37
|
+
var path = require('path');
|
|
38
|
+
var mbgl = require('@maplibre/maplibre-gl-native');
|
|
39
|
+
var sharp = require('sharp');
|
|
40
|
+
|
|
41
|
+
var options = {
|
|
42
|
+
request: function(req, callback) {
|
|
43
|
+
fs.readFile(path.join(__dirname, 'test', req.url), function(err, data) {
|
|
44
|
+
callback(err, { data: data });
|
|
45
|
+
});
|
|
46
|
+
},
|
|
47
|
+
ratio: 1
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
var map = new mbgl.Map(options);
|
|
51
|
+
|
|
52
|
+
map.load(require('./test/fixtures/style.json'));
|
|
53
|
+
|
|
54
|
+
map.render({zoom: 0}, function(err, buffer) {
|
|
55
|
+
if (err) throw err;
|
|
56
|
+
|
|
57
|
+
map.release();
|
|
58
|
+
|
|
59
|
+
var image = sharp(buffer, {
|
|
60
|
+
raw: {
|
|
61
|
+
width: 512,
|
|
62
|
+
height: 512,
|
|
63
|
+
channels: 4
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Convert raw image buffer to PNG
|
|
68
|
+
image.toFile('image.png', function(err) {
|
|
69
|
+
if (err) throw err;
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The first argument passed to `map.render` is an options object, all keys are optional:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
{
|
|
78
|
+
zoom: {zoom}, // number, defaults to 0
|
|
79
|
+
width: {width}, // number (px), defaults to 512
|
|
80
|
+
height: {height}, // number (px), defaults to 512
|
|
81
|
+
center: [{longitude}, {latitude}], // array of numbers (coordinates), defaults to [0,0]
|
|
82
|
+
bearing: {bearing}, // number (in degrees, counter-clockwise from north), defaults to 0
|
|
83
|
+
pitch: {pitch}, // number (in degrees, arcing towards the horizon), defaults to 0
|
|
84
|
+
classes: {classes} // array of strings
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
When you are finished using a map object, you can call `map.release()` to permanently dispose the internal map resources. This is not necessary, but can be helpful to optimize resource usage (memory, file sockets) on a more granular level than V8's garbage collector. Calling `map.release()` will prevent a map object from being used for any further render calls, but can be safely called as soon as the `map.render()` callback returns, as the returned pixel buffer will always be retained for the scope of the callback.
|
|
89
|
+
|
|
90
|
+
## Implementing a file source
|
|
91
|
+
|
|
92
|
+
When creating a `Map`, you must pass an options object (with a required `request` method and optional 'ratio' number) as the first parameter.
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
var map = new mbgl.Map({
|
|
96
|
+
request: function(req) {
|
|
97
|
+
// TODO
|
|
98
|
+
},
|
|
99
|
+
ratio: 2.0
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The `request()` method handles a request for a resource. The `ratio` sets the scale at which the map will render tiles, such as `2.0` for rendering images for high pixel density displays. The `req` parameter has two properties:
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"url": "http://example.com",
|
|
108
|
+
"kind": 1
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The `kind` is an enum and defined in [`mbgl.Resource`](https://github.com/maplibre/maplibre-gl-native/blob/master/include/mbgl/storage/resource.hpp):
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"Unknown": 0,
|
|
117
|
+
"Style": 1,
|
|
118
|
+
"Source": 2,
|
|
119
|
+
"Tile": 3,
|
|
120
|
+
"Glyphs": 4,
|
|
121
|
+
"SpriteImage": 5,
|
|
122
|
+
"SpriteJSON": 6
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The `kind` enum has no significance for anything but serves as a hint to your implemention as to what sort of resource to expect. E.g., your implementation could choose caching strategies based on the expected file type.
|
|
127
|
+
|
|
128
|
+
The `request` implementation should pass uncompressed data to `callback`. If you are downloading assets from a source that applies gzip transport encoding, the implementation must decompress the results before passing them on.
|
|
129
|
+
|
|
130
|
+
A sample implementation that reads files from disk would look like the following:
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
var map = new mbgl.Map({
|
|
134
|
+
request: function(req, callback) {
|
|
135
|
+
fs.readFile(path.join('base/path', req.url), function(err, data) {
|
|
136
|
+
callback(err, { data: data });
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
This is a very barebones implementation and you'll probably want a better implementation. E.g. it passes the url verbatim to the file system, but you'd want add some logic that normalizes `http` URLs. You'll notice that once your implementation has obtained the requested file, you have to deliver it to the requestee by calling `callback()`, which takes either an error object or `null` and an object with several keys:
|
|
143
|
+
|
|
144
|
+
```js
|
|
145
|
+
{
|
|
146
|
+
modified: new Date(),
|
|
147
|
+
expires: new Date(),
|
|
148
|
+
etag: "string",
|
|
149
|
+
data: new Buffer()
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
A sample implementation that uses [`request`](https://github.com/request/request) to fetch data from a remote source:
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
var mbgl = require('@maplibre/maplibre-gl-native');
|
|
157
|
+
var request = require('request');
|
|
158
|
+
|
|
159
|
+
var map = new mbgl.Map({
|
|
160
|
+
request: function(req, callback) {
|
|
161
|
+
request({
|
|
162
|
+
url: req.url,
|
|
163
|
+
encoding: null,
|
|
164
|
+
gzip: true
|
|
165
|
+
}, function (err, res, body) {
|
|
166
|
+
if (err) {
|
|
167
|
+
callback(err);
|
|
168
|
+
} else if (res.statusCode == 200) {
|
|
169
|
+
var response = {};
|
|
170
|
+
|
|
171
|
+
if (res.headers.modified) { response.modified = new Date(res.headers.modified); }
|
|
172
|
+
if (res.headers.expires) { response.expires = new Date(res.headers.expires); }
|
|
173
|
+
if (res.headers.etag) { response.etag = res.headers.etag; }
|
|
174
|
+
|
|
175
|
+
response.data = body;
|
|
176
|
+
|
|
177
|
+
callback(null, response);
|
|
178
|
+
} else {
|
|
179
|
+
callback(new Error(JSON.parse(body).message));
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Stylesheets are free to use any protocols, but your implementation of `request` must support these; e.g. you could use `s3://` to indicate that files are supposed to be loaded from S3.
|
|
187
|
+
|
|
188
|
+
## Listening for log events
|
|
189
|
+
|
|
190
|
+
The module imported with `require('maplibre-gl-native')` inherits from [`EventEmitter`](https://nodejs.org/api/events.html), and the `NodeLogObserver` will push log events to this. Log messages can have [`class`](https://github.com/maplibre/maplibre-gl-native/blob/node-v2.1.0/include/mbgl/platform/event.hpp#L43-L60), [`severity`](https://github.com/maplibre/maplibre-gl-native/blob/node-v2.1.0/include/mbgl/platform/event.hpp#L17-L23), `code` ([HTTP status codes](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)), and `text` parameters.
|
|
191
|
+
|
|
192
|
+
```js
|
|
193
|
+
var mbgl = require('@maplibre/maplibre-gl-native');
|
|
194
|
+
mbgl.on('message', function(msg) {
|
|
195
|
+
t.ok(msg, 'emits error');
|
|
196
|
+
t.equal(msg.class, 'Style');
|
|
197
|
+
t.equal(msg.severity, 'ERROR');
|
|
198
|
+
t.ok(msg.text.match(/Failed to load/), 'error text matches');
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Contributing
|
|
203
|
+
|
|
204
|
+
See [DEVELOPING.md](DEVELOPING.md) for instructions on building this module for development.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Shim to wrap req.respond while preserving callback-passing API
|
|
4
|
+
|
|
5
|
+
var mbgl = require('../../lib/node-v' + process.versions.modules + '/mbgl');
|
|
6
|
+
var constructor = mbgl.Map.prototype.constructor;
|
|
7
|
+
|
|
8
|
+
var Map = function(options) {
|
|
9
|
+
if (!(options instanceof Object)) {
|
|
10
|
+
throw TypeError("Requires an options object as first argument");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!options.hasOwnProperty('request') || !(options.request instanceof Function)) {
|
|
14
|
+
throw TypeError("Options object must have a 'request' method");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
var request = options.request;
|
|
18
|
+
|
|
19
|
+
return new constructor(Object.assign(options, {
|
|
20
|
+
request: function(req) {
|
|
21
|
+
// Protect against `request` implementations that call the callback synchronously,
|
|
22
|
+
// call it multiple times, or throw exceptions.
|
|
23
|
+
// http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony
|
|
24
|
+
|
|
25
|
+
var responded = false;
|
|
26
|
+
var callback = function() {
|
|
27
|
+
var args = arguments;
|
|
28
|
+
if (!responded) {
|
|
29
|
+
responded = true;
|
|
30
|
+
process.nextTick(function() {
|
|
31
|
+
req.respond.apply(req, args);
|
|
32
|
+
});
|
|
33
|
+
} else {
|
|
34
|
+
console.warn('request function responded multiple times; it should call the callback only once');
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
request(req, callback);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.warn('request function threw an exception; it should call the callback with an error instead');
|
|
42
|
+
callback(e);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}));
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
Map.prototype = mbgl.Map.prototype;
|
|
49
|
+
Map.prototype.constructor = Map;
|
|
50
|
+
|
|
51
|
+
module.exports = Object.assign(mbgl, { Map: Map });
|