-
Notifications
You must be signed in to change notification settings - Fork 592
/
feature.js
70 lines (59 loc) · 1.57 KB
/
feature.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import hat from 'hat';
import * as Constants from '../constants.js';
const Feature = function(ctx, geojson) {
this.ctx = ctx;
this.properties = geojson.properties || {};
this.coordinates = geojson.geometry.coordinates;
this.id = geojson.id || hat();
this.type = geojson.geometry.type;
};
Feature.prototype.changed = function() {
this.ctx.store.featureChanged(this.id);
};
Feature.prototype.incomingCoords = function(coords) {
this.setCoordinates(coords);
};
Feature.prototype.setCoordinates = function(coords) {
this.coordinates = coords;
this.changed();
};
Feature.prototype.getCoordinates = function() {
return JSON.parse(JSON.stringify(this.coordinates));
};
Feature.prototype.setProperty = function(property, value) {
this.properties[property] = value;
};
Feature.prototype.toGeoJSON = function() {
return JSON.parse(JSON.stringify({
id: this.id,
type: Constants.geojsonTypes.FEATURE,
properties: this.properties,
geometry: {
coordinates: this.getCoordinates(),
type: this.type
}
}));
};
Feature.prototype.internal = function(mode) {
const properties = {
id: this.id,
meta: Constants.meta.FEATURE,
'meta:type': this.type,
active: Constants.activeStates.INACTIVE,
mode
};
if (this.ctx.options.userProperties) {
for (const name in this.properties) {
properties[`user_${name}`] = this.properties[name];
}
}
return {
type: Constants.geojsonTypes.FEATURE,
properties,
geometry: {
coordinates: this.getCoordinates(),
type: this.type
}
};
};
export default Feature;