Add spherical photo

This commit is contained in:
Nicolò P 2024-11-24 21:43:52 +01:00
parent 444955337d
commit e5d5f826ce
5 changed files with 220 additions and 3 deletions

View File

@ -38,8 +38,6 @@ class SiteController extends AbstractController
$site->setLat((float) $coords['lat']);
$site->setLng((float) $coords['lng']);
return $this->json(
$site,
);
return $this->json($site);
}
}

View File

@ -0,0 +1,49 @@
<?php
namespace App\Controller;
use App\Entity\SphericalPhoto;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
class SphericalPhotoController extends AbstractController
{
#[Route('/spherical', name: 'app_spherical')]
public function index(
EntityManagerInterface $em
): JsonResponse
{
$repo = $em->getRepository(SphericalPhoto::class);
$photos = $repo->findAll();
foreach ($photos as $key => $photo) {
$coords = $repo->coordinates($photo->getId());
$photo->setLat($coords['lat']);
$photo->setLng($coords['lng']);
$photos[$key] = $photo;
}
return $this->json($photos);
}
#[Route('/spherical/by/gis/{gisId<[\w_]+>}', name: 'app_spherical_by_gis')]
public function byGis(
string $gisId,
EntityManagerInterface $em
): JsonResponse
{
$repo = $em->getRepository(SphericalPhoto::class);
$photos = $repo->findBySiteGis($gisId);
foreach ($photos as $key => $photo) {
$coords = $repo->coordinates($photo->getId());
$photo->setLat($coords['lat']);
$photo->setLng($coords['lng']);
$photos[$key] = $photo;
}
return $this->json($photos);
}
}

View File

@ -371,6 +371,7 @@ class Site implements JsonSerializable
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'gisId' => $this->gisId,
'place' => $this->place,
'genericPlace' => $this->genericPlace,

View File

@ -0,0 +1,97 @@
<?php
namespace App\Entity;
use App\Repository\SphericalPhotoRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: SphericalPhotoRepository::class)]
#[ORM\Table(name: 'foto_sferica')]
class SphericalPhoto implements \JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 100)]
private ?string $filename = null;
#[ORM\Column(type: Types::BIGINT, nullable: true, name: 'sito')]
#[ORM\ManyToOne(targetEntity: Site::class)]
private ?string $site = null;
private ?float $lat = null;
private ?float $lng = null;
public function getId(): ?int
{
return $this->id;
}
public function setId(string $id): static
{
$this->id = $id;
return $this;
}
public function getFilename(): ?string
{
return $this->filename;
}
public function setFilename(string $filename): static
{
$this->filename = $filename;
return $this;
}
public function getSite(): ?string
{
return $this->site;
}
public function setSite(?string $site): static
{
$this->site = $site;
return $this;
}
public function getLat(): ?float
{
return $this->lat;
}
public function setLat(?float $lat): static
{
$this->lat = $lat;
return $this;
}
public function getLng(): ?float
{
return $this->lng;
}
public function setLng(?float $lng): static
{
$this->lng = $lng;
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'filename' => $this->filename,
'coordinates' => [$this->lat, $this->lng],
];
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Repository;
use App\Entity\SphericalPhoto;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<SphericalPhoto>
*/
class SphericalPhotoRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, SphericalPhoto::class);
}
public function coordinates(int $id)
{
$conn = $this->getEntityManager()->getConnection();
$sql = '
SELECT
ST_X(ST_AsText(coordinate)) as lng,
ST_Y(ST_AsText(coordinate)) as lat
FROM
foto_sferica
WHERE id = :id
';
return $conn->executeQuery($sql, ['id' => $id])
->fetchAssociative();
}
/**
* @return SphericalPhoto[] Returns an array of SphericalPhoto objects
*/
public function findBySite(int $siteId): array
{
return $this->createQueryBuilder('s')
->andWhere('s.site = :siteId')
->setParameter('siteId', $siteId)
->orderBy('s.id', 'ASC')
->getQuery()
->getResult()
;
}
/**
* @return SphericalPhoto[] Returns an array of SphericalPhoto objects
*/
public function findBySiteGis(string $gisId): array
{
$rsm = new ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('App\Entity\SphericalPhoto', 'sf');
$query = $this->getEntityManager()->createNativeQuery(
'SELECT
sf.id,
filename
FROM foto_sferica sf
JOIN sito
ON sito.id = sf.sito
WHERE sito.gis_id = :gisId
',
$rsm
);
$query->setParameter('gisId', $gisId);
return $query->getResult();
}
}