From 71531508c9ae4679f3be651aaaa0c14ca9c8d6e6 Mon Sep 17 00:00:00 2001 From: Steve McDonald Date: Tue, 19 Apr 2016 14:31:39 -0400 Subject: [PATCH] #6 improved popup added support for single field formatter also, some small bug fixes --- Readme.md | 5 ++- example/example.js | 79 ++++++++++++++++++++++++++++++++++++++----- leafletSolrHeatmap.js | 22 +++++++++--- 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/Readme.md b/Readme.md index aca966c..1f248f6 100644 --- a/Readme.md +++ b/Readme.md @@ -35,7 +35,10 @@ Solr intersection is used on the field of type BBox. To display popups, set popupDisplay to the name of the Solr field you would like displayed. If you need more than one field displayed, set -popupDisplay to a comma seperated list of Solr field names. If you do +popupDisplay to a comma seperated list of Solr field names. If you +want to provide a formatter for a single field, rather than providing +the field name provide an array whose first element is the field name +and second element a function that takes a Solr document. If you do not like the default formatting of Solr fields, set popupDisplay to a function that accepts one argument, a Solr document in JSON format. This function should return a string with HTML tags. diff --git a/example/example.js b/example/example.js index e2efc3c..64d1a87 100644 --- a/example/example.js +++ b/example/example.js @@ -10,14 +10,75 @@ function onEachFeature(feature, layer) { } // Create and add a solrHeatmap layer to the map -var solr = L.solrHeatmap('http://127.0.0.1:8983/solr/gettingstarted', { - // Solr field with geospatial data (should be type Spatial Recursive Prefix Tree) - field: 'loc_srpt', +//var solr = L.solrHeatmap('http://127.0.0.1:8983/solr/gettingstarted', { +//var solr = L.solrHeatmap('http://dev.jdarchive.org:8080/solr/zeega', { - // Set type of visualization. Allowed types: 'geojsonGrid', 'clusters' Note: 'clusters' requires LeafletMarkerClusterer - type: 'geojsonGrid', - // type: 'clusters', +function resetSolr() +{ + var solrUrl = jQuery('#solrUrl').val(); + var rptField = jQuery('#rptField').val(); + var bboxField = jQuery('#bboxField').val(); + var popupDisplayField = jQuery('#popupDisplayField').val(); + var areaField = jQuery('#areaField').val(); + if (popupDisplayField.contains(',')) + popupDisplayField = popupDisplayField.split(','); + else + popupDisplayField = [popupDisplayField]; + - // Inherited from L.GeoJSON - onEachFeature: onEachFeature -}).addTo(map); + if (solr) + { + map.removeLayer(solr); + if (solr.heatmapLayer) + { + // this code highlights the problem of L.SolrHeatmap creating L.Heat + // perhaps L.SolrHeatmap needs to override remove layer + map.removeLayer(solr.heatmapLayer); + console.log("listener", solr.heatmapLayerListener); + map.off("click", solr.heatmapLayerListener); + } + + } + + // if the doi field is present, we format it as an html link to the jstor document + // first, a function to generate the html + doiLinker = function(doc) + { + value = doc['doi']; + if (Array.isArray(value)) + value = value.join(); + return "" + value + ""; + }; + doiIndex = popupDisplayField.indexOf('doi'); + if (doiIndex > -1 ) + popupDisplayField[doiIndex] = ['doi', function(doc) {return doiLinker(doc);}]; + + //http://localhost:8983/solr/jstorTest + solr = L.solrHeatmap(solrUrl, { + // Solr field with geospatial data (should be type Spatial Recursive Prefix Tree) + //field: 'loc_srpt', + //field: 'bbox_srpt', + field: rptField, + bboxField: bboxField, + + // Set type of visualization. Allowed types: 'geojsonGrid', 'clusters' Note: 'clusters' requires LeafletMarkerClusterer + type: 'heatmap', + //type: 'geojsonGrid', + //type: 'clusters', + colors: ['#000000', '#0000df', '#00effe', '#00ff42', '#feec30', '#ff5f00', '#ff0000'], + maxSampleSize: 400, + //popupDisplay: ['title', 'doi'], + popupDisplay: popupDisplayField, + areaField: areaField, + //popupDisplay: function(doc) {return doc['title']}, + + // Inherited from L.GeoJSON + onEachFeature: onEachFeature + }); + solr.addTo(map); + +} + +var solr = null; + +resetSolr(); \ No newline at end of file diff --git a/leafletSolrHeatmap.js b/leafletSolrHeatmap.js index dfc25fc..1e6642d 100644 --- a/leafletSolrHeatmap.js +++ b/leafletSolrHeatmap.js @@ -108,8 +108,9 @@ L.SolrHeatmap = L.GeoJSON.extend({ _createHeatmap: function(){ var _this = this; + if (_this.facetHeatmap.counts_ints2D == null) return; var heatmapCells = []; - var cellSize = _this._getCellSize() * .75; + var cellSize = _this._getCellSize() * 1.; var colors = _this.options.colors; var classifications = _this._getClassifications(colors.length - 1); @@ -227,6 +228,7 @@ L.SolrHeatmap = L.GeoJSON.extend({ { var _this = this; var one_d_array = []; + if (_this.facetHeatmap.counts_ints2D == null) return; for(var i = 0; i < _this.facetHeatmap.counts_ints2D.length; i++) { if (_this.facetHeatmap.counts_ints2D[i] != null) one_d_array = one_d_array.concat(_this.facetHeatmap.counts_ints2D[i]); @@ -320,6 +322,8 @@ L.SolrHeatmap = L.GeoJSON.extend({ var metersPerPixel = _this._getMetersPerPixel(latlng); var cellSizePixels = _this._getCellSize(); var cellSizeKm = (metersPerPixel * cellSizePixels) / 1000.; + var cellSizeDegrees = cellSizeKm / 111. + console.log('cellSizeDegrees', cellSizeDegrees); var lat = parseFloat(latlng.lat); var lng = parseFloat(latlng.lng); var bboxField = _this.options.bboxField @@ -327,7 +331,7 @@ L.SolrHeatmap = L.GeoJSON.extend({ // why does this intersection box have to be so large? var query = '{!field f=' + bboxField + ' score=overlapRatio}Intersects(ENVELOPE(' + - (lng - .5) + ',' + (lng + .5) + ',' + (lat + .5) + ',' + (lat - .5) + (lng - cellSizeDegrees) + ',' + (lng + cellSizeDegrees) + ',' + (lat + cellSizeDegrees) + ',' + (lat - cellSizeDegrees) + '))'; var queryHash = {q: query, rows: 20, wt: 'json'}; if (areaField) @@ -384,8 +388,18 @@ L.SolrHeatmap = L.GeoJSON.extend({ for (var i = 0 ; i < popupDisplay.length ; i++) { var field = popupDisplay[i]; - var value = _this._popupFieldFormatter(doc, field); - fullValue += value + ". " + if ((typeof field) === "object") + { + // if current field is an array it is ['fieldname', function(doc,fieldname){...} + var fieldname = field[0]; + var passedFunction = field[1]; + fullValue += passedFunction(doc, fieldname); + } + else + { + var value = _this._popupFieldFormatter(doc, field); + fullValue += value + ". "; + } } return fullValue + "
"; }