Skip to content

Commit

Permalink
mapnik.Feature.intersects(x,y)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Dec 4, 2013
1 parent 2843f9f commit 592844e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
46 changes: 46 additions & 0 deletions src/mapnik_feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include <mapnik/unicode.hpp>
#include <mapnik/feature_factory.hpp>
#include <mapnik/value_types.hpp>
#include <mapnik/geom_util.hpp>

// boost
#include <boost/version.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/foreach.hpp>

#if MAPNIK_VERSION >= 200100
#include <mapnik/json/geojson_generator.hpp>
Expand All @@ -29,6 +31,7 @@ void Feature::Initialize(Handle<Object> target) {

NODE_SET_PROTOTYPE_METHOD(constructor, "id", id);
NODE_SET_PROTOTYPE_METHOD(constructor, "extent", extent);
NODE_SET_PROTOTYPE_METHOD(constructor, "intersects", intersects);
NODE_SET_PROTOTYPE_METHOD(constructor, "attributes", attributes);
NODE_SET_PROTOTYPE_METHOD(constructor, "addGeometry", addGeometry);
NODE_SET_PROTOTYPE_METHOD(constructor, "addAttributes", addAttributes);
Expand Down Expand Up @@ -119,6 +122,49 @@ Handle<Value> Feature::extent(const Arguments& args)
return scope.Close(a);
}

Handle<Value> Feature::intersects(const Arguments& args)
{
HandleScope scope;
if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsNumber())
{
return ThrowException(Exception::Error(
String::New("expects x,y args (in coordinate system of the data)")));
}
double tolerance = 0.0; // meters
if (args.Length() > 2)
{
Local<Object> options = Object::New();
if (!args[2]->IsObject())
{
return ThrowException(Exception::TypeError(String::New("optional third argument must be an options object")));
}
options = args[2]->ToObject();
if (options->Has(String::NewSymbol("tolerance")))
{
Local<Value> tol = options->Get(String::New("tolerance"));
if (!tol->IsNumber())
{
return ThrowException(Exception::TypeError(String::New("tolerance value must be a number")));
}
tolerance = tol->NumberValue();
}
}

double x = args[0]->NumberValue();
double y = args[1]->NumberValue();

Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());
bool hit = false;
BOOST_FOREACH ( mapnik::geometry_type const& geom, fp->get()->paths() )
{
if (mapnik::label::hit_test(geom,x,y,tolerance))
{
hit = true;
break;
}
}
return scope.Close(Boolean::New(hit));
}
Handle<Value> Feature::attributes(const Arguments& args)
{
HandleScope scope;
Expand Down
1 change: 1 addition & 0 deletions src/mapnik_feature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Feature: public node::ObjectWrap {
static Handle<Value> New(mapnik::feature_ptr f_ptr);
static Handle<Value> id(const Arguments &args);
static Handle<Value> extent(const Arguments &args);
static Handle<Value> intersects(const Arguments &args);
static Handle<Value> attributes(const Arguments &args);
static Handle<Value> addGeometry(const Arguments &args);
static Handle<Value> addAttributes(const Arguments &args);
Expand Down
39 changes: 34 additions & 5 deletions test/feature.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ describe('mapnik.Feature ', function() {
type: 'Polygon',
coordinates: [[[1,1],[1,2],[2,2],[2,1],[1,1]]]
}
},

ds = new mapnik.Datasource({type:'csv', 'inline': "geojson\n'" + JSON.stringify(expected.geometry) + "'"}),
f = ds.featureset().next(),
feature = JSON.parse(f.toJSON());
};
var ds = new mapnik.Datasource({type:'csv', 'inline': "geojson\n'" + JSON.stringify(expected.geometry) + "'"});
var f = ds.featureset().next();
var feature = JSON.parse(f.toJSON());

assert.equal(expected.type, feature.type);
assert.deepEqual(expected.properties, feature.properties);
Expand All @@ -80,4 +79,34 @@ describe('mapnik.Feature ', function() {
assert.deepEqual(expected.geometry.coordinates, feature.geometry.coordinates);
}
});

it('should allow pt to pt intersect test',function() {
var extent = '-180,-60,180,60';
var ds = new mapnik.MemoryDatasource({'extent': extent});
var feat = {x:0,y:0,properties: {feat_id:1,null_val:null,name:"name"}};
ds.add(feat);
var featureset = ds.featureset();
var feature = featureset.next();
assert.equal(feature.id(),1);
assert.equal(feature.intersects(1,1,{tolerance:1.5}),true);
assert.equal(feature.intersects(1,1,{tolerance:0}),false);
});

it('should allow pt in polygon intersect test',function() {
var expected = {
type: "Feature",
properties: {},
geometry: {
type: 'Polygon',
coordinates: [[[1,1],[1,2],[2,2],[2,1],[1,1]]]
}
};

var ds = new mapnik.Datasource({type:'csv', 'inline': "geojson\n'" + JSON.stringify(expected.geometry) + "'"});
var feature = ds.featureset().next();
assert.equal(feature.id(),1);
assert.equal(feature.intersects(1.5,1.5),true);
assert.equal(feature.intersects(0,0),false);
});

});

0 comments on commit 592844e

Please sign in to comment.