/**
  *  issue: when the neighborhood /Region/Neighborhood is pulled for neighborhoods that
  *  have user polygons drawn but no neighborhood borders drawn, they fail
  */
 public function testNeighborhoodsWithNoBorders()
 {
     $this->initDb();
     $region = new \Whathood\Entity\Region(array('name' => 'TestRegion' . rand(0, 99999999)));
     $this->m()->regionMapper()->save($region);
     $whathoodUser = new \Whathood\Entity\WhathoodUser(array('ipAddress' => '0.0.0.0'));
     $neighborhood = new Neighborhood(array('id' => 1, 'name' => "MyTest" . $this->getTestName(), 'region' => $region));
     $userPolygon = new UserPolygon(array('polygon' => Polygon::build(array(new LineString(array(new Point(30, 40), new Point(30, 50), new Point(40, 50), new Point(30, 40)))), 4326), 'neighborhood' => $neighborhood, 'region' => $region, 'whathoodUser' => $whathoodUser));
     $this->m()->userPolygonMapper()->save($userPolygon);
     $this->dispatch(sprintf("/%s/%s", $region->getName(), $neighborhood->getName()));
     $this->assertResponseStatusCode(200);
     $this->assertControllerName('whathood\\controller\\neighborhood');
 }
Пример #2
0
 /**
  * take the user polygons, run a point election, and save the points in the db
  **/
 public function buildAndSaveHeatmapPoints($user_polygons, Neighborhood $n, $grid_resolution)
 {
     $timer = Timer::start("heatmap_builder");
     $electionCollection = $this->m()->pointElectionMapper()->getCollection($user_polygons, $n->getId(), $grid_resolution);
     $heatmap_points = $electionCollection->heatMapPointsByNeighborhood($n);
     if (!empty($heatmap_points)) {
         $this->m()->heatMapPoint()->deleteByNeighborhood($n);
         $this->m()->heatMapPoint()->savePoints($heatmap_points);
         $this->m()->heatMapPoint()->detach($heatmap_points);
         $this->infoLog(sprintf("saved %s heatmap points from %s points elapsed=%s", count($heatmap_points), count($electionCollection->getPointElections()), $timer->elapsedReadableString()));
     } else {
         $this->infoLog("\t\tno heatmap_points generated to save");
     }
     return $heatmap_points;
 }
Пример #3
0
 /**
  * 
  * @param \Whathood\Entity\Neighborhood $testNeighborhood
  * @param \Whathood\Entity\Region $testRegion
  * @param type $sideLength
  * @return null|\Whathood\Entity\HeatMap
  * @throws \InvalidArgumentException
  */
 public function getLatestHeatMap(Neighborhood $testNeighborhood, Region $testRegion, $sideLength)
 {
     if (empty($sideLength)) {
         throw new \InvalidArgumentException('sidLength may not be null');
     }
     $testNeighborhoodName = $testNeighborhood->getName();
     $testRegionName = $testRegion->getName();
     $neighborhoodPolygons = $this->neighborhoodPolygonMapper()->getNeighborhoodByName($testNeighborhoodName, $testRegionName);
     $testPoints = $this->getBoundarySquare($neighborhoodPolygons)->getTestPoints($sideLength);
     $heatMapPoints = $this->getHeatMapPoints($testPoints, $testNeighborhoodName);
     if (count($heatMapPoints) > 0) {
         return new HeatMap(array('neighborhood' => $testNeighborhood, 'region' => $testRegion, 'heatMapPoints' => $heatMapPoints, 'neighborhoodPolygons' => new ArrayCollection($neighborhoodPolygons)));
     } else {
         return null;
     }
 }
Пример #4
0
 public function save(NeighborhoodEntity $neighborhood)
 {
     if ($neighborhood->getName() == null) {
         throw new \InvalidArgumentException("neighborhood.name may" . " not be null");
     }
     /*
      * does the region already exist, if it does reset it in the neighborhood
      */
     try {
         $region = $this->regionMapper()->getRegionByName($neighborhood->getRegion()->getName());
         $neighborhood->setRegion($region);
     } catch (\Doctrine\ORM\NoResultException $e) {
     }
     if ($neighborhood->getDateTimeAdded() == null) {
         $neighborhood->setDateTimeAdded(date("Y-m-d H:i:s"));
     }
     $this->em->persist($neighborhood);
     $this->em->flush($neighborhood);
 }
Пример #5
0
 public function backupDb($file)
 {
     $neighborhoodMapper = $this->sm->get('Whathood\\Mapper\\Neighborhood');
     $neighborhoods = $neighborhoodMapper->fetchAll();
     foreach ($neighborhoods as $n) {
         print $n->getName() . "\n";
     }
     $json = \Whathood\Entity\Neighborhood::neighborhoodsToJson($neighborhoods);
     file_put_contents($file, $json);
 }
Пример #6
0
 public function getPolygon()
 {
     $filename = $this->file;
     if (!file_exists($filename)) {
         throw new \InvalidArgumentException("{$filename} does not exist");
     }
     if ($this->getRegion() == null) {
         throw new \InvalidArgumentException("region must be defined");
     }
     if ($this->getWhathoodUser() == null) {
         throw new \InvalidArguementException("user must be defined");
     }
     $jsonStr = file_get_contents($filename);
     $whathoodUser = $this->getWhathoodUser();
     $json = \Zend\Json\Json::decode($jsonStr);
     $neighborhoods = array();
     $counter = 1;
     foreach ($json->kml->Document->Folder->Placemark as $n) {
         $name = $n->ExtendedData->SchemaData->SimpleData[2]->{'#text'};
         $coordinateString = $n->Polygon->outerBoundaryIs->LinearRing->coordinates;
         $array = explode(' ', $coordinateString);
         $neighborhood = new Neighborhood();
         $neighborhood->setName($name);
         $neighborhood->setRegion($this->getRegion());
         $points = array();
         foreach ($array as $point) {
             list($lng, $lat) = explode(",", $point);
             $points[] = new Point($lng, $lat);
         }
         $lineString = new LineString($points);
         $lineString->close();
         $polygon = new Polygon(array($lineString));
         $neighborhoodPolygon = new UserPolygon();
         $neighborhoodPolygon->setWhathoodUser($whathoodUser);
         $neighborhoodPolygon->setAuthority($this->isAuthority());
         $neighborhoodPolygon->setNeighborhood($neighborhood);
         $neighborhoodPolygon->setPolygon($polygon);
         $neighborhoodPolygon->setRegion($this->getRegion());
         $neighborhoods[] = $neighborhoodPolygon;
     }
     return $neighborhoods;
 }
Пример #7
0
 public function testValidData()
 {
     $this->initDb();
     /**
      * set up test data
      **/
     $region = new \Whathood\Entity\Region(array('name' => "Region_" . $this->getTestName()));
     $whathoodUser = new \Whathood\Entity\WhathoodUser(array('ipAddress' => '0.0.0.0'));
     $neighborhood = new Neighborhood(array('id' => 1, 'name' => "MyTest" . $this->getTestName(), 'region' => $region));
     $userPolygon = new UserPolygon(array('polygon' => Polygon::build(array(new LineString(array(new Point(30, 40), new Point(30, 50), new Point(40, 50), new Point(30, 40)))), 4326), 'neighborhood' => $neighborhood, 'region' => $region, 'whathoodUser' => $whathoodUser));
     $this->m()->userPolygonMapper()->save($userPolygon);
     /*
      * now run the job
      *
      **/
     $factory = new \Whathood\Factory\HeatmapBuilderJobFactory();
     $job = $factory->createService(\WhathoodTest\Bootstrap::getServiceManager());
     $job->setGridResolution(1);
     if (count($neighborhood->getUserPolygons())) {
         die("should have had more than 0 user polygons");
     }
     $job->setContent(array('neighborhood_id' => $userPolygon->getNeighborhood()->getId()));
     $job->execute();
 }
Пример #8
0
 public function createByNeighborhoodQuery(Neighborhood $neighborhood)
 {
     $dql = "SELECT up FROM Whathood\\Entity\\UserPolygon up\n            WHERE up.neighborhood = :neighborhood ORDER BY up.id ASC";
     return $this->em->createQuery($dql)->setParameter(':neighborhood', $neighborhood->getId());
 }
 /**
  * @description returns all the NeighborhoodBoundary objects associated with the given neighborhood
  *
  * @return array - an array of NeighborhoodBoundary entities
  */
 public function byNeighborhood(Neighborhood $neighborhood)
 {
     $dql = "SELECT np FROM Whathood\\Entity\\NeighborhoodBoundary np\n            WHERE np.neighborhood = :neighborhood";
     $query = $this->em->createQuery($dql)->setParameter(':neighborhood', $neighborhood->getId());
     return $query->getResult();
 }
Пример #10
0
 public function addAction()
 {
     if (!$this->getRequest()->isPost()) {
         return new ViewModel();
     }
     $neighborhood_name = $this->getRequest()->getPost('neighborhood_name');
     $region_name = $this->getRequest()->getPost('region_name');
     $polygon_array = $this->getRequest()->getPost('polygon_json');
     if (empty($polygon_array)) {
         throw new \InvalidArgumentException("polygon_json may not be empty");
     }
     if (empty($neighborhood_name)) {
         throw new \InvalidArgumentException("neighborhood_name may not be empty");
     }
     $neighborhood = new Neighborhood(array('name' => $neighborhood_name));
     $region = new Region(array('name' => $region_name));
     $polygon = \Whathood\Polygon::buildPolygonFromGeoJsonArray($polygon_array, $srid = 4326);
     $whathoodUser = $this->getWhathoodUser();
     $userPolygon = new UserPolygon(array('neighborhood' => $neighborhood, 'polygon' => $polygon, 'region' => $region, 'whathoodUser' => $whathoodUser));
     $this->logger()->info(sprintf("saving user-polygon id=%s neighborhood=%s region=%s ip-address=%s", $userPolygon->getId(), $neighborhood->getName(), $region->getName(), $whathoodUser->getIpAddress()));
     $this->userPolygonMapper()->save($userPolygon);
     $this->logger()->info("user polygon added id(" . $userPolygon->getId() . ")");
     return new JsonModel(array('status' => 'success', 'user_polygon_id' => $userPolygon->getId()));
 }
Пример #11
0
 public function __invoke(Neighborhood $n)
 {
     $url = $this->root . '?id=' . $n->getId();
     return $url;
 }
 public function getIteratorClass()
 {
     $this->__load();
     return parent::getIteratorClass();
 }
Пример #13
0
 public function isWinner(Neighborhood $neighborhood)
 {
     foreach ($this->getWinningCandidates() as $cn) {
         if ($neighborhood->getId() == $cn->getNeighborhood()->getId()) {
             return true;
         }
     }
     return false;
 }
Пример #14
0
 public function deleteByNeighborhood(Neighborhood $neighborhood)
 {
     $sql = "DELETE FROM Whathood\\Entity\\HeatMapPoint hmp WHERE hmp.neighborhood = :neighborhood_id";
     $this->em->createQuery($sql)->setParameter(':neighborhood_id', $neighborhood->getId())->execute();
 }