Skip to content

Commit

Permalink
Merge pull request #160 from willemarcel/changeset-osmcha-link
Browse files Browse the repository at this point in the history
add link to osmcha in oldversion changeset id and change indentation to 2 spaces
  • Loading branch information
Sanjay Bhangar authored Sep 26, 2017
2 parents 10e1775 + 11361a9 commit fbad275
Show file tree
Hide file tree
Showing 13 changed files with 1,343 additions and 1,313 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
],
"rules": {
"space-before-function-paren": 0,
"object-curly-spacing": 0
"object-curly-spacing": 0,
"indent": [2, 2]
},
"env": {
"browser": true,
Expand All @@ -20,4 +21,4 @@
"ignore": [
"dist",
]
}
}
9 changes: 5 additions & 4 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const config = {
overpassBase: '//overpass-cfn-production.tilestream.net/api/interpreter',
osmBase: '//www.openstreetmap.org/api/0.6/',
mapboxAccessToken: 'pk.eyJ1IjoicmFzYWd5IiwiYSI6ImNpejVrMjc4eTAwNGczM2thNWozYnJ1OHkifQ.yFRr3Sd39TJiwEguQpIkWQ',
S3_URL: '//s3.amazonaws.com/mapbox/real-changesets/production/'
overpassBase: '//overpass-cfn-production.tilestream.net/api/interpreter',
osmBase: '//www.openstreetmap.org/api/0.6/',
mapboxAccessToken:
'pk.eyJ1IjoicmFzYWd5IiwiYSI6ImNpejVrMjc4eTAwNGczM2thNWozYnJ1OHkifQ.yFRr3Sd39TJiwEguQpIkWQ',
S3_URL: '//s3.amazonaws.com/mapbox/real-changesets/production/'
};
104 changes: 52 additions & 52 deletions lib/getChangeset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,91 @@ import { query } from './query';
import { config } from './config';

export function getChangeset(changesetID, overpassBase = config.overpassBase) {
return query(changesetID).then(changeset => {
var url = config.S3_URL + changesetID + '.json';
return fetch(url)
return query(changesetID).then(changeset => {
var url = config.S3_URL + changesetID + '.json';
return fetch(url)
.then(r => {
if (r.ok) return r.json();
if (r.ok) return r.json();
// Fallback to overpass
return Promise.reject();
return Promise.reject();
})
.then(r => {
var geojson = jsonParser(r);
var featureMap = getFeatureMap(geojson);
var ret = {
geojson: geojson,
featureMap: featureMap,
changeset: changeset
};
return ret;
var geojson = jsonParser(r);
var featureMap = getFeatureMap(geojson);
var ret = {
geojson: geojson,
featureMap: featureMap,
changeset: changeset
};
return ret;
})
.catch(() => fetchFromOverPass(changesetID, changeset, overpassBase));
});
});
}

function fetchFromOverPass(changesetID, changeset, overpassBase) {
var data = getDataParam(changeset);
var bbox = getBboxParam(changeset.bbox);
var url = overpassBase + '?data=' + data + '&bbox=' + bbox;
var data = getDataParam(changeset);
var bbox = getBboxParam(changeset.bbox);
var url = overpassBase + '?data=' + data + '&bbox=' + bbox;

return fetch(url, {
'Response-Type': 'application/osm3s+xml'
})
return fetch(url, {
'Response-Type': 'application/osm3s+xml'
})
.then(r => r.text())
.then(response => {
return new Promise((res, rej) => {
adiffParser(response, null, (err, json) => {
if (err) {
return rej({
msg: 'Failed to parser adiff xml.',
error: err
});
}
var geojson = jsonParser({
elements: json[changesetID]
});
var featureMap = getFeatureMap(geojson);

var ret = {
geojson: geojson,
featureMap: featureMap,
changeset: changeset
};
return res(ret);
return new Promise((res, rej) => {
adiffParser(response, null, (err, json) => {
if (err) {
return rej({
msg: 'Failed to parser adiff xml.',
error: err
});
}
var geojson = jsonParser({
elements: json[changesetID]
});
var featureMap = getFeatureMap(geojson);

var ret = {
geojson: geojson,
featureMap: featureMap,
changeset: changeset
};
return res(ret);
});
});
})
.catch(err =>
Promise.reject({
msg: 'Overpass query failed.',
error: err
msg: 'Overpass query failed.',
error: err
})
);
}

function getDataParam(c) {
return (
return (
'[out:xml][adiff:%22' +
c.from.toString() +
',%22,%22' +
c.to.toString() +
'%22];(node(bbox)(changed);way(bbox)(changed);relation(bbox)(changed));out%20meta%20geom(bbox);'
);
);
}

function getBboxParam(bbox) {
return [bbox.left, bbox.bottom, bbox.right, bbox.top].join(',');
return [bbox.left, bbox.bottom, bbox.right, bbox.top].join(',');
}

function getFeatureMap(geojson) {
var features = geojson.features;
var featureMap = {};
var features = geojson.features;
var featureMap = {};

for (var i = 0, len = features.length; i < len; i++) {
var id = features[i].properties.id;
featureMap[id] = featureMap[id] || [];
featureMap[id].push(features[i]);
}
for (var i = 0, len = features.length; i < len; i++) {
var id = features[i].properties.id;
featureMap[id] = featureMap[id] || [];
featureMap[id].push(features[i]);
}

return featureMap;
return featureMap;
}
44 changes: 22 additions & 22 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ import turfBboxPolygon from '@turf/bbox-polygon';
const featureCollection = turfHelpers.featureCollection;

export function getBounds(bbox) {
var left = +bbox.left,
right = +bbox.right,
top = +bbox.top,
bottom = +bbox.bottom;
var left = +bbox.left,
right = +bbox.right,
top = +bbox.top,
bottom = +bbox.bottom;

return new mapboxgl.LngLatBounds(
return new mapboxgl.LngLatBounds(
new mapboxgl.LngLat(left, bottom),
new mapboxgl.LngLat(right, top)
);
}

export function getBoundingBox(bounds) {
var left = bounds.getWest(),
right = bounds.getEast(),
top = bounds.getNorth(),
bottom = bounds.getSouth();
var left = bounds.getWest(),
right = bounds.getEast(),
top = bounds.getNorth(),
bottom = bounds.getSouth();

var padX = 0;
var padY = 0;
if (!(left === -180 && right === 180 && top === 90 && bottom === -90)) {
padX = Math.max((right - left) / 5, 0.0001);
padY = Math.max((top - bottom) / 5, 0.0001);
}
var padX = 0;
var padY = 0;
if (!(left === -180 && right === 180 && top === 90 && bottom === -90)) {
padX = Math.max((right - left) / 5, 0.0001);
padY = Math.max((top - bottom) / 5, 0.0001);
}

var bboxPolygon = turfBboxPolygon([
left - padX,
bottom - padY,
right + padX,
top + padY
]);
var bboxPolygon = turfBboxPolygon([
left - padX,
bottom - padY,
right + padX,
top + padY
]);

return featureCollection([bboxPolygon]);
return featureCollection([bboxPolygon]);
}
Loading

0 comments on commit fbad275

Please sign in to comment.