up edit form

This commit is contained in:
Tykayn 2025-05-26 16:22:01 +02:00 committed by tykayn
parent fd72a1cedc
commit b1965abe06
6 changed files with 325 additions and 49 deletions

View file

@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface;
class Motocultrice
{
private $overpassApiUrl = 'https://overpass-api.de/api/interpreter';
private $osmApiUrl = 'https://www.openstreetmap.org/api/0.6';
public function __construct(
private HttpClientInterface $client,
@ -141,4 +142,98 @@ QUERY;
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
public function formatOsmDataForSubmit(array $data): array
{
// Garder uniquement les tags essentiels
$essentialTags = [
'name',
'opening_hours',
'phone',
'contact:email',
'contact:phone',
'website',
'contact:website',
'wheelchair',
'addr:housenumber',
'addr:street',
'addr:city',
'addr:postcode',
'amenity',
'shop',
'tourism',
'source',
'ref:FR:SIRET'
];
$formattedData = [
'node' => [
'@attributes' => [
'id' => $data['@attributes']['id'],
'version' => $data['@attributes']['version'],
'changeset' => $data['@attributes']['changeset'],
'lat' => $data['@attributes']['lat'],
'lon' => $data['@attributes']['lon']
],
'tag' => []
]
];
// Filtrer et ajouter uniquement les tags essentiels
if (isset($data['tag'])) {
foreach ($data['tag'] as $tag) {
if (in_array($tag['@attributes']['k'], $essentialTags)) {
$formattedData['node']['tag'][] = $tag;
}
}
}
return $formattedData;
}
private function arrayToXml(array $data): string
{
$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><osm></osm>');
if (isset($data['node'])) {
$node = $xml->addChild('node');
foreach ($data['node']['@attributes'] as $key => $value) {
$node->addAttribute($key, $value);
}
if (isset($data['node']['tag'])) {
foreach ($data['node']['tag'] as $tag) {
$tagElement = $node->addChild('tag');
$tagElement->addAttribute('k', $tag['@attributes']['k']);
$tagElement->addAttribute('v', $tag['@attributes']['v']);
}
}
}
return $xml->asXML();
}
public function submitOsmData(array $data): void
{
$formattedData = $this->formatOsmDataForSubmit($data);
$xmlData = $this->arrayToXml($formattedData);
try {
$response = $this->client->request('PUT',
"{$this->osmApiUrl}/node/{$data['@attributes']['id']}",
[
'body' => $xmlData,
'headers' => [
'Content-Type' => 'application/xml; charset=utf-8'
]
]
);
if ($response->getStatusCode() !== 200) {
throw new \Exception("Erreur lors de la soumission des données : " . $response->getContent());
}
} catch (\Exception $e) {
throw new \Exception("Erreur lors de la communication avec l'API OSM : " . $e->getMessage());
}
}
}