public static function batchGeocode($max_records_to_geocode) { $count = 0; $cached_count = 0; $hcps = DB::table(static::$table)->where('geo_cache_id', null)->take(5000)->get(); $hcps_not_geocoded = []; foreach ($hcps as $hcp) { Log::debug("HCP ID:" . $hcp->hcp_id); list($geo, $cached, $status) = GeoCache::findByAddressOrCreateWithNewnessIndicator(Hcp::getGeocodeAddress($hcp)); if ($status == 'OK' || $status == 'LOCALLY_CACHED') { HcpImport::update($hcp->hcp_id, ['lat' => $geo->lat, 'lng' => $geo->lng, 'geo_cache_id' => $geo->id, 'geo_address' => $geo->address]); Log::debug("Geocode OK:", ['hcp_id' => $hcp->hcp_id, 'status' => $status]); } else { Log::error("Geocode Error:", ['hcp_id' => $hcp->hcp_id, 'address' => Hcp::getGeocodeAddress($hcp), 'status' => $status]); $hcps_not_geocoded[] = $hcp; } if (!$cached) { Log::debug("{$count} new geocodes"); $count++; } else { Log::debug("{$cached_count} cached geocodes"); $cached_count++; } if ($count > $max_records_to_geocode) { break; } } return $hcps_not_geocoded; }
/** * @param $hcp_id * @param Request $request * @return \Illuminate\View\View */ public function force_geocode_hcp($hcp_id, Request $request) { $hcp = HcpImport::get($hcp_id); $hcp_address = HcpImport::getGeocodeAddress($hcp); $geo = null; $geo_address = $request->input('address'); if ($hcp) { $resp = Geocode::geocode($geo_address); if ($resp['status'] == 'OK') { $id = GeoCache::create(new GeoCache(['address' => $hcp_address, 'geocoded_address' => $geo_address, 'lat' => $resp['results'][0]['geometry']['location']['lat'], 'lng' => $resp['results'][0]['geometry']['location']['lng'], 'formatted_address' => $resp['results'][0]['formatted_address'], 'location_type' => $resp['results'][0]['geometry']['location_type'], 'partial_match' => Geocode::getPartialMatchFromResponse($resp)])); $geo = GeoCache::get($id); HcpImport::update($hcp_id, ['lat' => $geo->lat, 'lng' => $geo->lng, 'geo_cache_id' => $geo->id, 'geo_address' => $geo->address]); $hcp = HcpImport::get($hcp_id); $message = "Geolocating succeeded"; } else { $message = "Geolocating failed"; } } else { $message = "HCP ID:{$hcp_id} not found"; } return view("data.geolocate", ['hcp' => $hcp, 'geo' => $geo, 'message' => $message, 'api_key' => Geocode::$google_api_key]); }