Skip to content

Commit

Permalink
Merge pull request #3300 from 1ec5/role-taginfo
Browse files Browse the repository at this point in the history
taginfo for relation roles
  • Loading branch information
bhousel authored Aug 3, 2016
2 parents 0d2f78c + c149ac8 commit f7530a0
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
50 changes: 50 additions & 0 deletions modules/services/taginfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ var taginfo = {},
area: 'count_ways',
line: 'count_ways'
},
tag_sort_members = {
point: 'count_node_members',
vertex: 'count_node_members',
area: 'count_way_members',
line: 'count_way_members',
relation: 'count_relation_members'
},
tag_filters = {
point: 'nodes',
vertex: 'nodes',
area: 'ways',
line: 'ways'
};
tag_members_fractions = {
point: 'count_node_members_fraction',
vertex: 'count_node_members_fraction',
area: 'count_way_members_fraction',
line: 'count_way_members_fraction',
relation: 'count_relation_members_fraction'
};


function sets(parameters, n, o) {
Expand All @@ -32,6 +46,10 @@ function setSort(parameters) {
return sets(parameters, 'sortname', tag_sorts);
}

function setSortMembers(parameters) {
return sets(parameters, 'sortname', tag_sort_members);
}

function clean(parameters) {
return _.omit(parameters, 'geometry', 'debounce');
}
Expand All @@ -57,6 +75,14 @@ function filterValues(allowUpperCase) {
};
}

function filterRoles(geometry) {
return function(d) {
if (d.role === '') return false; // exclude empty role
if (d.role.match(/[A-Z*;,]/) !== null) return false; // exclude uppercase letters and some punctuation
return parseFloat(d[tag_members_fractions[geometry]]) > 0.0;
};
}

function valKey(d) {
return {
value: d.key,
Expand All @@ -71,6 +97,13 @@ function valKeyDescription(d) {
};
}

function roleKey(d) {
return {
value: d.role,
title: d.role
};
}

// sort keys with ':' lower than keys without ':'
function sortKeys(a, b) {
return (a.key.indexOf(':') === -1 && b.key.indexOf(':') !== -1) ? -1
Expand Down Expand Up @@ -146,6 +179,23 @@ export function init() {
});
};

taginfo.roles = function(parameters, callback) {
var debounce = parameters.debounce;
var geometry = parameters.geometry;
parameters = clean(setSortMembers(parameters));
request(endpoint + 'relation/roles?' +
qsString(_.extend({
rp: 25,
sortname: 'count_all_members',
sortorder: 'desc',
page: 1
}, parameters)), debounce, function(err, d) {
if (err) return callback(err);
var f = filterRoles(geometry);
callback(null, d.data.filter(f).map(roleKey));
});
};

taginfo.docs = function(parameters, callback) {
var debounce = parameters.debounce;
parameters = clean(setSort(parameters));
Expand Down
43 changes: 43 additions & 0 deletions modules/ui/raw_member_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,51 @@ export function RawMemberEditor(context) {
.on('click', deleteMember)
.call(Icon('#operation-delete'));

if (context.taginfo()) {
$enter.each(bindTypeahead);
}

$items.exit()
.each(unbind)
.remove();

function bindTypeahead(d) {
var row = d3.select(this),
role = row.selectAll('input.member-role');

function sort(value, data) {
var sameletter = [],
other = [];
for (var i = 0; i < data.length; i++) {
if (data[i].value.substring(0, value.length) === value) {
sameletter.push(data[i]);
} else {
other.push(data[i]);
}
}
return sameletter.concat(other);
}

role.call(d3.combobox()
.fetcher(function(role, callback) {
var rtype = entity.tags.type;
context.taginfo().roles({
debounce: true,
rtype: rtype || '',
geometry: context.geometry(d.member.id),
query: role
}, function(err, data) {
if (!err) callback(sort(role, data));
});
}));
}

function unbind() {
var row = d3.select(this);

row.selectAll('input.member-role')
.call(d3.combobox.off);
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions modules/ui/raw_membership_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ export function RawMembershipEditor(context) {
.on('click', deleteMembership)
.call(Icon('#operation-delete'));

if (context.taginfo()) {
$enter.each(bindTypeahead);
}

$items.exit()
.each(unbind)
.remove();

if (showBlank) {
Expand Down Expand Up @@ -213,6 +218,44 @@ export function RawMembershipEditor(context) {
content($wrap);
$list.selectAll('.member-entity-input').node().focus();
});

function bindTypeahead(d) {
var row = d3.select(this),
role = row.selectAll('input.member-role');

function sort(value, data) {
var sameletter = [],
other = [];
for (var i = 0; i < data.length; i++) {
if (data[i].value.substring(0, value.length) === value) {
sameletter.push(data[i]);
} else {
other.push(data[i]);
}
}
return sameletter.concat(other);
}

role.call(d3.combobox()
.fetcher(function(role, callback) {
var rtype = d.relation.tags.type;
context.taginfo().roles({
debounce: true,
rtype: rtype || '',
geometry: context.geometry(id),
query: role
}, function(err, data) {
if (!err) callback(sort(role, data));
});
}));
}

function unbind() {
var row = d3.select(this);

row.selectAll('input.member-role')
.call(d3.combobox.off);
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/spec/services/taginfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ describe('iD.services.taginfo', function() {
});
});

describe('#roles', function() {
it('calls the given callback with the results of the roles query', function() {
var callback = sinon.spy();
taginfo.roles({rtype: 'route', query: 's', geometry: 'relation'}, callback);

server.respondWith('GET', new RegExp('https://taginfo.openstreetmap.org/api/4/relation/roles'),
[200, { 'Content-Type': 'application/json' },
'{"data":[{"role":"stop","count_relation_members_fraction":0.1757},' +
'{"role":"south","count_relation_members_fraction":0.0035}]}']);
server.respond();

expect(query(server.requests[0].url)).to.eql(
{rtype: 'route', query: 's', page: '1', rp: '25', sortname: 'count_relation_members', sortorder: 'desc'});
expect(callback).to.have.been.calledWith(null, [
{'value': 'stop', 'title': 'stop'},
{'value': 'south', 'title': 'south'}
]);
});
});

describe('#docs', function() {
it('calls the given callback with the results of the docs query', function() {
var callback = sinon.spy();
Expand Down

0 comments on commit f7530a0

Please sign in to comment.