xref: /webtrees/app/PlaceLocation.php (revision 90949315619051b772509f417394730bd8b1bda0)
15333da53SGreg Roach<?php
25333da53SGreg Roach
35333da53SGreg Roach/**
45333da53SGreg Roach * webtrees: online genealogy
5*90949315SGreg 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
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 Illuminate\Database\Capsule\Manager as DB;
23*90949315SGreg Roachuse Illuminate\Database\Query\Builder;
245333da53SGreg Roachuse Illuminate\Support\Collection;
255333da53SGreg Roachuse stdClass;
265333da53SGreg Roach
27*90949315SGreg Roachuse function max;
28*90949315SGreg Roachuse function min;
2979de923dSGreg Roachuse function preg_split;
30*90949315SGreg Roachuse function trim;
31*90949315SGreg Roach
32*90949315SGreg 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".
53*90949315SGreg Roach        $location_name = trim($location_name);
54*90949315SGreg 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    /**
71*90949315SGreg 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     *
74*90949315SGreg Roach     * @return int|null
755333da53SGreg Roach     */
76*90949315SGreg 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()) {
80*90949315SGreg Roach            return null;
815333da53SGreg Roach        }
825333da53SGreg Roach
83*90949315SGreg Roach        return Registry::cache()->array()->remember('location-' . $this->location_name, function () {
84*90949315SGreg Roach            $parent_id = $this->parent()->id();
855333da53SGreg Roach
86*90949315SGreg Roach            $place = $this->parts->first();
87*90949315SGreg Roach            $place = mb_substr($place, 0, 120);
885333da53SGreg Roach
89*90949315SGreg Roach            if ($parent_id === null) {
90*90949315SGreg Roach                $location_id = DB::table('place_location')
91*90949315SGreg Roach                    ->where('place', '=', $place)
92*90949315SGreg Roach                    ->whereNull('parent_id')
93*90949315SGreg Roach                    ->value('id');
94*90949315SGreg Roach            } else {
95*90949315SGreg Roach                $location_id = DB::table('place_location')
96*90949315SGreg Roach                    ->where('place', '=', $place)
97*90949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
98*90949315SGreg Roach                    ->value('id');
99*90949315SGreg Roach            }
1005333da53SGreg Roach
101*90949315SGreg Roach            $location_id = $location_id ?? DB::table('place_location')->insertGetId([
102*90949315SGreg Roach                    'parent_id' => $parent_id,
103*90949315SGreg Roach                    'place'     => $place,
1045333da53SGreg Roach                ]);
1055333da53SGreg Roach
106*90949315SGreg 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    {
118*90949315SGreg Roach        $parent_id = null;
1195333da53SGreg Roach
120*90949315SGreg Roach        foreach ($this->parts->reverse() as $place) {
121*90949315SGreg Roach            if ($parent_id === null) {
122*90949315SGreg Roach                $parent_id = DB::table('place_location')
123*90949315SGreg Roach                    ->whereNull('parent_id')
124*90949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
125*90949315SGreg Roach                    ->value('id');
126*90949315SGreg Roach            } else {
127*90949315SGreg Roach                $parent_id = DB::table('place_location')
128*90949315SGreg Roach                    ->where('parent_id', '=', $parent_id)
129*90949315SGreg Roach                    ->where('place', '=', mb_substr($place, 0, 120))
130*90949315SGreg Roach                    ->value('id');
1315333da53SGreg Roach            }
1325333da53SGreg Roach
133*90949315SGreg Roach            if ($parent_id === null) {
134*90949315SGreg Roach                return false;
135*90949315SGreg Roach            }
136*90949315SGreg Roach        }
137*90949315SGreg Roach
138*90949315SGreg 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) [
150*90949315SGreg Roach                    'latitude'  => null,
151*90949315SGreg Roach                    'longitude' => null,
1525333da53SGreg Roach                ];
1535333da53SGreg Roach            }
1545333da53SGreg Roach
155*90949315SGreg Roach            $row = DB::table('place_location')
156*90949315SGreg Roach                ->where('id', '=', $this->id())
157*90949315SGreg Roach                ->select(['latitude', 'longitude'])
1585333da53SGreg Roach                ->first();
159*90949315SGreg Roach
160*90949315SGreg Roach            if ($row->latitude !== null) {
161*90949315SGreg Roach                $row->latitude = (float) $row->latitude;
162*90949315SGreg Roach            }
163*90949315SGreg Roach
164*90949315SGreg Roach            if ($row->longitude !== null) {
165*90949315SGreg Roach                $row->longitude = (float) $row->longitude;
166*90949315SGreg Roach            }
167*90949315SGreg Roach
168*90949315SGreg Roach            return $row;
1695333da53SGreg Roach        });
1705333da53SGreg Roach    }
1715333da53SGreg Roach
1725333da53SGreg Roach    /**
1735333da53SGreg Roach     * Latitude of the location.
1745333da53SGreg Roach     *
175*90949315SGreg Roach     * @return float|null
1765333da53SGreg Roach     */
177*90949315SGreg Roach    public function latitude(): ?float
1785333da53SGreg Roach    {
179*90949315SGreg Roach        return $this->details()->latitude;
1805333da53SGreg Roach    }
1815333da53SGreg Roach
1825333da53SGreg Roach    /**
1830874af26SRichard Cissée     * Longitude of the location.
1845333da53SGreg Roach     *
185*90949315SGreg Roach     * @return float|null
1865333da53SGreg Roach     */
187*90949315SGreg Roach    public function longitude(): ?float
1885333da53SGreg Roach    {
189*90949315SGreg 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    {
2079a9dfcf7SGreg Roach        if ($this->id() === 0) {
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
212*90949315SGreg Roach        $latitudes = DB::table('place_location')
213*90949315SGreg Roach            ->whereNotNull('latitude')
214*90949315SGreg Roach            ->where(function (Builder $query): void {
215*90949315SGreg Roach                $query
216*90949315SGreg Roach                    ->where('parent_id', '=', $this->id())
217*90949315SGreg Roach                    ->orWhere('id', '=', $this->id());
218*90949315SGreg Roach            })
219*90949315SGreg Roach            ->groupBy(['latitude'])
220*90949315SGreg Roach            ->pluck('latitude')
2219a9dfcf7SGreg Roach            ->map(static function (string $x): float {
222*90949315SGreg Roach                return (float) $x;
2239a9dfcf7SGreg Roach            });
2249a9dfcf7SGreg Roach
225*90949315SGreg Roach        $longitudes = DB::table('place_location')
226*90949315SGreg Roach            ->whereNotNull('longitude')
227*90949315SGreg Roach            ->where(function (Builder $query): void {
228*90949315SGreg Roach                $query
229*90949315SGreg Roach                    ->where('parent_id', '=', $this->id())
230*90949315SGreg Roach                    ->orWhere('id', '=', $this->id());
231*90949315SGreg Roach            })
232*90949315SGreg Roach            ->groupBy(['longitude'])
233*90949315SGreg Roach            ->pluck('longitude')
2349a9dfcf7SGreg Roach            ->map(static function (string $x): float {
235*90949315SGreg Roach                return (float) $x;
2369a9dfcf7SGreg Roach            });
2379a9dfcf7SGreg Roach
23814cb4388SGreg Roach        // No co-ordinates?  Use the parent place instead.
239*90949315SGreg 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