bbox.js 967 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import transform from "./transform.js";
  2. export default function(topology) {
  3. var t = transform(topology.transform), key,
  4. x0 = Infinity, y0 = x0, x1 = -x0, y1 = -x0;
  5. function bboxPoint(p) {
  6. p = t(p);
  7. if (p[0] < x0) x0 = p[0];
  8. if (p[0] > x1) x1 = p[0];
  9. if (p[1] < y0) y0 = p[1];
  10. if (p[1] > y1) y1 = p[1];
  11. }
  12. function bboxGeometry(o) {
  13. switch (o.type) {
  14. case "GeometryCollection": o.geometries.forEach(bboxGeometry); break;
  15. case "Point": bboxPoint(o.coordinates); break;
  16. case "MultiPoint": o.coordinates.forEach(bboxPoint); break;
  17. }
  18. }
  19. topology.arcs.forEach(function(arc) {
  20. var i = -1, n = arc.length, p;
  21. while (++i < n) {
  22. p = t(arc[i], i);
  23. if (p[0] < x0) x0 = p[0];
  24. if (p[0] > x1) x1 = p[0];
  25. if (p[1] < y0) y0 = p[1];
  26. if (p[1] > y1) y1 = p[1];
  27. }
  28. });
  29. for (key in topology.objects) {
  30. bboxGeometry(topology.objects[key]);
  31. }
  32. return [x0, y0, x1, y1];
  33. }