Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeometryCollection type support added #903

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const isEqual = require('lodash.isequal');
const normalize = require('@mapbox/geojson-normalize');
const hat = require('hat');
const featuresAt = require('./lib/features_at');
Expand All @@ -13,7 +12,8 @@ const featureTypes = {
Point: require('./feature_types/point'),
MultiPolygon: require('./feature_types/multi_feature'),
MultiLineString: require('./feature_types/multi_feature'),
MultiPoint: require('./feature_types/multi_feature')
MultiPoint: require('./feature_types/multi_feature'),
GeometryCollection: require('./feature_types/geometryCollection').default
};

module.exports = function(ctx, api) {
Expand All @@ -25,18 +25,18 @@ module.exports = function(ctx, api) {
return features.map(feature => feature.properties.id);
};

api.getSelectedIds = function () {
api.getSelectedIds = function() {
return ctx.store.getSelectedIds();
};

api.getSelected = function () {
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 () {
api.getSelectedPoints = function() {
return {
type: Constants.geojsonTypes.FEATURE_COLLECTION,
features: ctx.store.getSelectedCoordinates().map(coordinate => {
Expand Down Expand Up @@ -70,7 +70,7 @@ module.exports = function(ctx, api) {
return newIds;
};

api.add = function (geojson) {
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);
Expand All @@ -96,8 +96,8 @@ module.exports = function(ctx, api) {
// 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);
if (!internalFeature.isEqual(feature)) {
internalFeature.update(feature);
}
}
return feature.id;
Expand All @@ -108,7 +108,7 @@ module.exports = function(ctx, api) {
};


api.get = function (id) {
api.get = function(id) {
const feature = ctx.store.get(id);
if (feature) {
return feature.toGeoJSON();
Expand Down
11 changes: 11 additions & 0 deletions src/feature_types/feature.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const hat = require('hat');
const Constants = require('../constants');
const isEqual = require('lodash.isequal');

const Feature = function(ctx, geojson) {
this.ctx = ctx;
Expand Down Expand Up @@ -67,4 +68,14 @@ Feature.prototype.internal = function(mode) {
};
};

Feature.prototype.isEqual = function(feature) {
return isEqual(this.getCoordinates(), feature.geometry.coordinates);
};

Feature.prototype.update = function(feature) {
if (!this.isEqual(feature)) {
this.incomingCoords(feature.geometry.coordinates);
}
};

module.exports = Feature;
115 changes: 115 additions & 0 deletions src/feature_types/geometryCollection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import Constants from '../constants';
import hat from 'hat';
const isEqual = require('lodash.isequal');

const models = {
Point: require('./point'),
LineString: require('./line_string'),
Polygon: require('./polygon')
};

const GeometryCollection = function(ctx, geojson) {
this.ctx = ctx;
this.id = geojson.id || hat();
this.type = 'GeometryCollection';
this.properties = geojson.properties;
this.features = geojson.geometry.geometries.map(g => {
return this._coordinatesToFeatures(g);
});
};
GeometryCollection.prototype._coordinatesToFeatures = function(geometry) {
const model = models[geometry.type];
if (model === undefined) throw new TypeError(`${geometry.type} is not a valid type`);

const Model = model.bind(this);
return new Model(this.ctx, {
id: hat(),
type: Constants.geojsonTypes.FEATURE,
properties: {},
geometry
});
};
GeometryCollection.prototype.changed = function() {
this.ctx.store.featureChanged(this.id);
};
GeometryCollection.prototype.isValid = function() {
return this.features.every(f => f.isValid());
};
GeometryCollection.prototype.incomingCoords = function() {
throw Error('incomingCoords');
};
GeometryCollection.prototype.setCoordinates = function() {
throw Error('setCoordinates');
};
GeometryCollection.prototype.getCoordinates = function() {
throw Error('getCoordinates');
};
GeometryCollection.prototype.setProperty = function(property, value) {
this.properties[property] = value;
};
GeometryCollection.prototype.toGeoJSON = function() {
const result = JSON.parse(JSON.stringify({
id: this.id,
type: Constants.geojsonTypes.FEATURE,
properties: this.properties,
geometry: {
geometries: this.features.map(f => {
return {
type: f.type,
coordinates: f.getCoordinates()
};
}),
type: this.type
}
}));
return result;
};
GeometryCollection.prototype.internal = function(mode) {
const properties = {
id: this.id,
meta: Constants.meta.FEATURE,
'meta:type': this.type,
active: Constants.activeStates.INACTIVE,
mode: mode
};

if (this.ctx.options.userProperties) {
for (const name in this.properties) {
properties[`user_${name}`] = this.properties[name];
}
}
const result = {
type: Constants.geojsonTypes.FEATURE,
properties: properties,
geometry: {
type: this.type,
geometries: this.features.map(f => {
return {
type: f.type,
coordinates: f.getCoordinates()
};
})
}
};
return result;
};

GeometryCollection.prototype.isEqual = function(feature) {
const { geometries } = feature.geometry;
return this.features.every((myf, index) => {
return isEqual(myf.getCoordinates(), geometries[index].coordinates);
});
};

GeometryCollection.prototype.update = function(feature) {
const { geometries } = feature.geometry;
this.features.forEach((myf, index) => {
const coordinates = geometries[index].coordinates;
if (!isEqual(myf.getCoordinates(), coordinates)) {
myf.incomingCoords(coordinates);
}
});
};

export default GeometryCollection;

2 changes: 1 addition & 1 deletion test/feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('Feature contrusctor and API', t => {
t.equal(typeof Feature.prototype.toGeoJSON, 'function', 'feature.toGeoJSON');
t.equal(typeof Feature.prototype.internal, 'function', 'feature.internal');
t.equal(typeof Feature.prototype.setProperty, 'function', 'feature.setProperty');
t.equal(getPublicMemberKeys(Feature.prototype).length, 7, 'no unexpected prototype members');
t.equal(getPublicMemberKeys(Feature.prototype).length, 9, 'no unexpected prototype members');

const simpleFeatureGeoJson = {
type: 'Feature',
Expand Down