stream.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. function streamGeometry(geometry, stream) {
  2. if (geometry && streamGeometryType.hasOwnProperty(geometry.type)) {
  3. streamGeometryType[geometry.type](geometry, stream);
  4. }
  5. }
  6. var streamObjectType = {
  7. Feature: function(object, stream) {
  8. streamGeometry(object.geometry, stream);
  9. },
  10. FeatureCollection: function(object, stream) {
  11. var features = object.features, i = -1, n = features.length;
  12. while (++i < n) streamGeometry(features[i].geometry, stream);
  13. }
  14. };
  15. var streamGeometryType = {
  16. Sphere: function(object, stream) {
  17. stream.sphere();
  18. },
  19. Point: function(object, stream) {
  20. object = object.coordinates;
  21. stream.point(object[0], object[1], object[2]);
  22. },
  23. MultiPoint: function(object, stream) {
  24. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  25. while (++i < n) object = coordinates[i], stream.point(object[0], object[1], object[2]);
  26. },
  27. LineString: function(object, stream) {
  28. streamLine(object.coordinates, stream, 0);
  29. },
  30. MultiLineString: function(object, stream) {
  31. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  32. while (++i < n) streamLine(coordinates[i], stream, 0);
  33. },
  34. Polygon: function(object, stream) {
  35. streamPolygon(object.coordinates, stream);
  36. },
  37. MultiPolygon: function(object, stream) {
  38. var coordinates = object.coordinates, i = -1, n = coordinates.length;
  39. while (++i < n) streamPolygon(coordinates[i], stream);
  40. },
  41. GeometryCollection: function(object, stream) {
  42. var geometries = object.geometries, i = -1, n = geometries.length;
  43. while (++i < n) streamGeometry(geometries[i], stream);
  44. }
  45. };
  46. function streamLine(coordinates, stream, closed) {
  47. var i = -1, n = coordinates.length - closed, coordinate;
  48. stream.lineStart();
  49. while (++i < n) coordinate = coordinates[i], stream.point(coordinate[0], coordinate[1], coordinate[2]);
  50. stream.lineEnd();
  51. }
  52. function streamPolygon(coordinates, stream) {
  53. var i = -1, n = coordinates.length;
  54. stream.polygonStart();
  55. while (++i < n) streamLine(coordinates[i], stream, 1);
  56. stream.polygonEnd();
  57. }
  58. export default function(object, stream) {
  59. if (object && streamObjectType.hasOwnProperty(object.type)) {
  60. streamObjectType[object.type](object, stream);
  61. } else {
  62. streamGeometry(object, stream);
  63. }
  64. }