Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REP-247 Switch geocoder #203

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- run: sed -i 's/HONEYPOT_DISABLE=.*$/HONEYPOT_DISABLE=TRUE/g' .env

# Add in private values.
- run: sudo sed -ie "s/GOOGLE_MAPS_API_KEY=zzz/GOOGLE_MAPS_API_KEY=$GOOGLE_MAPS_API_KEY/g" .env
- run: sudo sed -ie "s/MAPBOX_TOKEN=zzz/MAPBOX_TOKEN=$MAPBOX_TOKEN/g" .env
- run: sudo sed -ie "s/GRAVITYFORMS_KEY=zzz/GRAVITYFORMS_KEY=$GRAVITYFORMS_KEY/g" .env
- run: sudo sed -ie "s/GRAVITYFORMS_SECRET=zzz/GRAVITYFORMS_SECRET=$GRAVITYFORMS_SECRET/g" .env
- run: sudo sed -ie "s/GRAVITYFORMS_SUBMISSION_FORM_ID=zzz/GRAVITYFORMS_SUBMISSION_FORM_ID=$GRAVITYFORMS_SUBMISSION_FORM_ID/g" .env
Expand Down
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

PHANTOM_PATH=

GOOGLE_MAPS_API_KEY=zzz
MAPBOX_TOKEN=zzz
GA_TRACKING_ID=
MAP_SHARE_BASE_URL=https://map.restarters.test

Expand Down
7 changes: 3 additions & 4 deletions app/Console/Commands/GeocodeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,15 @@ public function handle(BusinessRepository $repository, Geocoder $geocoder)
if ($businesses) {
foreach ($businesses as $business) {
$count++;
$address = "{$business->getAddress()},{$business->getCity()},{$business->getLocalAreaName()},{$business->getPostcode()}";
$point = $geocoder->geocode($address);
$point = $geocoder->geocode($business->getPostcode());

if (!$point) {
// The address doesn't geocode. Log an error, with the expectation that the spreadsheet
// will then get fixed.
$this->error("{$business->getUid()} can't geocode $address");
$this->error("{$business->getUid()} can't geocode {$business->getPostcode()}");
$invalid++;
} else {
#$this->info("{$business->getUid()} geocoded $address to " . $point->getLatitude() . "," . $point->getLongitude());
#$this->info("{$business->getUid()} geocoded {$business->getPostcode()} to " . $point->getLatitude() . "," . $point->getLongitude());
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions app/Console/Commands/ImportBusinessesSpreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ public function handle(EntityManagerInterface $em, Geocoder $geocoder, BusinessR
$point = null;

try {
$point = $geocoder->geocode("$address,$city,$borough,$postcode");
$point = $geocoder->geocode($postcode);

if ($point && ($point->getLatitude() < $swlat || $point->getLatitude() > $nelat || $point->getLongitude() < $swlng || $point->getLongitude() > $nelng)) {
// The address doesn't geocode. Log an error, with the expectation that the spreadsheet
// will then get fixed.
$this->error("$name address $address geocodes to invalid lat/lng " . $point->getLatitude() . "," . $point->getLongitude());
$this->error("$name address $postcode geocodes to invalid lat/lng " . $point->getLatitude() . "," . $point->getLongitude());
$point = null;
}
} catch (\Exception $e) {
Expand Down
142 changes: 142 additions & 0 deletions app/Console/Commands/ReGeocode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use TheRestartProject\RepairDirectory\Application\QueryLanguage\Operators;
use TheRestartProject\RepairDirectory\Domain\Enums\PublishingStatus;
use TheRestartProject\RepairDirectory\Domain\Repositories\BusinessRepository;
use TheRestartProject\RepairDirectory\Domain\Services\Geocoder;

class ReGeocode extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'geocode:refresh';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Re-gecode all businesses ';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* From https://stackoverflow.com/questions/10053358/measuring-the-distance-between-two-coordinates-in-php with
* thanks.
*
* Calculates the great-circle distance between two points, with
* the Haversine formula.
* @param float $latitudeFrom Latitude of start point in [deg decimal]
* @param float $longitudeFrom Longitude of start point in [deg decimal]
* @param float $latitudeTo Latitude of target point in [deg decimal]
* @param float $longitudeTo Longitude of target point in [deg decimal]
* @param float $earthRadius Mean earth radius in [m]
* @return float Distance between points in [m] (same as earthRadius)
*/
function haversineGreatCircleDistance(
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);

$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;

$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
return $angle * $earthRadius;
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(BusinessRepository $repository, Geocoder $geocoder)
{
// We only want to check businesses that we would show.
$criteria = [
[
'field' => 'address',
'operator' => Operators::NOT_EQUAL,
'value' => ''
],
[
'field' => 'postcode',
'operator' => Operators::NOT_EQUAL,
'value' => ''
],
[
'field' => 'city',
'operator' => Operators::NOT_EQUAL,
'value' => ''
],
[
'field' => 'publishingStatus',
'operator' => Operators::EQUAL,
'value' => PublishingStatus::PUBLISHED
]
];

$businesses = $repository->findBy($criteria);
$count = 0;
$invalid = 0;

if ($businesses) {
foreach ($businesses as $business) {
$count++;
$address = $business->getPostcode();

if ($address == 'N23 6HL') {
$address = '13 Market Street, Ebbw Vale NP23 6HL';
} else if ($address == 'EC1 Y8QP' || $address == 'EC1Y8QP') {
$address = '195 Whitecross Street, London, EC1Y 8QP';
} else if ($address == 'W1T 1BZ') {
$address = '38 Tottenham Court Road, London, W1T 1BZ';
}

$point = $geocoder->geocode($address);

if (!$point) {
// The address doesn't geocode. Log an error, with the expectation that the spreadsheet
// will then get fixed.
$this->error("{$business->getUid()} can't geocode $address");
$invalid++;
} else {
$geolocation = $business->getGeolocation();

$dist = round($this->haversineGreatCircleDistance($geolocation->getLatitude(), $geolocation->getLongitude(), $point->getLatitude(), $point->getLongitude()));
#$this->info("{$business->getUid()} geocoded $address to " . $point->getLatitude() . "," . $point->getLongitude()) . " distance $dist";

if ($dist > 500) {
$this->error("{$business->getUid()} was {$geolocation->getLatitude()}, {$geolocation->getLongitude()} geocoded $address to {$point->getLatitude()},{$point->getLongitude()} distance {$dist}m");
$invalid++;
} else {
$business->setGeolocation($point);
}
}
}
} else {
$this->error("No businesses found");
}

$this->info("Checked $count, found $invalid invalid");
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"type": "project",
"require": {
"php": "^8.0",
"geocoder-php/mapbox-provider": "^1.4",
"guzzlehttp/guzzle": "^7.2",
"laracasts/utilities": "^3.2",
"laravel-doctrine/migrations": "^3.0",
Expand All @@ -27,7 +28,6 @@
"paquettg/php-html-parser": "^1.7",
"php-coveralls/php-coveralls": "^2.5",
"phpoffice/phpspreadsheet": "^1.19",
"skagarwal/google-places-api": "^2.0",
"symfony/http-client": "^6.2",
"symfony/mailgun-mailer": "^6.2",
"toin0u/geocoder-laravel": "^4.5"
Expand Down
128 changes: 60 additions & 68 deletions composer.lock

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

Loading