geometryUtil.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import GeoJSON from 'ol/format/GeoJSON';
  20. import { Point } from 'geojson';
  21. import {
  22. getExtentFromFeatures,
  23. getProjectedCoordinateFromPointGeoJson,
  24. } from '../../src/util/geometryUtil';
  25. import { ChartConfig } from '../../src/types';
  26. describe('geometryUtil', () => {
  27. describe('getProjectedCoordinateFromPointGeoJson', () => {
  28. it('returns a plausible result', () => {
  29. const pointGeoJson: Point = {
  30. type: 'Point',
  31. coordinates: [6.6555, 49.74283],
  32. };
  33. const result = getProjectedCoordinateFromPointGeoJson(pointGeoJson);
  34. expect(result.length).toEqual(2);
  35. const valuesAreNumbers =
  36. !Number.isNaN(result[0]) && !Number.isNaN(result[1]);
  37. expect(valuesAreNumbers).toEqual(true);
  38. });
  39. });
  40. describe('getExtentFromFeatures', () => {
  41. it('computes correct extent with valid input', () => {
  42. const expectedExtent = [1, 2, 3, 4];
  43. const chartConfig: ChartConfig = {
  44. type: 'FeatureCollection',
  45. features: [
  46. {
  47. type: 'Feature',
  48. geometry: {
  49. type: 'Point',
  50. coordinates: [expectedExtent[0], expectedExtent[1]],
  51. },
  52. properties: {
  53. setDataMask: '',
  54. labelMap: '',
  55. labelMapB: '',
  56. groupby: '',
  57. selectedValues: '',
  58. formData: '',
  59. groupbyB: '',
  60. seriesBreakdown: '',
  61. legendData: '',
  62. echartOptions: '',
  63. },
  64. },
  65. {
  66. type: 'Feature',
  67. geometry: {
  68. type: 'Point',
  69. coordinates: [expectedExtent[2], expectedExtent[3]],
  70. },
  71. properties: {
  72. setDataMask: '',
  73. labelMap: '',
  74. labelMapB: '',
  75. groupby: '',
  76. selectedValues: '',
  77. formData: '',
  78. groupbyB: '',
  79. seriesBreakdown: '',
  80. legendData: '',
  81. echartOptions: '',
  82. },
  83. },
  84. ],
  85. };
  86. const features = new GeoJSON().readFeatures(chartConfig);
  87. const extent = getExtentFromFeatures(features);
  88. expect(extent).toEqual(expectedExtent);
  89. });
  90. it('returns undefined on invalid input', () => {
  91. const emptyExtent = getExtentFromFeatures([]);
  92. expect(emptyExtent).toBeUndefined();
  93. });
  94. });
  95. });