Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 57 additions & 30 deletions mapsense.js
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,17 @@ ms.geoJson = function(fetch) {
pointRadius = 4.5,
features,
tileBackground = false,
selection;
transformData,
key = (function() {
var k = 0;
return function(f) {
k = k + 1 | 0;
return k;
};
})(),
selection,
dataGeneration = 0,
selectionGeneration = 0;

container.setAttribute("fill-rule", "evenodd");
clipPath.setAttribute("id", clipId);
Expand Down Expand Up @@ -1294,16 +1304,14 @@ ms.geoJson = function(fetch) {
function load(tile, proj) {
var g = tile.element = ms.svg("g");

var tileProj = proj(tile);

tile.proj = proj(tile);
tile.fetched = [];
tile.features = [];
tile.draw = function() {
draw(g, tile);
};

function update(data) {
var updated = [];

/* Fetch the next batch of features, if so directed. */
if (data.next) tile.request = fetch(data.next.href, update);

if (geoJson.tile() && tileBackground) {
var tileSize = geoJson.map().tileSize();
d3.select(g.insertBefore(ms.svg("rect"), g.firstChild))
Expand All @@ -1312,11 +1320,11 @@ ms.geoJson = function(fetch) {
.attr("class", "tile-background");
}

draw(g, data, tileProj, updated, tile);
tile.fetched = data.features;
tile.draw();

tile.ready = true;
updated.push.apply(tile.features, updated);
geoJson.dispatch({type: "load", tile: tile, features: updated});
geoJson.dispatch({type: "load", tile: tile, features: tile.features});
}

if (url != null) {
Expand Down Expand Up @@ -1350,15 +1358,15 @@ ms.geoJson = function(fetch) {
return points;
}

function draw(g, data, tileProj, updated, tile) {
var proj = projection(tileProj.locationPoint);
function draw(g, tile) {
var proj = projection(tile.proj.locationPoint),
path = projectSpherical(tile.proj),
pathFeatures = [],
pointFeatures = [],
features = transformData ? transformData(tile.fetched, tile) : tile.fetched,
updated = [];

var path = projectSpherical(tileProj);

var pathFeatures = [],
pointFeatures = [];

data.features.forEach(function(f) {
features.forEach(function(f) {
if (f.geometry.type === "Point")
pointFeatures.push(projectPoint(f, proj));
else if (f.geometry.type === "MultiPoint")
Expand All @@ -1369,13 +1377,15 @@ ms.geoJson = function(fetch) {

var pathUpdate = d3.select(g)
.selectAll("path")
.data(pathFeatures)
.enter()
.data(pathFeatures, key);

pathUpdate.enter()
.append("path")
.attr("d", function(f) { return path(f); });

if (updated)
pathUpdate.each(function(f) { updated.push({ element: this, data: f }); });
pathUpdate.exit().remove();

pathUpdate.each(function(f) { updated.push({ element: this, data: f }); });

var initialScale = "";
if (scale == "fixed") {
Expand All @@ -1384,21 +1394,27 @@ ms.geoJson = function(fetch) {

var pointUpdate = d3.select(g)
.selectAll("circle")
.data(pointFeatures)
.enter()
.data(pointFeatures, key);

pointUpdate.enter()
.append("circle")
.attr("transform", function(f) {
return "translate(" + f.x + "," + f.y + ")" + initialScale;
})
.attr("r", pointRadius);

if (updated)
pointUpdate.each(function(f) { updated.push({ element: this, data: f }); });
pointUpdate.exit().remove();

pointUpdate.each(function(f) { updated.push({ element: this, data: f }); });

if (selection) {
pathUpdate.push.apply(pathUpdate, pointUpdate);
selection(pathUpdate);
}

tile.features = updated;
tile.dataGeneration = dataGeneration;
tile.selectionGeneration = selectionGeneration;
}

function unload(tile) {
Expand Down Expand Up @@ -1437,6 +1453,14 @@ ms.geoJson = function(fetch) {
geoJson.selection = function(x) {
if (!arguments.length) return selection;
selection = x;
selectionGeneration = selectionGeneration + 1 | 0;
return geoJson.reshow();
};

geoJson.transformData = function(x) {
if (!arguments.length) return transformData;
transformData = x;
dataGeneration = dataGeneration + 1 | 0;
return geoJson.reshow();
};

Expand Down Expand Up @@ -1496,10 +1520,13 @@ ms.geoJson = function(fetch) {
geoJson.show = function(tile) {
if (clip) tile.element.setAttribute("clip-path", clipHref);
else tile.element.removeAttribute("clip-path");
if (selection) {
var s = d3.select(tile.element).selectAll("path");
s.push.apply(s, d3.select(tile.element).selectAll("circle"));
if (transformData && tile.dataGeneration !== dataGeneration) {
tile.draw();
}
else if (selection && tile.selectionGeneration !== selectionGeneration) {
var s = d3.select(tile.element).selectAll("path,circle");
selection(s);
tile.selectionGeneration = selectionGeneration;
}
geoJson.dispatch({type: "show", tile: tile, features: tile.features});
return geoJson;
Expand Down
87 changes: 57 additions & 30 deletions src/GeoJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ ms.geoJson = function(fetch) {
pointRadius = 4.5,
features,
tileBackground = false,
selection;
transformData,
key = (function() {
var k = 0;
return function(f) {
k = k + 1 | 0;
return k;
};
})(),
selection,
dataGeneration = 0,
selectionGeneration = 0;

container.setAttribute("fill-rule", "evenodd");
clipPath.setAttribute("id", clipId);
Expand Down Expand Up @@ -74,16 +84,14 @@ ms.geoJson = function(fetch) {
function load(tile, proj) {
var g = tile.element = ms.svg("g");

var tileProj = proj(tile);

tile.proj = proj(tile);
tile.fetched = [];
tile.features = [];
tile.draw = function() {
draw(g, tile);
};

function update(data) {
var updated = [];

/* Fetch the next batch of features, if so directed. */
if (data.next) tile.request = fetch(data.next.href, update);

if (geoJson.tile() && tileBackground) {
var tileSize = geoJson.map().tileSize();
d3.select(g.insertBefore(ms.svg("rect"), g.firstChild))
Expand All @@ -92,11 +100,11 @@ ms.geoJson = function(fetch) {
.attr("class", "tile-background");
}

draw(g, data, tileProj, updated, tile);
tile.fetched = data.features;
tile.draw();

tile.ready = true;
updated.push.apply(tile.features, updated);
geoJson.dispatch({type: "load", tile: tile, features: updated});
geoJson.dispatch({type: "load", tile: tile, features: tile.features});
}

if (url != null) {
Expand Down Expand Up @@ -130,15 +138,15 @@ ms.geoJson = function(fetch) {
return points;
}

function draw(g, data, tileProj, updated, tile) {
var proj = projection(tileProj.locationPoint);
function draw(g, tile) {
var proj = projection(tile.proj.locationPoint),
path = projectSpherical(tile.proj),
pathFeatures = [],
pointFeatures = [],
features = transformData ? transformData(tile.fetched, tile) : tile.fetched,
updated = [];

var path = projectSpherical(tileProj);

var pathFeatures = [],
pointFeatures = [];

data.features.forEach(function(f) {
features.forEach(function(f) {
if (f.geometry.type === "Point")
pointFeatures.push(projectPoint(f, proj));
else if (f.geometry.type === "MultiPoint")
Expand All @@ -149,13 +157,15 @@ ms.geoJson = function(fetch) {

var pathUpdate = d3.select(g)
.selectAll("path")
.data(pathFeatures)
.enter()
.data(pathFeatures, key);

pathUpdate.enter()
.append("path")
.attr("d", function(f) { return path(f); });

if (updated)
pathUpdate.each(function(f) { updated.push({ element: this, data: f }); });
pathUpdate.exit().remove();

pathUpdate.each(function(f) { updated.push({ element: this, data: f }); });

var initialScale = "";
if (scale == "fixed") {
Expand All @@ -164,21 +174,27 @@ ms.geoJson = function(fetch) {

var pointUpdate = d3.select(g)
.selectAll("circle")
.data(pointFeatures)
.enter()
.data(pointFeatures, key);

pointUpdate.enter()
.append("circle")
.attr("transform", function(f) {
return "translate(" + f.x + "," + f.y + ")" + initialScale;
})
.attr("r", pointRadius);

if (updated)
pointUpdate.each(function(f) { updated.push({ element: this, data: f }); });
pointUpdate.exit().remove();

pointUpdate.each(function(f) { updated.push({ element: this, data: f }); });

if (selection) {
pathUpdate.push.apply(pathUpdate, pointUpdate);
selection(pathUpdate);
}

tile.features = updated;
tile.dataGeneration = dataGeneration;
tile.selectionGeneration = selectionGeneration;
}

function unload(tile) {
Expand Down Expand Up @@ -217,6 +233,14 @@ ms.geoJson = function(fetch) {
geoJson.selection = function(x) {
if (!arguments.length) return selection;
selection = x;
selectionGeneration = selectionGeneration + 1 | 0;
return geoJson.reshow();
};

geoJson.transformData = function(x) {
if (!arguments.length) return transformData;
transformData = x;
dataGeneration = dataGeneration + 1 | 0;
return geoJson.reshow();
};

Expand Down Expand Up @@ -276,10 +300,13 @@ ms.geoJson = function(fetch) {
geoJson.show = function(tile) {
if (clip) tile.element.setAttribute("clip-path", clipHref);
else tile.element.removeAttribute("clip-path");
if (selection) {
var s = d3.select(tile.element).selectAll("path");
s.push.apply(s, d3.select(tile.element).selectAll("circle"));
if (transformData && tile.dataGeneration !== dataGeneration) {
tile.draw();
}
else if (selection && tile.selectionGeneration !== selectionGeneration) {
var s = d3.select(tile.element).selectAll("path,circle");
selection(s);
tile.selectionGeneration = selectionGeneration;
}
geoJson.dispatch({type: "show", tile: tile, features: tile.features});
return geoJson;
Expand Down
Loading