Filter by Distance #16

Closed
opened 2021-06-27 22:45:20 +02:00 by thisfro · 3 comments
Owner

To filter by distance, the following is needed:

  • Each offer should generate the coordinates when created from PLZ (to reduce calls to the Post API and thus save processing power and bandwith)
  • To filter, a form input with a PLZ is needed or maybe one could use the devices location (privacy?)
  • A function needs to calculate the distance to every offer and return a list from all within the given radius
  • Finally the SQL query needs to be built
To filter by distance, the following is needed: - [x] Each offer should generate the coordinates when created from PLZ (to reduce calls to the Post API and thus save processing power and bandwith) - [x] To filter, a form input with a PLZ is needed or maybe one could use the devices location (privacy?) - [x] A function needs to calculate the distance to every offer and return a list from all within the given radius - [ ] Finally the SQL query needs to be built
thisfro added this to the Basic functionality milestone 2021-06-27 22:45:20 +02:00
thisfro added the
Idea
Note
labels 2021-06-27 22:45:20 +02:00
Author
Owner

The SQL query should look like that in the end

SELECT * from offering WHERE ST_DISTANCE_SPHERE(POINT(`lat`, `lng`), POINT($lat,$lng)) < $distance;
The SQL query should look like that in the end ```sql SELECT * from offering WHERE ST_DISTANCE_SPHERE(POINT(`lat`, `lng`), POINT($lat,$lng)) < $distance; ```
Author
Owner

Since Doctrine doesn't support ST_DISTANCE, a simple solution could be:

#[Route('/offers/distanceexample', name: 'distance_example')]
public function distanceExample(Environment $twig, OfferingRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
{
    $filterDistance = 10;
    $filterPlz = 8041;

    $allOffers = $offerRepository->findAll();
    $filteredOffers = [];

    $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz);

	foreach ($allOffers as $offer) {
        $offerCoordinate = $offer->getCoordinate();
        
        $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate);

        if ($distance < $filterDistance) {
            array_push($filteredOffers, $offer);
        }
    }
    
    return new Response($twig->render('offer/index.html.twig', [
        'offers' => $filteredOffers,
    ]));
}

However, this might be related to performance losses.

This whole funciton should be refactored into a service, e.g. DistanceFilterHelper.

Since Doctrine doesn't support `ST_DISTANCE`, a simple solution could be: ```php #[Route('/offers/distanceexample', name: 'distance_example')] public function distanceExample(Environment $twig, OfferingRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response { $filterDistance = 10; $filterPlz = 8041; $allOffers = $offerRepository->findAll(); $filteredOffers = []; $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); foreach ($allOffers as $offer) { $offerCoordinate = $offer->getCoordinate(); $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate); if ($distance < $filterDistance) { array_push($filteredOffers, $offer); } } return new Response($twig->render('offer/index.html.twig', [ 'offers' => $filteredOffers, ])); } ``` However, this might be related to performance losses. This whole funciton should be refactored into a service, e.g. `DistanceFilterHelper`.
Author
Owner

Implemented by e52753446b

Implemented by e52753446b
This repo is archived. You cannot comment on issues.
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference: thisfro/pflaenz.li-Symfony#16
No description provided.