@mapbox/mapbox-gl-draw
Version:
A drawing component for Mapbox GL JS
71 lines (59 loc) • 1.63 kB
JavaScript
import {generateID} from '../lib/id.js';
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 || generateID();
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;