fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees; |
||
| 21 | |||
| 22 | use Illuminate\Database\Query\Builder; |
||
| 23 | use Illuminate\Support\Collection; |
||
| 24 | |||
| 25 | use function max; |
||
| 26 | use function min; |
||
| 27 | use function preg_split; |
||
| 28 | use function trim; |
||
| 29 | |||
| 30 | use const PREG_SPLIT_NO_EMPTY; |
||
| 31 | |||
| 32 | class PlaceLocation |
||
| 33 | { |
||
| 34 | // e.g. "Westminster, London, England" |
||
| 35 | private string $location_name; |
||
| 36 | |||
| 37 | /** @var Collection<int,string> The parts of a location name, e.g. ["Westminster", "London", "England"] */ |
||
| 38 | private Collection $parts; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a place-location. |
||
| 42 | * |
||
| 43 | * @param string $location_name |
||
| 44 | */ |
||
| 45 | public function __construct(string $location_name) |
||
| 46 | { |
||
| 47 | // Ignore any empty parts in location names such as "Village, , , Country". |
||
| 48 | $location_name = trim($location_name); |
||
| 49 | $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name, -1, PREG_SPLIT_NO_EMPTY)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 50 | |||
| 51 | // Rebuild the location name in the correct format. |
||
| 52 | $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the higher level location. |
||
| 57 | * |
||
| 58 | * @return PlaceLocation |
||
| 59 | */ |
||
| 60 | public function parent(): PlaceLocation |
||
| 61 | { |
||
| 62 | return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The database row id that contains this location. |
||
| 67 | * Note that due to database collation, both "Quebec" and "Québec" will share the same row. |
||
| 68 | * |
||
| 69 | * @return int|null |
||
| 70 | */ |
||
| 71 | public function id(): int|null |
||
| 72 | { |
||
| 73 | // The "top-level" location won't exist in the database. |
||
| 74 | if ($this->parts->isEmpty()) { |
||
| 75 | return null; |
||
| 76 | } |
||
| 77 | |||
| 78 | return Registry::cache()->array()->remember('location-' . $this->location_name, function () { |
||
| 79 | $parent_id = $this->parent()->id(); |
||
| 80 | |||
| 81 | $place = $this->parts->first(); |
||
| 82 | $place = mb_substr($place, 0, 120); |
||
| 83 | |||
| 84 | if ($parent_id === null) { |
||
| 85 | $location_id = DB::table('place_location') |
||
|
0 ignored issues
–
show
|
|||
| 86 | ->where('place', '=', $place) |
||
| 87 | ->whereNull('parent_id') |
||
| 88 | ->value('id'); |
||
| 89 | } else { |
||
| 90 | $location_id = DB::table('place_location') |
||
| 91 | ->where('place', '=', $place) |
||
| 92 | ->where('parent_id', '=', $parent_id) |
||
| 93 | ->value('id'); |
||
| 94 | } |
||
| 95 | |||
| 96 | $location_id ??= DB::table('place_location')->insertGetId([ |
||
| 97 | 'parent_id' => $parent_id, |
||
| 98 | 'place' => $place, |
||
| 99 | ]); |
||
| 100 | |||
| 101 | return (int) $location_id; |
||
| 102 | }); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Does this location exist in the database? Note that calls to PlaceLocation::id() will |
||
| 107 | * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function exists(): bool |
||
| 112 | { |
||
| 113 | $parent_id = null; |
||
| 114 | |||
| 115 | foreach ($this->parts->reverse() as $place) { |
||
| 116 | if ($parent_id === null) { |
||
| 117 | $parent_id = DB::table('place_location') |
||
| 118 | ->whereNull('parent_id') |
||
| 119 | ->where('place', '=', mb_substr($place, 0, 120)) |
||
| 120 | ->value('id'); |
||
| 121 | } else { |
||
| 122 | $parent_id = DB::table('place_location') |
||
| 123 | ->where('parent_id', '=', $parent_id) |
||
| 124 | ->where('place', '=', mb_substr($place, 0, 120)) |
||
| 125 | ->value('id'); |
||
| 126 | } |
||
| 127 | |||
| 128 | if ($parent_id === null) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return object |
||
| 138 | */ |
||
| 139 | private function details(): object |
||
| 140 | { |
||
| 141 | return Registry::cache()->array()->remember('location-details-' . $this->id(), function () { |
||
| 142 | // The "top-level" location won't exist in the database. |
||
| 143 | if ($this->parts->isEmpty()) { |
||
| 144 | return (object) [ |
||
| 145 | 'latitude' => null, |
||
| 146 | 'longitude' => null, |
||
| 147 | ]; |
||
| 148 | } |
||
| 149 | |||
| 150 | $row = DB::table('place_location') |
||
| 151 | ->where('id', '=', $this->id()) |
||
| 152 | ->select(['latitude', 'longitude']) |
||
| 153 | ->first(); |
||
| 154 | |||
| 155 | if ($row->latitude !== null) { |
||
| 156 | $row->latitude = (float) $row->latitude; |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($row->longitude !== null) { |
||
| 160 | $row->longitude = (float) $row->longitude; |
||
| 161 | } |
||
| 162 | |||
| 163 | return $row; |
||
| 164 | }); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Latitude of the location. |
||
| 169 | */ |
||
| 170 | public function latitude(): float|null |
||
| 171 | { |
||
| 172 | return $this->details()->latitude; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Longitude of the location. |
||
| 177 | */ |
||
| 178 | public function longitude(): float|null |
||
| 179 | { |
||
| 180 | return $this->details()->longitude; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function locationName(): string |
||
| 187 | { |
||
| 188 | return (string) $this->parts->first(); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Find a rectangle that (approximately) encloses this place. |
||
| 193 | * |
||
| 194 | * @return array<array<float>> |
||
| 195 | */ |
||
| 196 | public function boundingRectangle(): array |
||
| 197 | { |
||
| 198 | if ($this->id() === null) { |
||
| 199 | return [[-180.0, -90.0], [180.0, 90.0]]; |
||
| 200 | } |
||
| 201 | |||
| 202 | // Find our own co-ordinates and those of any child places |
||
| 203 | $latitudes = DB::table('place_location') |
||
| 204 | ->whereNotNull('latitude') |
||
| 205 | ->where(function (Builder $query): void { |
||
| 206 | $query |
||
| 207 | ->where('parent_id', '=', $this->id()) |
||
| 208 | ->orWhere('id', '=', $this->id()); |
||
| 209 | }) |
||
| 210 | ->groupBy(['latitude']) |
||
| 211 | ->pluck('latitude') |
||
| 212 | ->map(static fn (string $x): float => (float) $x); |
||
| 213 | |||
| 214 | $longitudes = DB::table('place_location') |
||
| 215 | ->whereNotNull('longitude') |
||
| 216 | ->where(function (Builder $query): void { |
||
| 217 | $query |
||
| 218 | ->where('parent_id', '=', $this->id()) |
||
| 219 | ->orWhere('id', '=', $this->id()); |
||
| 220 | }) |
||
| 221 | ->groupBy(['longitude']) |
||
| 222 | ->pluck('longitude') |
||
| 223 | ->map(static fn (string $x): float => (float) $x); |
||
| 224 | |||
| 225 | // No co-ordinates? Use the parent place instead. |
||
| 226 | if ($latitudes->isEmpty() || $longitudes->isEmpty()) { |
||
| 227 | return $this->parent()->boundingRectangle(); |
||
| 228 | } |
||
| 229 | |||
| 230 | // Many co-ordinates? Generate a bounding rectangle that includes them. |
||
| 231 | if ($latitudes->count() > 1 || $longitudes->count() > 1) { |
||
| 232 | return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]]; |
||
| 233 | } |
||
| 234 | |||
| 235 | // Just one co-ordinate? Draw a box around it. |
||
| 236 | switch ($this->parts->count()) { |
||
| 237 | case 1: |
||
| 238 | // Countries |
||
| 239 | $delta = 5.0; |
||
| 240 | break; |
||
| 241 | case 2: |
||
| 242 | // Regions |
||
| 243 | $delta = 1.0; |
||
| 244 | break; |
||
| 245 | default: |
||
| 246 | // Cities and districts |
||
| 247 | $delta = 0.2; |
||
| 248 | break; |
||
| 249 | } |
||
| 250 | |||
| 251 | return [[ |
||
| 252 | max($latitudes->min() - $delta, -90.0), |
||
| 253 | max($longitudes->min() - $delta, -180.0), |
||
| 254 | ], [ |
||
| 255 | min($latitudes->max() + $delta, 90.0), |
||
| 256 | min($longitudes->max() + $delta, 180.0), |
||
| 257 | ]]; |
||
| 258 | } |
||
| 259 | } |
||
| 260 |