GeoJSON zrušen, geo ukládáme do DB.

master
Kowalski 2026-10-12 17:43:00 +02:00
parent d21657c594
commit e20e0d45b1
7 changed files with 53 additions and 35 deletions

View File

@ -15,6 +15,9 @@ use Nette\Utils\DateTime;
*/
final class ObvodFacade
{
/** Sloupce bez GEO, aby seznam obvodů nenačítal hranice. */
private const SLOUPCE = 'ID, NAZEV, CISLO, SKUPINA, AKTIVNI';
public function __construct(
private Explorer $database,
private User $user,
@ -28,9 +31,9 @@ final class ObvodFacade
*/
public function getObvod(int $id): Row|null
{
$sql = "SELECT *
$sql = 'SELECT ' . self::SLOUPCE . '
FROM OBVOD
WHERE OBVOD.ID = ?";
WHERE OBVOD.ID = ?';
return $this->database->fetch($sql, $id);
}
@ -43,11 +46,11 @@ final class ObvodFacade
public function getObvody(?string $skupina = null): array
{
if ($skupina === null || $skupina === '') {
return $this->database->fetchAll('SELECT * FROM OBVOD ORDER BY CISLO');
return $this->database->fetchAll('SELECT ' . self::SLOUPCE . ' FROM OBVOD ORDER BY CISLO');
}
return $this->database->fetchAll(
'SELECT * FROM OBVOD WHERE SKUPINA = ? ORDER BY CISLO',
'SELECT ' . self::SLOUPCE . ' FROM OBVOD WHERE SKUPINA = ? ORDER BY CISLO',
$skupina,
);
}
@ -102,4 +105,29 @@ final class ObvodFacade
return $skupiny[0] ?? '';
}
/**
* Hranice obvodů jako GeoJSON FeatureCollection.
* GEO v databázi je GeoJSON geometrie (Polygon nebo MultiPolygon).
*/
public function getGeoJson(): string
{
$radky = $this->database->fetchAll(
"SELECT CISLO, GEO
FROM OBVOD
WHERE GEO IS NOT NULL AND LTRIM(RTRIM(GEO)) <> ''
ORDER BY CISLO",
);
$features = [];
foreach ($radky as $radek) {
$features[] = '{"type":"Feature","properties":{"number":'
. (int) $radek->CISLO
. '},"geometry":'
. $radek->GEO
. '}';
}
return '{"type":"FeatureCollection","features":[' . implode(',', $features) . ']}';
}
}

View File

@ -5,14 +5,13 @@ declare(strict_types=1);
namespace App\Presentation\Home;
use App\Core\Konstanty;
use App\Model\ObvodFacade;
use Nette;
use Nette\Utils\Json;
use Nette\Utils\FileSystem;
final class HomePresenter extends Nette\Application\UI\Presenter
{
public function __construct(
private ObvodFacade $obvody,
) {
}
@ -24,28 +23,11 @@ final class HomePresenter extends Nette\Application\UI\Presenter
}
}
public function renderDefault(int $obvod = null): void
public function renderDefault(): void
{
try {
// načti seznam obvodů:
$content = FileSystem::read('../www/data/geo-obvody.geojson');
// dekóduj geo-json:
$geoObvody = Json::decode($content, true);
} catch (Nette\Utils\JsonException $e) {
// Ošetření výjimky
}
if (empty($geoObvody))
return;
$obvody = array();
foreach ($geoObvody["features"] as $o) {
$obvody[] = $o["properties"]["number"];
}
asort($obvody);
$this->template->obvody = $obvody;
bdump($geoObvody["features"]);
bdump($obvody);
$this->template->obvody = array_map(
static fn($obvod): int => (int) $obvod->CISLO,
$this->obvody->getObvody(),
);
}
}

View File

@ -5,8 +5,10 @@ declare(strict_types=1);
namespace App\Presentation\Mapa;
use App\Core\Konstanty;
use App\Model\ObvodFacade;
use App\Model\ZaznamFacade;
use Nette;
use Nette\Application\Responses\TextResponse;
use Nette\Utils\Json;
@ -20,6 +22,7 @@ final class MapaPresenter extends Nette\Application\UI\Presenter
public function __construct(
private ZaznamFacade $zaznam,
private ObvodFacade $obvod,
) {
}
@ -31,6 +34,12 @@ final class MapaPresenter extends Nette\Application\UI\Presenter
}
}
public function actionGeojson(): void
{
$this->getHttpResponse()->setContentType('application/json', 'utf-8');
$this->sendResponse(new TextResponse($this->obvod->getGeoJson()));
}
public function actionDefault(?int $obvod = null, ?string $filtr = null): void
{
if (!$this->getUser()->isLoggedIn() && $filtr !== null && $filtr !== 'obvody') {

View File

@ -2,7 +2,7 @@
{block content}
<div id="map"
data-geojson="{$basePath}/data/geo-obvody.geojson"
data-geojson="{link geojson}"
data-max="{$maxMesicu}"
data-prideleni="{$maxPrideleni}"
data-filtr="{$filtr}"

View File

@ -35,7 +35,7 @@
<h2>Náhled</h2>
<div id="obvod-nahled"
data-obvod="{$obvod->CISLO}"
data-geojson="{$basePath}/data/geo-obvody.geojson"></div>
data-geojson="{link :Mapa:geojson}"></div>
</section>
<section class="panel detail-zaznamy">

View File

@ -1,7 +1,7 @@
{block title}Stáří obvodů{/block}
{block content}
<div id="map" data-geojson="{$basePath}/data/geo-obvody.geojson" data-max="{$maxMesicu}"></div>
<div id="map" data-geojson="{link :Mapa:geojson}" data-max="{$maxMesicu}"></div>
<link rel="stylesheet" href="https://unpkg.com/leaflet.markercluster@1.5.3/dist/MarkerCluster.css" />
<script src="https://unpkg.com/leaflet.markercluster@1.5.3/dist/leaflet.markercluster.js"></script>
<script type="application/json" id="stari-data">{$stariJson|noescape}</script>

File diff suppressed because one or more lines are too long