Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
fix(osmtogeojson): order of coordinates + parseFloat
Browse files Browse the repository at this point in the history
  • Loading branch information
toutpt committed May 18, 2016
1 parent 71eb56e commit e915253
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 66 deletions.
46 changes: 22 additions & 24 deletions src/togeojson/togeojson.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,45 @@ function factory(options) {
}

for (let i = 0; i < data.length; i++) {
var feature = {};
var d = data[i];
feature.properties = {
id: d._id,
tags: d.tags
var feature = {
type: 'Feature',
geometry: {},
properties: {
id: d.id,
tags: d.tags
}
};
if (d.type === "changeset") {
//build rectangle
// X = Long; Y = Lat, lets do it clockwise
feature.type = 'Polygon';
feature.geometry.type = 'Polygon';
var bounds = d.latLngBounds;
feature.coordinates = [
[bounds._min_lon, bounds._min_lat],
[bounds._min_lon, bounds._max_lat],
[bounds._max_lon, bounds._max_lat],
[bounds._max_lon, bounds._min_lat]
feature.geometry.coordinates = [
[parseFloat(bounds._min_lon), parseFloat(bounds._min_lat)],
[parseFloat(bounds._min_lon), parseFloat(bounds._max_lat)],
[parseFloat(bounds._max_lon), parseFloat(bounds._max_lat)],
[parseFloat(bounds._max_lon), parseFloat(bounds._min_lat)]
];
} else if (d.type === "node") {
//add a Point
feature.type = 'Point';
feature.coordinates = [d.latLng[1], d.latLng[0]];
feature.geometry.type = 'Point';
feature.geometry.coordinates = [d.latLng[1], d.latLng[0]];
} else {
var lngLats = new Array(d.nodes.length);

for (let j = 0; j < d.nodes.length; j++) {
lngLats[j] = d.nodes[j].latLng;
let latLng = d.nodes[j].latLng;
lngLats[j] = [latLng[1], latLng[0]];
}

if (isWayArea(d)) {
lngLats.pop(); // Remove last == first.
feature.type = 'Polygon';
feature.coordinates = lngLats;
feature.geometry.type = 'Polygon';
feature.geometry.coordinates = lngLats;
} else {
feature.type = 'MultiLineString';
feature.coordinates = lngLats;
feature.geometry.type = 'LineString';
feature.geometry.coordinates = lngLats;
}
}
features.push(feature);
Expand Down Expand Up @@ -113,7 +117,7 @@ function factory(options) {
nodesById[node._id] = {
id: node._id,
type: 'node',
latLng: [node._lon, node._lat],
latLng: [parseFloat(node._lat), parseFloat(node._lon)],
tags: getTags(node)
};
});
Expand Down Expand Up @@ -219,12 +223,6 @@ function factory(options) {
}
}

for (let key in node.tags) {
if (options.uninterestingTags.indexOf(key) < 0) {
return true;
}
}

return false;
}
function togeojson(data, options) {
Expand Down
76 changes: 34 additions & 42 deletions src/togeojson/togeojson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ ngDescribe({
expect(nodes['3'].id).toBe(3);
expect(nodes['3'].type).toBe('node');
expect(nodes['3'].latLng.length).toBe(2);
expect(nodes['3'].latLng[0]).toBe(321);
expect(nodes['3'].latLng[1]).toBe(123);
expect(nodes['3'].latLng[0]).toBe(123);
expect(nodes['3'].latLng[1]).toBe(321);
expect(nodes['3'].tags.highway).toBe('mini_roundabout');

//must work the same if node is array & tag is array of tags
Expand All @@ -76,8 +76,8 @@ ngDescribe({
expect(nodes['3'].id).toBe(3);
expect(nodes['3'].type).toBe('node');
expect(nodes['3'].latLng.length).toBe(2);
expect(nodes['3'].latLng[0]).toBe(321);
expect(nodes['3'].latLng[1]).toBe(123);
expect(nodes['3'].latLng[0]).toBe(123);
expect(nodes['3'].latLng[1]).toBe(321);
expect(nodes['3'].tags.highway).toBe(undefined);

});
Expand Down Expand Up @@ -251,14 +251,6 @@ ngDescribe({
result = deps.osmtogeojson.interestingNode(node, ways, relations);
expect(result).toBe(true);

relations = [];
node.tags = {name: 'toto'};
result = deps.osmtogeojson.interestingNode(node, ways, relations);
expect(result).toBe(true);

node.tags = {source: 'uninterestingTags'};
result = deps.osmtogeojson.interestingNode(node, ways, relations);
expect(result).toBe(false);
});

it('should getFeatures to work with changeset', function() {
Expand All @@ -274,16 +266,16 @@ ngDescribe({
}];
var features = deps.osmtogeojson.getFeatures(data);
expect(features.length).toBe(1);
expect(features[0].type).toBe('Polygon');
expect(features[0].coordinates[0][0]).toBe(-234);
expect(features[0].coordinates[0][1]).toBe(-123);
expect(features[0].coordinates[1][0]).toBe(-234);
expect(features[0].coordinates[1][1]).toBe(123);
expect(features[0].coordinates[2][0]).toBe(234);
expect(features[0].coordinates[2][1]).toBe(123);
expect(features[0].coordinates[3][0]).toBe(234);
expect(features[0].coordinates[3][1]).toBe(-123);
expect(features[0].coordinates[4]).toBe();
expect(features[0].geometry.type).toBe('Polygon');
expect(features[0].geometry.coordinates[0][0]).toBe(-234);
expect(features[0].geometry.coordinates[0][1]).toBe(-123);
expect(features[0].geometry.coordinates[1][0]).toBe(-234);
expect(features[0].geometry.coordinates[1][1]).toBe(123);
expect(features[0].geometry.coordinates[2][0]).toBe(234);
expect(features[0].geometry.coordinates[2][1]).toBe(123);
expect(features[0].geometry.coordinates[3][0]).toBe(234);
expect(features[0].geometry.coordinates[3][1]).toBe(-123);
expect(features[0].geometry.coordinates[4]).toBe();
});
it('should getFeatures to work with node', function() {
var data = [{
Expand All @@ -294,10 +286,10 @@ ngDescribe({
}];
var features = deps.osmtogeojson.getFeatures(data);
expect(features.length).toBe(1);
expect(features[0].type).toBe('Point');
expect(features[0].coordinates[0]).toBe(234);
expect(features[0].coordinates[1]).toBe(123);
expect(features[0].coordinates[2]).toBe();
expect(features[0].geometry.type).toBe('Point');
expect(features[0].geometry.coordinates[0]).toBe(234);
expect(features[0].geometry.coordinates[1]).toBe(123);
expect(features[0].geometry.coordinates[2]).toBe();
});
it('should getFeatures to work with way', function() {
var n1 = {
Expand All @@ -320,13 +312,13 @@ ngDescribe({
}];
var features = deps.osmtogeojson.getFeatures(data);
expect(features.length).toBe(1);
expect(features[0].type).toBe('MultiLineString');
expect(features[0].geometry.type).toBe('LineString');
expect(features[0].properties.tags.name).toBe('my way');
expect(features[0].coordinates[0][0]).toBe(123);
expect(features[0].coordinates[0][1]).toBe(234);
expect(features[0].coordinates[1][0]).toBe(1234);
expect(features[0].coordinates[1][1]).toBe(2345);
expect(features[0].coordinates[2]).toBe();
expect(features[0].geometry.coordinates[0][0]).toBe(234);
expect(features[0].geometry.coordinates[0][1]).toBe(123);
expect(features[0].geometry.coordinates[1][0]).toBe(2345);
expect(features[0].geometry.coordinates[1][1]).toBe(1234);
expect(features[0].geometry.coordinates[2]).toBe();
});

it('should getFeatures to work with way (area)', function() {
Expand Down Expand Up @@ -356,14 +348,14 @@ ngDescribe({
}];
var features = deps.osmtogeojson.getFeatures(data);
expect(features.length).toBe(1);
expect(features[0].type).toBe('Polygon');
expect(features[0].coordinates[0][0]).toBe(1);
expect(features[0].coordinates[0][1]).toBe(2);
expect(features[0].coordinates[1][0]).toBe(12);
expect(features[0].coordinates[1][1]).toBe(23);
expect(features[0].coordinates[2][0]).toBe(123);
expect(features[0].coordinates[2][1]).toBe(234);
expect(features[0].coordinates[3]).toBe();
expect(features[0].geometry.type).toBe('Polygon');
expect(features[0].geometry.coordinates[0][0]).toBe(2);
expect(features[0].geometry.coordinates[0][1]).toBe(1);
expect(features[0].geometry.coordinates[1][0]).toBe(23);
expect(features[0].geometry.coordinates[1][1]).toBe(12);
expect(features[0].geometry.coordinates[2][0]).toBe(234);
expect(features[0].geometry.coordinates[2][1]).toBe(123);
expect(features[0].geometry.coordinates[3]).toBe();
});


Expand Down Expand Up @@ -422,8 +414,8 @@ ngDescribe({
expect(geojson.features.length).toBe(2);
expect(Array.isArray(geojson.features)).toBe(true);
expect(geojson.features[1].properties.tags.highway).toBe('mini_roundabout');
expect(geojson.features[1].type).toBe('MultiLineString');
expect(geojson.features[0].type).toBe('Point');
expect(geojson.features[1].geometry.type).toBe('LineString');
expect(geojson.features[0].geometry.type).toBe('Point');
});

}
Expand Down

0 comments on commit e915253

Please sign in to comment.