xref: /webtrees/app/PlaceLocation.php (revision 1ff45046fabc22237b5d0d8e489c96f031fc598d)
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     */
74*1ff45046SGreg Roach    public function id(): int|null
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     */
173*1ff45046SGreg Roach    public function latitude(): float|null
1745333da53SGreg Roach    {
17590949315SGreg Roach        return $this->details()->latitude;
1765333da53SGreg Roach    }
1775333da53SGreg Roach
1785333da53SGreg Roach    /**
1790874af26SRichard Cissée     * Longitude of the location.
1805333da53SGreg Roach     */
181*1ff45046SGreg Roach    public function longitude(): float|null
1825333da53SGreg Roach    {
18390949315SGreg Roach        return $this->details()->longitude;
1845333da53SGreg Roach    }
1855333da53SGreg Roach
1865333da53SGreg Roach    /**
1875333da53SGreg Roach     * @return string
1885333da53SGreg Roach     */
1895333da53SGreg Roach    public function locationName(): string
1905333da53SGreg Roach    {
1915333da53SGreg Roach        return (string) $this->parts->first();
1925333da53SGreg Roach    }
19379de923dSGreg Roach
19479de923dSGreg Roach    /**
19579de923dSGreg Roach     * Find a rectangle that (approximately) encloses this place.
19679de923dSGreg Roach     *
19779de923dSGreg Roach     * @return array<array<float>>
19879de923dSGreg Roach     */
19979de923dSGreg Roach    public function boundingRectangle(): array
20079de923dSGreg Roach    {
20151f91c3cSGreg Roach        if ($this->id() === null) {
2029a9dfcf7SGreg Roach            return [[-180.0, -90.0], [180.0, 90.0]];
20379de923dSGreg Roach        }
20479de923dSGreg Roach
2059a9dfcf7SGreg Roach        // Find our own co-ordinates and those of any child places
20690949315SGreg Roach        $latitudes = DB::table('place_location')
20790949315SGreg Roach            ->whereNotNull('latitude')
20890949315SGreg Roach            ->where(function (Builder $query): void {
20990949315SGreg Roach                $query
21090949315SGreg Roach                    ->where('parent_id', '=', $this->id())
21190949315SGreg Roach                    ->orWhere('id', '=', $this->id());
21290949315SGreg Roach            })
21390949315SGreg Roach            ->groupBy(['latitude'])
21490949315SGreg Roach            ->pluck('latitude')
215f25fc0f9SGreg Roach            ->map(static fn(string $x): float => (float) $x);
2169a9dfcf7SGreg Roach
21790949315SGreg Roach        $longitudes = DB::table('place_location')
21890949315SGreg Roach            ->whereNotNull('longitude')
21990949315SGreg Roach            ->where(function (Builder $query): void {
22090949315SGreg Roach                $query
22190949315SGreg Roach                    ->where('parent_id', '=', $this->id())
22290949315SGreg Roach                    ->orWhere('id', '=', $this->id());
22390949315SGreg Roach            })
22490949315SGreg Roach            ->groupBy(['longitude'])
22590949315SGreg Roach            ->pluck('longitude')
226f25fc0f9SGreg Roach            ->map(static fn(string $x): float => (float) $x);
2279a9dfcf7SGreg Roach
22814cb4388SGreg Roach        // No co-ordinates?  Use the parent place instead.
22990949315SGreg Roach        if ($latitudes->isEmpty() || $longitudes->isEmpty()) {
23014cb4388SGreg Roach            return $this->parent()->boundingRectangle();
23114cb4388SGreg Roach        }
23214cb4388SGreg Roach
23314cb4388SGreg Roach        // Many co-ordinates?  Generate a bounding rectangle that includes them.
2349a9dfcf7SGreg Roach        if ($latitudes->count() > 1 || $longitudes->count() > 1) {
2359a9dfcf7SGreg Roach            return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]];
23679de923dSGreg Roach        }
23779de923dSGreg Roach
23814cb4388SGreg Roach        // Just one co-ordinate?  Draw a box around it.
23914cb4388SGreg Roach        switch ($this->parts->count()) {
24014cb4388SGreg Roach            case 1:
24114cb4388SGreg Roach                // Countries
24214cb4388SGreg Roach                $delta = 5.0;
24314cb4388SGreg Roach                break;
24414cb4388SGreg Roach            case 2:
24514cb4388SGreg Roach                // Regions
24614cb4388SGreg Roach                $delta = 1.0;
24714cb4388SGreg Roach                break;
24814cb4388SGreg Roach            default:
24914cb4388SGreg Roach                // Cities and districts
25014cb4388SGreg Roach                $delta = 0.2;
25114cb4388SGreg Roach                break;
25214cb4388SGreg Roach        }
25314cb4388SGreg Roach
25414cb4388SGreg Roach        return [[
25514cb4388SGreg Roach            max($latitudes->min() - $delta, -90.0),
25614cb4388SGreg Roach            max($longitudes->min() - $delta, -180.0),
25714cb4388SGreg Roach        ], [
25814cb4388SGreg Roach            min($latitudes->max() + $delta, 90.0),
25914cb4388SGreg Roach            min($longitudes->max() + $delta, 180.0),
26014cb4388SGreg Roach        ]];
26179de923dSGreg Roach    }
2625333da53SGreg Roach}
263