PHP Classes

File: readme.md

Recommend this page to a friend!
  Classes of Niko Peikrishvili   WMTS PHP Map Tile Downloader   readme.md   Download  
File: readme.md
Role: Documentation
Content type: text/markdown
Description: Read me
Class: WMTS PHP Map Tile Downloader
Download tiles of maps of a world region from WMTS
Author: By
Last change:
Date: 1 year ago
Size: 2,273 bytes
 

Contents

Class file image Download

Description

This library provides very basic functionality for downloading Tile layers from a WMTS server. Your goal is to implement the DownloaderInterface with you logic with your logic and pass it to the WMTSTileDownloader class constructor when creating an object from it.

Data about the tile will be passed to the download method, and then you decide what to do.

int $x, int $y, int $z, int $counter

Example

use WMTSTileDownloader\Downloader\BasicDownloader;
use WMTSTileDownloader\Helpers\Mercator;
use WMTSTileDownloader\Types\LatLng;
use WMTSTileDownloader\Types\ZoomLevel;
use WMTSTileDownloader\WMTSTileDownloader;

require 'vendor/autoload.php';

$downloader = new WMTSTileDownloader(mercator: new Mercator(), downloader: new BasicDownloader(saveTo: '/path/to/directory/for/storing/files'));
$nw = new LatLng(59.977005492196, -12.2607421875);
$se = new LatLng(49.610709938074, 1.93359375);
$downloader->generateFromLatLongs(northWest: $nw, southEast: $se, zoomLevel: new ZoomLevel(9));

The library comes with an example BasicDownloader

<?php

declare(strict_types=1);

namespace WMTSTileDownloader\Downloader;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

final readonly class BasicDownloader implements DownloaderInterface
{

    public function __construct(private string $saveTo)
    {
    }

    /
     * @throws GuzzleException
     */
    public function download(int $x, int $y, int $z, int $counter): bool
    {
        $url = 'https://tile.openstreetmap.org/'.$z.'/'.$x.'/'.$y.'.png';
        $this->downloadFile($url,$z.'_'.$x.'_'.$y,'.png');
        return true;
    }

    /
     * @throws GuzzleException
     */
    public function downloadFile(string $url, string $name, string $extensions): void
    {
        $path = rtrim($this->saveTo,'/').'/' . $name . $extensions;
        $client = new Client();
        $client->get($url, ['sink' => $path]);
    }
}

class that implements the DownloaderInterface It is just for example purposes, but in many cases, it may be the only thing that you need: just download tiles in a folder.