Skip to content

Commit

Permalink
Merge pull request #57 from monishdeb/add_geocoder_xyz
Browse files Browse the repository at this point in the history
Add geocoding provider - Geocoder.xyz
  • Loading branch information
eileenmcnaughton authored Jan 21, 2024
2 parents da8ca22 + 0256801 commit 4806b23
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CRM/Utils/Geocode/GeocoderCa.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public static function format(&$values, $stateName = FALSE) {
$values['geo_code_error'] = $coord['geo_code_error'];
}

CRM_Utils_Hook::geocoderFormat('GeocoderCa', $values, $coord['request_xml']);
$geoCoder = array_pop(explode('_', __CLASS__));
CRM_Utils_Hook::geocoderFormat($geoCoder, $values, $coord['request_xml']);

return isset($coord['geo_code_1'], $coord['geo_code_2']);
}
Expand Down
71 changes: 71 additions & 0 deletions CRM/Utils/Geocode/GeocoderXyz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/

/**
* Class that uses Geocoder.xyz geocoder
*/
class CRM_Utils_Geocode_GeocoderXyz extends CRM_Utils_Geocode_GeocoderCa {

/**
* Server to retrieve the lat/long
*
* @var string
*/
static protected $_server = 'geocoder.xyz';

/**
* @param string $add
* Url-encoded address
*
* @return array
* An array of values with the following possible keys:
* geo_code_error: String error message
* geo_code_1: Float latitude
* geo_code_2: Float longitude
* request_xml: SimpleXMLElement parsed xml from geocoding API
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private static function makeRequest($add) {
$coords = [];
$config = CRM_Core_Config::singleton();
if (!empty($config->geoAPIKey)) {
$add .= '&geoit=XML&auth=' . urlencode($config->geoAPIKey);
}

$query = 'https://' . self::$_server . '?' . $add;
$client = new GuzzleHttp\Client();
$request = $client->request('GET', $query, ['timeout' => \Civi::settings()->get('http_timeout')]);
$string = $request->getBody();

libxml_use_internal_errors(TRUE);
$xml = @simplexml_load_string($string);
$coords['request_xml'] = $xml;
if (isset($xml->error)) {
$string = sprintf('Error %s: %s', $xml->error->code, $xml->error->description);
CRM_Core_Error::debug_var('Geocoding failed. Message from Geocoder.xyz:', $string);
$coords['geo_code_error'] = $string;
}
if (isset($xml->latt) && isset($xml->longt)) {
$coords['geo_code_1'] = (float) $xml->latt;
$coords['geo_code_2'] = (float) $xml->longt;
}

return $coords;
}

}

0 comments on commit 4806b23

Please sign in to comment.