xref: /webtrees/app/Http/RequestHandlers/MapDataImportAction.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Exception;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\I18N;
26use Fisharebest\Webtrees\PlaceLocation;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\MapDataService;
29use Illuminate\Database\Capsule\Manager as DB;
30use League\Flysystem\FilesystemException;
31use League\Flysystem\UnableToCheckFileExistence;
32use League\Flysystem\UnableToReadFile;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Message\UploadedFileInterface;
36use Psr\Http\Server\RequestHandlerInterface;
37
38use function array_filter;
39use function array_reverse;
40use function array_slice;
41use function count;
42use function fclose;
43use function fgetcsv;
44use function implode;
45use function is_numeric;
46use function json_decode;
47use function redirect;
48use function rewind;
49use function route;
50use function str_contains;
51use function stream_get_contents;
52
53use const JSON_THROW_ON_ERROR;
54use const UPLOAD_ERR_OK;
55
56/**
57 * Import geographic data.
58 */
59class MapDataImportAction implements RequestHandlerInterface
60{
61    private MapDataService $map_data_service;
62
63    /**
64     * MapDataImportAction constructor.
65     *
66     * @param MapDataService $map_data_service
67     */
68    public function __construct(MapDataService $map_data_service)
69    {
70        $this->map_data_service = $map_data_service;
71    }
72
73    /**
74     * This function assumes the input file layout is
75     * level followed by a variable number of placename fields
76     * followed by Longitude, Latitude, Zoom & Icon
77     *
78     * @param ServerRequestInterface $request
79     *
80     * @return ResponseInterface
81     * @throws Exception
82     */
83    public function handle(ServerRequestInterface $request): ResponseInterface
84    {
85        $data_filesystem = Registry::filesystem()->data();
86
87        $params = (array) $request->getParsedBody();
88
89        $serverfile     = $params['serverfile'] ?? '';
90        $options        = $params['import-options'] ?? '';
91        $local_file     = $request->getUploadedFiles()['localfile'] ?? null;
92
93        $places = [];
94
95        $url = route(MapDataList::class, ['parent_id' => 0]);
96
97        $fp = false;
98
99        try {
100            $file_exists = $data_filesystem->fileExists(MapDataService::PLACES_FOLDER . $serverfile);
101        } catch (FilesystemException | UnableToCheckFileExistence $ex) {
102            $file_exists = false;
103        }
104
105
106        if ($serverfile !== '' && $file_exists) {
107            // first choice is file on server
108            try {
109                $fp = $data_filesystem->readStream(MapDataService::PLACES_FOLDER . $serverfile);
110            } catch (FilesystemException | UnableToReadFile $ex) {
111                $fp = false;
112            }
113        } elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) {
114            // 2nd choice is local file
115            $fp = $local_file->getStream()->detach();
116        }
117
118        if ($fp === false || $fp === null) {
119            return redirect($url);
120        }
121
122        $string = stream_get_contents($fp);
123
124        // Check the file type
125        if (str_contains($string, 'FeatureCollection')) {
126            $input_array = json_decode($string, false, 512, JSON_THROW_ON_ERROR);
127
128            foreach ($input_array->features as $feature) {
129                $places[] = [
130                    'latitude'  => $feature->geometry->coordinates[1],
131                    'longitude' => $feature->geometry->coordinates[0],
132                    'name'      => $feature->properties->name,
133                ];
134            }
135        } else {
136            rewind($fp);
137            while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) {
138                // Skip the header
139                if (!is_numeric($row[0])) {
140                    continue;
141                }
142
143                $level = (int) $row[0];
144                $count = count($row);
145                $name  = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level)));
146
147                $places[] = [
148                    'latitude'  => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']),
149                    'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']),
150                    'name'      => $name
151                ];
152            }
153        }
154
155        fclose($fp);
156
157        $added   = 0;
158        $updated = 0;
159
160        // Remove places with 0,0 coordinates at lower levels.
161        $callback = static fn (array $place): bool => !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0;
162
163        $places = array_filter($places, $callback);
164
165        foreach ($places as $place) {
166            $location = new PlaceLocation($place['name']);
167            $exists   = $location->exists();
168
169            // Only update existing records
170            if ($options === 'update' && !$exists) {
171                continue;
172            }
173
174            // Only add new records
175            if ($options === 'add' && $exists) {
176                continue;
177            }
178
179            if (!$exists) {
180                $added++;
181            }
182
183            $updated += DB::table('place_location')
184                ->where('id', '=', $location->id())
185                ->update([
186                    'latitude'  => $place['latitude'],
187                    'longitude' => $place['longitude'],
188                ]);
189        }
190
191        FlashMessages::addMessage(
192            I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)),
193            'info'
194        );
195
196        return redirect($url);
197    }
198}
199