190949315SGreg Roach<?php 290949315SGreg Roach 390949315SGreg Roach/** 490949315SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 690949315SGreg Roach * This program is free software: you can redistribute it and/or modify 790949315SGreg Roach * it under the terms of the GNU General Public License as published by 890949315SGreg Roach * the Free Software Foundation, either version 3 of the License, or 990949315SGreg Roach * (at your option) any later version. 1090949315SGreg Roach * This program is distributed in the hope that it will be useful, 1190949315SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1290949315SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1390949315SGreg Roach * GNU General Public License for more details. 1490949315SGreg 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/>. 1690949315SGreg Roach */ 1790949315SGreg Roach 1890949315SGreg Roachdeclare(strict_types=1); 1990949315SGreg Roach 2090949315SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2190949315SGreg Roach 22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 2390949315SGreg Roachuse Fisharebest\Webtrees\Gedcom; 2490949315SGreg Roachuse Fisharebest\Webtrees\PlaceLocation; 2590949315SGreg Roachuse Fisharebest\Webtrees\Services\MapDataService; 2690949315SGreg Roachuse Psr\Http\Message\ResponseInterface; 2790949315SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 2890949315SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 2990949315SGreg Roach 3090949315SGreg Roachuse function addcslashes; 3190949315SGreg Roachuse function array_reverse; 3290949315SGreg Roachuse function array_unshift; 3390949315SGreg Roachuse function count; 3490949315SGreg Roachuse function implode; 3590949315SGreg Roachuse function preg_replace; 3690949315SGreg Roachuse function response; 3790949315SGreg Roach 3890949315SGreg Roach/** 3990949315SGreg Roach * Export geographic data. 4090949315SGreg Roach */ 4190949315SGreg Roachclass MapDataExportGeoJson implements RequestHandlerInterface 4290949315SGreg Roach{ 43c4943cffSGreg Roach private MapDataService $map_data_service; 4490949315SGreg Roach 4590949315SGreg Roach /** 4690949315SGreg Roach * Dependency injection. 4790949315SGreg Roach * 4890949315SGreg Roach * @param MapDataService $map_data_service 4990949315SGreg Roach */ 5090949315SGreg Roach public function __construct(MapDataService $map_data_service) 5190949315SGreg Roach { 5290949315SGreg Roach $this->map_data_service = $map_data_service; 5390949315SGreg Roach } 5490949315SGreg Roach 5590949315SGreg Roach /** 5690949315SGreg Roach * @param ServerRequestInterface $request 5790949315SGreg Roach * 5890949315SGreg Roach * @return ResponseInterface 5990949315SGreg Roach */ 6090949315SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 6190949315SGreg Roach { 6290949315SGreg Roach $parent_id = $request->getAttribute('parent_id'); 6390949315SGreg Roach 6490949315SGreg Roach if ($parent_id === null) { 6590949315SGreg Roach $parent = new PlaceLocation(''); 6690949315SGreg Roach } else { 6790949315SGreg Roach $parent = $this->map_data_service->findById((int) $parent_id); 6890949315SGreg Roach } 6990949315SGreg Roach 7090949315SGreg Roach for ($tmp = $parent, $hierarchy = []; $tmp->id() !== null; $tmp = $tmp->parent()) { 7190949315SGreg Roach $hierarchy[] = $tmp->locationName(); 7290949315SGreg Roach } 7390949315SGreg Roach 7490949315SGreg Roach // Create the file name 7590949315SGreg Roach $filename = preg_replace('/[^\p{L}]+/u', '-', $hierarchy[0] ?? 'Global') . '.geojson'; 7690949315SGreg Roach 7790949315SGreg Roach // Recursively search for child places 7890949315SGreg Roach $features = []; 7990949315SGreg Roach $queue = [[ 8090949315SGreg Roach $parent->id(), array_reverse($hierarchy), $parent->latitude(), $parent->longitude() 8190949315SGreg Roach ]]; 8290949315SGreg Roach 8390949315SGreg Roach while ($queue !== []) { 8490949315SGreg Roach [$id, $hierarchy, $latitude, $longitude] = array_shift($queue); 8590949315SGreg Roach 8690949315SGreg Roach if ($latitude !== null && !$longitude !== null) { 8790949315SGreg Roach $features[] = [ 8890949315SGreg Roach 'type' => 'Feature', 8990949315SGreg Roach 'geometry' => [ 9090949315SGreg Roach 'type' => 'Point', 9190949315SGreg Roach 'coordinates' => [ 924d0e7df2SGreg Roach (float) $longitude, 934d0e7df2SGreg Roach (float) $latitude, 9490949315SGreg Roach ], 9590949315SGreg Roach ], 9690949315SGreg Roach 'properties' => [ 9790949315SGreg Roach 'name' => implode(Gedcom::PLACE_SEPARATOR, array_reverse($hierarchy)), 9890949315SGreg Roach ], 9990949315SGreg Roach ]; 10090949315SGreg Roach } 10190949315SGreg Roach 10290949315SGreg Roach $query = DB::table('place_location'); 10390949315SGreg Roach // Data for the next level. 10490949315SGreg Roach 10590949315SGreg Roach if ($id === null) { 10690949315SGreg Roach $query->whereNull('parent_id'); 10790949315SGreg Roach } else { 10890949315SGreg Roach $query->where('parent_id', '=', $id); 10990949315SGreg Roach } 11090949315SGreg Roach 11190949315SGreg Roach $rows = $query 11290949315SGreg Roach ->orderBy('place', 'DESC') 11390949315SGreg Roach ->select(['id', 'place', 'latitude', 'longitude']) 11490949315SGreg Roach ->get(); 11590949315SGreg Roach 11690949315SGreg Roach $next_level = count($hierarchy); 11790949315SGreg Roach 11890949315SGreg Roach foreach ($rows as $row) { 11990949315SGreg Roach $hierarchy[$next_level] = $row->place; 12090949315SGreg Roach array_unshift($queue, [$row->id, $hierarchy, $row->latitude, $row->longitude]); 12190949315SGreg Roach } 12290949315SGreg Roach } 12390949315SGreg Roach 12490949315SGreg Roach $geojson = [ 12590949315SGreg Roach 'type' => 'FeatureCollection', 12690949315SGreg Roach 'features' => $features, 12790949315SGreg Roach ]; 12890949315SGreg Roach 12990949315SGreg Roach $filename = addcslashes($filename, '"'); 13090949315SGreg Roach 13190949315SGreg Roach return response($geojson) 1326172e7f6SGreg Roach ->withHeader('content-type', 'application/vnd.geo+json') 1336172e7f6SGreg Roach ->withHeader('content-disposition', 'attachment; filename="' . $filename . '"'); 13490949315SGreg Roach } 13590949315SGreg Roach} 136