-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Display image overlay annotations #750
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4a913c5
(WIP) Display image overlay annotations
naglepuff c9fdce4
Include overlays in removeAnnotation function
naglepuff 8409bfe
Apply image overlay transform using proj strings
naglepuff 084f76e
Fix some lint/testing issues
naglepuff 1cd2881
Unclamp map bounds if image overlays are present
naglepuff a20f3ce
Create test for overlay annotations
naglepuff 5b3f52b
Don't specify full transform if not specified
naglepuff dc210e3
Add property to control clamping bounds
naglepuff 0bbbe08
Use canvas to render overlays if many present
naglepuff 2cf5c3f
Don't unconditionally re-clamp bounds
naglepuff 546ddf0
Fix proj string generation
naglepuff 3a5bb26
Update layer params to account for zoom level
naglepuff 86cc293
Allow negative x/y offset for image overlays
naglepuff a1743d5
Fix various issues with layer param generation
naglepuff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import Backbone from 'backbone'; | |
|
||
import events from '@girder/core/events'; | ||
import { wrap } from '@girder/core/utilities/PluginUtils'; | ||
import { restRequest } from '@girder/core/rest'; | ||
|
||
import convertAnnotation from '../../annotations/geojs/convert'; | ||
|
||
|
@@ -25,6 +26,7 @@ var GeojsImageViewerWidgetExtension = function (viewer) { | |
|
||
this._annotations = {}; | ||
this._featureOpacity = {}; | ||
this._unclampBoundsForOverlay = true; | ||
this._globalAnnotationOpacity = settings.globalAnnotationOpacity || 1.0; | ||
this._globalAnnotationFillOpacity = settings.globalAnnotationFillOpacity || 1.0; | ||
this._highlightFeatureSizeLimit = settings.highlightFeatureSizeLimit || 10000; | ||
|
@@ -55,6 +57,114 @@ var GeojsImageViewerWidgetExtension = function (viewer) { | |
|
||
annotationAPI: _.constant(true), | ||
|
||
/** | ||
* @returns whether to clamp viewer bounds when image overlays are | ||
* rendered | ||
*/ | ||
getUnclampBoundsForOverlay: function () { | ||
return this._unclampBoundsForOverlay; | ||
}, | ||
|
||
/** | ||
* | ||
* @param {bool} newValue Set whether to clamp viewer bounds when image | ||
* overlays are rendered. | ||
*/ | ||
setUnclampBoundsForOverlay: function (newValue) { | ||
this._unclampBoundsForOverlay = newValue; | ||
}, | ||
|
||
/** | ||
* Given an image overlay annotation element, compute and return | ||
* a proj-string representation of its transform specification. | ||
* @param {object} overlay An imageoverlay annotation element. | ||
* @returns a proj-string representing how to overlay should be tranformed. | ||
*/ | ||
_getOverlayTransformProjString: function (overlay) { | ||
const transformInfo = overlay.transform || {}; | ||
let xOffset = transformInfo.xoffset || 0; | ||
const yOffset = transformInfo.yoffset || 0; | ||
const matrix = transformInfo.matrix || [[1, 0], [0, 1]]; | ||
const s11 = matrix[0][0]; | ||
const s12 = matrix[0][1]; | ||
const s21 = matrix[1][0]; | ||
const s22 = matrix[1][1]; | ||
|
||
let projString = '+proj=longlat +axis=enu'; | ||
if (xOffset !== 0) { | ||
// negate x offset so positive values specified in the annotation | ||
// move overlays to the right | ||
xOffset = -1 * xOffset; | ||
projString = projString + ` +xoff=${xOffset}`; | ||
} | ||
if (yOffset !== 0) { | ||
projString = projString + ` +yoff=${yOffset}`; | ||
} | ||
if (s11 !== 1 || s12 !== 0 || s21 !== 0 || s22 !== 1) { | ||
// add affine matrix vals to projection string if not identity matrix | ||
projString = projString + ` +s11=${1 / s11} +s12=${s12} +s21=${s21} +s22=${1 / s22}`; | ||
} | ||
return projString; | ||
}, | ||
|
||
/** | ||
* @returns The number of currently drawn overlay elements across | ||
* all annotations. | ||
*/ | ||
_countDrawnImageOverlays: function () { | ||
let numOverlays = 0; | ||
_.each(this._annotations, (value, key, obj) => { | ||
let annotationOverlays = value.overlays || []; | ||
numOverlays += annotationOverlays.length; | ||
}); | ||
return numOverlays; | ||
}, | ||
|
||
/** | ||
* Generate layer parameters for an image overlay layer | ||
* @param {object} overlayImageMetadata metadata such as size, tile size, and levels for the overlay image | ||
* @param {string} overlayImageId ID of a girder image item | ||
* @param {object} overlay information about the overlay such as opacity | ||
* @returns layer params for the image overlay layer | ||
*/ | ||
_generateOverlayLayerParams(overlayImageMetadata, overlayImageId, overlay) { | ||
const geo = window.geo; | ||
let params = geo.util.pixelCoordinateParams( | ||
this.viewer.node(), overlayImageMetadata.sizeX, overlayImageMetadata.sizeY, overlayImageMetadata.tileHeight, overlayImageMetadata.tileWidth | ||
); | ||
params.layer.useCredentials = true; | ||
params.layer.url = `api/v1/item/${overlayImageId}/tiles/zxy/{z}/{x}/{y}`; | ||
if (this._countDrawnImageOverlays() <= 6) { | ||
params.layer.autoshareRenderer = false; | ||
} else { | ||
params.layer.renderer = 'canvas'; | ||
} | ||
params.layer.opacity = overlay.opacity || 1; | ||
|
||
if (this.levels !== overlayImageMetadata.levels) { | ||
const levelDifference = Math.abs(this.levels - overlayImageMetadata.levels); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should NOT be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in a1743d5 |
||
params.layer.url = (x, y, z) => 'api/v1/item/' + overlayImageId + `/tiles/zxy/${z - levelDifference}/${x}/${y}`; | ||
params.layer.minLevel = levelDifference; | ||
params.layer.maxLevel += levelDifference; | ||
|
||
params.layer.tilesMaxBounds = (level) => { | ||
var scale = Math.pow(2, params.layer.maxLevel - level); | ||
return { | ||
x: Math.floor(overlayImageMetadata.sizeX / scale), | ||
y: Math.floor(overlayImageMetadata.sizeY / scale) | ||
}; | ||
}; | ||
params.layer.tilesAtZoom = (level) => { | ||
var scale = Math.pow(2, params.layer.maxLevel - level); | ||
return { | ||
x: Math.ceil(overlayImageMetadata.sizeX / overlayImageMetadata.tileWidth / scale), | ||
y: Math.ceil(overlayImageMetadata.sizeY / overlayImageMetadata.tileHeight / scale) | ||
}; | ||
}; | ||
} | ||
return params.layer; | ||
}, | ||
|
||
/** | ||
* Render an annotation model on the image. Currently, this is limited | ||
* to annotation types that can be (1) directly converted into geojson | ||
|
@@ -93,11 +203,18 @@ var GeojsImageViewerWidgetExtension = function (viewer) { | |
centroidFeature = feature; | ||
} | ||
}); | ||
_.each(this._annotations[annotation.id].overlays, (overlay) => { | ||
const overlayLayer = this.viewer.layers().find( | ||
(layer) => layer.id() === overlay.id); | ||
this.viewer.deleteLayer(overlayLayer); | ||
}); | ||
} | ||
const overlays = annotation.overlays() || []; | ||
this._annotations[annotation.id] = { | ||
features: centroidFeature ? [centroidFeature] : [], | ||
options: options, | ||
annotation: annotation | ||
annotation: annotation, | ||
overlays: overlays | ||
}; | ||
if (options.fetch && (!present || annotation.refresh() || annotation._inFetch === 'centroids')) { | ||
annotation.off('g:fetched', null, this).on('g:fetched', () => { | ||
|
@@ -190,6 +307,26 @@ var GeojsImageViewerWidgetExtension = function (viewer) { | |
} | ||
}); | ||
} | ||
// draw overlays | ||
if (this.getUnclampBoundsForOverlay() && this._annotations[annotation.id].overlays.length > 0) { | ||
this.viewer.clampBoundsY(false); | ||
this.viewer.clampBoundsX(false); | ||
manthey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
_.each(this._annotations[annotation.id].overlays, (overlay) => { | ||
const overlayItemId = overlay.girderId; | ||
restRequest({ | ||
url: `item/${overlayItemId}/tiles` | ||
}).done((response) => { | ||
let params = this._generateOverlayLayerParams(response, overlayItemId, overlay); | ||
const overlayLayer = this.viewer.createLayer('osm', params); | ||
overlayLayer.id(overlay.id); | ||
const proj = this._getOverlayTransformProjString(overlay); | ||
overlayLayer.gcs(proj); | ||
this.viewer.scheduleAnimationFrame(this.viewer.draw, true); | ||
}).fail((response) => { | ||
console.error(`There was an error overlaying image with ID ${overlayItemId}`); | ||
}); | ||
}); | ||
this._featureOpacity[annotation.id] = {}; | ||
geo.createFileReader('jsonReader', {layer: this.featureLayer}) | ||
.read(geojson, (features) => { | ||
|
@@ -402,8 +539,20 @@ var GeojsImageViewerWidgetExtension = function (viewer) { | |
this.featureLayer.deleteFeature(feature); | ||
} | ||
}); | ||
_.each(this._annotations[annotation.id].overlays, (overlay) => { | ||
const overlayLayer = this.viewer.layers().find( | ||
(layer) => layer.id() === overlay.id); | ||
this.viewer.deleteLayer(overlayLayer); | ||
}); | ||
delete this._annotations[annotation.id]; | ||
delete this._featureOpacity[annotation.id]; | ||
|
||
// If removing an overlay annotation results in no more overlays drawn, and we've | ||
// previously un-clamped bounds for overlays, re-clamp bounds | ||
if (this._countDrawnImageOverlays() === 0 && this.getUnclampBoundsForOverlay()) { | ||
this.viewer.clampBoundsY(true); | ||
this.viewer.clampBoundsX(true); | ||
} | ||
this.viewer.scheduleAnimationFrame(this.viewer.draw); | ||
} | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tileWidth and tileHeight are swapped in this call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in a1743d5