mirror of
https://forge.chapril.org/tykayn/osm-commerces
synced 2025-10-09 17:02:46 +02:00
récupérer les objets avec email dans une zone par code postal
This commit is contained in:
parent
f5ab0c8205
commit
cb240dd169
8 changed files with 348 additions and 73 deletions
|
@ -3,28 +3,48 @@
|
|||
namespace App\Service;
|
||||
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class Motocultrice
|
||||
{
|
||||
private $overpassApiUrl = 'https://overpass-api.de/api/interpreter';
|
||||
|
||||
public function __construct(
|
||||
private HttpClientInterface $client
|
||||
private HttpClientInterface $client,
|
||||
private EntityManagerInterface $entityManager
|
||||
) {
|
||||
}
|
||||
|
||||
public function labourer(string $zone): array
|
||||
{
|
||||
if (!$zone) {
|
||||
throw new \InvalidArgumentException("La zone ne peut pas être vide");
|
||||
}
|
||||
|
||||
// Nettoyer et échapper la zone pour la requête
|
||||
$zone = addslashes(trim($zone));
|
||||
|
||||
$query = <<<QUERY
|
||||
[out:json][timeout:25];
|
||||
area["name"="{$zone}"]->.searchArea;
|
||||
area["postal_code"="{$zone}"]->.searchArea;
|
||||
(
|
||||
nwr["shop"](area.searchArea);
|
||||
// Recherche des commerces et services avec email
|
||||
nw["amenity"]["contact:email"](area.searchArea);
|
||||
nw["amenity"]["email"](area.searchArea);
|
||||
nw["shop"]["contact:email"](area.searchArea);
|
||||
nw["shop"]["email"](area.searchArea);
|
||||
nw["office"]["contact:email"](area.searchArea);
|
||||
nw["office"]["email"](area.searchArea);
|
||||
|
||||
// Recherche des commerces et services sans email pour référence
|
||||
nw["amenity"](area.searchArea);
|
||||
nw["shop"](area.searchArea);
|
||||
nw["office"](area.searchArea);
|
||||
);
|
||||
out body;
|
||||
>;
|
||||
out skel qt;
|
||||
QUERY;
|
||||
QUERY;
|
||||
|
||||
try {
|
||||
$response = $this->client->request('POST', $this->overpassApiUrl, [
|
||||
|
@ -33,15 +53,21 @@ class Motocultrice
|
|||
|
||||
$data = json_decode($response->getContent(), true);
|
||||
|
||||
$shops = [];
|
||||
$places = [];
|
||||
if (isset($data['elements'])) {
|
||||
foreach ($data['elements'] as $element) {
|
||||
if (isset($element['tags']['shop'])) {
|
||||
$shops[] = [
|
||||
if (isset($element['tags'])) {
|
||||
$email = $element['tags']['contact:email'] ?? $element['tags']['email'] ?? null;
|
||||
// On passe si pas d'email
|
||||
if (!$email) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$places[] = [
|
||||
'id' => $element['id'],
|
||||
'type' => $element['type'],
|
||||
'name' => $element['tags']['name'] ?? 'Sans nom',
|
||||
'shop_type' => $element['tags']['shop'],
|
||||
'name' => $element['tags']['name'] ?? '',
|
||||
'email' => $email,
|
||||
'lat' => $element['lat'] ?? null,
|
||||
'lon' => $element['lon'] ?? null,
|
||||
'tags' => $element['tags']
|
||||
|
@ -50,15 +76,15 @@ class Motocultrice
|
|||
}
|
||||
}
|
||||
|
||||
return $shops;
|
||||
return $places;
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("Erreur lors de la requête Overpass : " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function get_osm_object_data($osm_object_id = 12855459190)
|
||||
public function get_osm_object_data($osm_kind = 'node', $osm_object_id = 12855459190)
|
||||
{
|
||||
$object_id = "https://www.openstreetmap.org/api/0.6/node/".$osm_object_id;
|
||||
$object_id = "https://www.openstreetmap.org/api/0.6/".$osm_kind."/".$osm_object_id;
|
||||
|
||||
try {
|
||||
$response = $this->client->request('GET', $object_id);
|
||||
|
@ -71,21 +97,48 @@ class Motocultrice
|
|||
|
||||
// convertir les tags en clés et valeurs
|
||||
$osm_object_data['tags_converted'] = [];
|
||||
|
||||
foreach ($osm_object_data['node']['tag'] as $attribute) {
|
||||
$osm_object_data['node']['tags_converted'][$attribute['@attributes']['k']] = $attribute['@attributes']['v'];
|
||||
// Initialiser le tableau des tags convertis
|
||||
if (isset($osm_object_data['node'])) {
|
||||
$osm_object_data['node']['tags_converted'] = [];
|
||||
} elseif (isset($osm_object_data['way'])) {
|
||||
$osm_object_data['way']['tags_converted'] = [];
|
||||
}
|
||||
if(isset($osm_object_data['node'])){
|
||||
foreach ($osm_object_data['node']['tag'] as $attribute) {
|
||||
$osm_object_data['node']['tags_converted'][$attribute['@attributes']['k']] = $attribute['@attributes']['v'];
|
||||
}
|
||||
return $osm_object_data['node'];
|
||||
}
|
||||
if(isset($osm_object_data['way'])){
|
||||
foreach ($osm_object_data['way']['tag'] as $attribute) {
|
||||
$osm_object_data['way']['tags_converted'][$attribute['@attributes']['k']] = $attribute['@attributes']['v'];
|
||||
}
|
||||
return $osm_object_data['way'];
|
||||
}
|
||||
|
||||
return $osm_object_data['node'];
|
||||
return $osm_object_data;
|
||||
}
|
||||
|
||||
|
||||
public function semer(): string
|
||||
{
|
||||
return "La motocultrice sème les graines";
|
||||
}
|
||||
public function uuid_create() {
|
||||
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
// 32 bits for "time_low"
|
||||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
|
||||
|
||||
public function récolter(): string
|
||||
{
|
||||
return "La motocultrice récolte les cultures";
|
||||
}
|
||||
// 16 bits for "time_mid"
|
||||
mt_rand( 0, 0xffff ),
|
||||
|
||||
// 16 bits for "time_hi_and_version",
|
||||
// four most significant bits holds version number 4
|
||||
mt_rand( 0, 0x0fff ) | 0x4000,
|
||||
|
||||
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||||
// 8 bits for "clk_seq_low",
|
||||
// two most significant bits holds zero and one for variant DCE1.1
|
||||
mt_rand( 0, 0x3fff ) | 0x8000,
|
||||
|
||||
// 48 bits for "node"
|
||||
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue