Skip to content

Instantly share code, notes, and snippets.

@cspanring
Last active December 14, 2015 16:19
Show Gist options
  • Save cspanring/5114078 to your computer and use it in GitHub Desktop.
Save cspanring/5114078 to your computer and use it in GitHub Desktop.
Dabbling with GeoJSON and ember.js
// The GeoJSON object
//
// {
// "type": "FeatureCollection",
// "features": [
// {
// "type": "Feature",
// "geometry": {
// "type": "Point",
// "coordinates": [42.3875, -71.1]
// },
// "properties": {
// "prop0": "value0"
// }
// }
// ]
// }
// ...as ember.js model
App.Adapter = DS.RESTAdapter.extend();
App.Store = DS.Store.extend({
revision: 12,
adapter: 'App.Adapter'
});
App.FeatureCollection = DS.Model.extend({
type: DS.attr('string'),
features: DS.hasMany('App.Feature')
});
App.Adapter.map('App.FeatureCollection', {
features: { embedded: 'always' }
});
App.Feature = DS.Model.extend({
type: DS.attr('string'),
featurecollection: DS.belongsTo('App.FeatureCollection'),
geometry: DS.belongsTo('App.Geometry'),
properties: DS.belongsTo('App.Properties')
});
App.Adapter.map('App.Feature', {
geometry: { embedded: 'always' },
properties: { embedded: 'always' }
});
// a point
DS.RESTAdapter.registerTransform('point', {
serialize: function(value) {
return [value.get('x'), value.get('y')];
},
deserialize: function(value) {
return Ember.create({ x: value[1], y: value[0] });
}
});
App.Geometry = DS.Model.extend({
feature: DS.belongsTo('App.Feature'),
type: DS.attr('string'),
coordinates: DS.attr('point')
});
App.Properties = DS.Model.extend({
feature: DS.belongsTo('App.Feature'),
prop0: DS.attr('string')
});
// ...should behave like...
var feature = App.Feature.find(1);
feature.get('geometry.coordinates.x');
feature.get('geometry.coordinates.y');
feature.get('properties.prop0');
@miguelcobain
Copy link

From what I can see, this only supports Point Geometries, like on the provided example.
LineString geometries have an array of points as coordinates. Trying to implement this using ember-data models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment