-
Notifications
You must be signed in to change notification settings - Fork 594
/
api.js
196 lines (165 loc) · 6.27 KB
/
api.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const isEqual = require('lodash.isequal');
const normalize = require('@mapbox/geojson-normalize');
const hat = require('hat');
const featuresAt = require('./lib/features_at');
const stringSetsAreEqual = require('./lib/string_sets_are_equal');
const geojsonhint = require('@mapbox/geojsonhint');
const Constants = require('./constants');
const StringSet = require('./lib/string_set');
const featureTypes = {
Polygon: require('./feature_types/polygon'),
LineString: require('./feature_types/line_string'),
Point: require('./feature_types/point'),
MultiPolygon: require('./feature_types/multi_feature'),
MultiLineString: require('./feature_types/multi_feature'),
MultiPoint: require('./feature_types/multi_feature')
};
module.exports = function(ctx, api) {
api.modes = Constants.modes;
api.getFeatureIdsAt = function(point) {
const features = featuresAt.click({ point }, null, ctx);
return features.map(feature => feature.properties.id);
};
api.getSelectedIds = function () {
return ctx.store.getSelectedIds();
};
api.getSelected = function () {
return {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: ctx.store.getSelectedIds().map(id => ctx.store.get(id)).map(feature => feature.toGeoJSON())
};
};
api.getSelectedPoints = function () {
return {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: ctx.store.getSelectedCoordinates().map(coordinate => {
return {
type: Constants.geojsonTypes.FEATURE,
properties: {},
geometry: {
type: Constants.geojsonTypes.POINT,
coordinates: coordinate.coordinates
}
};
})
};
};
api.set = function(featureCollection) {
if (featureCollection.type === undefined || featureCollection.type !== Constants.geojsonTypes.FEATURE_COLLECTION || !Array.isArray(featureCollection.features)) {
throw new Error('Invalid FeatureCollection');
}
const renderBatch = ctx.store.createRenderBatch();
let toDelete = ctx.store.getAllIds().slice();
const newIds = api.add(featureCollection);
const newIdsLookup = new StringSet(newIds);
toDelete = toDelete.filter(id => !newIdsLookup.has(id));
if (toDelete.length) {
api.delete(toDelete);
}
renderBatch();
return newIds;
};
api.add = function (geojson) {
const errors = geojsonhint.hint(geojson, { precisionWarning: false }).filter(e => e.level !== 'message');
if (errors.length) {
throw new Error(errors[0].message);
}
const featureCollection = JSON.parse(JSON.stringify(normalize(geojson)));
const ids = featureCollection.features.map(feature => {
feature.id = feature.id || hat();
if (feature.geometry === null) {
throw new Error('Invalid geometry: null');
}
if (ctx.store.get(feature.id) === undefined || ctx.store.get(feature.id).type !== feature.geometry.type) {
// If the feature has not yet been created ...
const Model = featureTypes[feature.geometry.type];
if (Model === undefined) {
throw new Error(`Invalid geometry type: ${feature.geometry.type}.`);
}
const internalFeature = new Model(ctx, feature);
ctx.store.add(internalFeature);
} else {
// If a feature of that id has already been created, and we are swapping it out ...
const internalFeature = ctx.store.get(feature.id);
internalFeature.properties = feature.properties;
if (!isEqual(internalFeature.getCoordinates(), feature.geometry.coordinates)) {
internalFeature.incomingCoords(feature.geometry.coordinates);
}
}
return feature.id;
});
ctx.store.render();
return ids;
};
api.get = function (id) {
const feature = ctx.store.get(id);
if (feature) {
return feature.toGeoJSON();
}
};
api.getAll = function() {
return {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: ctx.store.getAll().map(feature => feature.toGeoJSON())
};
};
api.delete = function(featureIds) {
ctx.store.delete(featureIds, { silent: true });
// If we were in direct select mode and our selected feature no longer exists
// (because it was deleted), we need to get out of that mode.
if (api.getMode() === Constants.modes.DIRECT_SELECT && !ctx.store.getSelectedIds().length) {
ctx.events.changeMode(Constants.modes.SIMPLE_SELECT, undefined, { silent: true });
} else {
ctx.store.render();
}
return api;
};
api.deleteAll = function() {
ctx.store.delete(ctx.store.getAllIds(), { silent: true });
// If we were in direct select mode, now our selected feature no longer exists,
// so escape that mode.
if (api.getMode() === Constants.modes.DIRECT_SELECT) {
ctx.events.changeMode(Constants.modes.SIMPLE_SELECT, undefined, { silent: true });
} else {
ctx.store.render();
}
return api;
};
api.changeMode = function(mode, modeOptions = {}) {
// Avoid changing modes just to re-select what's already selected
if (mode === Constants.modes.SIMPLE_SELECT && api.getMode() === Constants.modes.SIMPLE_SELECT) {
if (stringSetsAreEqual((modeOptions.featureIds || []), ctx.store.getSelectedIds())) return api;
// And if we are changing the selection within simple_select mode, just change the selection,
// instead of stopping and re-starting the mode
ctx.store.setSelected(modeOptions.featureIds, { silent: true });
ctx.store.render();
return api;
}
if (mode === Constants.modes.DIRECT_SELECT && api.getMode() === Constants.modes.DIRECT_SELECT &&
modeOptions.featureId === ctx.store.getSelectedIds()[0]) {
return api;
}
ctx.events.changeMode(mode, modeOptions, { silent: true });
return api;
};
api.getMode = function() {
return ctx.events.getMode();
};
api.trash = function() {
ctx.events.trash({ silent: true });
return api;
};
api.combineFeatures = function() {
ctx.events.combineFeatures({ silent: true });
return api;
};
api.uncombineFeatures = function() {
ctx.events.uncombineFeatures({ silent: true });
return api;
};
api.setFeatureProperty = function(featureId, property, value) {
ctx.store.setFeatureProperty(featureId, property, value);
return api;
};
return api;
};