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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2026 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\Services;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\PlaceLocation;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;
use function abs;
use function array_filter;
use function array_unshift;
use function implode;
use function mb_strtolower;
use function round;
/**
* Process geographic data.
*/
class MapDataService
{
// Location of files to import
public const string PLACES_FOLDER = 'places/';
// Format of CSV files
public const string CSV_SEPARATOR = ';';
public function findById(int $id): PlaceLocation
{
$hierarchy = [];
while (true) {
$row = DB::table('place_location')
->where('id', '=', $id)
->select(['place', 'parent_id'])
->first();
if ($row === null) {
return new PlaceLocation(implode(Gedcom::PLACE_SEPARATOR, $hierarchy));
}
$hierarchy[] = $row->place;
$id = $row->parent_id;
}
}
/**
* Which trees use a particular location?
*
* @return array<list<object{p_place:string,tree_name:string,tree_title:string,p_id:int}>>
*/
public function activePlaces(PlaceLocation $location): array
{
$parents = $this->placeIdsForLocation($location);
$children = [];
$rows = DB::table('places')
->join('gedcom', 'gedcom_id', '=', 'p_file')
->whereIn('p_parent_id', $parents)
->select(['p_place', 'gedcom_name AS tree_name', 'title AS tree_title', 'p_id'])
->get()
->map(static fn (object $row): object => (object) [
'p_place' => $row->p_place,
'tree_name' => $row->tree_name,
'tree_title' => $row->tree_title,
'p_id' => (int) $row->p_id,
]);
foreach ($rows as $row) {
$children[mb_strtolower($row->p_place)][] = $row;
}
return $children;
}
/**
* Make sure that all places in the genealogy data also exist in the location data.
*/
public function importMissingLocations(): void
{
$all_places = DB::table('places AS p0')
->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id')
->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id')
->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id')
->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id')
->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id')
->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id')
->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id')
->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id')
->select([
'p0.p_place AS part_0',
'p1.p_place AS part_1',
'p2.p_place AS part_2',
'p3.p_place AS part_3',
'p4.p_place AS part_4',
'p5.p_place AS part_5',
'p6.p_place AS part_6',
'p7.p_place AS part_7',
'p8.p_place AS part_8',
])
->get()
->map(static fn (object $row): string => implode(Gedcom::PLACE_SEPARATOR, array_filter((array) $row)));
$all_locations = DB::table('place_location AS p0')
->leftJoin('place_location AS p1', 'p1.id', '=', 'p0.parent_id')
->leftJoin('place_location AS p2', 'p2.id', '=', 'p1.parent_id')
->leftJoin('place_location AS p3', 'p3.id', '=', 'p2.parent_id')
->leftJoin('place_location AS p4', 'p4.id', '=', 'p3.parent_id')
->leftJoin('place_location AS p5', 'p5.id', '=', 'p4.parent_id')
->leftJoin('place_location AS p6', 'p6.id', '=', 'p5.parent_id')
->leftJoin('place_location AS p7', 'p7.id', '=', 'p6.parent_id')
->leftJoin('place_location AS p8', 'p8.id', '=', 'p7.parent_id')
->select([
'p0.place AS part_0',
'p1.place AS part_1',
'p2.place AS part_2',
'p3.place AS part_3',
'p4.place AS part_4',
'p5.place AS part_5',
'p6.place AS part_6',
'p7.place AS part_7',
'p8.place AS part_8',
])
->get()
->map(static fn (object $row): string => implode(Gedcom::PLACE_SEPARATOR, array_filter((array) $row)));
$missing = $all_places->diff($all_locations);
foreach ($missing as $location) {
(new PlaceLocation($location))->id();
}
}
public function deleteRecursively(int $id): void
{
// Uses on-delete-cascade
DB::table('place_location')
->where('id', '=', $id)
->delete();
}
/**
* @param list<int> $parent_place_ids
*/
public function deleteUnusedLocations(int|null $parent_location_id, array $parent_place_ids): void
{
if ($parent_location_id === null) {
$location_query = DB::table('place_location')
->whereNull('parent_id');
} else {
$location_query = DB::table('place_location')
->where('parent_id', '=', $parent_location_id);
}
foreach ($location_query->get() as $location) {
$places = DB::table('places')
->whereIn('p_parent_id', $parent_place_ids)
->where('p_place', '=', $location->place)
->get();
if ($places->isEmpty()) {
FlashMessages::addMessage(I18N::translate('“%s” has been deleted.', e($location->place)));
DB::table('place_location')
->where('id', '=', $location->id)
->delete();
} else {
$place_ids = $places->map(static fn (object $place): int => (int) $place->p_id)->all();
$this->deleteUnusedLocations((int) $location->id, $place_ids);
}
}
}
/**
* Find a list of child places.
* How many children does each child place have? How many have co-ordinates?
*
* @return Collection<int,object{id:int,key:string,place:string,latitude:float|null,longitude:float|null,child_count:int,no_coord:int}>
*/
public function getPlaceListLocation(int|null $parent_id): Collection
{
$expression =
DB::prefix('p1') . '.place IS NOT NULL AND ' . DB::prefix('p1') . '.latitude IS NULL OR ' .
DB::prefix('p2') . '.place IS NOT NULL AND ' . DB::prefix('p2') . '.latitude IS NULL OR ' .
DB::prefix('p3') . '.place IS NOT NULL AND ' . DB::prefix('p3') . '.latitude IS NULL OR ' .
DB::prefix('p4') . '.place IS NOT NULL AND ' . DB::prefix('p4') . '.latitude IS NULL OR ' .
DB::prefix('p5') . '.place IS NOT NULL AND ' . DB::prefix('p5') . '.latitude IS NULL OR ' .
DB::prefix('p6') . '.place IS NOT NULL AND ' . DB::prefix('p6') . '.latitude IS NULL OR ' .
DB::prefix('p7') . '.place IS NOT NULL AND ' . DB::prefix('p7') . '.latitude IS NULL OR ' .
DB::prefix('p8') . '.place IS NOT NULL AND ' . DB::prefix('p8') . '.latitude IS NULL OR ' .
DB::prefix('p9') . '.place IS NOT NULL AND ' . DB::prefix('p9') . '.latitude IS NULL';
$expression = 'CASE ' . $expression . ' WHEN TRUE THEN 1 ELSE 0 END';
$query = DB::table('place_location AS p0')
->leftJoin('place_location AS p1', 'p1.parent_id', '=', 'p0.id')
->leftJoin('place_location AS p2', 'p2.parent_id', '=', 'p1.id')
->leftJoin('place_location AS p3', 'p3.parent_id', '=', 'p2.id')
->leftJoin('place_location AS p4', 'p4.parent_id', '=', 'p3.id')
->leftJoin('place_location AS p5', 'p5.parent_id', '=', 'p4.id')
->leftJoin('place_location AS p6', 'p6.parent_id', '=', 'p5.id')
->leftJoin('place_location AS p7', 'p7.parent_id', '=', 'p6.id')
->leftJoin('place_location AS p8', 'p8.parent_id', '=', 'p7.id')
->leftJoin('place_location AS p9', 'p9.parent_id', '=', 'p8.id');
if ($parent_id === null) {
$query->whereNull('p0.parent_id');
} else {
$query->where('p0.parent_id', '=', $parent_id);
}
return $query
->groupBy(['p0.id'])
->select([
'p0.*',
new Expression('COUNT(' . DB::prefix('p1') . '.id) AS child_count'),
new Expression('SUM(' . $expression . ') AS no_coord'),
])
->get()
->map(static fn (object $row): object => (object) [
'id' => (int) $row->id,
'place' => $row->place,
'key' => mb_strtolower($row->place),
'latitude' => $row->latitude === null ? null : (float) $row->latitude,
'longitude' => $row->longitude === null ? null : (float) $row->longitude,
'child_count' => (int) $row->child_count,
'no_coord' => (int) $row->no_coord,
])
->sort(static fn (object $x, object $y): int => I18N::comparator()($x->place, $y->place));
}
public function writeLatitude(float $latitude): string
{
return $this->writeDegrees($latitude, Gedcom::LATITUDE_NORTH, Gedcom::LATITUDE_SOUTH);
}
public function writeLongitude(float $longitude): string
{
return $this->writeDegrees($longitude, Gedcom::LONGITUDE_EAST, Gedcom::LONGITUDE_WEST);
}
/**
* Find all active places that match a location
*
* @return array<string>
*/
private function placeIdsForLocation(PlaceLocation $location): array
{
$hierarchy = [];
while ($location->id() !== null) {
array_unshift($hierarchy, $location->locationName());
$location = $location->parent();
}
$place_ids = ['0'];
foreach ($hierarchy as $place_name) {
$place_ids = DB::table('places')
->whereIn('p_parent_id', $place_ids)
->where('p_place', '=', $place_name)
->groupBy(['p_id'])
->pluck('p_id')
->all();
}
return $place_ids;
}
private function writeDegrees(float $degrees, string $positive, string $negative): string
{
$degrees = round($degrees, 5);
if ($degrees < 0.0) {
return $negative . abs($degrees);
}
return $positive . $degrees;
}
}
|