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

Experiment with autogenerating grid-position attributes and a recipe for gridded data #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 96 additions & 27 deletions mapbox_tilesets/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tilesets command line interface"""
import json
import tempfile
import math

import click
import cligj
Expand Down Expand Up @@ -33,7 +34,7 @@ def cli():
type=click.Path(exists=True),
help="path to a Recipe JSON document",
)
@click.option("--name", "-n", required=True, type=str, help="name of the tileset")
@click.option("--name", "-n", required=False, type=str, help="name of the tileset")
@click.option(
"--description", "-d", required=False, type=str, help="description of the tileset"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ def create(
mapbox_api, tileset, mapbox_token
)
body = {}
body["name"] = name or ""
body["name"] = name or tileset
body["description"] = description or ""
if privacy:
body["private"] = True if privacy == "private" else False
Expand All @@ -98,6 +99,11 @@ def create(
r = s.post(url, json=body)

click.echo(json.dumps(r.json(), indent=indent))
if r.status_code == 200:
click.echo(
f"You can publish your tileset with the `tilesets publish {tileset}` command.",
err=True,
)


@cli.command("publish")
Expand Down Expand Up @@ -509,19 +515,20 @@ def validate_source(features):
is_flag=True,
help="Replace the existing source with the new source file",
)
@click.option("--grid", is_flag=True, help="Add attributes for gridded data")
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
@click.pass_context
def upload_source(
ctx, username, id, features, no_validation, quiet, replace, token=None, indent=None
ctx, username, id, features, no_validation, quiet, replace, grid, token=None, indent=None
):
return _upload_source(
ctx, username, id, features, no_validation, quiet, replace, token, indent
ctx, username, id, features, no_validation, quiet, replace, grid, token, indent
)


def _upload_source(
ctx, username, id, features, no_validation, quiet, replace, token=None, indent=None
ctx, username, id, features, no_validation, quiet, replace, grid, token=None, indent=None
):
"""Create/add a tileset source

Expand Down Expand Up @@ -559,46 +566,108 @@ def _upload_source(
f"Token {mapbox_token} does not contain a username"
)

grid_x_size = 0
grid_y_size = 0
grid_keys = set()

with tempfile.TemporaryFile() as file:
for feature in features:
if not no_validation:
utils.validate_geojson(feature)

if grid:
(x, y, wx, wy) = utils.centroid(feature);

if grid_x_size == 0:
grid_x_size = wx
grid_y_size = wy

for k in feature['properties']:
grid_keys.add(k)

feature['properties']['grid_x'] = int(math.floor(x / grid_x_size))
feature['properties']['grid_y'] = int(math.floor(y / grid_y_size))

file.write(
(json.dumps(feature, separators=(",", ":")) + "\n").encode("utf-8")
)

if grid:
aggregations = {}
for k in grid_keys:
aggregations[k] = 'arbitrary'
# 7 because of the expectation that a 128x128 (2^7) grid is appropriate in each tile
maxzoom = math.ceil(math.log(360 / ((grid_x_size + grid_y_size) / 2)) / math.log(2)) - 7
recipe = {
'version': 1,
'layers': {
'grid': {
'minzoom': 0,
'maxzoom': maxzoom,
'source': 'mapbox://tileset-source/' + username + '/' + id,
'features': {
'simplification': 0,
'attributes': {
'allowed_output': builtins.list(grid_keys),
'set': {
'grid_x': [ 'let', 'maxzoom', maxzoom, [ 'floor', [ '/', [ 'get', 'grid_x' ], [ '^', 2, [ '-', [ 'var', 'maxzoom' ], [ 'zoom' ] ] ] ] ] ],
'grid_y': [ 'let', 'maxzoom', maxzoom, [ 'floor', [ '/', [ 'get', 'grid_y' ], [ '^', 2, [ '-', [ 'var', 'maxzoom' ], [ 'zoom' ] ] ] ] ] ]
}
}
},
'tiles': {
'union': [
{
'group_by': [ 'grid_x', 'grid_y' ],
'aggregate': aggregations
},
{
'group_by': builtins.list(grid_keys)
}
]
}
}
}
}
click.echo('Suggested recipe:')
click.echo(json.dumps(recipe, indent=indent))
click.echo('If your tiles get capped, try changing the "maxzoom" and the "set" expressions to use a zoom level of ' + str(maxzoom + 1) + ' instead of ' + str(maxzoom) + ', or remove some unneeded attributes.')
click.echo('You may want to change the "aggregate" to use "sum", "min", "max", or "mean" instead of taking the aggregated attribute value from some "arbitrary" one of the features being unioned.')

file.seek(0)
m = MultipartEncoder(fields={"file": ("file", file)})
upload_multipart(m, s, method, url, quiet, indent)


def upload_multipart(m, s, method, url, quiet, indent):
if quiet:
resp = getattr(s, method)(
url,
data=m,
headers={
"Content-Disposition": "multipart/form-data",
"Content-type": m.content_type,
},
)
else:
prog = click.progressbar(
length=m.len, fill_char="=", width=0, label="upload progress"
)
with prog:

if quiet:
def callback(m):
prog.pos = m.bytes_read
prog.update(0) # Step is 0 because we set pos above

monitor = MultipartEncoderMonitor(m, callback)
resp = getattr(s, method)(
url,
data=m,
data=monitor,
headers={
"Content-Disposition": "multipart/form-data",
"Content-type": m.content_type,
"Content-type": monitor.content_type,
},
)
else:
prog = click.progressbar(
length=m.len, fill_char="=", width=0, label="upload progress"
)
with prog:

def callback(m):
prog.pos = m.bytes_read
prog.update(0) # Step is 0 because we set pos above

monitor = MultipartEncoderMonitor(m, callback)
resp = getattr(s, method)(
url,
data=monitor,
headers={
"Content-Disposition": "multipart/form-data",
"Content-type": monitor.content_type,
},
)

if resp.status_code == 200:
click.echo(json.dumps(resp.json(), indent=indent))
Expand Down
34 changes: 34 additions & 0 deletions mapbox_tilesets/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import math

import numpy as np

Expand Down Expand Up @@ -209,3 +210,36 @@ def calculate_tiles_area(features, precision):
zoom = _convert_precision_to_zoom(precision)
tiles = burn(features, zoom)
return np.sum(_calculate_tile_area(tiles))


def centroid(poly):
if poly['geometry']['type'] != 'Polygon':
raise mapbox_tilesets.errors.TilesetsError(
"Gridded date must be Polygons: " + json.dumps(poly)
)
if len(poly['geometry']['coordinates']) != 1:
raise mapbox_tilesets.errors.TilesetsError(
"Gridded date must be single-ring Polygons: " + json.dumps(poly)
)

x = 0
y = 0
count = 0;

x_min = math.inf
x_max = -math.inf
y_min = math.inf
y_max = -math.inf

for i in range(len(poly['geometry']['coordinates'][0]) - 1):
px = poly['geometry']['coordinates'][0][i][0]
py = poly['geometry']['coordinates'][0][i][1]
count = count + 1
x = x + px
y = y + py
x_max = max(x_max, px);
x_min = min(x_min, px);
y_max = max(y_max, py);
y_min = min(y_min, py);

return (x / count, y / count, x_max - x_min, y_max - y_min)