xref: /webtrees/app/PlaceLocation.php (revision ac701fbd36864ec7630310142b9b16f78934a505)
15333da53SGreg Roach<?php
25333da53SGreg Roach
35333da53SGreg Roach/**
45333da53SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 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
225333da53SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2390949315SGreg Roachuse Illuminate\Database\Query\Builder;
245333da53SGreg Roachuse Illuminate\Support\Collection;
255333da53SGreg Roach
2690949315SGreg Roachuse function max;
2790949315SGreg Roachuse function min;
2879de923dSGreg Roachuse function preg_split;
2990949315SGreg Roachuse function trim;
3090949315SGreg Roach
3190949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3279de923dSGreg Roach
335333da53SGreg Roach/**
345333da53SGreg Roach * Class PlaceLocation
355333da53SGreg Roach */
365333da53SGreg Roachclass PlaceLocation
375333da53SGreg Roach{
385333da53SGreg Roach    /** @var string e.g. "Westminster, London, England" */
395333da53SGreg Roach    private $location_name;
405333da53SGreg Roach
4194e35917SGreg Roach    /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */
425333da53SGreg Roach    private $parts;
435333da53SGreg Roach
445333da53SGreg Roach    /**
455333da53SGreg Roach     * Create a place-location.
465333da53SGreg Roach     *
475333da53SGreg Roach     * @param string $location_name
485333da53SGreg Roach     */
495333da53SGreg Roach    public function __construct(string $location_name)
505333da53SGreg Roach    {
515333da53SGreg Roach        // Ignore any empty parts in location names such as "Village, , , Country".
5290949315SGreg Roach        $location_name = trim($location_name);
5390949315SGreg Roach        $this->parts   = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name, -1, PREG_SPLIT_NO_EMPTY));
545333da53SGreg Roach
555333da53SGreg Roach        // Rebuild the location name in the correct format.
565333da53SGreg Roach        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
575333da53SGreg Roach    }
585333da53SGreg Roach
595333da53SGreg Roach    /**
605333da53SGreg Roach     * Get the higher level location.
615333da53SGreg Roach     *
625333da53SGreg Roach     * @return PlaceLocation
635333da53SGreg Roach     */
645333da53SGreg Roach    public function parent(): PlaceLocation
655333da53SGreg Roach    {
665333da53SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
675333da53SGreg Roach    }
685333da53SGreg Roach
695333da53SGreg Roach    /**
7090949315SGreg Roach     * The database row id that contains this location.
715333da53SGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
725333da53SGreg Roach     *
7390949315SGreg Roach     * @return int|null
745333da53SGreg Roach     */
7590949315SGreg Roach    public function id(): ?int
765333da53SGreg Roach    {
775333da53SGreg Roach        // The "top-level" location won't exist in the database.
785333da53SGreg Roach        if ($this->parts->isEmpty()) {
7990949315SGreg Roach            return null;
805333da53SGreg Roach        }
815333da53SGreg Roach
8290949315SGreg Roach        return Registry::cache()->array()->remember('location-' . $this->location_name, function () {
8390949315SGreg Roach            $parent_id = $this->parent()->id();
845333da53SGreg Roach
8590949315SGreg Roach            $place = $this->parts->first();
8690949315SGreg Roach            $place = mb_substr($place, 0, 120);
875333da53SGreg Roach
8890949315SGreg Roach            if ($parent_id === null) {
8990949315SGreg Roach                $location_id = DB::table('place_location')
9090949315SGreg Roach                    ->where('place', '=', $place)
9190949315SGreg Roach                    ->whereNull('parent_id')
9290949315SGreg Roach                    ->value('id');
9390949315SGreg Roach            } else {
9490949315SGreg Roach                $location_id = DB::table('place_location')
9590949315SGreg Roach                    ->where('place', '=', $place)
9690949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
9790949315SGreg Roach                    ->value('id');
9890949315SGreg Roach            }
995333da53SGreg Roach
10090949315SGreg Roach            $location_id = $location_id ?? DB::table('place_location')->insertGetId([
10190949315SGreg Roach                    'parent_id' => $parent_id,
10290949315SGreg Roach                    'place'     => $place,
1035333da53SGreg Roach                ]);
1045333da53SGreg Roach
10590949315SGreg Roach            return (int) $location_id;
1065333da53SGreg Roach        });
1075333da53SGreg Roach    }
1085333da53SGreg Roach
1095333da53SGreg Roach    /**
1105333da53SGreg Roach     * Does this location exist in the database?  Note that calls to PlaceLocation::id() will
1115333da53SGreg Roach     * create the row, so this function is only meaningful when called before a call to PlaceLocation::id().
1125333da53SGreg Roach     *
1135333da53SGreg Roach     * @return bool
1145333da53SGreg Roach     */
1155333da53SGreg Roach    public function exists(): bool
1165333da53SGreg Roach    {
11790949315SGreg Roach        $parent_id = null;
1185333da53SGreg Roach
11990949315SGreg Roach        foreach ($this->parts->reverse() as $place) {
12090949315SGreg Roach            if ($parent_id === null) {
12190949315SGreg Roach                $parent_id = DB::table('place_location')
12290949315SGreg Roach                    ->whereNull('parent_id')
12390949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
12490949315SGreg Roach                    ->value('id');
12590949315SGreg Roach            } else {
12690949315SGreg Roach                $parent_id = DB::table('place_location')
12790949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
12890949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
12990949315SGreg Roach                    ->value('id');
1305333da53SGreg Roach            }
1315333da53SGreg Roach
13290949315SGreg Roach            if ($parent_id === null) {
13390949315SGreg Roach                return false;
13490949315SGreg Roach            }
13590949315SGreg Roach        }
13690949315SGreg Roach
13790949315SGreg Roach        return true;
1385333da53SGreg Roach    }
1395333da53SGreg Roach
1405333da53SGreg Roach    /**
141*ac701fbdSGreg Roach     * @return object
1425333da53SGreg Roach     */
143*ac701fbdSGreg Roach    private function details(): object
1445333da53SGreg Roach    {
1456b9cb339SGreg Roach        return Registry::cache()->array()->remember('location-details-' . $this->id(), function () {
1465333da53SGreg Roach            // The "top-level" location won't exist in the database.
1475333da53SGreg Roach            if ($this->parts->isEmpty()) {
1485333da53SGreg Roach                return (object) [
14990949315SGreg Roach                    'latitude'  => null,
15090949315SGreg Roach                    'longitude' => null,
1515333da53SGreg Roach                ];
1525333da53SGreg Roach            }
1535333da53SGreg Roach
15490949315SGreg Roach            $row = DB::table('place_location')
15590949315SGreg Roach                ->where('id', '=', $this->id())
15690949315SGreg Roach                ->select(['latitude', 'longitude'])
1575333da53SGreg Roach                ->first();
15890949315SGreg Roach
15990949315SGreg Roach            if ($row->latitude !== null) {
16090949315SGreg Roach                $row->latitude = (float) $row->latitude;
16190949315SGreg Roach            }
16290949315SGreg Roach
16390949315SGreg Roach            if ($row->longitude !== null) {
16490949315SGreg Roach                $row->longitude = (float) $row->longitude;
16590949315SGreg Roach            }
16690949315SGreg Roach
16790949315SGreg Roach            return $row;
1685333da53SGreg Roach        });
1695333da53SGreg Roach    }
1705333da53SGreg Roach
1715333da53SGreg Roach    /**
1725333da53SGreg Roach     * Latitude of the location.
1735333da53SGreg Roach     *
17490949315SGreg Roach     * @return float|null
1755333da53SGreg Roach     */
17690949315SGreg Roach    public function latitude(): ?float
1775333da53SGreg Roach    {
17890949315SGreg Roach        return $this->details()->latitude;
1795333da53SGreg Roach    }
1805333da53SGreg Roach
1815333da53SGreg Roach    /**
1820874af26SRichard Cissée     * Longitude of the location.
1835333da53SGreg Roach     *
18490949315SGreg Roach     * @return float|null
1855333da53SGreg Roach     */
18690949315SGreg Roach    public function longitude(): ?float
1875333da53SGreg Roach    {
18890949315SGreg Roach        return $this->details()->longitude;
1895333da53SGreg Roach    }
1905333da53SGreg Roach
1915333da53SGreg Roach    /**
1925333da53SGreg Roach     * @return string
1935333da53SGreg Roach     */
1945333da53SGreg Roach    public function locationName(): string
1955333da53SGreg Roach    {
1965333da53SGreg Roach        return (string) $this->parts->first();
1975333da53SGreg Roach    }
19879de923dSGreg Roach
19979de923dSGreg Roach    /**
20079de923dSGreg Roach     * Find a rectangle that (approximately) encloses this place.
20179de923dSGreg Roach     *
20279de923dSGreg Roach     * @return array<array<float>>
20379de923dSGreg Roach     */
20479de923dSGreg Roach    public function boundingRectangle(): array
20579de923dSGreg Roach    {
20651f91c3cSGreg Roach        if ($this->id() === null) {
2079a9dfcf7SGreg Roach            return [[-180.0, -90.0], [180.0, 90.0]];
20879de923dSGreg Roach        }
20979de923dSGreg Roach
2109a9dfcf7SGreg Roach        // Find our own co-ordinates and those of any child places
21190949315SGreg Roach        $latitudes = DB::table('place_location')
21290949315SGreg Roach            ->whereNotNull('latitude')
21390949315SGreg Roach            ->where(function (Builder $query): void {
21490949315SGreg Roach                $query
21590949315SGreg Roach                    ->where('parent_id', '=', $this->id())
21690949315SGreg Roach                    ->orWhere('id', '=', $this->id());
21790949315SGreg Roach            })
21890949315SGreg Roach            ->groupBy(['latitude'])
21990949315SGreg Roach            ->pluck('latitude')
2209a9dfcf7SGreg Roach            ->map(static function (string $x): float {
22190949315SGreg Roach                return (float) $x;
2229a9dfcf7SGreg Roach            });
2239a9dfcf7SGreg Roach
22490949315SGreg Roach        $longitudes = DB::table('place_location')
22590949315SGreg Roach            ->whereNotNull('longitude')
22690949315SGreg Roach            ->where(function (Builder $query): void {
22790949315SGreg Roach                $query
22890949315SGreg Roach                    ->where('parent_id', '=', $this->id())
22990949315SGreg Roach                    ->orWhere('id', '=', $this->id());
23090949315SGreg Roach            })
23190949315SGreg Roach            ->groupBy(['longitude'])
23290949315SGreg Roach            ->pluck('longitude')
2339a9dfcf7SGreg Roach            ->map(static function (string $x): float {
23490949315SGreg Roach                return (float) $x;
2359a9dfcf7SGreg Roach            });
2369a9dfcf7SGreg Roach
23714cb4388SGreg Roach        // No co-ordinates?  Use the parent place instead.
23890949315SGreg Roach        if ($latitudes->isEmpty() || $longitudes->isEmpty()) {
23914cb4388SGreg Roach            return $this->parent()->boundingRectangle();
24014cb4388SGreg Roach        }
24114cb4388SGreg Roach
24214cb4388SGreg Roach        // Many co-ordinates?  Generate a bounding rectangle that includes them.
2439a9dfcf7SGreg Roach        if ($latitudes->count() > 1 || $longitudes->count() > 1) {
2449a9dfcf7SGreg Roach            return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]];
24579de923dSGreg Roach        }
24679de923dSGreg Roach
24714cb4388SGreg Roach        // Just one co-ordinate?  Draw a box around it.
24814cb4388SGreg Roach        switch ($this->parts->count()) {
24914cb4388SGreg Roach            case 1:
25014cb4388SGreg Roach                // Countries
25114cb4388SGreg Roach                $delta = 5.0;
25214cb4388SGreg Roach                break;
25314cb4388SGreg Roach            case 2:
25414cb4388SGreg Roach                // Regions
25514cb4388SGreg Roach                $delta = 1.0;
25614cb4388SGreg Roach                break;
25714cb4388SGreg Roach            default:
25814cb4388SGreg Roach                // Cities and districts
25914cb4388SGreg Roach                $delta = 0.2;
26014cb4388SGreg Roach                break;
26114cb4388SGreg Roach        }
26214cb4388SGreg Roach
26314cb4388SGreg Roach        return [[
26414cb4388SGreg Roach            max($latitudes->min() - $delta, -90.0),
26514cb4388SGreg Roach            max($longitudes->min() - $delta, -180.0),
26614cb4388SGreg Roach        ], [
26714cb4388SGreg Roach            min($latitudes->max() + $delta, 90.0),
26814cb4388SGreg Roach            min($longitudes->max() + $delta, 180.0),
26914cb4388SGreg Roach        ]];
27079de923dSGreg Roach    }
2715333da53SGreg Roach}
272