xref: /webtrees/app/PlaceLocation.php (revision 51f91c3c9c5c2e6fb77ada0be88ea7fac77b8eb0)
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 Roachuse stdClass;
265333da53SGreg Roach
2790949315SGreg Roachuse function max;
2890949315SGreg Roachuse function min;
2979de923dSGreg Roachuse function preg_split;
3090949315SGreg Roachuse function trim;
3190949315SGreg Roach
3290949315SGreg Roachuse const PREG_SPLIT_NO_EMPTY;
3379de923dSGreg Roach
345333da53SGreg Roach/**
355333da53SGreg Roach * Class PlaceLocation
365333da53SGreg Roach */
375333da53SGreg Roachclass PlaceLocation
385333da53SGreg Roach{
395333da53SGreg Roach    /** @var string e.g. "Westminster, London, England" */
405333da53SGreg Roach    private $location_name;
415333da53SGreg Roach
4294e35917SGreg Roach    /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */
435333da53SGreg Roach    private $parts;
445333da53SGreg Roach
455333da53SGreg Roach    /**
465333da53SGreg Roach     * Create a place-location.
475333da53SGreg Roach     *
485333da53SGreg Roach     * @param string $location_name
495333da53SGreg Roach     */
505333da53SGreg Roach    public function __construct(string $location_name)
515333da53SGreg Roach    {
525333da53SGreg Roach        // Ignore any empty parts in location names such as "Village, , , Country".
5390949315SGreg Roach        $location_name = trim($location_name);
5490949315SGreg Roach        $this->parts   = new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name, -1, PREG_SPLIT_NO_EMPTY));
555333da53SGreg Roach
565333da53SGreg Roach        // Rebuild the location name in the correct format.
575333da53SGreg Roach        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
585333da53SGreg Roach    }
595333da53SGreg Roach
605333da53SGreg Roach    /**
615333da53SGreg Roach     * Get the higher level location.
625333da53SGreg Roach     *
635333da53SGreg Roach     * @return PlaceLocation
645333da53SGreg Roach     */
655333da53SGreg Roach    public function parent(): PlaceLocation
665333da53SGreg Roach    {
675333da53SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
685333da53SGreg Roach    }
695333da53SGreg Roach
705333da53SGreg Roach    /**
7190949315SGreg Roach     * The database row id that contains this location.
725333da53SGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
735333da53SGreg Roach     *
7490949315SGreg Roach     * @return int|null
755333da53SGreg Roach     */
7690949315SGreg Roach    public function id(): ?int
775333da53SGreg Roach    {
785333da53SGreg Roach        // The "top-level" location won't exist in the database.
795333da53SGreg Roach        if ($this->parts->isEmpty()) {
8090949315SGreg Roach            return null;
815333da53SGreg Roach        }
825333da53SGreg Roach
8390949315SGreg Roach        return Registry::cache()->array()->remember('location-' . $this->location_name, function () {
8490949315SGreg Roach            $parent_id = $this->parent()->id();
855333da53SGreg Roach
8690949315SGreg Roach            $place = $this->parts->first();
8790949315SGreg Roach            $place = mb_substr($place, 0, 120);
885333da53SGreg Roach
8990949315SGreg Roach            if ($parent_id === null) {
9090949315SGreg Roach                $location_id = DB::table('place_location')
9190949315SGreg Roach                    ->where('place', '=', $place)
9290949315SGreg Roach                    ->whereNull('parent_id')
9390949315SGreg Roach                    ->value('id');
9490949315SGreg Roach            } else {
9590949315SGreg Roach                $location_id = DB::table('place_location')
9690949315SGreg Roach                    ->where('place', '=', $place)
9790949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
9890949315SGreg Roach                    ->value('id');
9990949315SGreg Roach            }
1005333da53SGreg Roach
10190949315SGreg Roach            $location_id = $location_id ?? DB::table('place_location')->insertGetId([
10290949315SGreg Roach                    'parent_id' => $parent_id,
10390949315SGreg Roach                    'place'     => $place,
1045333da53SGreg Roach                ]);
1055333da53SGreg Roach
10690949315SGreg Roach            return (int) $location_id;
1075333da53SGreg Roach        });
1085333da53SGreg Roach    }
1095333da53SGreg Roach
1105333da53SGreg Roach    /**
1115333da53SGreg Roach     * Does this location exist in the database?  Note that calls to PlaceLocation::id() will
1125333da53SGreg Roach     * create the row, so this function is only meaningful when called before a call to PlaceLocation::id().
1135333da53SGreg Roach     *
1145333da53SGreg Roach     * @return bool
1155333da53SGreg Roach     */
1165333da53SGreg Roach    public function exists(): bool
1175333da53SGreg Roach    {
11890949315SGreg Roach        $parent_id = null;
1195333da53SGreg Roach
12090949315SGreg Roach        foreach ($this->parts->reverse() as $place) {
12190949315SGreg Roach            if ($parent_id === null) {
12290949315SGreg Roach                $parent_id = DB::table('place_location')
12390949315SGreg Roach                    ->whereNull('parent_id')
12490949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
12590949315SGreg Roach                    ->value('id');
12690949315SGreg Roach            } else {
12790949315SGreg Roach                $parent_id = DB::table('place_location')
12890949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
12990949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
13090949315SGreg Roach                    ->value('id');
1315333da53SGreg Roach            }
1325333da53SGreg Roach
13390949315SGreg Roach            if ($parent_id === null) {
13490949315SGreg Roach                return false;
13590949315SGreg Roach            }
13690949315SGreg Roach        }
13790949315SGreg Roach
13890949315SGreg Roach        return true;
1395333da53SGreg Roach    }
1405333da53SGreg Roach
1415333da53SGreg Roach    /**
1425333da53SGreg Roach     * @return stdClass
1435333da53SGreg Roach     */
1445333da53SGreg Roach    private function details(): stdClass
1455333da53SGreg Roach    {
1466b9cb339SGreg Roach        return Registry::cache()->array()->remember('location-details-' . $this->id(), function () {
1475333da53SGreg Roach            // The "top-level" location won't exist in the database.
1485333da53SGreg Roach            if ($this->parts->isEmpty()) {
1495333da53SGreg Roach                return (object) [
15090949315SGreg Roach                    'latitude'  => null,
15190949315SGreg Roach                    'longitude' => null,
1525333da53SGreg Roach                ];
1535333da53SGreg Roach            }
1545333da53SGreg Roach
15590949315SGreg Roach            $row = DB::table('place_location')
15690949315SGreg Roach                ->where('id', '=', $this->id())
15790949315SGreg Roach                ->select(['latitude', 'longitude'])
1585333da53SGreg Roach                ->first();
15990949315SGreg Roach
16090949315SGreg Roach            if ($row->latitude !== null) {
16190949315SGreg Roach                $row->latitude = (float) $row->latitude;
16290949315SGreg Roach            }
16390949315SGreg Roach
16490949315SGreg Roach            if ($row->longitude !== null) {
16590949315SGreg Roach                $row->longitude = (float) $row->longitude;
16690949315SGreg Roach            }
16790949315SGreg Roach
16890949315SGreg Roach            return $row;
1695333da53SGreg Roach        });
1705333da53SGreg Roach    }
1715333da53SGreg Roach
1725333da53SGreg Roach    /**
1735333da53SGreg Roach     * Latitude of the location.
1745333da53SGreg Roach     *
17590949315SGreg Roach     * @return float|null
1765333da53SGreg Roach     */
17790949315SGreg Roach    public function latitude(): ?float
1785333da53SGreg Roach    {
17990949315SGreg Roach        return $this->details()->latitude;
1805333da53SGreg Roach    }
1815333da53SGreg Roach
1825333da53SGreg Roach    /**
1830874af26SRichard Cissée     * Longitude of the location.
1845333da53SGreg Roach     *
18590949315SGreg Roach     * @return float|null
1865333da53SGreg Roach     */
18790949315SGreg Roach    public function longitude(): ?float
1885333da53SGreg Roach    {
18990949315SGreg Roach        return $this->details()->longitude;
1905333da53SGreg Roach    }
1915333da53SGreg Roach
1925333da53SGreg Roach    /**
1935333da53SGreg Roach     * @return string
1945333da53SGreg Roach     */
1955333da53SGreg Roach    public function locationName(): string
1965333da53SGreg Roach    {
1975333da53SGreg Roach        return (string) $this->parts->first();
1985333da53SGreg Roach    }
19979de923dSGreg Roach
20079de923dSGreg Roach    /**
20179de923dSGreg Roach     * Find a rectangle that (approximately) encloses this place.
20279de923dSGreg Roach     *
20379de923dSGreg Roach     * @return array<array<float>>
20479de923dSGreg Roach     */
20579de923dSGreg Roach    public function boundingRectangle(): array
20679de923dSGreg Roach    {
207*51f91c3cSGreg Roach        if ($this->id() === null) {
2089a9dfcf7SGreg Roach            return [[-180.0, -90.0], [180.0, 90.0]];
20979de923dSGreg Roach        }
21079de923dSGreg Roach
2119a9dfcf7SGreg Roach        // Find our own co-ordinates and those of any child places
21290949315SGreg Roach        $latitudes = DB::table('place_location')
21390949315SGreg Roach            ->whereNotNull('latitude')
21490949315SGreg Roach            ->where(function (Builder $query): void {
21590949315SGreg Roach                $query
21690949315SGreg Roach                    ->where('parent_id', '=', $this->id())
21790949315SGreg Roach                    ->orWhere('id', '=', $this->id());
21890949315SGreg Roach            })
21990949315SGreg Roach            ->groupBy(['latitude'])
22090949315SGreg Roach            ->pluck('latitude')
2219a9dfcf7SGreg Roach            ->map(static function (string $x): float {
22290949315SGreg Roach                return (float) $x;
2239a9dfcf7SGreg Roach            });
2249a9dfcf7SGreg Roach
22590949315SGreg Roach        $longitudes = DB::table('place_location')
22690949315SGreg Roach            ->whereNotNull('longitude')
22790949315SGreg Roach            ->where(function (Builder $query): void {
22890949315SGreg Roach                $query
22990949315SGreg Roach                    ->where('parent_id', '=', $this->id())
23090949315SGreg Roach                    ->orWhere('id', '=', $this->id());
23190949315SGreg Roach            })
23290949315SGreg Roach            ->groupBy(['longitude'])
23390949315SGreg Roach            ->pluck('longitude')
2349a9dfcf7SGreg Roach            ->map(static function (string $x): float {
23590949315SGreg Roach                return (float) $x;
2369a9dfcf7SGreg Roach            });
2379a9dfcf7SGreg Roach
23814cb4388SGreg Roach        // No co-ordinates?  Use the parent place instead.
23990949315SGreg Roach        if ($latitudes->isEmpty() || $longitudes->isEmpty()) {
24014cb4388SGreg Roach            return $this->parent()->boundingRectangle();
24114cb4388SGreg Roach        }
24214cb4388SGreg Roach
24314cb4388SGreg Roach        // Many co-ordinates?  Generate a bounding rectangle that includes them.
2449a9dfcf7SGreg Roach        if ($latitudes->count() > 1 || $longitudes->count() > 1) {
2459a9dfcf7SGreg Roach            return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]];
24679de923dSGreg Roach        }
24779de923dSGreg Roach
24814cb4388SGreg Roach        // Just one co-ordinate?  Draw a box around it.
24914cb4388SGreg Roach        switch ($this->parts->count()) {
25014cb4388SGreg Roach            case 1:
25114cb4388SGreg Roach                // Countries
25214cb4388SGreg Roach                $delta = 5.0;
25314cb4388SGreg Roach                break;
25414cb4388SGreg Roach            case 2:
25514cb4388SGreg Roach                // Regions
25614cb4388SGreg Roach                $delta = 1.0;
25714cb4388SGreg Roach                break;
25814cb4388SGreg Roach            default:
25914cb4388SGreg Roach                // Cities and districts
26014cb4388SGreg Roach                $delta = 0.2;
26114cb4388SGreg Roach                break;
26214cb4388SGreg Roach        }
26314cb4388SGreg Roach
26414cb4388SGreg Roach        return [[
26514cb4388SGreg Roach            max($latitudes->min() - $delta, -90.0),
26614cb4388SGreg Roach            max($longitudes->min() - $delta, -180.0),
26714cb4388SGreg Roach        ], [
26814cb4388SGreg Roach            min($latitudes->max() + $delta, 90.0),
26914cb4388SGreg Roach            min($longitudes->max() + $delta, 180.0),
27014cb4388SGreg Roach        ]];
27179de923dSGreg Roach    }
2725333da53SGreg Roach}
273