15333da53SGreg Roach<?php 25333da53SGreg Roach 35333da53SGreg Roach/** 45333da53SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 65333da53SGreg Roach * This program is free software: you can redistribute it and/or modify 75333da53SGreg Roach * it under the terms of the GNU General Public License as published by 85333da53SGreg Roach * the Free Software Foundation, either version 3 of the License, or 95333da53SGreg Roach * (at your option) any later version. 105333da53SGreg Roach * This program is distributed in the hope that it will be useful, 115333da53SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 125333da53SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 135333da53SGreg Roach * GNU General Public License for more details. 145333da53SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 165333da53SGreg Roach */ 175333da53SGreg Roach 185333da53SGreg Roachdeclare(strict_types=1); 195333da53SGreg Roach 205333da53SGreg Roachnamespace Fisharebest\Webtrees; 215333da53SGreg Roach 2290949315SGreg Roachuse Illuminate\Database\Query\Builder; 235333da53SGreg Roachuse Illuminate\Support\Collection; 245333da53SGreg Roach 2590949315SGreg Roachuse function max; 2690949315SGreg Roachuse function min; 2779de923dSGreg Roachuse function preg_split; 2890949315SGreg Roachuse function trim; 2990949315SGreg Roach 3090949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY; 3179de923dSGreg Roach 325333da53SGreg Roach/** 335333da53SGreg Roach * Class PlaceLocation 345333da53SGreg Roach */ 355333da53SGreg Roachclass PlaceLocation 365333da53SGreg Roach{ 3743f2f523SGreg Roach // e.g. "Westminster, London, England" 3843f2f523SGreg Roach private string $location_name; 395333da53SGreg Roach 4036779af1SGreg Roach /** @var Collection<int,string> The parts of a location name, e.g. ["Westminster", "London", "England"] */ 4143f2f523SGreg Roach private Collection $parts; 425333da53SGreg Roach 435333da53SGreg Roach /** 445333da53SGreg Roach * Create a place-location. 455333da53SGreg Roach * 465333da53SGreg Roach * @param string $location_name 475333da53SGreg Roach */ 485333da53SGreg Roach public function __construct(string $location_name) 495333da53SGreg Roach { 505333da53SGreg Roach // Ignore any empty parts in location names such as "Village, , , Country". 5190949315SGreg Roach $location_name = trim($location_name); 5290949315SGreg Roach $this->parts = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name, -1, PREG_SPLIT_NO_EMPTY)); 535333da53SGreg Roach 545333da53SGreg Roach // Rebuild the location name in the correct format. 555333da53SGreg Roach $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 565333da53SGreg Roach } 575333da53SGreg Roach 585333da53SGreg Roach /** 595333da53SGreg Roach * Get the higher level location. 605333da53SGreg Roach * 615333da53SGreg Roach * @return PlaceLocation 625333da53SGreg Roach */ 635333da53SGreg Roach public function parent(): PlaceLocation 645333da53SGreg Roach { 655333da53SGreg Roach return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 665333da53SGreg Roach } 675333da53SGreg Roach 685333da53SGreg Roach /** 6990949315SGreg Roach * The database row id that contains this location. 705333da53SGreg Roach * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 715333da53SGreg Roach * 7290949315SGreg Roach * @return int|null 735333da53SGreg Roach */ 7490949315SGreg Roach public function id(): ?int 755333da53SGreg Roach { 765333da53SGreg Roach // The "top-level" location won't exist in the database. 775333da53SGreg Roach if ($this->parts->isEmpty()) { 7890949315SGreg Roach return null; 795333da53SGreg Roach } 805333da53SGreg Roach 8190949315SGreg Roach return Registry::cache()->array()->remember('location-' . $this->location_name, function () { 8290949315SGreg Roach $parent_id = $this->parent()->id(); 835333da53SGreg Roach 8490949315SGreg Roach $place = $this->parts->first(); 8590949315SGreg Roach $place = mb_substr($place, 0, 120); 865333da53SGreg Roach 8790949315SGreg Roach if ($parent_id === null) { 8890949315SGreg Roach $location_id = DB::table('place_location') 8990949315SGreg Roach ->where('place', '=', $place) 9090949315SGreg Roach ->whereNull('parent_id') 9190949315SGreg Roach ->value('id'); 9290949315SGreg Roach } else { 9390949315SGreg Roach $location_id = DB::table('place_location') 9490949315SGreg Roach ->where('place', '=', $place) 9590949315SGreg Roach ->where('parent_id', '=', $parent_id) 9690949315SGreg Roach ->value('id'); 9790949315SGreg Roach } 985333da53SGreg Roach 993529c469SGreg Roach $location_id ??= DB::table('place_location')->insertGetId([ 10090949315SGreg Roach 'parent_id' => $parent_id, 10190949315SGreg Roach 'place' => $place, 1025333da53SGreg Roach ]); 1035333da53SGreg Roach 10490949315SGreg Roach return (int) $location_id; 1055333da53SGreg Roach }); 1065333da53SGreg Roach } 1075333da53SGreg Roach 1085333da53SGreg Roach /** 1095333da53SGreg Roach * Does this location exist in the database? Note that calls to PlaceLocation::id() will 1105333da53SGreg Roach * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). 1115333da53SGreg Roach * 1125333da53SGreg Roach * @return bool 1135333da53SGreg Roach */ 1145333da53SGreg Roach public function exists(): bool 1155333da53SGreg Roach { 11690949315SGreg Roach $parent_id = null; 1175333da53SGreg Roach 11890949315SGreg Roach foreach ($this->parts->reverse() as $place) { 11990949315SGreg Roach if ($parent_id === null) { 12090949315SGreg Roach $parent_id = DB::table('place_location') 12190949315SGreg Roach ->whereNull('parent_id') 12290949315SGreg Roach ->where('place', '=', mb_substr($place, 0, 120)) 12390949315SGreg Roach ->value('id'); 12490949315SGreg Roach } else { 12590949315SGreg Roach $parent_id = DB::table('place_location') 12690949315SGreg Roach ->where('parent_id', '=', $parent_id) 12790949315SGreg Roach ->where('place', '=', mb_substr($place, 0, 120)) 12890949315SGreg Roach ->value('id'); 1295333da53SGreg Roach } 1305333da53SGreg Roach 13190949315SGreg Roach if ($parent_id === null) { 13290949315SGreg Roach return false; 13390949315SGreg Roach } 13490949315SGreg Roach } 13590949315SGreg Roach 13690949315SGreg Roach return true; 1375333da53SGreg Roach } 1385333da53SGreg Roach 1395333da53SGreg Roach /** 140ac701fbdSGreg Roach * @return object 1415333da53SGreg Roach */ 142ac701fbdSGreg Roach private function details(): object 1435333da53SGreg Roach { 1446b9cb339SGreg Roach return Registry::cache()->array()->remember('location-details-' . $this->id(), function () { 1455333da53SGreg Roach // The "top-level" location won't exist in the database. 1465333da53SGreg Roach if ($this->parts->isEmpty()) { 1475333da53SGreg Roach return (object) [ 14890949315SGreg Roach 'latitude' => null, 14990949315SGreg Roach 'longitude' => null, 1505333da53SGreg Roach ]; 1515333da53SGreg Roach } 1525333da53SGreg Roach 15390949315SGreg Roach $row = DB::table('place_location') 15490949315SGreg Roach ->where('id', '=', $this->id()) 15590949315SGreg Roach ->select(['latitude', 'longitude']) 1565333da53SGreg Roach ->first(); 15790949315SGreg Roach 15890949315SGreg Roach if ($row->latitude !== null) { 15990949315SGreg Roach $row->latitude = (float) $row->latitude; 16090949315SGreg Roach } 16190949315SGreg Roach 16290949315SGreg Roach if ($row->longitude !== null) { 16390949315SGreg Roach $row->longitude = (float) $row->longitude; 16490949315SGreg Roach } 16590949315SGreg Roach 16690949315SGreg Roach return $row; 1675333da53SGreg Roach }); 1685333da53SGreg Roach } 1695333da53SGreg Roach 1705333da53SGreg Roach /** 1715333da53SGreg Roach * Latitude of the location. 1725333da53SGreg Roach * 17390949315SGreg Roach * @return float|null 1745333da53SGreg Roach */ 17590949315SGreg Roach public function latitude(): ?float 1765333da53SGreg Roach { 17790949315SGreg Roach return $this->details()->latitude; 1785333da53SGreg Roach } 1795333da53SGreg Roach 1805333da53SGreg Roach /** 1810874af26SRichard Cissée * Longitude of the location. 1825333da53SGreg Roach * 18390949315SGreg Roach * @return float|null 1845333da53SGreg Roach */ 18590949315SGreg Roach public function longitude(): ?float 1865333da53SGreg Roach { 18790949315SGreg Roach return $this->details()->longitude; 1885333da53SGreg Roach } 1895333da53SGreg Roach 1905333da53SGreg Roach /** 1915333da53SGreg Roach * @return string 1925333da53SGreg Roach */ 1935333da53SGreg Roach public function locationName(): string 1945333da53SGreg Roach { 1955333da53SGreg Roach return (string) $this->parts->first(); 1965333da53SGreg Roach } 19779de923dSGreg Roach 19879de923dSGreg Roach /** 19979de923dSGreg Roach * Find a rectangle that (approximately) encloses this place. 20079de923dSGreg Roach * 20179de923dSGreg Roach * @return array<array<float>> 20279de923dSGreg Roach */ 20379de923dSGreg Roach public function boundingRectangle(): array 20479de923dSGreg Roach { 20551f91c3cSGreg Roach if ($this->id() === null) { 2069a9dfcf7SGreg Roach return [[-180.0, -90.0], [180.0, 90.0]]; 20779de923dSGreg Roach } 20879de923dSGreg Roach 2099a9dfcf7SGreg Roach // Find our own co-ordinates and those of any child places 21090949315SGreg Roach $latitudes = DB::table('place_location') 21190949315SGreg Roach ->whereNotNull('latitude') 21290949315SGreg Roach ->where(function (Builder $query): void { 21390949315SGreg Roach $query 21490949315SGreg Roach ->where('parent_id', '=', $this->id()) 21590949315SGreg Roach ->orWhere('id', '=', $this->id()); 21690949315SGreg Roach }) 21790949315SGreg Roach ->groupBy(['latitude']) 21890949315SGreg Roach ->pluck('latitude') 219*f25fc0f9SGreg Roach ->map(static fn(string $x): float => (float) $x); 2209a9dfcf7SGreg Roach 22190949315SGreg Roach $longitudes = DB::table('place_location') 22290949315SGreg Roach ->whereNotNull('longitude') 22390949315SGreg Roach ->where(function (Builder $query): void { 22490949315SGreg Roach $query 22590949315SGreg Roach ->where('parent_id', '=', $this->id()) 22690949315SGreg Roach ->orWhere('id', '=', $this->id()); 22790949315SGreg Roach }) 22890949315SGreg Roach ->groupBy(['longitude']) 22990949315SGreg Roach ->pluck('longitude') 230*f25fc0f9SGreg Roach ->map(static fn(string $x): float => (float) $x); 2319a9dfcf7SGreg Roach 23214cb4388SGreg Roach // No co-ordinates? Use the parent place instead. 23390949315SGreg Roach if ($latitudes->isEmpty() || $longitudes->isEmpty()) { 23414cb4388SGreg Roach return $this->parent()->boundingRectangle(); 23514cb4388SGreg Roach } 23614cb4388SGreg Roach 23714cb4388SGreg Roach // Many co-ordinates? Generate a bounding rectangle that includes them. 2389a9dfcf7SGreg Roach if ($latitudes->count() > 1 || $longitudes->count() > 1) { 2399a9dfcf7SGreg Roach return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]]; 24079de923dSGreg Roach } 24179de923dSGreg Roach 24214cb4388SGreg Roach // Just one co-ordinate? Draw a box around it. 24314cb4388SGreg Roach switch ($this->parts->count()) { 24414cb4388SGreg Roach case 1: 24514cb4388SGreg Roach // Countries 24614cb4388SGreg Roach $delta = 5.0; 24714cb4388SGreg Roach break; 24814cb4388SGreg Roach case 2: 24914cb4388SGreg Roach // Regions 25014cb4388SGreg Roach $delta = 1.0; 25114cb4388SGreg Roach break; 25214cb4388SGreg Roach default: 25314cb4388SGreg Roach // Cities and districts 25414cb4388SGreg Roach $delta = 0.2; 25514cb4388SGreg Roach break; 25614cb4388SGreg Roach } 25714cb4388SGreg Roach 25814cb4388SGreg Roach return [[ 25914cb4388SGreg Roach max($latitudes->min() - $delta, -90.0), 26014cb4388SGreg Roach max($longitudes->min() - $delta, -180.0), 26114cb4388SGreg Roach ], [ 26214cb4388SGreg Roach min($latitudes->max() + $delta, 90.0), 26314cb4388SGreg Roach min($longitudes->max() + $delta, 180.0), 26414cb4388SGreg Roach ]]; 26579de923dSGreg Roach } 2665333da53SGreg Roach} 267