Skip to content

Commit

Permalink
wip zones autoaffectation #3286
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeneuvrier committed Dec 3, 2024
1 parent 5fadf87 commit 07efa4c
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 10 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"knplabs/knp-snappy-bundle": "^1.9",
"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-bundle": "^3.0",
"longitude-one/wkt-parser": "^3.0",
"mjaschen/phpgeo": "^5.0",
"nelmio/cors-bundle": "^2.2",
"phpdocumentor/reflection-docblock": "^5.3",
"phpoffice/phpspreadsheet": "^2.2",
Expand Down
159 changes: 155 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Form/AutoAffectationRuleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
])
->add('inseeToInclude', TextType::class, [
'label' => 'Code insee à inclure',
'label' => 'Périmètre géographique à inclure',
'attr' => [
'class' => 'fr-input',
],
'required' => true,
'help' => 'Valeurs possibles : "all", "partner_list" ou une liste de codes insee séparés par des virgules.',
'help' => 'Valeurs possibles : "all", "partner_list" (codes insee, et zones) ou une liste de codes insee séparés par des virgules.',
'help_attr' => [
'class' => 'fr-hint-text',
],
Expand Down
91 changes: 90 additions & 1 deletion src/Specification/Affectation/CodeInseeSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

use App\Entity\Partner;
use App\Entity\Signalement;
use App\Entity\Zone;
use App\Specification\Context\PartnerSignalementContext;
use App\Specification\Context\SpecificationContextInterface;
use App\Specification\SpecificationInterface;
use Location\Coordinate;
use Location\Polygon;
use LongitudeOne\Geo\WKT\Parser;

class CodeInseeSpecification implements SpecificationInterface
{
Expand Down Expand Up @@ -45,8 +49,34 @@ public function isSatisfiedBy(SpecificationContextInterface $context): bool
$result = true;
break;
case 'partner_list':
$result = !empty($partner->getInsee())
$isZoneOK = true;
$isZoneExcludedOK = true;
$isInseeOK = !empty($partner->getInsee())
&& \in_array($signalement->getInseeOccupant(), $partner->getInsee());
// var_dump($isInseeOK);
if ($isInseeOK) {
if (!empty($partner->getZones()) && $partner->getZones()->count() > 0) {
$isZoneOK = false;
foreach ($partner->getZones() as $zone) {
$isZoneOK = $this->isSignalementInZone($signalement, $zone);
if ($isZoneOK) {
break;
}
}
}
// var_dump($isZoneOK);
if ($isZoneOK && !empty($partner->getExcludedZones()) && $partner->getExcludedZones()->count() > 0) {
foreach ($partner->getExcludedZones() as $zoneExcluded) {
$isZoneExcludedOK = !$this->isSignalementInZone($signalement, $zoneExcluded);
if (!$isZoneExcludedOK) {
break;
}
}
}
// var_dump($isZoneExcludedOK);
}

$result = $isInseeOK && $isZoneOK && $isZoneExcludedOK;
break;
default:
$result = !empty($this->inseeToInclude)
Expand All @@ -56,4 +86,63 @@ public function isSatisfiedBy(SpecificationContextInterface $context): bool

return $result;
}

private function isSignalementInZone(Signalement $signalement, Zone $zone): bool
{
$parser = new Parser($zone->getArea());
$zoneArea = $parser->parse();
$signalementCoordinate = new Coordinate($signalement->getGeoloc()['lat'], $signalement->getGeoloc()['lng']);
// var_dump($signalement->getGeoloc());
if ('POLYGON' === $zoneArea['type']) {
$polygon = $this->buildPolygon($zoneArea['value']);
// var_dump($polygon->contains($signalementCoordinate));
if ($polygon->contains($signalementCoordinate)) {
return true;
}
}
if ('MULTIPOLYGON' === $zoneArea['type']) {
foreach ($zoneArea['value'] as $value) {
$polygon = $this->buildPolygon($value);
if ($polygon->contains($signalementCoordinate)) {
return true;
}
}
}
if ('GEOMETRYCOLLECTION' === $zoneArea['type']) {
foreach ($zoneArea['value'] as $value) {
if ('POLYGON' === $value['type']) {
$polygon = $this->buildPolygon($value['value']);
if ($polygon->contains($signalementCoordinate)) {
return true;
}
}
if ('MULTIPOLYGON' === $value['type']) {
foreach ($value['value'] as $val) {
$polygon = $this->buildPolygon($val);
if ($polygon->contains($signalementCoordinate)) {
return true;
}
}
}
}
}

return false;
}

private function buildPolygon(array $points): Polygon
{
$geofence = new Polygon();
// var_dump($points);
if (1 === \count($points) && \count($points[0]) > 2) {
$points = $points[0];
}
// var_dump($points);
foreach ($points as $point) {
$geofence->addPoint(new Coordinate($point[1], $point[0]));
}

// var_dump($geofence->getPoints());
return $geofence;
}
}
Loading

0 comments on commit 07efa4c

Please sign in to comment.