Add GeoImage endpoints

This commit is contained in:
Nicolò P 2025-06-19 16:21:16 +02:00
parent e98cbc585b
commit 72af052651
3 changed files with 94 additions and 1 deletions

View File

@ -10,7 +10,7 @@ use Doctrine\ORM\EntityManagerInterface;
class GeoImageController extends AbstractController class GeoImageController extends AbstractController
{ {
#[Route('/geoimage', name: 'app_geo_image')] #[Route('/geoimages', name: 'app_geo_images')]
public function index(EntityManagerInterface $em): JsonResponse public function index(EntityManagerInterface $em): JsonResponse
{ {
$repo = $em->getRepository(GeoImage::class); $repo = $em->getRepository(GeoImage::class);
@ -18,4 +18,19 @@ class GeoImageController extends AbstractController
return $this->json($geoImages); return $this->json($geoImages);
} }
#[Route('/geoimages/menu', name: 'app_geo_image_menu')]
public function menu(EntityManagerInterface $em): JsonResponse
{
$repo = $em->getRepository(GeoImage::class);
$geoImages = $repo->findLabelsAndIds();
return $this->json($geoImages);
}
#[Route('/geoimages/{id}', name: 'app_geo_image_record')]
public function record(GeoImage $geoImage): JsonResponse
{
return $this->json($geoImage);
}
} }

View File

@ -22,6 +22,9 @@ class GeoImage implements JsonSerializable
#[ORM\Column(type: Types::JSON, name: 'poligono')] #[ORM\Column(type: Types::JSON, name: 'poligono')]
private ?string $polygon = null; private ?string $polygon = null;
#[ORM\Column(length: 60, name: 'etichetta')]
private ?string $label = null;
public function getId(): ?int public function getId(): ?int
{ {
return $this->id; return $this->id;
@ -58,11 +61,25 @@ class GeoImage implements JsonSerializable
return $this; return $this;
} }
public function getLabel(): ?string
{
return $this->polygon;
}
public function setLabel(string $label): static
{
$this->label = $label;
return $this;
}
public function jsonSerialize(): mixed public function jsonSerialize(): mixed
{ {
return [ return [
'id' => $this->id,
'filename' => $this->filename, 'filename' => $this->filename,
'polygon' => $this->polygon, 'polygon' => $this->polygon,
'label' => $this->label,
]; ];
} }
} }

View File

@ -4,7 +4,9 @@ namespace App\Repository;
use App\Entity\GeoImage; use App\Entity\GeoImage;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\LockMode;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
use Override;
/** /**
* @extends ServiceEntityRepository<GeoImage> * @extends ServiceEntityRepository<GeoImage>
@ -16,6 +18,34 @@ class GeoImageRepository extends ServiceEntityRepository
parent::__construct($registry, GeoImage::class); parent::__construct($registry, GeoImage::class);
} }
#[Override]
public function find(mixed $id, LockMode|int|null $lockMode = null, ?int $lockVersion = null): ?object
{
$conn = $this->getEntityManager()->getConnection();
$sql = "SELECT
id,
filename,
etichetta as label,
ST_AsGeoJSON(poligono) as polygon
FROM geo_immagine_catasto
WHERE id = :id
";
$result = $conn->executeQuery($sql, ['id' => $id])
->fetchAssociative();
if (! $result) return null;
$geoImage = new GeoImage();
$geoImage->setId($result['id']);
$geoImage->setFilename($result['filename']);
$geoImage->setPolygon($result['polygon']);
$geoImage->setLabel($result['label']);
return $geoImage;
}
/** /**
* Returns all geoimages with associated geometry (polygon) * Returns all geoimages with associated geometry (polygon)
* as GeoJSON * as GeoJSON
@ -28,8 +58,10 @@ class GeoImageRepository extends ServiceEntityRepository
$sql = "SELECT $sql = "SELECT
id, id,
filename, filename,
etichetta as label,
ST_AsGeoJSON(poligono) as polygon ST_AsGeoJSON(poligono) as polygon
FROM geo_immagine_catasto FROM geo_immagine_catasto
ORDER BY etichetta ASC
"; ";
$results = $conn->executeQuery($sql) $results = $conn->executeQuery($sql)
@ -41,6 +73,35 @@ class GeoImageRepository extends ServiceEntityRepository
$geoimage->setId($result['id']); $geoimage->setId($result['id']);
$geoimage->setFilename($result['filename']); $geoimage->setFilename($result['filename']);
$geoimage->setPolygon($result['polygon']); $geoimage->setPolygon($result['polygon']);
$geoimage->setLabel($result['label']);
$entities[] = $geoimage;
}
return $entities;
}
/**
* Returns all labels and ids for menu
* @return GeoImage[]
*/
public function findLabelsAndIds()
{
$conn = $this->getEntityManager()->getConnection();
$sql = "SELECT
id,
etichetta as label
FROM geo_immagine_catasto
ORDER BY etichetta ASC
";
$results = $conn->executeQuery($sql)
->fetchAllAssociative();
$entities = [];
foreach ($results as $result) {
$geoimage = new GeoImage();
$geoimage->setId($result['id']);
$geoimage->setLabel($result['label']);
$entities[] = $geoimage; $entities[] = $geoimage;
} }