Skip to content
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

Native MGL integration #886

Merged
merged 41 commits into from
Aug 24, 2018
Merged

Native MGL integration #886

merged 41 commits into from
Aug 24, 2018

Conversation

Jesus89
Copy link
Member

@Jesus89 Jesus89 commented Aug 22, 2018

Related to #700, #428, #847.

This branch adds a re-implementation with some refactors for the MGL integration interface using the new Custom Layers API. Since this feature is not already in a stable version I have released from our fork the version v0.48.0-carto1 (branch), from the stable v0.48.0 tag and the changes from custom-layers.

This is a pretty big PR. However, the main activity is in the files Layer, Renderer and Map. Here is a list of changes in this branch:

  • Use onAdd, onRemove, prerender and render functions of Custom Layers
  • Implement Layer.removeFrom
  • Refactor maps/Map to match the new interface
  • Allow layer.update(source) using current viz by default
  • Get zoom and center from the matrix
  • Request data when matrix changes
  • Remove integrators. Instead of a set of integrators, keep a set of renderers associated to each map.
  • Clean unused functions and attributes in class Layer
  • Minor fixes in the test framework
  • Use local basemaps for testing
  • Update examples to the new MGL version
  • Fix animation example
  • Fix interactivity examples

If we are interested in testing MGL upstream I can release another version (v0.48.0-carto2) pulling the changes from master.

@Jesus89 Jesus89 added the status: do-not-merge DO NOT MERGE... label Aug 22, 2018
Copy link

@davidmanzanares davidmanzanares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! But, pls, try to avoid huge PR like this one, at least some changes were outside the scope of the PR like the movement of some docs. Thx!

@@ -37,10 +37,7 @@ <h1>Add layer</h1>
container: 'map',
style: 'https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json',
center: [0, 30],
zoom: 2,
scrollZoom: false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for testing. I'll restore it

src/Layer.js Outdated
});

this.id = id;
this.type = 'custom';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this added?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to convert carto.Layer in a native MGL CustomLayer. I'll add a comment there

src/Layer.js Outdated
@@ -138,7 +115,7 @@ export default class Layer {
}

/**
* Add this layer to a map.
* Add this layer to a map. It waits for the map to be loaded.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change the wording

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait for the map to load?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say something like: If the map's style is not loaded yet it will wait for it to add the layer as soon as possible

src/Layer.js Outdated
* @api
*/
removeFrom (map) {
map.removeLayer(this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw an error if the map hasn't been loaded yet and the layer hasn't been added yet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and MGL this is the expected behaviour IMO.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but since we are making things easier in addTo for a similar problem, we could improve this, but I realize that this feature request is not simple, I think we could create an issue with it, are you ok?

* @api
*/
async update (source, viz) {
if (viz === undefined) {
// Use current viz
viz = this._viz;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember well but I think this wasn't done before for a reason, this should be tested well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an example using layer.getViz() as a second argument, so this could be the default behaviour. I'll add tests for this.

const b = map.getBounds();
const nw = b.getNorthWest();
const sw = b.getSouthWest();
const z = (util.projectToWebMercator(nw).y - util.projectToWebMercator(sw).y) / util.WM_2R;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a good idea either, the Renderer didn't know about webmercator or maps at all before

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have decoupled the map so this functions are not in the Renderer anymore

this._zoom = zoom;
_initCenter (map) {
const c = map.getCenter();
this.setCenter(c.lng / 180.0, util.projectToWebMercator(c).y / util.WM_R);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (m1 && m2 && m1.length === m2.length) {
let ret = true;
for (let i = 0; i < m1.length; i++) {
ret &= m1[i] === m2[i];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be simpler and faster without the ret variable, early returning a false when the values mismatch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I changed it already 🤔

@@ -1,8 +1,6 @@
const map = new carto.Map({
container: 'map',
background: 'black',
center: [-75, 40],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was set to avoid draw ordering issues

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this test uses carto.Map that has no center, zoom attributes...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, true, yes, I probably copy pasted them from the example I was thinking about

@@ -57,6 +57,6 @@ function project (coordinates) {
const wm = projectToWebMercator(coordinates);
return {
x: mapSize * (0.5 + wm.x / WM_2R),
y: mapSize * (0.5 - wm.y / WM_2R + 0.03 /* offset */)
y: mapSize * (0.5 - wm.y / WM_2R)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain this since you introduced the magic 0.03? Why was it added? The addition was done on: 7b46dee

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed was required to match the bounding box in the simulated mouse events. I have updated the bounding box in the carto.Map so this is not required anymore.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand it, but if you are sure... 👍

@Jesus89
Copy link
Member Author

Jesus89 commented Aug 23, 2018

Suggestions and more improvements added.

Copy link

@davidmanzanares davidmanzanares left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ready to be merged, but I also think this is the most dangerous PR of this release cycle, please wait for the merging of the other PRs to leave this PR as the last PR of the new release

@elenatorro elenatorro self-requested a review August 24, 2018 07:13
Copy link
Contributor

@elenatorro elenatorro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor code style issues

src/Layer.js Outdated
* @type {LayerEvent}
* @api
*/
function getRenderer (map, gl) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a private function that should be placed at the bottom (after the class) and should be _getRenderer instead of getRenderer.

src/Layer.js Outdated
// TODO: the best solution is to use the matrix at the shader
// level and remove the aspect and scale logic from the renderer
this.renderer.setCenter(
-(1 + 2 * matrix[12] / matrix[0]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this center logic to an external utils file or similar?

src/Layer.js Outdated

_setRendererCenter () {
const c = this.map.getCenter();
this.renderer.setCenter(c.lng / 180.0, util.projectToWebMercator(c).y / util.WM_R);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use constant instead of a number (180.0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is legacy code. It can be done with projectToWebMercator(c).x, etc but this is shorter. However, I'll move this to an utils file

@Jesus89 Jesus89 removed the status: do-not-merge DO NOT MERGE... label Aug 24, 2018
@Jesus89 Jesus89 merged commit 2a13c0b into master Aug 24, 2018
@Jesus89 Jesus89 deleted the mgl-integration branch August 24, 2018 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants