/** * Creates a new WeatherStation * * @param string $name * @param string $locality * @param string $city * @param string $country * @return \Javiacei\WeatherGuyBundle\Entity\WeatherStation */ public function create($name, $locality, $city, GeocodingLocation $geoLocation, $country = self::DEFAULT_COUNTRY) { $address = $locality . " " . $city . ", " . $country; $data = array('name' => $name, 'city' => $city, 'locality' => $locality, 'country' => $country, 'latitude' => $geoLocation->getLatitude(), 'longitude' => $geoLocation->getLongitude()); if ($geoLocation = $this->geocoding->getLocation($address)) { $data = array_merge($data, array('latitude' => $geoLocation->getLatitude(), 'longitude' => $geoLocation->getLongitude())); } $weatherStation = new WeatherStation(); $weatherStation->fromArray($data); return $weatherStation; }
public function findClosestStation(GeocodingLocation $location, $distance) { $rsm = new ResultSetMapping(); $rsm->addEntityResult('JaviaceiWeatherGuyBundle:WeatherStation', 'ws'); $rsm->addScalarResult('id', 'id'); $query = $this->getEntityManager()->createNativeQuery(' SELECT ws.id as id, SQRT( POW(69.1 * (ws.latitude - :latitude), 2) + POW(69.1 * (:longitude - ws.longitude) * COS(ws.latitude / 57.3), 2) ) AS distance FROM weather_guy_weather_station ws HAVING distance < :distance ORDER BY distance LIMIT 0,1; ', $rsm)->setParameters(array('latitude' => $location->getLatitude(), 'longitude' => $location->getLongitude(), 'distance' => $distance)); try { $result = $query->getSingleResult(); } catch (\Doctrine\ORM\NoResultException $e) { return null; } return $result; }