Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0158ea0

Browse files
Robert Harmwilldurand
Robert Harm
authored andcommitted
Add OGDViennaAustriaProvider
1 parent 1e6ead6 commit 0158ea0

File tree

3 files changed

+221
-1
lines changed

3 files changed

+221
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This repository hosts Geocoder extra features that do not belong to the core
55
but can be nonetheless interesting to share with other developers. It mostly
66
contains **providers**.
77

8-
98
Installation
109
------------
1110

@@ -30,6 +29,10 @@ Usage
3029

3130
Please, read the [Geocoder's documentation](http://geocoder-php.org/Geocoder/).
3231

32+
### Providers
33+
34+
* [OGD Vienna](https://open.wien.at/site/datensatz/?id=c223b93a-2634-4f06-ac73-8709b9e16888) as Address-Based geocoding provider (exclusively in Vienna / Austria).
35+
3336

3437
Contributing
3538
------------
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Provider;
12+
13+
use Geocoder\Exception\UnsupportedException;
14+
use Geocoder\Exception\NoResultException;
15+
16+
/**
17+
* Data source: City of Vienna, http://data.wien.gv.at
18+
*
19+
* @author Robert Harm <www.harm.co.at>
20+
*/
21+
class OGDViennaAustriaProvider extends AbstractProvider implements ProviderInterface
22+
{
23+
/**
24+
* @var string
25+
*/
26+
const ENDPOINT_URL = 'http://data.wien.gv.at/daten/OGDAddressService.svc/GetAddressInfo?CRS=EPSG:4326&Address=%s';
27+
28+
/**
29+
* {@inheritDoc}
30+
*/
31+
public function getGeocodedData($address)
32+
{
33+
// This API doesn't handle IPs
34+
if (filter_var($address, FILTER_VALIDATE_IP)) {
35+
throw new UnsupportedException('The OGDViennaAustriaProvider does not support IP addresses.');
36+
}
37+
38+
$query = sprintf(self::ENDPOINT_URL, urlencode($address));
39+
40+
return $this->executeQuery($query);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function getReversedData(array $coordinates)
47+
{
48+
throw new UnsupportedException('The OGDViennaAustriaProvider is not able to do reverse geocoding.');
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
public function getName()
55+
{
56+
return 'ogd_vienna_austria';
57+
}
58+
59+
/**
60+
* @param string $query
61+
*
62+
* @return array
63+
*/
64+
protected function executeQuery($query)
65+
{
66+
$content = $this->getAdapter()->getContent($query);
67+
68+
if (!$data) {
69+
throw new NoResultException(sprintf('Could not execute query %s', $query));
70+
}
71+
72+
$data = json_decode($content, true);
73+
74+
if (empty($data) || false === $data) {
75+
throw new NoResultException(sprintf('Could not execute query %s', $query));
76+
}
77+
78+
$bounds = array(
79+
'south' => isset($data['features'][0]['bbox'][0]) ? $data['features'][0]['bbox'][0] : null,
80+
'west' => isset($data['features'][0]['bbox'][1]) ? $data['features'][0]['bbox'][1] : null,
81+
'north' => isset($data['features'][0]['bbox'][2]) ? $data['features'][0]['bbox'][3] : null,
82+
'east' => isset($data['features'][0]['bbox'][3]) ? $data['features'][0]['bbox'][2] : null,
83+
);
84+
85+
return array(array_merge($this->getDefaults(), array(
86+
'longitude' => isset($data['features'][0]['geometry']['coordinates'][0]) ? $data['features'][0]['geometry']['coordinates'][0] : null,
87+
'latitude' => isset($data['features'][0]['geometry']['coordinates'][1]) ? $data['features'][0]['geometry']['coordinates'][1] : null,
88+
'bounds' => $bounds,
89+
'streetNumber' => NULL, //info: zB 1/a - not available yet
90+
'streetName' => isset($data['features'][0]['properties']['Adresse']) ? $data['features'][0]['properties']['Adresse'] : null,
91+
'cityDistrict' => NULL, //info: z.B. Donaustadt - not available yet
92+
'city' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'Vienna' : null,
93+
'zipcode' => isset($data['features'][0]['properties']['PLZ']) ? $data['features'][0]['properties']['PLZ'] : null,
94+
'county' => NULL, //info: ??? - not available yet
95+
'countyCode' => isset($data['features'][0]['properties']['Zaehlbezirk']) ? $data['features'][0]['properties']['Zaehlbezirk'] : null,
96+
'region' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'VIENNA' : null,
97+
'regionCode' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'VIENNA' : null,
98+
'country' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'Austria' : null,
99+
'countryCode' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'AT' : null,
100+
'timezone' => isset($data['features'][0]['geometry']['coordinates'][0]) ? 'Europe/Vienna' : null,
101+
)));
102+
}
103+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Geocoder\Tests\Provider;
4+
5+
use Geocoder\Tests\TestCase;
6+
use Geocoder\Provider\OGDViennaAustriaProvider;
7+
8+
/**
9+
* @author Robert Harm <www.harm.co.at>
10+
* Data source: City of Vienna, http://data.wien.gv.at
11+
*/
12+
class OGDViennaAustriaProviderTest extends TestCase
13+
{
14+
public function testGetName()
15+
{
16+
$provider = new OGDViennaAustriaProvider($this->getMockAdapter($this->never()));
17+
$this->assertEquals('ogd_vienna_austria', $provider->getName());
18+
}
19+
20+
/**
21+
* @expectedException \Geocoder\Exception\NoResultException
22+
* @expectedExceptionMessage Could not execute query http://data.wien.gv.at/daten/OGDAddressService.svc/GetAddressInfo?CRS=EPSG:4326&Address=Stephansplatz
23+
*/
24+
public function testGetGeocodedDataWithAddress()
25+
{
26+
$provider = new OGDViennaAustriaProvider($this->getMockAdapter());
27+
$provider->getGeocodedData('Stephansplatz');
28+
}
29+
30+
/**
31+
* @expectedException \Geocoder\Exception\NoResultException
32+
* @expectedExceptionMessage Could not execute query http://data.wien.gv.at/daten/OGDAddressService.svc/GetAddressInfo?CRS=EPSG:4326&Address=yyyyyyy
33+
*/
34+
public function testGetGeocodedDataWithWrongAddress()
35+
{
36+
$provider = new OGDViennaAustriaProvider($this->getAdapter());
37+
$provider->getGeocodedData('yyyyyyy');
38+
}
39+
40+
public function testGetGeocodedDataWithRealAddress()
41+
{
42+
$provider = new OGDViennaAustriaProvider($this->getAdapter());
43+
$result = $provider->getGeocodedData('Stephansplatz');
44+
45+
$this->assertInternalType('array', $result);
46+
$this->assertCount(1, $result);
47+
48+
$result = $result[0];
49+
$this->assertInternalType('array', $result);
50+
$this->assertEquals(48.208583576583, $result['latitude'], '', 0.0001);
51+
$this->assertEquals(16.373089928434, $result['longitude'], '', 0.0001);
52+
$this->assertNull($result['bounds']);
53+
//$this->assertNull($result['streetNumber']);
54+
$this->assertNull($result['streetName']);
55+
$this->assertNull($result['zipcode']);
56+
$this->assertNull($result['city']);
57+
//$this->assertNull($result['cityDistrict']);
58+
$this->assertNull($result['region']);
59+
$this->assertNull($result['regionCode']);
60+
//$this->assertNull($result['country']);
61+
$this->assertNull($result['countryCode']);
62+
$this->assertNull($result['timezone']);
63+
}
64+
65+
/**
66+
* @expectedException \Geocoder\Exception\UnsupportedException
67+
* @expectedExceptionMessage The OGDViennaAustriaProvider does not support IP addresses.
68+
*/
69+
public function testGetGeocodedDataWithLocalhostIPv4()
70+
{
71+
$provider = new OGDViennaAustriaProvider($this->getMockAdapter($this->never()));
72+
$provider->getGeocodedData('127.0.0.1');
73+
}
74+
75+
/**
76+
* @expectedException \Geocoder\Exception\UnsupportedException
77+
* @expectedExceptionMessage The OGDViennaAustriaProvider does not support IP addresses.
78+
*/
79+
public function testGetGeocodedDataWithLocalhostIPv6()
80+
{
81+
$provider = new OGDViennaAustriaProvider($this->getMockAdapter($this->never()));
82+
$provider->getGeocodedData('::1');
83+
}
84+
85+
/**
86+
* @expectedException \Geocoder\Exception\UnsupportedException
87+
* @expectedExceptionMessage The OGDViennaAustriaProvider does not support IP addresses.
88+
*/
89+
public function testGetGeocodedDataWithIPv4()
90+
{
91+
$provider = new OGDViennaAustriaProvider($this->getAdapter());
92+
$provider->getGeocodedData('74.200.247.59');
93+
}
94+
95+
/**
96+
* @expectedException \Geocoder\Exception\UnsupportedException
97+
* @expectedExceptionMessage The OGDViennaAustriaProvider does not support IP addresses.
98+
*/
99+
public function testGetGeocodedDataWithIPv6()
100+
{
101+
$provider = new OGDViennaAustriaProvider($this->getAdapter());
102+
$provider->getGeocodedData('::ffff:74.200.247.59');
103+
}
104+
105+
/**
106+
* @expectedException \Geocoder\Exception\UnsupportedException
107+
* @expectedExceptionMessage The OGDViennaAustriaProvider is not able to do reverse geocoding.
108+
*/
109+
public function testGetReverseData()
110+
{
111+
$provider = new OGDViennaAustriaProvider($this->getMockAdapter($this->never()));
112+
$provider->getReversedData(array(1, 2));
113+
}
114+
}

0 commit comments

Comments
 (0)