geometry.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Given a hash of GeoJSON objects, returns a hash of GeoJSON geometry objects.
  2. // Any null input geometry objects are represented as {type: null} in the output.
  3. // Any feature.{id,properties,bbox} are transferred to the output geometry object.
  4. // Each output geometry object is a shallow copy of the input (e.g., properties, coordinates)!
  5. export default function(inputs) {
  6. var outputs = {}, key;
  7. for (key in inputs) outputs[key] = geomifyObject(inputs[key]);
  8. return outputs;
  9. }
  10. function geomifyObject(input) {
  11. return input == null ? {type: null}
  12. : (input.type === "FeatureCollection" ? geomifyFeatureCollection
  13. : input.type === "Feature" ? geomifyFeature
  14. : geomifyGeometry)(input);
  15. }
  16. function geomifyFeatureCollection(input) {
  17. var output = {type: "GeometryCollection", geometries: input.features.map(geomifyFeature)};
  18. if (input.bbox != null) output.bbox = input.bbox;
  19. return output;
  20. }
  21. function geomifyFeature(input) {
  22. var output = geomifyGeometry(input.geometry), key; // eslint-disable-line no-unused-vars
  23. if (input.id != null) output.id = input.id;
  24. if (input.bbox != null) output.bbox = input.bbox;
  25. for (key in input.properties) { output.properties = input.properties; break; }
  26. return output;
  27. }
  28. function geomifyGeometry(input) {
  29. if (input == null) return {type: null};
  30. var output = input.type === "GeometryCollection" ? {type: "GeometryCollection", geometries: input.geometries.map(geomifyGeometry)}
  31. : input.type === "Point" || input.type === "MultiPoint" ? {type: input.type, coordinates: input.coordinates}
  32. : {type: input.type, arcs: input.coordinates}; // TODO Check for unknown types?
  33. if (input.bbox != null) output.bbox = input.bbox;
  34. return output;
  35. }