-
Notifications
You must be signed in to change notification settings - Fork 17
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
Add geocoding provider - Geocoder.xyz #57
Merged
eileenmcnaughton
merged 5 commits into
eileenmcnaughton:master
from
monishdeb:add_geocoder_xyz
Jan 21, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1b3ebf7
Adding new Geocoder.ca geocoder instance
bf2e22d
Updating managed file
7f1ab53
Add GecoderCa geocoder
monishdeb 9da6386
Fix parameters and format response to fetch coordinates or throw erro…
monishdeb 0256801
Add geocoding provider - Geocoder.xyz
monishdeb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
<?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.ca geocoder | ||
*/ | ||
class CRM_Utils_Geocode_GeocoderCa { | ||
|
||
/** | ||
* Server to retrieve the lat/long | ||
* | ||
* @var string | ||
*/ | ||
static protected $_server = 'geocoder.ca'; | ||
|
||
/** | ||
* Function that takes an address object and gets the latitude / longitude for this | ||
* address. Note that at a later stage, we could make this function also clean up | ||
* the address into a more valid format | ||
* | ||
* @param array $values | ||
* @param bool $stateName | ||
* | ||
* @return bool | ||
* true if we modified the address, false otherwise | ||
*/ | ||
public static function format(&$values, $stateName = FALSE) { | ||
// we need a valid country, else we ignore | ||
if (empty($values['country'])) { | ||
return FALSE; | ||
} | ||
|
||
$add = []; | ||
|
||
|
||
$city = $values['city'] ?? NULL; | ||
|
||
if (!empty($values['state_province']) || (!empty($values['state_province_id']) && $values['state_province_id'] != 'null')) { | ||
if (!empty($values['state_province_id'])) { | ||
$stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']) ?? ''; | ||
} | ||
else { | ||
if (!$stateName) { | ||
$stateProvince = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', | ||
$values['state_province'], | ||
'name', | ||
'abbreviation' | ||
) ?? ''; | ||
} | ||
else { | ||
$stateProvince = $values['state_province'] ?? ''; | ||
} | ||
} | ||
|
||
// dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632) | ||
if ($stateProvince != $city) { | ||
$add[] = 'state=' . urlencode(str_replace('', '+', $stateProvince)); | ||
} | ||
} | ||
|
||
foreach ([ | ||
'country' => 'country', | ||
'city' => 'city', | ||
'postal' => 'postal_code', | ||
'staddress' => 'street_address', | ||
] as $key => $addressFieldName) { | ||
if (!empty($values[$addressFieldName])) { | ||
$add[] = $key . '=' . urlencode(str_replace('', '+', $values[$addressFieldName])); | ||
} | ||
} | ||
|
||
$add = implode('&', $add); | ||
|
||
$coord = self::makeRequest($add); | ||
|
||
$values['geo_code_1'] = $coord['geo_code_1'] ?? 'null'; | ||
$values['geo_code_2'] = $coord['geo_code_2'] ?? 'null'; | ||
|
||
if (isset($coord['geo_code_error'])) { | ||
$values['geo_code_error'] = $coord['geo_code_error']; | ||
} | ||
|
||
$geoCoder = array_pop(explode('_', __CLASS__)); | ||
CRM_Utils_Hook::geocoderFormat($geoCoder, $values, $coord['request_xml']); | ||
|
||
return isset($coord['geo_code_1'], $coord['geo_code_2']); | ||
} | ||
|
||
/** | ||
* @param string $address | ||
* Plain text address | ||
* @return array | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
*/ | ||
public static function getCoordinates($address) { | ||
return self::makeRequest(urlencode($address)); | ||
} | ||
|
||
/** | ||
* @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.ca:', $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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
return [ | ||
[ | ||
'name' => 'geocoder_dot_ca', | ||
'entity' => 'Geocoder', | ||
'update' => 'never', | ||
'params' => [ | ||
'version' => 3, | ||
'name' => 'geocoder_dot_ca', | ||
'title' => 'Geocoder.ca', | ||
'class' => 'Geocoder.ca\Geocoder.ca', | ||
'api_key' => \Civi::settings()->get('geoAPIKey'), | ||
'url' => 'https://geocoder.ca', | ||
'is_active' => FALSE, | ||
'weight' => 8, | ||
], | ||
'help_text' => ts('api key required - sign up https://geocoder.ca/?register=1'), | ||
'user_editable_fields' => ['api_key', 'threshold_standdown'], | ||
'metadata' => [ | ||
'argument' => ['geocoder.api_key', 'geocoder.url'], | ||
'required_config_fields' => ['api_key'], | ||
'is_enabled_on_install' => FALSE, | ||
], | ||
] | ||
]; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be using \Civi::log()->debug() here I think. Non-blocking