15333da53SGreg Roach<?php 25333da53SGreg Roach 35333da53SGreg Roach/** 45333da53SGreg Roach * webtrees: online genealogy 55333da53SGreg Roach * Copyright (C) 2020 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 155333da53SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 165333da53SGreg Roach */ 175333da53SGreg Roach 185333da53SGreg Roachdeclare(strict_types=1); 195333da53SGreg Roach 205333da53SGreg Roachnamespace Fisharebest\Webtrees; 215333da53SGreg Roach 225333da53SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService; 235333da53SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 245333da53SGreg Roachuse Illuminate\Support\Collection; 255333da53SGreg Roachuse stdClass; 265333da53SGreg Roach 2779de923dSGreg Roachuse function preg_split; 2879de923dSGreg Roach 295333da53SGreg Roach/** 305333da53SGreg Roach * Class PlaceLocation 315333da53SGreg Roach */ 325333da53SGreg Roachclass PlaceLocation 335333da53SGreg Roach{ 345333da53SGreg Roach /** @var string e.g. "Westminster, London, England" */ 355333da53SGreg Roach private $location_name; 365333da53SGreg Roach 3794e35917SGreg Roach /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */ 385333da53SGreg Roach private $parts; 395333da53SGreg Roach 405333da53SGreg Roach /** 415333da53SGreg Roach * Create a place-location. 425333da53SGreg Roach * 435333da53SGreg Roach * @param string $location_name 445333da53SGreg Roach */ 455333da53SGreg Roach public function __construct(string $location_name) 465333da53SGreg Roach { 475333da53SGreg Roach // Ignore any empty parts in location names such as "Village, , , Country". 485333da53SGreg Roach $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name))) 495333da53SGreg Roach ->filter(); 505333da53SGreg Roach 515333da53SGreg Roach // Rebuild the location name in the correct format. 525333da53SGreg Roach $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR); 535333da53SGreg Roach } 545333da53SGreg Roach 555333da53SGreg Roach /** 565333da53SGreg Roach * Get the higher level location. 575333da53SGreg Roach * 585333da53SGreg Roach * @return PlaceLocation 595333da53SGreg Roach */ 605333da53SGreg Roach public function parent(): PlaceLocation 615333da53SGreg Roach { 625333da53SGreg Roach return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR)); 635333da53SGreg Roach } 645333da53SGreg Roach 655333da53SGreg Roach /** 665333da53SGreg Roach * The database row that contains this location. 675333da53SGreg Roach * Note that due to database collation, both "Quebec" and "Québec" will share the same row. 685333da53SGreg Roach * 695333da53SGreg Roach * @return int 705333da53SGreg Roach */ 715333da53SGreg Roach public function id(): int 725333da53SGreg Roach { 73*6b9cb339SGreg Roach return Registry::cache()->array()->remember('location-' . $this->location_name, function () { 745333da53SGreg Roach // The "top-level" location won't exist in the database. 755333da53SGreg Roach if ($this->parts->isEmpty()) { 765333da53SGreg Roach return 0; 775333da53SGreg Roach } 785333da53SGreg Roach 795333da53SGreg Roach $parent_location_id = $this->parent()->id(); 805333da53SGreg Roach 815333da53SGreg Roach $location_id = (int) DB::table('placelocation') 82c4ab7783SGreg Roach ->where('pl_place', '=', mb_substr($this->parts->first(), 0, 120)) 835333da53SGreg Roach ->where('pl_parent_id', '=', $parent_location_id) 845333da53SGreg Roach ->value('pl_id'); 855333da53SGreg Roach 865333da53SGreg Roach if ($location_id === 0) { 875333da53SGreg Roach $location = $this->parts->first(); 885333da53SGreg Roach 895333da53SGreg Roach $location_id = 1 + (int) DB::table('placelocation')->max('pl_id'); 905333da53SGreg Roach 915333da53SGreg Roach DB::table('placelocation')->insert([ 925333da53SGreg Roach 'pl_id' => $location_id, 93c4ab7783SGreg Roach 'pl_place' => mb_substr($location, 0, 120), 945333da53SGreg Roach 'pl_parent_id' => $parent_location_id, 955333da53SGreg Roach 'pl_level' => $this->parts->count() - 1, 965333da53SGreg Roach 'pl_lati' => '', 975333da53SGreg Roach 'pl_long' => '', 985333da53SGreg Roach 'pl_icon' => '', 995333da53SGreg Roach 'pl_zoom' => 2, 1005333da53SGreg Roach ]); 1015333da53SGreg Roach } 1025333da53SGreg Roach 1035333da53SGreg Roach return $location_id; 1045333da53SGreg Roach }); 1055333da53SGreg Roach } 1065333da53SGreg Roach 1075333da53SGreg Roach /** 1085333da53SGreg Roach * Does this location exist in the database? Note that calls to PlaceLocation::id() will 1095333da53SGreg Roach * create the row, so this function is only meaningful when called before a call to PlaceLocation::id(). 1105333da53SGreg Roach * 1115333da53SGreg Roach * @return bool 1125333da53SGreg Roach */ 1135333da53SGreg Roach public function exists(): bool 1145333da53SGreg Roach { 1155333da53SGreg Roach $location_id = 0; 1165333da53SGreg Roach 1175333da53SGreg Roach $this->parts->reverse()->each(static function (string $place) use (&$location_id) { 1185333da53SGreg Roach if ($location_id !== null) { 1195333da53SGreg Roach $location_id = DB::table('placelocation') 1205333da53SGreg Roach ->where('pl_parent_id', '=', $location_id) 121c4ab7783SGreg Roach ->where('pl_place', '=', mb_substr($place, 0, 120)) 1225333da53SGreg Roach ->value('pl_id'); 1235333da53SGreg Roach } 1245333da53SGreg Roach }); 1255333da53SGreg Roach 1265333da53SGreg Roach return $location_id !== null; 1275333da53SGreg Roach } 1285333da53SGreg Roach 1295333da53SGreg Roach /** 1305333da53SGreg Roach * @return stdClass 1315333da53SGreg Roach */ 1325333da53SGreg Roach private function details(): stdClass 1335333da53SGreg Roach { 134*6b9cb339SGreg Roach return Registry::cache()->array()->remember('location-details-' . $this->id(), function () { 1355333da53SGreg Roach // The "top-level" location won't exist in the database. 1365333da53SGreg Roach if ($this->parts->isEmpty()) { 1375333da53SGreg Roach return (object) [ 1385333da53SGreg Roach 'pl_id' => '0', 1395333da53SGreg Roach 'pl_parent_id' => '0', 1405333da53SGreg Roach 'pl_level' => null, 1415333da53SGreg Roach 'pl_place' => '', 1425333da53SGreg Roach 'pl_lati' => null, 1435333da53SGreg Roach 'pl_long' => null, 1445333da53SGreg Roach 'pl_zoom' => null, 1455333da53SGreg Roach 'pl_icon' => null, 1465333da53SGreg Roach 'pl_media' => null, 1475333da53SGreg Roach ]; 1485333da53SGreg Roach } 1495333da53SGreg Roach 1505333da53SGreg Roach return DB::table('placelocation') 1515333da53SGreg Roach ->where('pl_id', '=', $this->id()) 1525333da53SGreg Roach ->first(); 1535333da53SGreg Roach }); 1545333da53SGreg Roach } 1555333da53SGreg Roach 1565333da53SGreg Roach /** 1575333da53SGreg Roach * Latitude of the location. 1585333da53SGreg Roach * 1595333da53SGreg Roach * @return float 1605333da53SGreg Roach */ 1615333da53SGreg Roach public function latitude(): float 1625333da53SGreg Roach { 1635333da53SGreg Roach $gedcom_service = new GedcomService(); 16414cb4388SGreg Roach $pl_lati = (string) $this->details()->pl_lati; 165e340fe65SGreg Roach 166e340fe65SGreg Roach return $gedcom_service->readLatitude($pl_lati); 1675333da53SGreg Roach } 1685333da53SGreg Roach 1695333da53SGreg Roach /** 1700874af26SRichard Cissée * Longitude of the location. 1715333da53SGreg Roach * 1725333da53SGreg Roach * @return float 1735333da53SGreg Roach */ 1745333da53SGreg Roach public function longitude(): float 1755333da53SGreg Roach { 1765333da53SGreg Roach $gedcom_service = new GedcomService(); 17714cb4388SGreg Roach $pl_long = (string) $this->details()->pl_long; 178e340fe65SGreg Roach 179e340fe65SGreg Roach return $gedcom_service->readLongitude($pl_long); 1805333da53SGreg Roach } 1815333da53SGreg Roach 1825333da53SGreg Roach /** 1835333da53SGreg Roach * The icon for the location. 1845333da53SGreg Roach * 1855333da53SGreg Roach * @return string 1865333da53SGreg Roach */ 1875333da53SGreg Roach public function icon(): string 1885333da53SGreg Roach { 1895333da53SGreg Roach return (string) $this->details()->pl_icon; 1905333da53SGreg Roach } 1915333da53SGreg Roach 1925333da53SGreg Roach /** 1935333da53SGreg Roach * Zoom level for the location. 1945333da53SGreg Roach * 1955333da53SGreg Roach * @return int 1965333da53SGreg Roach */ 1975333da53SGreg Roach public function zoom(): int 1985333da53SGreg Roach { 199e340fe65SGreg Roach return (int) $this->details()->pl_zoom ?: 2; 2005333da53SGreg Roach } 2015333da53SGreg Roach 2025333da53SGreg Roach /** 2035333da53SGreg Roach * @return string 2045333da53SGreg Roach */ 2055333da53SGreg Roach public function locationName(): string 2065333da53SGreg Roach { 2075333da53SGreg Roach return (string) $this->parts->first(); 2085333da53SGreg Roach } 20979de923dSGreg Roach 21079de923dSGreg Roach /** 21179de923dSGreg Roach * Find a rectangle that (approximately) encloses this place. 21279de923dSGreg Roach * 21379de923dSGreg Roach * @return array<array<float>> 21479de923dSGreg Roach */ 21579de923dSGreg Roach public function boundingRectangle(): array 21679de923dSGreg Roach { 2179a9dfcf7SGreg Roach if ($this->id() === 0) { 2189a9dfcf7SGreg Roach return [[-180.0, -90.0], [180.0, 90.0]]; 21979de923dSGreg Roach } 22079de923dSGreg Roach 2219a9dfcf7SGreg Roach // Find our own co-ordinates and those of any child places 2229a9dfcf7SGreg Roach $latitudes = DB::table('placelocation') 2239a9dfcf7SGreg Roach ->where('pl_parent_id', '=', $this->id()) 2249a9dfcf7SGreg Roach ->orWhere('pl_id', '=', $this->id()) 22514cb4388SGreg Roach ->groupBy(['pl_lati']) 2269a9dfcf7SGreg Roach ->pluck('pl_lati') 2279a9dfcf7SGreg Roach ->filter() 2289a9dfcf7SGreg Roach ->map(static function (string $x): float { 2299a9dfcf7SGreg Roach return (new GedcomService())->readLatitude($x); 2309a9dfcf7SGreg Roach }); 2319a9dfcf7SGreg Roach 2329a9dfcf7SGreg Roach $longitudes = DB::table('placelocation') 2339a9dfcf7SGreg Roach ->where('pl_parent_id', '=', $this->id()) 2349a9dfcf7SGreg Roach ->orWhere('pl_id', '=', $this->id()) 23514cb4388SGreg Roach ->groupBy(['pl_long']) 2369a9dfcf7SGreg Roach ->pluck('pl_long') 2379a9dfcf7SGreg Roach ->filter() 2389a9dfcf7SGreg Roach ->map(static function (string $x): float { 2399a9dfcf7SGreg Roach return (new GedcomService())->readLongitude($x); 2409a9dfcf7SGreg Roach }); 2419a9dfcf7SGreg Roach 24214cb4388SGreg Roach // No co-ordinates? Use the parent place instead. 24314cb4388SGreg Roach if ($latitudes->isEmpty() && $longitudes->isEmpty()) { 24414cb4388SGreg Roach return $this->parent()->boundingRectangle(); 24514cb4388SGreg Roach } 24614cb4388SGreg Roach 24714cb4388SGreg Roach // Many co-ordinates? Generate a bounding rectangle that includes them. 2489a9dfcf7SGreg Roach if ($latitudes->count() > 1 || $longitudes->count() > 1) { 2499a9dfcf7SGreg Roach return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]]; 25079de923dSGreg Roach } 25179de923dSGreg Roach 25214cb4388SGreg Roach // Just one co-ordinate? Draw a box around it. 25314cb4388SGreg Roach switch ($this->parts->count()) { 25414cb4388SGreg Roach case 1: 25514cb4388SGreg Roach // Countries 25614cb4388SGreg Roach $delta = 5.0; 25714cb4388SGreg Roach break; 25814cb4388SGreg Roach case 2: 25914cb4388SGreg Roach // Regions 26014cb4388SGreg Roach $delta = 1.0; 26114cb4388SGreg Roach break; 26214cb4388SGreg Roach default: 26314cb4388SGreg Roach // Cities and districts 26414cb4388SGreg Roach $delta = 0.2; 26514cb4388SGreg Roach break; 26614cb4388SGreg Roach } 26714cb4388SGreg Roach 26814cb4388SGreg Roach return [[ 26914cb4388SGreg Roach max($latitudes->min() - $delta, -90.0), 27014cb4388SGreg Roach max($longitudes->min() - $delta, -180.0), 27114cb4388SGreg Roach ], [ 27214cb4388SGreg Roach min($latitudes->max() + $delta, 90.0), 27314cb4388SGreg Roach min($longitudes->max() + $delta, 180.0), 27414cb4388SGreg Roach ]]; 27579de923dSGreg Roach } 2765333da53SGreg Roach} 277