#Bundle\Resources\config\doctrine\Location.orm.yml Location: type: entity table: Location repositoryClass: LocationRepository id: id: type: integer nullable: false generator: strategy: AUTO fields: title: type: string length: 500 nullable: false area: type: polygon nullable: false
//Bundle\Entity\Location.php private $__area; const COORDS_SEPARATOR = '; '; const POLYGON_SEPARATOR = '[]'; public function get_Area($raw = false) { if ($raw) { return $this->__area; } $result = array(); if (is_null($this->getArea())) { return $result; } $rings = $this->getArea()->getRings(); $count_rings = count($rings) -1; foreach ($rings as $key => $line) { foreach ($line->getPoints() as $point) { $result[] = $point->getX() . self::COORDS_SEPARATOR . $point->getY(); } if($count_rings != $key) { $result[] = Task::POLYGON_SEPARATOR; } } return $result; } public function set_Area($__area) { $this->__area = $__area; return $this; }
$formMapper .... ->add('__area','sonata_type_native_collection',[ 'options'=>['label'=>'(GpsX;GpsY)'], 'allow_add'=>true, 'allow_delete'=>true, 'label' => ' ' ]) ....
public function update($object) { $object = $this->prepareTask($object); return parent::update($object); } public function create($object) { $object = $this->prepareTask($object); return parent::create($object); } protected function prepareTask($object) { $res = array(); if (count($object->get_Area(true))) { $flb = $this->getConfigurationPool()->getContainer()->get('session')->getFlashBag(); $i = 0; foreach ($object->get_Area(true) as $point) { if((string) $point === Task::POLYGON_SEPARATOR) { if(count($res[$i]) > 2) { $this->fillLastPoint($res[$i]); } $i++; continue; } if (!preg_match('/[\d]+[.]{0,1}[\d]{0,}' . preg_quote(Task::COORDS_SEPARATOR) . '[\d]+[.]{0,1}[\d]{0,}/', (string) $point)) { $flb->add( 'danger', ' ' . (string) $point . '. ( .) ";"' ); return $object; } list($x, $y) = explode(Task::COORDS_SEPARATOR, $point); $res[$i][] = new Point($x, $y); } foreach ($res as $key => $ring) { if(count($ring) < 3) { $flb->add( 'danger', " №" . $key + 1 . " , .. 3 , " . count($ring) . '.' ); unset($res[$key]); continue; } } if(count($res)) { end($res); $key = key($res); $this->fillLastPoint($res[$key]); $object->setArea(new Polygon($res)); } } return $object; } private function fillLastPoint(&$arr) { if ($arr[0]->getX() !== end($arr)->getX() || $arr[0]->getY() !== end($arr)->getY()) { $arr[] = $arr[0]; } }
$query = $this->getEntityManager() ->createQuery( 'SELECT t FROM Bundle:Location t where ST_CONTAINS(t.area, Point(:x,:y)) = 1' ) ->setParameter('x', $x) ->setParameter('y', $y) ; $query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'CrEOF\Spatial\ORM\Query\GeometryWalker'); $result = $query->getResult();
Source: https://habr.com/ru/post/240493/
All Articles