parseGeoJson.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. /**
  41. * Parse and decode geo json
  42. */
  43. import * as zrUtil from 'zrender/lib/core/util.js';
  44. import { GeoJSONLineStringGeometry, GeoJSONPolygonGeometry, GeoJSONRegion } from './Region.js';
  45. function decode(json) {
  46. if (!json.UTF8Encoding) {
  47. return json;
  48. }
  49. var jsonCompressed = json;
  50. var encodeScale = jsonCompressed.UTF8Scale;
  51. if (encodeScale == null) {
  52. encodeScale = 1024;
  53. }
  54. var features = jsonCompressed.features;
  55. zrUtil.each(features, function (feature) {
  56. var geometry = feature.geometry;
  57. var encodeOffsets = geometry.encodeOffsets;
  58. var coordinates = geometry.coordinates;
  59. // Geometry may be appeded manually in the script after json loaded.
  60. // In this case this geometry is usually not encoded.
  61. if (!encodeOffsets) {
  62. return;
  63. }
  64. switch (geometry.type) {
  65. case 'LineString':
  66. geometry.coordinates = decodeRing(coordinates, encodeOffsets, encodeScale);
  67. break;
  68. case 'Polygon':
  69. decodeRings(coordinates, encodeOffsets, encodeScale);
  70. break;
  71. case 'MultiLineString':
  72. decodeRings(coordinates, encodeOffsets, encodeScale);
  73. break;
  74. case 'MultiPolygon':
  75. zrUtil.each(coordinates, function (rings, idx) {
  76. return decodeRings(rings, encodeOffsets[idx], encodeScale);
  77. });
  78. }
  79. });
  80. // Has been decoded
  81. jsonCompressed.UTF8Encoding = false;
  82. return jsonCompressed;
  83. }
  84. function decodeRings(rings, encodeOffsets, encodeScale) {
  85. for (var c = 0; c < rings.length; c++) {
  86. rings[c] = decodeRing(rings[c], encodeOffsets[c], encodeScale);
  87. }
  88. }
  89. function decodeRing(coordinate, encodeOffsets, encodeScale) {
  90. var result = [];
  91. var prevX = encodeOffsets[0];
  92. var prevY = encodeOffsets[1];
  93. for (var i = 0; i < coordinate.length; i += 2) {
  94. var x = coordinate.charCodeAt(i) - 64;
  95. var y = coordinate.charCodeAt(i + 1) - 64;
  96. // ZigZag decoding
  97. x = x >> 1 ^ -(x & 1);
  98. y = y >> 1 ^ -(y & 1);
  99. // Delta deocding
  100. x += prevX;
  101. y += prevY;
  102. prevX = x;
  103. prevY = y;
  104. // Dequantize
  105. result.push([x / encodeScale, y / encodeScale]);
  106. }
  107. return result;
  108. }
  109. export default function parseGeoJSON(geoJson, nameProperty) {
  110. geoJson = decode(geoJson);
  111. return zrUtil.map(zrUtil.filter(geoJson.features, function (featureObj) {
  112. // Output of mapshaper may have geometry null
  113. return featureObj.geometry && featureObj.properties && featureObj.geometry.coordinates.length > 0;
  114. }), function (featureObj) {
  115. var properties = featureObj.properties;
  116. var geo = featureObj.geometry;
  117. var geometries = [];
  118. switch (geo.type) {
  119. case 'Polygon':
  120. var coordinates = geo.coordinates;
  121. // According to the GeoJSON specification.
  122. // First must be exterior, and the rest are all interior(holes).
  123. geometries.push(new GeoJSONPolygonGeometry(coordinates[0], coordinates.slice(1)));
  124. break;
  125. case 'MultiPolygon':
  126. zrUtil.each(geo.coordinates, function (item) {
  127. if (item[0]) {
  128. geometries.push(new GeoJSONPolygonGeometry(item[0], item.slice(1)));
  129. }
  130. });
  131. break;
  132. case 'LineString':
  133. geometries.push(new GeoJSONLineStringGeometry([geo.coordinates]));
  134. break;
  135. case 'MultiLineString':
  136. geometries.push(new GeoJSONLineStringGeometry(geo.coordinates));
  137. }
  138. var region = new GeoJSONRegion(properties[nameProperty || 'name'], geometries, properties.cp);
  139. region.properties = properties;
  140. return region;
  141. });
  142. }