aboutsummaryrefslogtreecommitdiff
path: root/resources/op2geojson.js
diff options
context:
space:
mode:
Diffstat (limited to 'resources/op2geojson.js')
-rw-r--r--resources/op2geojson.js178
1 files changed, 89 insertions, 89 deletions
diff --git a/resources/op2geojson.js b/resources/op2geojson.js
index bef1153..6bea219 100644
--- a/resources/op2geojson.js
+++ b/resources/op2geojson.js
@@ -6,76 +6,73 @@ op2geojson = function() {
geojson;
instance.fetch = function(url, data, zoom, callback) {
- $.post(url, data,
- function(data) {
- // Add nodes and ways to the layer
- var features = [];
-
- // Process the data
- var nodes = {};
- var ways = {};
- var relations = {};
- $.each(data.elements, function(i, item) {
- if (item.type === 'node') {
- nodes[item.id] = item;
-
- // As the nodes do not relate to other bits,
- // they can be added here
- if (item.type === 'node'
- && item.tags != undefined
- && item.tags['amenity'] != undefined) {
-
- features.push( instance.point(item) );
- }
- } else if (item.type === 'way') {
- ways[item.id] = item;
- } else if (item.type === 'relation') {
- relations[item.id] = item;
+ var postCallback = function(data) {
+ // Add nodes and ways to the layer
+ var features = [];
+
+ // Process the data
+ var nodes = {};
+ var ways = {};
+ var relations = {};
+ $.each(data.elements, function(i, item) {
+ if (item.type === 'node') {
+ nodes[item.id] = item;
+
+ // As the nodes do not relate to other bits,
+ // they can be added here
+ if (item.tags != undefined && item.tags['amenity'] != undefined) {
+ features.push( instance.point(item) );
}
- });
-
- $.each(ways, function(i, way) {
- if (zoom < 16) {
- var node = nodes[way.nodes[0]];
- var point = {
- "type" : "Feature",
- "id" : way.id,
- "geometry" : {
- "type" : "Point",
- "coordinates" : [node.lon,node.lat]
- },
- "properties" : {}
- };
- _.extend(point.properties, way.tags);
- features.push( point );
- } else {
- features.push( instance.lineString(way, nodes) );
- }
- });
-
- $.each(relations, function(i, relation) {
- if (relation.tags != undefined &&
- relation.tags['type'] == 'boundary' &&
- relation.tags['boundary'] == 'catchment_area') {
-
- features.push( instance.polygon(relation, ways, nodes) );
- }
- });
- geojson = instance.featureCollection(features);
- callback(geojson);
- }
- , "json");;
+ } else if (item.type === 'way') {
+ ways[item.id] = item;
+ } else if (item.type === 'relation') {
+ relations[item.id] = item;
+ }
+ });
+
+ $.each(ways, function(i, way) {
+ if (zoom < 16) {
+ var node = nodes[way.nodes[0]];
+ var point = {
+ type : "Feature",
+ id : way.id,
+ geometry : {
+ type : "Point",
+ coordinates : [node.lon,node.lat]
+ },
+ properties : {}
+ };
+ _.extend(point.properties, way.tags);
+ features.push(point);
+ } else {
+ features.push(instance.lineString(way, nodes));
+ }
+ });
+
+ $.each(relations, function(i, relation) {
+ if (relation.tags != undefined &&
+ relation.tags['type'] == 'boundary' &&
+ relation.tags['boundary'] == 'catchment_area') {
+
+ features.push( instance.polygon(relation, ways, nodes) );
+ }
+ });
+
+ geojson = instance.featureCollection(features);
+ callback(geojson);
+ }
+ $.post(url, data, postCallback, "json");
};
instance.point = function(node) {
var point = {
- "type" : "Feature",
- "geometry" : {
- "type" : "Point",
- "coordinates" : [node.lon,node.lat]
+ type : "Feature",
+ geometry : {
+ type : "Point",
+ coordinates : [node.lon,node.lat]
},
- "properties" : {}
+ properties : {}
};
_.extend(point.properties, node.tags);
return point;
@@ -91,12 +88,12 @@ op2geojson = function() {
// Create the LineString
var lineString = {
- "type" : "Feature",
- "geometry" : {
- "type" : "LineString",
- "coordinates" : coordinates
+ type : "Feature",
+ geometry : {
+ type : "LineString",
+ coordinates : coordinates
},
- "properties" : {}
+ properties : {}
};
// Add the tags
@@ -106,55 +103,58 @@ op2geojson = function() {
instance.featureCollection = function(features) {
collection = {
- "type" : "FeatureCollection",
- "features" : features
+ type : "FeatureCollection",
+ features : features
};
return collection;
}
instance.polygon = function(relation, ways, nodes) {
- polyCoordinates = [];
+ polyCoords = [];
var firstCheck = true;
var subject;
$.each(relation.members, function(i, member) {
if (member.role == "outer") {
var way = ways[member.ref];
- var wayCoordinates = instance.lineString(way, nodes).geometry.coordinates;
+ var wayCoords = instance.lineString(way, nodes).geometry.coordinates;
+ var numNodes = wayCoords.length
// Need to ensure that the first way is in the correct direction, but this can
// only be checked when looking at the second way
- if (firstCheck && polyCoordinates.length != 0) {
+ if (firstCheck && polyCoords.length != 0) {
firstCheck = false;
- if ((polyCoordinates[0][0] == wayCoordinates[0][0] &&
- polyCoordinates[0][1] == wayCoordinates[0][1]) ||
- (polyCoordinates[0][0] == wayCoordinates[wayCoordinates.length - 1][0] &&
- polyCoordinates[0][1] == wayCoordinates[wayCoordinates.length - 1][1])) {
- polyCoordinates.reverse();
+ if ((polyCoords[0][0] == wayCoords[0][0] &&
+ polyCoords[0][1] == wayCoords[0][1]) ||
+ (polyCoords[0][0] == wayCoords[numNodes - 1][0] &&
+ polyCoords[0][1] == wayCoords[numNodes - 1][1])) {
+
+ polyCoords.reverse();
}
}
- if (polyCoordinates.length != 0) {
+ if (polyCoords.length != 0) {
// If this way is backward
- if (polyCoordinates[polyCoordinates.length - 1][0] != wayCoordinates[0][0] ||
- polyCoordinates[polyCoordinates.length - 1][1] != wayCoordinates[0][1]) {
- wayCoordinates.reverse();
+ if (polyCoords[polyCoords.length - 1][0] != wayCoords[0][0] ||
+ polyCoords[polyCoords.length - 1][1] != wayCoords[0][1]) {
+
+ wayCoords.reverse();
}
- polyCoordinates.pop();
+ polyCoords.pop();
}
- polyCoordinates = polyCoordinates.concat( wayCoordinates );
+ polyCoords = polyCoords.concat( wayCoords );
} else if (member.role == "subject") {
subject = member.ref;
}
});
var poly = {
- "type" : "Feature",
- "geometry" : {
- "type" : "Polygon",
- "coordinates" : [polyCoordinates]
+ type : "Feature",
+ geometry : {
+ type : "Polygon",
+ coordinates : [polyCoords]
},
- "properties" : {}
+ properties : {}
};
// Add the tags