Nav apraksta

OLMapWidget.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* global ol */
  2. 'use strict';
  3. function GeometryTypeControl(opt_options) {
  4. // Map control to switch type when geometry type is unknown
  5. const options = opt_options || {};
  6. const element = document.createElement('div');
  7. element.className = 'switch-type type-' + options.type + ' ol-control ol-unselectable';
  8. if (options.active) {
  9. element.classList.add("type-active");
  10. }
  11. const self = this;
  12. const switchType = function(e) {
  13. e.preventDefault();
  14. if (options.widget.currentGeometryType !== self) {
  15. options.widget.map.removeInteraction(options.widget.interactions.draw);
  16. options.widget.interactions.draw = new ol.interaction.Draw({
  17. features: options.widget.featureCollection,
  18. type: options.type
  19. });
  20. options.widget.map.addInteraction(options.widget.interactions.draw);
  21. options.widget.currentGeometryType.element.classList.remove('type-active');
  22. options.widget.currentGeometryType = self;
  23. element.classList.add("type-active");
  24. }
  25. };
  26. element.addEventListener('click', switchType, false);
  27. element.addEventListener('touchstart', switchType, false);
  28. ol.control.Control.call(this, {
  29. element: element
  30. });
  31. };
  32. ol.inherits(GeometryTypeControl, ol.control.Control);
  33. // TODO: allow deleting individual features (#8972)
  34. {
  35. const jsonFormat = new ol.format.GeoJSON();
  36. function MapWidget(options) {
  37. this.map = null;
  38. this.interactions = {draw: null, modify: null};
  39. this.typeChoices = false;
  40. this.ready = false;
  41. // Default options
  42. this.options = {
  43. default_lat: 0,
  44. default_lon: 0,
  45. default_zoom: 12,
  46. is_collection: options.geom_name.includes('Multi') || options.geom_name.includes('Collection')
  47. };
  48. // Altering using user-provided options
  49. for (const property in options) {
  50. if (options.hasOwnProperty(property)) {
  51. this.options[property] = options[property];
  52. }
  53. }
  54. if (!options.base_layer) {
  55. this.options.base_layer = new ol.layer.Tile({source: new ol.source.OSM()});
  56. }
  57. this.map = this.createMap();
  58. this.featureCollection = new ol.Collection();
  59. this.featureOverlay = new ol.layer.Vector({
  60. map: this.map,
  61. source: new ol.source.Vector({
  62. features: this.featureCollection,
  63. useSpatialIndex: false // improve performance
  64. }),
  65. updateWhileAnimating: true, // optional, for instant visual feedback
  66. updateWhileInteracting: true // optional, for instant visual feedback
  67. });
  68. // Populate and set handlers for the feature container
  69. const self = this;
  70. this.featureCollection.on('add', function(event) {
  71. const feature = event.element;
  72. feature.on('change', function() {
  73. self.serializeFeatures();
  74. });
  75. if (self.ready) {
  76. self.serializeFeatures();
  77. if (!self.options.is_collection) {
  78. self.disableDrawing(); // Only allow one feature at a time
  79. }
  80. }
  81. });
  82. const initial_value = document.getElementById(this.options.id).value;
  83. if (initial_value) {
  84. const features = jsonFormat.readFeatures('{"type": "Feature", "geometry": ' + initial_value + '}');
  85. const extent = ol.extent.createEmpty();
  86. features.forEach(function(feature) {
  87. this.featureOverlay.getSource().addFeature(feature);
  88. ol.extent.extend(extent, feature.getGeometry().getExtent());
  89. }, this);
  90. // Center/zoom the map
  91. this.map.getView().fit(extent, {maxZoom: this.options.default_zoom});
  92. } else {
  93. this.map.getView().setCenter(this.defaultCenter());
  94. }
  95. this.createInteractions();
  96. if (initial_value && !this.options.is_collection) {
  97. this.disableDrawing();
  98. }
  99. this.ready = true;
  100. }
  101. MapWidget.prototype.createMap = function() {
  102. const map = new ol.Map({
  103. target: this.options.map_id,
  104. layers: [this.options.base_layer],
  105. view: new ol.View({
  106. zoom: this.options.default_zoom
  107. })
  108. });
  109. return map;
  110. };
  111. MapWidget.prototype.createInteractions = function() {
  112. // Initialize the modify interaction
  113. this.interactions.modify = new ol.interaction.Modify({
  114. features: this.featureCollection,
  115. deleteCondition: function(event) {
  116. return ol.events.condition.shiftKeyOnly(event) &&
  117. ol.events.condition.singleClick(event);
  118. }
  119. });
  120. // Initialize the draw interaction
  121. let geomType = this.options.geom_name;
  122. if (geomType === "Geometry" || geomType === "GeometryCollection") {
  123. // Default to Point, but create icons to switch type
  124. geomType = "Point";
  125. this.currentGeometryType = new GeometryTypeControl({widget: this, type: "Point", active: true});
  126. this.map.addControl(this.currentGeometryType);
  127. this.map.addControl(new GeometryTypeControl({widget: this, type: "LineString", active: false}));
  128. this.map.addControl(new GeometryTypeControl({widget: this, type: "Polygon", active: false}));
  129. this.typeChoices = true;
  130. }
  131. this.interactions.draw = new ol.interaction.Draw({
  132. features: this.featureCollection,
  133. type: geomType
  134. });
  135. this.map.addInteraction(this.interactions.draw);
  136. this.map.addInteraction(this.interactions.modify);
  137. };
  138. MapWidget.prototype.defaultCenter = function() {
  139. const center = [this.options.default_lon, this.options.default_lat];
  140. if (this.options.map_srid) {
  141. return ol.proj.transform(center, 'EPSG:4326', this.map.getView().getProjection());
  142. }
  143. return center;
  144. };
  145. MapWidget.prototype.enableDrawing = function() {
  146. this.interactions.draw.setActive(true);
  147. if (this.typeChoices) {
  148. // Show geometry type icons
  149. const divs = document.getElementsByClassName("switch-type");
  150. for (let i = 0; i !== divs.length; i++) {
  151. divs[i].style.visibility = "visible";
  152. }
  153. }
  154. };
  155. MapWidget.prototype.disableDrawing = function() {
  156. if (this.interactions.draw) {
  157. this.interactions.draw.setActive(false);
  158. if (this.typeChoices) {
  159. // Hide geometry type icons
  160. const divs = document.getElementsByClassName("switch-type");
  161. for (let i = 0; i !== divs.length; i++) {
  162. divs[i].style.visibility = "hidden";
  163. }
  164. }
  165. }
  166. };
  167. MapWidget.prototype.clearFeatures = function() {
  168. this.featureCollection.clear();
  169. // Empty textarea widget
  170. document.getElementById(this.options.id).value = '';
  171. this.enableDrawing();
  172. };
  173. MapWidget.prototype.serializeFeatures = function() {
  174. // Three use cases: GeometryCollection, multigeometries, and single geometry
  175. let geometry = null;
  176. const features = this.featureOverlay.getSource().getFeatures();
  177. if (this.options.is_collection) {
  178. if (this.options.geom_name === "GeometryCollection") {
  179. const geometries = [];
  180. for (let i = 0; i < features.length; i++) {
  181. geometries.push(features[i].getGeometry());
  182. }
  183. geometry = new ol.geom.GeometryCollection(geometries);
  184. } else {
  185. geometry = features[0].getGeometry().clone();
  186. for (let j = 1; j < features.length; j++) {
  187. switch (geometry.getType()) {
  188. case "MultiPoint":
  189. geometry.appendPoint(features[j].getGeometry().getPoint(0));
  190. break;
  191. case "MultiLineString":
  192. geometry.appendLineString(features[j].getGeometry().getLineString(0));
  193. break;
  194. case "MultiPolygon":
  195. geometry.appendPolygon(features[j].getGeometry().getPolygon(0));
  196. }
  197. }
  198. }
  199. } else {
  200. if (features[0]) {
  201. geometry = features[0].getGeometry();
  202. }
  203. }
  204. document.getElementById(this.options.id).value = jsonFormat.writeGeometry(geometry);
  205. };
  206. window.MapWidget = MapWidget;
  207. }