-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from sebalis/provider-de-postalcode
provider for German postal codes (replaces #36)
- Loading branch information
Showing
4 changed files
with
8,407 additions
and
0 deletions.
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,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file works with the Geocoder package. | ||
* | ||
* @author Detlev Sieber / civiservice.de <[email protected]> | ||
*/ | ||
|
||
namespace Geocoder\Provider; | ||
|
||
use Civi; | ||
use Geocoder\Collection; | ||
use Geocoder\Exception\UnsupportedOperation; | ||
use Geocoder\Model\AddressBuilder; | ||
use Geocoder\Model\AddressCollection; | ||
use Geocoder\Query\GeocodeQuery; | ||
use Geocoder\Query\ReverseQuery; | ||
use Geocoder\Provider\AbstractProvider; | ||
use Geocoder\Provider\Provider; | ||
use Geocoder\Exception\CollectionIsEmpty; | ||
|
||
final class DEPlzProvider extends AbstractProvider implements Provider | ||
{ | ||
/** | ||
* @param HttpClient $dummy (unused) | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function __construct($dummy) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function geocodeQuery(GeocodeQuery $query): Collection { | ||
|
||
if (!isset(Civi::$statics[__CLASS__])) { | ||
Civi::$statics[__CLASS__] = (bool) (\CRM_Core_DAO::singleValueQuery("SHOW TABLES LIKE 'civicrm_geocoder_plzde_dataset'")); | ||
} | ||
if (!Civi::$statics[__CLASS__]) { | ||
// We don't have the data table available. | ||
throw new CollectionIsEmpty(); | ||
} | ||
|
||
$postcodeNoSpace = preg_replace('/ +/', '', $query->getText()); | ||
|
||
$sql = " | ||
SELECT * | ||
FROM civicrm_geocoder_plzde_dataset | ||
WHERE postcode_no_space = %1"; | ||
|
||
$result = \CRM_Core_DAO::executeQuery( | ||
$sql, | ||
[1 => [$postcodeNoSpace, 'String']] | ||
); | ||
|
||
$builder = new AddressBuilder($this->getName()); | ||
if ($result->fetch()) { | ||
$builder->setCoordinates($result->latitude, $result->longitude); | ||
$builder->setLocality($result->city); | ||
$builder->setPostalCode($result->postcode); | ||
return new AddressCollection([$builder->build()]); | ||
} | ||
|
||
throw new CollectionIsEmpty(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reverseQuery(ReverseQuery $query): Collection | ||
{ | ||
throw new UnsupportedOperation('The data table provider is not able to do reverse geocoding yet.'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName(): string { | ||
return 'plzde'; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
return [ | ||
[ | ||
'name' => 'de_plz', | ||
'entity' => 'Geocoder', | ||
'params' => [ | ||
'version' => 3, | ||
'name' => 'de_plz', | ||
'title' => 'DE PLZ based geocoding', | ||
'class' => 'DEPlzProvider', | ||
'valid_countries' => ['DE'], | ||
'required_fields' => ['postal_code'], | ||
'retained_response_fields' => '["geo_code_1","geo_code_2", "postal_code"]', | ||
'datafill_response_fields' => ["city"], | ||
], | ||
'metadata' => [ | ||
'is_enabled_on_install' => FALSE, | ||
] | ||
] | ||
]; | ||
|
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
Oops, something went wrong.