Skip to content

Commit

Permalink
#6 improved popup
Browse files Browse the repository at this point in the history
added support for single field formatter
also, some small bug fixes
  • Loading branch information
spacemansteve committed Apr 19, 2016
1 parent 387175b commit 7153150
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 14 deletions.
5 changes: 4 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
79 changes: 70 additions & 9 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<a target='_blank' href='http://www.jstor.org/stable/" + value + "'>" + value + "</a>";
};
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();
22 changes: 18 additions & 4 deletions leafletSolrHeatmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -320,14 +322,16 @@ 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
var areaField = _this.options.areaField;

// 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)
Expand Down Expand Up @@ -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 + "<br/>";
}
Expand Down

0 comments on commit 7153150

Please sign in to comment.