xref: /webtrees/app/PlaceLocation.php (revision 0874af268f0117b9dcc708200448449d80c9c87f)
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 app;
2879de923dSGreg Roachuse function preg_split;
2979de923dSGreg Roach
305333da53SGreg Roach/**
315333da53SGreg Roach * Class PlaceLocation
325333da53SGreg Roach */
335333da53SGreg Roachclass PlaceLocation
345333da53SGreg Roach{
355333da53SGreg Roach    /** @var string e.g. "Westminster, London, England" */
365333da53SGreg Roach    private $location_name;
375333da53SGreg Roach
3894e35917SGreg Roach    /** @var Collection<string> The parts of a location name, e.g. ["Westminster", "London", "England"] */
395333da53SGreg Roach    private $parts;
405333da53SGreg Roach
415333da53SGreg Roach    /**
425333da53SGreg Roach     * Create a place-location.
435333da53SGreg Roach     *
445333da53SGreg Roach     * @param string $location_name
455333da53SGreg Roach     */
465333da53SGreg Roach    public function __construct(string $location_name)
475333da53SGreg Roach    {
485333da53SGreg Roach        // Ignore any empty parts in location names such as "Village, , , Country".
495333da53SGreg Roach        $this->parts = (new Collection(preg_split(Gedcom::PLACE_SEPARATOR_REGEX, $location_name)))
505333da53SGreg Roach            ->filter();
515333da53SGreg Roach
525333da53SGreg Roach        // Rebuild the location name in the correct format.
535333da53SGreg Roach        $this->location_name = $this->parts->implode(Gedcom::PLACE_SEPARATOR);
545333da53SGreg Roach    }
555333da53SGreg Roach
565333da53SGreg Roach    /**
575333da53SGreg Roach     * Get the higher level location.
585333da53SGreg Roach     *
595333da53SGreg Roach     * @return PlaceLocation
605333da53SGreg Roach     */
615333da53SGreg Roach    public function parent(): PlaceLocation
625333da53SGreg Roach    {
635333da53SGreg Roach        return new self($this->parts->slice(1)->implode(Gedcom::PLACE_SEPARATOR));
645333da53SGreg Roach    }
655333da53SGreg Roach
665333da53SGreg Roach    /**
675333da53SGreg Roach     * The database row that contains this location.
685333da53SGreg Roach     * Note that due to database collation, both "Quebec" and "Québec" will share the same row.
695333da53SGreg Roach     *
705333da53SGreg Roach     * @return int
715333da53SGreg Roach     */
725333da53SGreg Roach    public function id(): int
735333da53SGreg Roach    {
745333da53SGreg Roach        return app('cache.array')->remember('location-' . $this->location_name, function () {
755333da53SGreg Roach            // The "top-level" location won't exist in the database.
765333da53SGreg Roach            if ($this->parts->isEmpty()) {
775333da53SGreg Roach                return 0;
785333da53SGreg Roach            }
795333da53SGreg Roach
805333da53SGreg Roach            $parent_location_id = $this->parent()->id();
815333da53SGreg Roach
825333da53SGreg Roach            $location_id = (int) DB::table('placelocation')
83c4ab7783SGreg Roach                ->where('pl_place', '=', mb_substr($this->parts->first(), 0, 120))
845333da53SGreg Roach                ->where('pl_parent_id', '=', $parent_location_id)
855333da53SGreg Roach                ->value('pl_id');
865333da53SGreg Roach
875333da53SGreg Roach            if ($location_id === 0) {
885333da53SGreg Roach                $location = $this->parts->first();
895333da53SGreg Roach
905333da53SGreg Roach                $location_id = 1 + (int) DB::table('placelocation')->max('pl_id');
915333da53SGreg Roach
925333da53SGreg Roach                DB::table('placelocation')->insert([
935333da53SGreg Roach                    'pl_id'        => $location_id,
94c4ab7783SGreg Roach                    'pl_place'     => mb_substr($location, 0, 120),
955333da53SGreg Roach                    'pl_parent_id' => $parent_location_id,
965333da53SGreg Roach                    'pl_level'     => $this->parts->count() - 1,
975333da53SGreg Roach                    'pl_lati'      => '',
985333da53SGreg Roach                    'pl_long'      => '',
995333da53SGreg Roach                    'pl_icon'      => '',
1005333da53SGreg Roach                    'pl_zoom'      => 2,
1015333da53SGreg Roach                ]);
1025333da53SGreg Roach            }
1035333da53SGreg Roach
1045333da53SGreg Roach            return $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    {
1165333da53SGreg Roach        $location_id = 0;
1175333da53SGreg Roach
1185333da53SGreg Roach        $this->parts->reverse()->each(static function (string $place) use (&$location_id) {
1195333da53SGreg Roach            if ($location_id !== null) {
1205333da53SGreg Roach                $location_id = DB::table('placelocation')
1215333da53SGreg Roach                    ->where('pl_parent_id', '=', $location_id)
122c4ab7783SGreg Roach                    ->where('pl_place', '=', mb_substr($place, 0, 120))
1235333da53SGreg Roach                    ->value('pl_id');
1245333da53SGreg Roach            }
1255333da53SGreg Roach        });
1265333da53SGreg Roach
1275333da53SGreg Roach        return $location_id !== null;
1285333da53SGreg Roach    }
1295333da53SGreg Roach
1305333da53SGreg Roach    /**
1315333da53SGreg Roach     * @return stdClass
1325333da53SGreg Roach     */
1335333da53SGreg Roach    private function details(): stdClass
1345333da53SGreg Roach    {
1355333da53SGreg Roach        return app('cache.array')->remember('location-details-' . $this->id(), function () {
1365333da53SGreg Roach            // The "top-level" location won't exist in the database.
1375333da53SGreg Roach            if ($this->parts->isEmpty()) {
1385333da53SGreg Roach                return (object) [
1395333da53SGreg Roach                    'pl_id'        => '0',
1405333da53SGreg Roach                    'pl_parent_id' => '0',
1415333da53SGreg Roach                    'pl_level' => null,
1425333da53SGreg Roach                    'pl_place' => '',
1435333da53SGreg Roach                    'pl_lati' => null,
1445333da53SGreg Roach                    'pl_long' => null,
1455333da53SGreg Roach                    'pl_zoom' => null,
1465333da53SGreg Roach                    'pl_icon' => null,
1475333da53SGreg Roach                    'pl_media' => null,
1485333da53SGreg Roach                ];
1495333da53SGreg Roach            }
1505333da53SGreg Roach
1515333da53SGreg Roach            return DB::table('placelocation')
1525333da53SGreg Roach                ->where('pl_id', '=', $this->id())
1535333da53SGreg Roach                ->first();
1545333da53SGreg Roach        });
1555333da53SGreg Roach    }
1565333da53SGreg Roach
1575333da53SGreg Roach    /**
1585333da53SGreg Roach     * Latitude of the location.
1595333da53SGreg Roach     *
1605333da53SGreg Roach     * @return float
1615333da53SGreg Roach     */
1625333da53SGreg Roach    public function latitude(): float
1635333da53SGreg Roach    {
1645333da53SGreg Roach        $gedcom_service = new GedcomService();
16514cb4388SGreg Roach        $pl_lati        = (string) $this->details()->pl_lati;
166e340fe65SGreg Roach
167e340fe65SGreg Roach        return $gedcom_service->readLatitude($pl_lati);
1685333da53SGreg Roach    }
1695333da53SGreg Roach
1705333da53SGreg Roach    /**
171*0874af26SRichard Cissée     * Longitude of the location.
1725333da53SGreg Roach     *
1735333da53SGreg Roach     * @return float
1745333da53SGreg Roach     */
1755333da53SGreg Roach    public function longitude(): float
1765333da53SGreg Roach    {
1775333da53SGreg Roach        $gedcom_service = new GedcomService();
17814cb4388SGreg Roach        $pl_long        = (string) $this->details()->pl_long;
179e340fe65SGreg Roach
180e340fe65SGreg Roach        return $gedcom_service->readLongitude($pl_long);
1815333da53SGreg Roach    }
1825333da53SGreg Roach
1835333da53SGreg Roach    /**
1845333da53SGreg Roach     * The icon for the location.
1855333da53SGreg Roach     *
1865333da53SGreg Roach     * @return string
1875333da53SGreg Roach     */
1885333da53SGreg Roach    public function icon(): string
1895333da53SGreg Roach    {
1905333da53SGreg Roach        return (string) $this->details()->pl_icon;
1915333da53SGreg Roach    }
1925333da53SGreg Roach
1935333da53SGreg Roach    /**
1945333da53SGreg Roach     * Zoom level for the location.
1955333da53SGreg Roach     *
1965333da53SGreg Roach     * @return int
1975333da53SGreg Roach     */
1985333da53SGreg Roach    public function zoom(): int
1995333da53SGreg Roach    {
200e340fe65SGreg Roach        return (int) $this->details()->pl_zoom ?: 2;
2015333da53SGreg Roach    }
2025333da53SGreg Roach
2035333da53SGreg Roach    /**
2045333da53SGreg Roach     * @return string
2055333da53SGreg Roach     */
2065333da53SGreg Roach    public function locationName(): string
2075333da53SGreg Roach    {
2085333da53SGreg Roach        return (string) $this->parts->first();
2095333da53SGreg Roach    }
21079de923dSGreg Roach
21179de923dSGreg Roach    /**
21279de923dSGreg Roach     * Find a rectangle that (approximately) encloses this place.
21379de923dSGreg Roach     *
21479de923dSGreg Roach     * @return array<array<float>>
21579de923dSGreg Roach     */
21679de923dSGreg Roach    public function boundingRectangle(): array
21779de923dSGreg Roach    {
2189a9dfcf7SGreg Roach        if ($this->id() === 0) {
2199a9dfcf7SGreg Roach            return [[-180.0, -90.0], [180.0, 90.0]];
22079de923dSGreg Roach        }
22179de923dSGreg Roach
2229a9dfcf7SGreg Roach        // Find our own co-ordinates and those of any child places
2239a9dfcf7SGreg Roach        $latitudes = DB::table('placelocation')
2249a9dfcf7SGreg Roach            ->where('pl_parent_id', '=', $this->id())
2259a9dfcf7SGreg Roach            ->orWhere('pl_id', '=', $this->id())
22614cb4388SGreg Roach            ->groupBy(['pl_lati'])
2279a9dfcf7SGreg Roach            ->pluck('pl_lati')
2289a9dfcf7SGreg Roach            ->filter()
2299a9dfcf7SGreg Roach            ->map(static function (string $x): float {
2309a9dfcf7SGreg Roach                return (new GedcomService())->readLatitude($x);
2319a9dfcf7SGreg Roach            });
2329a9dfcf7SGreg Roach
2339a9dfcf7SGreg Roach        $longitudes = DB::table('placelocation')
2349a9dfcf7SGreg Roach            ->where('pl_parent_id', '=', $this->id())
2359a9dfcf7SGreg Roach            ->orWhere('pl_id', '=', $this->id())
23614cb4388SGreg Roach            ->groupBy(['pl_long'])
2379a9dfcf7SGreg Roach            ->pluck('pl_long')
2389a9dfcf7SGreg Roach            ->filter()
2399a9dfcf7SGreg Roach            ->map(static function (string $x): float {
2409a9dfcf7SGreg Roach                return (new GedcomService())->readLongitude($x);
2419a9dfcf7SGreg Roach            });
2429a9dfcf7SGreg Roach
24314cb4388SGreg Roach        // No co-ordinates?  Use the parent place instead.
24414cb4388SGreg Roach        if ($latitudes->isEmpty() && $longitudes->isEmpty()) {
24514cb4388SGreg Roach            return $this->parent()->boundingRectangle();
24614cb4388SGreg Roach        }
24714cb4388SGreg Roach
24814cb4388SGreg Roach        // Many co-ordinates?  Generate a bounding rectangle that includes them.
2499a9dfcf7SGreg Roach        if ($latitudes->count() > 1 || $longitudes->count() > 1) {
2509a9dfcf7SGreg Roach            return [[$latitudes->min(), $longitudes->min()], [$latitudes->max(), $longitudes->max()]];
25179de923dSGreg Roach        }
25279de923dSGreg Roach
25314cb4388SGreg Roach        // Just one co-ordinate?  Draw a box around it.
25414cb4388SGreg Roach        switch ($this->parts->count()) {
25514cb4388SGreg Roach            case 1:
25614cb4388SGreg Roach                // Countries
25714cb4388SGreg Roach                $delta = 5.0;
25814cb4388SGreg Roach                break;
25914cb4388SGreg Roach            case 2:
26014cb4388SGreg Roach                // Regions
26114cb4388SGreg Roach                $delta = 1.0;
26214cb4388SGreg Roach                break;
26314cb4388SGreg Roach            default:
26414cb4388SGreg Roach                // Cities and districts
26514cb4388SGreg Roach                $delta = 0.2;
26614cb4388SGreg Roach                break;
26714cb4388SGreg Roach        }
26814cb4388SGreg Roach
26914cb4388SGreg Roach        return [[
27014cb4388SGreg Roach            max($latitudes->min() - $delta, -90.0),
27114cb4388SGreg Roach            max($longitudes->min() - $delta, -180.0),
27214cb4388SGreg Roach        ], [
27314cb4388SGreg Roach            min($latitudes->max() + $delta, 90.0),
27414cb4388SGreg Roach            min($longitudes->max() + $delta, 180.0),
27514cb4388SGreg Roach        ]];
27679de923dSGreg Roach    }
2775333da53SGreg Roach}
278