summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Philipp2014-11-11 18:03:54 +0100
committerPeter Philipp2014-11-13 15:14:12 +0100
commit5bf34ea4d8f824539a227220683aa81b6939e9cf (patch)
tree8e26404260fb2f2e7a6bd66ce94aed21c233f6af
parentf2defa1d25eb012a10195312ef21bd6c87319f7e (diff)
First shot at the bbox handling.
-rw-r--r--includes/openlayers.default_sources.inc49
-rw-r--r--modules/openlayers_examples/includes/openlayers_examples.default_sources.inc2
-rw-r--r--plugins/openlayers/source/openlayers__source__geojson.inc54
-rw-r--r--plugins/openlayers/source/openlayers__source__geojson.js109
4 files changed, 196 insertions, 18 deletions
diff --git a/includes/openlayers.default_sources.inc b/includes/openlayers.default_sources.inc
index 654806d..3c0a295 100644
--- a/includes/openlayers.default_sources.inc
+++ b/includes/openlayers.default_sources.inc
@@ -165,23 +165,40 @@ function openlayers_default_sources() {
);
$export['source_esri'] = $source;
- // Include all "views_geojson_feed" views as sources.
- if (module_exists('views_geojson')) {
- foreach (views_get_all_views() as $view) {
- foreach ($view->display as $display => $data) {
- $view->set_display($display);
- if ($view->display_handler->get_option('style_plugin') == 'views_geojson_feed') {
- $source = new stdClass;
- $source->api_version = 1;
- $source->machine_name = 'views_geojson_'. $view->name . '_' . $display;
- $source->name = 'Views GeoJSON source '. $view->name . '_' . $display;
- $source->description = 'This source is automatically generated. See View ' . $view->name . ' at display ' . $display;
- $source->class = 'openlayers__source__geojson';
- $source->options = array(
- 'url' => url($view->display[$display]->display_options['path'], array('absolute' => TRUE))
- );
- $export[$source->machine_name] = $source;
+ return $export;
+}
+
+/**
+ * On behalf of views_geojson.
+ */
+function views_geojson_default_sources() {
+ $export = array();
+ foreach (views_get_all_views() as $view) {
+ foreach ($view->display as $display => $data) {
+ $view->set_display($display);
+ if (in_array($view->display_handler->get_option('style_plugin'), array('views_geojson_feed', 'views_geojson'))) {
+ $source = new stdClass();
+ $source->api_version = 1;
+ $source->machine_name = 'views_geojson_' . $view->name . '_' . $display;
+ $source->name = 'Views GeoJSON source: ' . $view->name . '_' . $display;
+ $source->description = 'This source is automatically generated. See View ' . $view->name . ' at display ' . $display;
+ $source->class = 'openlayers__source__geojson';
+
+ $source->options = array(
+ 'url' => url($view->display_handler->get_url(), array('absolute' => TRUE)),
+ );
+
+ // Determine if we should use a BBox strategy.
+ if ($arguments = $view->display_handler->get_option('arguments')) {
+ foreach ($arguments as $id => $argument) {
+ if (strpos($id, 'bbox') !== FALSE && $argument['default_argument_type'] == 'querystring') {
+ if (isset($argument['default_argument_options'])) {
+ $source->options['useBBOX'] = TRUE;
+ }
+ }
+ }
}
+ $export[$source->machine_name] = $source;
}
}
}
diff --git a/modules/openlayers_examples/includes/openlayers_examples.default_sources.inc b/modules/openlayers_examples/includes/openlayers_examples.default_sources.inc
index de8e278..32c6bc1 100644
--- a/modules/openlayers_examples/includes/openlayers_examples.default_sources.inc
+++ b/modules/openlayers_examples/includes/openlayers_examples.default_sources.inc
@@ -47,7 +47,7 @@ function openlayers_examples_default_sources() {
$source->description = '';
$source->class = 'openlayers__source__geojson';
$source->options = array(
- 'url' => url(drupal_get_path('module', 'openlayers_examples') . '/assets/cities.json', array('absolute' => TRUE)),
+ 'url' => file_create_url(drupal_get_path('module', 'openlayers_examples') . '/assets/cities.json'),
);
$export['source_geojson_cities'] = $source;
diff --git a/plugins/openlayers/source/openlayers__source__geojson.inc b/plugins/openlayers/source/openlayers__source__geojson.inc
index e51cba5..85eabb5 100644
--- a/plugins/openlayers/source/openlayers__source__geojson.inc
+++ b/plugins/openlayers/source/openlayers__source__geojson.inc
@@ -5,7 +5,7 @@ function openlayers_openlayers__source__geojson_openlayers_source() {
'handler' => array(
'class' => 'openlayers__source__geojson',
'file' => 'openlayers__source__geojson.inc',
- )
+ ),
);
}
@@ -17,5 +17,57 @@ class openlayers__source__geojson extends openlayers_source {
'#type' => 'textfield',
'#default_value' => $this->getOption('url'),
);
+ $form['options']['useBBOX'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Use Bounding Box Strategy'),
+ '#description' => t('Bounding Box strategy will add a query string onto the GeoJSON URL, which means that only data in the viewport of the map will be loaded. This can be helpful if you have lots of data coming from the feed.'),
+ '#default_value' => $this->getOption('useBBOX'),
+ );
+ //see http://dev.openlayers.org/docs/files/OpenLayers/Strategy/BBOX-js.html#OpenLayers.Strategy.BBOX.resFactor
+ $form['options']['resFactor'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Bounding Box resFactor'),
+ '#description' => t('Used to determine when previously requested features are invalid (set to 1 if unsure).
+ The resFactor will be compared to the resolution of the previous request to the current map resolution.<br />
+ If resFactor > (old / new) and 1/resFactor < (old / new).
+ <ul>
+ <li>If you set a resFactor of 1, data will be requested every time the resolution changes.</li>
+ <li>If you set a resFactor of 3, data will be requested if the old resolution is 3 times the new, or if the new is 3 times the old.</li>
+ <li>If the old bounds do not contain the new bounds new data will always be requested (with or without considering resFactor).</li>
+ </ul>
+ '),
+ '#default_value' => $this->getOption('resFactor', 1),
+ );
+ //see hZttp://dev.openlayers.org/docs/files/OpenLayers/Strategy/BBOX-js.html#OpenLayers.Strategy.BBOX.ratio
+ $form['options']['ratio'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Bounding Box ratio'),
+ '#description' => t('The ratio of the data bounds to the viewport bounds (in each dimension). Default is 3.'),
+ '#default_value' => $this->getOption('ratio', 3),
+ );
+ $form['options']['preload'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Preload layer'),
+ '#description' => t('Load data before layer is made visible. Useful when you want to avoid having to wait for an Ajax call to load the data'),
+ '#default_value' => $this->getOption('preload', FALSE),
+ );
+ $form['options']['useScript'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Use Script Method'),
+ '#description' => t('Avoid 405 error and XSS issues load data from another server with ajax'),
+ '#default_value' => $this->getOption('useScript', FALSE),
+ );
+ $form['options']['callbackKey'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Script Callback Key'),
+ '#description' => t('Key returned by callback along with geoJSON'),
+ '#default_value' => $this->getOption('callbackKey', NULL),
+ );
+ $form['options']['geojson_data'] = array(
+ '#type' => 'textarea',
+ '#title' => t('GeoJSON Data'),
+ '#description' => t('Paste raw GeoJSON data here. It is better to use a URL. This is provided for very simple use cases like one or two features. If there is data here, it will override the URL above.'),
+ '#default_value' => $this->getOption('geojson_data'),
+ );
}
}
diff --git a/plugins/openlayers/source/openlayers__source__geojson.js b/plugins/openlayers/source/openlayers__source__geojson.js
index d9bfef3..d8c004d 100644
--- a/plugins/openlayers/source/openlayers__source__geojson.js
+++ b/plugins/openlayers/source/openlayers__source__geojson.js
@@ -1,4 +1,113 @@
Drupal.openlayers.openlayers__source__geojson = function(data) {
data.options.projection = 'EPSG:3857';
+
+ //// If GeoJSON data is provided with the layer, use that. Otherwise
+ //// check if BBOX, then finally use AJAX method.
+ //if (data.options.geojson_data) {
+ // var layer = new ol.Layer.Vector(title, options);
+ //
+ // // Read data in.
+ // features = new ol.Format.GeoJSON(geojson_options).read(data.options.geojson_data);
+ // if (features) {
+ // // If not array (ie only one feature)
+ // if (features.constructor != Array) {
+ // features = [features];
+ // }
+ // }
+ //
+ // // Add features, if needed
+ // if (features) {
+ // layer.addFeatures(features);
+ // layer.events.triggerEvent('loadend');
+ // }
+ //}
+ //else {
+ // @todo Add more strategies. Paging strategy would be really interesting
+ // to use with views_geojson.
+ if (data.options.useBBOX) {
+ data.options.format = new ol.format.GeoJSON();
+ data.options.strategy = ol.loadingstrategy.bbox;
+ data.options.loader = function(extent, resolution, projection) {
+ // Ensure the bbox values are in the correct projection.
+ var bbox = ol.proj.transformExtent(extent, data.map.getView().getProjection(), 'EPSG:4326');
+
+ var params = {
+ 'bbox': bbox.join(','),
+ 'zoom': data.map.getView().getZoom()
+ };
+ var url = data.options.url;
+ jQuery(document).trigger('openlayers.bbox_pre_loading', [{'url': url, 'params': params, 'data': data}]);
+
+ var that = this;
+ jQuery.ajax({
+ url: url,
+ data: params,
+ success: function(data) {
+ that.addFeatures(that.readFeatures(data));
+ }
+ });
+ };
+ var vectorSource = new ol.source.ServerVector(data.options);
+ return vectorSource;
+
+ ///*
+ // * We override the triggerRead of the strategy so we can add a zoom=thecurrentzoomlevel in the URL
+ // * This is used by the geocluster module http://drupal.org/project/geocluster
+ // */
+ //strategy.triggerRead =
+ // function(options) {
+ // if (this.response && !(options && data.options.noAbort === true)) {
+ // this.layer.protocol.abort(this.response);
+ // this.layer.events.triggerEvent("loadend");
+ // }
+ // this.layer.events.triggerEvent("loadstart");
+ // data.options.params = new Array();
+ // data.options.params['zoom'] = data.options.object.map.zoom;
+ // this.response = this.layer.protocol.read(
+ // ol.Util.applyDefaults({
+ // filter: this.createFilter(),
+ // callback: this.merge,
+ // scope: this
+ // }, options));
+ // };
+ //data.options.strategies = [strategy];
+ }
+ else {
+ // Fixed strategy.
+ // @see http://dev.ol.org/releases/OpenLayers-2.12/doc/apidocs/files/OpenLayers/Strategy/Fixed-js.html
+ if (data.options.preload) {
+ data.options.strategies = [new ol.Strategy.Fixed({preload: true})];
+ }
+ else {
+ data.options.strategies = [new ol.Strategy.Fixed()];
+ }
+ }
+ // if(data.options.useScript){
+ // //use Script protocol to get around xss issues and 405 error
+ // data.options.protocol = new ol.Protocol.Script({
+ // url: data.options.url,
+ // callbackKey: data.options.callbackKey,
+ // callbackPrefix: "callback:",
+ // filterToParams: function(filter, params) {
+ // // example to demonstrate BBOX serialization
+ // if (filter.type === ol.Filter.Spatial.BBOX) {
+ // params.bbox = filter.value.toArray();
+ // if (filter.projection) {
+ // params.bbox.push(filter.projection.getCode());
+ // }
+ // }
+ // return params;
+ // }
+ // });
+ // }
+ // else{
+ // data.options.protocol = new ol.Protocol.HTTP({
+ // url: data.options.url,
+ // format: new ol.Format.GeoJSON()
+ // });
+ // }
+ // var layer = new ol.Layer.Vector(title, options);
+ //}
+
return new ol.source.GeoJSON(data.options);
};