backspaces / agentscript

An Agent Based Modeling system (ABM)
http://backspaces.github.io/agentscript/
GNU General Public License v3.0
107 stars 14 forks source link

Might be nice to guess a good Z also. #64

Open m0ose opened 2 years ago

m0ose commented 2 years ago

The center is guessed from the bbox. It might be nice to guess the Z value also. https://github.com/backspaces/agentscript/blob/8e999fe5f2e6af122c35df2fcaa43499c2e8d3aa/gis/leafletInit.js#L68

backspaces commented 2 years ago

So the center is basically the center coords of the model. That seems reasonable for Leaflet's Map constructor .. centers the model in the middle of Leaflet.

What could we do for Z? Possibly figure out the Z that would make sure all the model is in Leaflet's view? I imagine there is a power of 2 stunt for that.

For both LeafletDraw and leafleIInit we could calculate these in the options object. And possibly add the Z calc in src/gis.js. I think center is already there.

We might want to add pixel versions too? I.e. the programmer could know the pixel width/height of Leaflet's div and prefer to not bother with lon/lats

backspaces commented 2 years ago

Maybe tilebelt has a hint?

backspaces commented 2 years ago

tilebelt has a function that gives a zoom given a bbox. It is not documented or mentioned, but is used by bboxToTile which "Get the smallest tile to cover a bbox"

I have no idea how it works!

function getBboxZoom(bbox) {
    var MAX_ZOOM = 28;
    for (var z = 0; z < MAX_ZOOM; z++) {
        var mask = 1 << (32 - (z + 1));
        if (((bbox[0] & mask) !== (bbox[2] & mask)) ||
            ((bbox[1] & mask) !== (bbox[3] & mask))) {
            return z;
        }
    }

    return MAX_ZOOM;
}
m0ose commented 2 years ago

That is wild. Is bbox in the form [west, south, east, north] ?

backspaces commented 2 years ago

Yup, but I'm not so sure it works. It gave a pretty odd result for the santa fe bbox:

bbox = [-109.050044, 31.332301, -103.001964, 37.000104] (4) [-109.050044, 31.332301, -103.001964, 37.000104] bboxToTile(bbox) (3) [3, 6, 4]