Skip to content
This repository was archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
removed deferred from add method in geostore
Browse files Browse the repository at this point in the history
  • Loading branch information
chelm committed Aug 15, 2013
1 parent 709e9ad commit c25a929
Showing 1 changed file with 69 additions and 77 deletions.
146 changes: 69 additions & 77 deletions src/geostore.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,6 @@
// calculate the envelope and add it to the rtree
// should return a deferred
GeoStore.prototype.add = function(geojson, callback){
var dfd = new this.deferred(), bbox;

if(callback){
dfd.then(function(result){
callback(null, result);
}, function(error){
callback(error, null);
});
}

if (!geojson.type.match(/Feature/)) {
throw new Error("Terraform.GeoStore : only Features and FeatureCollections are supported");
Expand All @@ -99,7 +90,7 @@
h: Math.abs(bbox[1] - bbox[3])
}, feature.id);
}
this.store.add(geojson, dfd);
this.store.add(geojson, callback );
} else {
bbox = Terraformer.Tools.calculateBounds(geojson);
this.index.insert({
Expand All @@ -108,13 +99,10 @@
w: Math.abs(bbox[0] - bbox[2]),
h: Math.abs(bbox[1] - bbox[3])
}, geojson.id);
this.store.add(geojson, dfd);
this.store.add(geojson, callback );
}

// store the data (use the stores store method to decide how to do this.)

// return the deferred;
return dfd;
};

GeoStore.prototype.remove = function(id, callback){
Expand All @@ -127,18 +115,19 @@
callback(error, null);
});
}

this.get(id).then(bind(this, function(geojson){
this.index.remove(geojson, id, bind(this, function(error, leaf){
if(error){
dfd.reject("Could not remove from index");
} else {
this.store.remove(id, dfd);
}
}));
}), function(error){
dfd.reject("Could not remove feature");
});
this.get(id, bind(this, function(error, geojson){
if ( error ){
callback("Could not get feature to remove", null);
} else {
this.index.remove(geojson, id, bind(this, function(error, leaf){
if(error){
callback("Could not remove from index", null);
} else {
this.store.remove(id, callback);
}
}));
}
}));

return dfd;
};
Expand Down Expand Up @@ -168,22 +157,24 @@
// the function to evalute results from the index
var evaluate = function(primitive){
completed++;
var geometry = new Terraformer.Primitive(primitive.geometry);
if ( primitive ){
var geometry = new Terraformer.Primitive(primitive.geometry);

if(shape.within(geometry)){
results.push(primitive);
}
if(shape.within(geometry)){
results.push(primitive);
}

if(completed >= found.length){
if(!errors){
dfd.resolve(results);
} else {
dfd.reject("Could not get all geometries");
if(completed >= found.length){
if(!errors){
dfd.resolve(results);
} else {
dfd.reject("Could not get all geometries");
}
}
}

if(completed >= found.length && errors){
dfd.reject("Could not get all geometries");
if(completed >= found.length && errors){
dfd.reject("Could not get all geometries");
}
}

};
Expand All @@ -198,12 +189,17 @@

// for each result see if the polygon contains the point
if(found.length){
var getCB = function(err, result){
if (err) error();
else evaluate( result );
};

for (var i = 0; i < found.length; i++) {
this.get(found[i]).then(evaluate, error);
this.get(found[i], getCB);
}
} else {
dfd.resolve(results);
//if ( callback ) callback( null, results );
}

}));
Expand Down Expand Up @@ -237,24 +233,25 @@
// the function to evalute results from the index
var evaluate = function(primitive){
completed++;
var geometry = new Terraformer.Primitive(primitive.geometry);
if ( primitive ){
var geometry = new Terraformer.Primitive(primitive.geometry);

if(geometry.within(shape)){
results.push(primitive);
}
if(geometry.within(shape)){
results.push(primitive);
}

if(completed >= found.length){
if(!errors){
dfd.resolve(results);
} else {
dfd.reject("Could not get all geometries");
if(completed >= found.length){
if(!errors){
dfd.resolve(results);
} else {
dfd.reject("Could not get all geometries");
}
}
}

if(completed >= found.length && errors){
dfd.reject("Could not get all geometries");
if(completed >= found.length && errors){
dfd.reject("Could not get all geometries");
}
}

};

var error = function(){
Expand All @@ -267,9 +264,13 @@

// for each result see if the polygon contains the point
if(found.length){
var getCB = function(err, result){
if (err) error();
else evaluate( result );
};

for (var i = 0; i < found.length; i++) {
this.get(found[i]).then(evaluate, error);
this.get(found[i], getCB);
}
} else {
dfd.resolve(results);
Expand Down Expand Up @@ -301,38 +302,29 @@
throw new Error("Terraform.GeoStore : Feature does not have an id property");
}

this.get(feature.id).then(bind(this, function(oldFeatureGeoJSON){
var oldFeature = new Terraformer.Primitive(oldFeatureGeoJSON);
this.index.remove(oldFeature.envelope(), oldFeature.id);
this.index.insert(feature.envelope(), feature.id);
this.store.update(feature, dfd);
}), function(error){
dfd.reject("Could find feature");
});
this.get(feature.id, bind(this, function( error, oldFeatureGeoJSON ){
if ( error ){
callback("Could find feature", null);
} else {
var oldFeature = new Terraformer.Primitive(oldFeatureGeoJSON);
this.index.remove(oldFeature.envelope(), oldFeature.id);
this.index.insert(feature.envelope(), feature.id);
this.store.update(feature, callback);
}
}));
//, function(error){
// dfd.reject("Could find feature");
//});

return dfd;
};

// gets an item by id
GeoStore.prototype.get = function(id, callback){

// make a new deferred
var dfd = new this.deferred();

if(callback){
dfd.then(function(result){
callback(null, result);
}, function(error){
callback(error, null);
});
}

this.store.get(id, dfd);

return dfd;
this.store.get( id, callback );
};

exports.GeoStore = GeoStore;

return exports;
}));
}));

0 comments on commit c25a929

Please sign in to comment.