-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoundaryCommand.php
85 lines (71 loc) · 2.31 KB
/
BoundaryCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace App\Command;
use App\Exception\ConfigException;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Based on `relationId` from `config.php` file, download city boundary geometry as GeoJSON file.
*
* @package App\Command
*/
class BoundaryCommand extends AbstractCommand
{
/** {@inheritdoc} */
protected static $defaultName = 'boundary';
/** @var string Script URL (by OpenStreetMap France). */
protected const URL = 'http://polygons.openstreetmap.fr/get_geojson.py';
/** @var string Filename for the result. */
protected const FILENAME = 'boundary.geojson';
/**
* {@inheritdoc}
*
* @return void
*
* @throws InvalidArgumentException
*/
protected function configure(): void
{
parent::configure();
$this->setDescription('Download city boundary from OpenStreetMap.');
}
/**
* {@inheritdoc}
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
parent::execute($input, $output);
if (!isset($this->config->relationId)) {
throw new ConfigException('"relationId" parameter is missing or is invalid in "config.php".');
}
self::save($this->config->relationId, sprintf('%s/%s', $this->cityOutputDir, self::FILENAME));
return Command::SUCCESS;
} catch (Exception $error) {
$output->writeln(sprintf('<error>%s</error>', $error->getMessage()));
return Command::FAILURE;
}
}
/**
* Send request and store result.
*
* @param int $id OpenStreetMap relation identifier.
* @param string $path Path where to store the result.
* @return void
*
* @throws GuzzleException
*/
private static function save(int $id, string $path): void
{
$url = sprintf('%s?id=%d', self::URL, $id);
$client = new \GuzzleHttp\Client();
$client->request('GET', $url, ['sink' => $path]);
}
}