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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2021 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Exception;
use Fisharebest\Webtrees\Exceptions\FileUploadException;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\PlaceLocation;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\MapDataService;
use Illuminate\Database\Capsule\Manager as DB;
use League\Flysystem\FilesystemException;
use League\Flysystem\UnableToCheckFileExistence;
use League\Flysystem\UnableToReadFile;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function array_filter;
use function array_reverse;
use function array_slice;
use function count;
use function fclose;
use function fgetcsv;
use function implode;
use function is_numeric;
use function json_decode;
use function redirect;
use function rewind;
use function route;
use function str_contains;
use function stream_get_contents;
use const JSON_THROW_ON_ERROR;
use const UPLOAD_ERR_OK;
/**
* Import geographic data.
*/
class MapDataImportAction implements RequestHandlerInterface
{
/**
* This function assumes the input file layout is
* level followed by a variable number of placename fields
* followed by Longitude, Latitude, Zoom & Icon
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
* @throws Exception
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data_filesystem = Registry::filesystem()->data();
$params = (array) $request->getParsedBody();
$serverfile = $params['serverfile'] ?? '';
$options = $params['import-options'] ?? '';
$local_file = $request->getUploadedFiles()['localfile'] ?? null;
$places = [];
$url = route(MapDataList::class, ['parent_id' => 0]);
$fp = false;
try {
$file_exists = $data_filesystem->fileExists(MapDataService::PLACES_FOLDER . $serverfile);
} catch (FilesystemException | UnableToCheckFileExistence $ex) {
$file_exists = false;
}
if ($serverfile !== '' && $file_exists) {
// first choice is file on server
try {
$fp = $data_filesystem->readStream(MapDataService::PLACES_FOLDER . $serverfile);
} catch (FilesystemException | UnableToReadFile $ex) {
$fp = false;
}
} elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) {
// 2nd choice is local file
$fp = $local_file->getStream()->detach();
} else {
throw new FileUploadException($local_file);
}
if ($fp === false || $fp === null) {
return redirect($url);
}
$string = stream_get_contents($fp);
// Check the file type
if (str_contains($string, 'FeatureCollection')) {
$input_array = json_decode($string, false, 512, JSON_THROW_ON_ERROR);
foreach ($input_array->features as $feature) {
$places[] = [
'latitude' => $feature->geometry->coordinates[1],
'longitude' => $feature->geometry->coordinates[0],
'name' => $feature->properties->name,
];
}
} else {
rewind($fp);
while (($row = fgetcsv($fp, 0, MapDataService::CSV_SEPARATOR)) !== false) {
// Skip the header
if (!is_numeric($row[0])) {
continue;
}
$level = (int) $row[0];
$count = count($row);
$name = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level)));
$places[] = [
'latitude' => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']),
'longitude' => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']),
'name' => $name
];
}
}
fclose($fp);
$added = 0;
$updated = 0;
// Remove places with 0,0 coordinates at lower levels.
$callback = static fn (array $place): bool => !str_contains($place['name'], ',') || $place['longitude'] !== 0.0 || $place['latitude'] !== 0.0;
$places = array_filter($places, $callback);
foreach ($places as $place) {
$location = new PlaceLocation($place['name']);
$exists = $location->exists();
// Only update existing records
if ($options === 'update' && !$exists) {
continue;
}
// Only add new records
if ($options === 'add' && $exists) {
continue;
}
if (!$exists) {
$added++;
}
$updated += DB::table('place_location')
->where('id', '=', $location->id())
->update([
'latitude' => $place['latitude'],
'longitude' => $place['longitude'],
]);
}
FlashMessages::addMessage(
I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)),
'info'
);
return redirect($url);
}
}
|