Skip to content

Commit 4ad0d8d

Browse files
Krystof BernatKrystof Bernat
Krystof Bernat
authored and
Krystof Bernat
committed
Merge branch 'azure-municipality' of https://github.com/kryber/Geocoder into azure-municipality
2 parents 974e4a7 + 4e62d3e commit 4ad0d8d

10 files changed

+104
-27
lines changed

src/Provider/Ipstack/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 0.5.0
6+
7+
### Added
8+
9+
- Add support for PHP 8.3 and 8.4
10+
- Add support for region name (as admin level)
11+
12+
### Removed
13+
14+
- Drop support for PHP 7.4
15+
516
## 0.4.0
617

718
### Added

src/Provider/Ipstack/Ipstack.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7373
$url = sprintf('%s&language=%s', $url, $query->getLocale());
7474
}
7575

76+
return $this->executeQuery($url);
77+
}
78+
79+
public function reverseQuery(ReverseQuery $query): Collection
80+
{
81+
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
82+
}
83+
84+
public function getName(): string
85+
{
86+
return 'ipstack';
87+
}
88+
89+
private function executeQuery(string $url): AddressCollection
90+
{
7691
$body = $this->getUrlContents($url);
7792
$data = json_decode($body, true);
7893

@@ -99,26 +114,17 @@ public function geocodeQuery(GeocodeQuery $query): Collection
99114
return new AddressCollection([]);
100115
}
101116

102-
$locations[] = Address::createFromArray([
103-
'providedBy' => $this->getName(),
104-
'latitude' => $data['latitude'] ?: null,
105-
'longitude' => $data['longitude'] ?: null,
106-
'locality' => $data['city'] ?: null,
107-
'postalCode' => $data['zip'] ?: null,
108-
'country' => $data['country_name'] ?: null,
109-
'countryCode' => $data['country_code'] ?: null,
117+
return new AddressCollection([
118+
Address::createFromArray([
119+
'providedBy' => $this->getName(),
120+
'latitude' => $data['latitude'] ?? null,
121+
'longitude' => $data['longitude'] ?? null,
122+
'locality' => $data['city'] ?? null,
123+
'postalCode' => $data['zip'] ?? null,
124+
'country' => $data['country_name'] ?? null,
125+
'adminLevels' => isset($data['region_name']) ? [['name' => $data['region_name'], 'level' => 1]] : [],
126+
'countryCode' => $data['country_code'] ?? null,
127+
]),
110128
]);
111-
112-
return new AddressCollection($locations);
113-
}
114-
115-
public function reverseQuery(ReverseQuery $query): Collection
116-
{
117-
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
118-
}
119-
120-
public function getName(): string
121-
{
122-
return 'ipstack';
123129
}
124130
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
s:562:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"Am\u00e9rique du Nord","country_code":"US","country_name":"\u00c9tats-Unis","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
1+
s:1120:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"Am\u00e9rique du Nord","country_code":"US","country_name":"\u00c9tats-Unis","region_code":"NJ","region_name":"New Jersey","city":"Jersey City","zip":"07311","latitude":40.724,"longitude":-74.059,"msa":"35620","dma":"501","radius":null,"ip_routing_type":"fixed","connection_type":"tx","location":{"geoname_id":5099836,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"https://assets.ipstack.com/flags/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false},"time_zone":{"id":"America/New_York","current_time":"2025-02-16T06:13:52-05:00","gmt_offset":-18000,"code":"EST","is_daylight_saving":false},"currency":{"code":"USD","name":"US Dollar","plural":"US dollars","symbol":"$","symbol_native":"$"},"connection":{"asn":19994,"isp":"Rackspace Hosting","sld":null,"tld":null,"carrier":"rackspace hosting","home":false,"organization_type":"Internet Hosting Services","isic_code":"J6311","naics_code":"518210"}}";
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
s:552:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
1+
s:1110:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":"NJ","region_name":"New Jersey","city":"Jersey City","zip":"07311","latitude":40.724,"longitude":-74.059,"msa":"35620","dma":"501","radius":null,"ip_routing_type":"fixed","connection_type":"tx","location":{"geoname_id":5099836,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"https://assets.ipstack.com/flags/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false},"time_zone":{"id":"America/New_York","current_time":"2025-02-16T06:03:06-05:00","gmt_offset":-18000,"code":"EST","is_daylight_saving":false},"currency":{"code":"USD","name":"US Dollar","plural":"US dollars","symbol":"$","symbol_native":"$"},"connection":{"asn":19994,"isp":"Rackspace Hosting","sld":null,"tld":null,"carrier":"rackspace hosting","home":false,"organization_type":"Internet Hosting Services","isic_code":"J6311","naics_code":"518210"}}";

src/Provider/Ipstack/Tests/IpstackTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testGeocodeWithLocalhostIPv4(): void
6464
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
6565
$this->assertEquals('localhost', $result->getLocality());
6666
$this->assertEquals('localhost', $result->getCountry()->getName());
67+
$this->assertEmpty($result->getAdminLevels());
6768
}
6869

6970
public function testGeocodeWithLocalhostIPv6(): void
@@ -79,6 +80,7 @@ public function testGeocodeWithLocalhostIPv6(): void
7980
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
8081
$this->assertEquals('localhost', $result->getLocality());
8182
$this->assertEquals('localhost', $result->getCountry()->getName());
83+
$this->assertEmpty($result->getAdminLevels());
8284
}
8385

8486
public function testGeocodeWithRealIPv4(): void
@@ -92,10 +94,12 @@ public function testGeocodeWithRealIPv4(): void
9294
/** @var Location $result */
9395
$result = $results->first();
9496
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
95-
$this->assertEqualsWithDelta(37.751, $result->getCoordinates()->getLatitude(), 0.01);
96-
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
97+
$this->assertEqualsWithDelta(40.724, $result->getCoordinates()->getLatitude(), 0.01);
98+
$this->assertEqualsWithDelta(-74.059, $result->getCoordinates()->getLongitude(), 0.01);
9799
$this->assertEquals('United States', $result->getCountry()->getName());
98100
$this->assertEquals('US', $result->getCountry()->getCode());
101+
$this->assertCount(1, $result->getAdminLevels());
102+
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
99103
}
100104

101105
public function testGeocodeWithRealIPv4InFrench(): void
@@ -109,10 +113,12 @@ public function testGeocodeWithRealIPv4InFrench(): void
109113
/** @var Location $result */
110114
$result = $results->first();
111115
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
112-
$this->assertEqualsWithDelta(37.751, $result->getCoordinates()->getLatitude(), 0.01);
113-
$this->assertEqualsWithDelta(-97.822, $result->getCoordinates()->getLongitude(), 0.01);
116+
$this->assertEqualsWithDelta(40.724, $result->getCoordinates()->getLatitude(), 0.01);
117+
$this->assertEqualsWithDelta(-74.059, $result->getCoordinates()->getLongitude(), 0.01);
114118
$this->assertEquals('États-Unis', $result->getCountry()->getName());
115119
$this->assertEquals('US', $result->getCountry()->getCode());
120+
$this->assertCount(1, $result->getAdminLevels());
121+
$this->assertEquals('New Jersey', $result->getAdminLevels()->get(1)->getName());
116122
}
117123

118124
public function testReverse(): void

src/Provider/Photon/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5+
## 0.8.0
6+
7+
### Added
8+
9+
- Add support for `layer` and `radius` parameters
10+
- Improve locality extraction
11+
512
## 0.7.0
613

714
### Added

src/Provider/Photon/Model/PhotonAddress.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ final class PhotonAddress extends Address
5454
*/
5555
private $district;
5656

57+
/**
58+
* @var string|null
59+
*/
60+
private $type;
61+
62+
public function getLocality(): ?string
63+
{
64+
$locality = parent::getLocality();
65+
if (null === $locality && 'city' === $this->type && null !== $this->name) {
66+
$locality = $this->name;
67+
}
68+
69+
return $locality;
70+
}
71+
5772
/**
5873
* @return string|null
5974
*/
@@ -173,4 +188,17 @@ public function withDistrict(?string $district = null): self
173188

174189
return $new;
175190
}
191+
192+
public function getType(): ?string
193+
{
194+
return $this->type;
195+
}
196+
197+
public function withType(?string $type = null): self
198+
{
199+
$new = clone $this;
200+
$new->type = $type;
201+
202+
return $new;
203+
}
176204
}

src/Provider/Photon/Photon.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
6868
.'/api?'
6969
.http_build_query([
7070
'q' => $address,
71+
'layer' => $query->getData('layer'),
7172
'limit' => $query->getLimit(),
7273
'lang' => $query->getLocale(),
7374
]);
@@ -102,6 +103,8 @@ public function reverseQuery(ReverseQuery $query): Collection
102103
.http_build_query([
103104
'lat' => $latitude,
104105
'lon' => $longitude,
106+
'layer' => $query->getData('layer'),
107+
'radius' => $query->getData('radius'),
105108
'limit' => $query->getLimit(),
106109
'lang' => $query->getLocale(),
107110
]);
@@ -157,7 +160,8 @@ private function featureToAddress(\stdClass $feature): Location
157160
->withName($properties->name ?? null)
158161
->withState($properties->state ?? null)
159162
->withCounty($properties->county ?? null)
160-
->withDistrict($properties->district ?? null);
163+
->withDistrict($properties->district ?? null)
164+
->withType($properties->type ?? null);
161165

162166
return $address;
163167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:334:"{"features":[{"geometry":{"coordinates":[13.3989367,52.510885],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":62422,"extent":[13.088345,52.6755087,13.7611609,52.3382448],"country":"Deutschland","osm_key":"place","countrycode":"DE","osm_value":"city","name":"Berlin","type":"city"}}],"type":"FeatureCollection"}";

src/Provider/Photon/Tests/PhotonTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,18 @@ public function testReverseQueryWithOsmTagFilter(): void
178178
$this->assertEquals('pharmacy', $result->getOSMTag()->value);
179179
}
180180
}
181+
182+
public function testReverseQueryWithLayerCityAndRadiusFilter(): void
183+
{
184+
$provider = Photon::withKomootServer($this->getHttpClient());
185+
$reverseQuery = ReverseQuery::fromCoordinates(52.51644, 13.38890)
186+
->withData('layer', 'city')
187+
->withData('radius', 20)
188+
->withLimit(1);
189+
$result = $provider->reverseQuery($reverseQuery)->first();
190+
191+
$this->assertInstanceOf(PhotonAddress::class, $result);
192+
$this->assertEquals('city', $result->getType());
193+
$this->assertEquals('Berlin', $result->getLocality());
194+
}
181195
}

0 commit comments

Comments
 (0)