OWSLib icon indicating copy to clipboard operation
OWSLib copied to clipboard

Create world file from wmts.gettile, and other wmts suggestions

Open skinkie opened this issue 5 years ago • 0 comments

At this moment one could download a tile from a wmts like so:

tile = self.wmts.gettile(layer=layer, tilematrixset=tilematrixset, tilematrix=tilematrix, row=row, column=column, format=format)
with open(filename, 'wb') as f:
  bytes_written = f.write(tile.read())
  f.close()

To me it would make sense if writing a world file for the just acquired tile was embedded in the reply. Ideally a single call, but I could also understand if self.wmts.getworldfile with the same arguments would be used. The generation of the worldfile itself for tiles is not very complex.

    matrix = self.tilematrixsets[tilematrixset].tilematrix[tilematrix]
    a = matrix.scaledenominator * 0.00028
    e = matrix.scaledenominator * -0.00028
    left = ((column * matrix.tilewidth + 0.5) * a) + matrix.topleftcorner[0]
    top = ((row * matrix.tileheight + 0.5) * e) + matrix.topleftcorner[1]
    return '%f\n%d\n%d\n%f\n%f\n%f' % (a, 0, 0, e, left, top)

One could also argue that the actual computation to get the tile(s) of interest should be more convenient. For example:

    matrix = self.tilematrixsets[tilematrixset].tilematrix[tilematrix]

    a = matrix.scaledenominator * 0.00028
    e = matrix.scaledenominator * -0.00028

    column_orig = math.floor((bbox[0] - matrix.topleftcorner[0]) / (a * matrix.tilewidth))
    row_orig = math.floor((bbox[1] - matrix.topleftcorner[1]) / (e * matrix.tilewidth))

    column_dest = math.floor((bbox[2] - matrix.topleftcorner[0]) / (a * matrix.tilewidth))
    row_dest = math.floor((bbox[3] - matrix.topleftcorner[1]) / (e * matrix.tilewidth))

    if (column_orig > column_dest):
        t = column_orig
        column_orig = column_dest
        column_dest = t

    if (row_orig > row_dest):
        t = row_orig
        row_orig = row_dest
        row_dest = t

    column_dest += 1
    row_dest += 1

    return ((column_orig, column_dest), (row_orig, row_dest))

skinkie avatar Jan 10 '21 15:01 skinkie