forked from codingsince1985/geo-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new French API Gouv geo provider (codingsince1985#56)
- Loading branch information
1 parent
ee4a4e6
commit d62ccc6
Showing
4 changed files
with
320 additions
and
4 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
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
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,117 @@ | ||
// Package frenchapigouv is a geo-golang based French API Gouv geocode/reverse geocode client | ||
package frenchapigouv | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/codingsince1985/geo-golang" | ||
) | ||
|
||
type ( | ||
baseURL string | ||
geocodeResponse struct { | ||
Type string | ||
Version string | ||
Features []struct { | ||
Type string | ||
Geometry struct { | ||
Type string | ||
Coordinates []float64 | ||
} | ||
Properties struct { | ||
Label string | ||
Score float64 | ||
Housenumber string | ||
Citycode string | ||
Context string | ||
Postcode string | ||
Name string | ||
ID string | ||
Y float64 | ||
Importance float64 | ||
Type string | ||
City string | ||
X float64 | ||
Street string | ||
} | ||
} | ||
Attribution string | ||
Licence string | ||
Query string | ||
Limit int | ||
} | ||
context struct { | ||
state string | ||
county string | ||
countyCode string | ||
} | ||
) | ||
|
||
// Geocoder constructs FrenchApiGouv geocoder | ||
func Geocoder() geo.Geocoder { return GeocoderWithURL("https://api-adresse.data.gouv.fr/") } | ||
|
||
// GeocoderWithURL constructs French API Gouv geocoder using a custom installation of Nominatim | ||
func GeocoderWithURL(url string) geo.Geocoder { | ||
return geo.HTTPGeocoder{ | ||
EndpointBuilder: baseURL(url), | ||
ResponseParserFactory: func() geo.ResponseParser { return &geocodeResponse{} }, | ||
} | ||
} | ||
|
||
func (b baseURL) GeocodeURL(address string) string { | ||
return string(b) "search?limit=1&q=" address | ||
} | ||
|
||
func (b baseURL) ReverseGeocodeURL(l geo.Location) string { | ||
return string(b) "reverse?" fmt.Sprintf("lat=%f&lon=%f", l.Lat, l.Lng) | ||
} | ||
|
||
func (r *geocodeResponse) Location() (*geo.Location, error) { | ||
if len(r.Features) == 0 || len(r.Features[0].Geometry.Coordinates) < 2 { | ||
return nil, nil | ||
} | ||
p := r.Features[0].Geometry.Coordinates | ||
return &geo.Location{ | ||
Lat: p[1], | ||
Lng: p[0], | ||
}, nil | ||
} | ||
|
||
func (r *geocodeResponse) Address() (*geo.Address, error) { | ||
if len(r.Features) == 0 || r.Features[0].Properties.Label == "baninfo" { | ||
return nil, nil | ||
} | ||
p := r.Features[0].Properties | ||
c := r.parseContext() | ||
return &geo.Address{ | ||
FormattedAddress: strings.Join(strings.Fields(strings.TrimSpace(fmt.Sprintf("%s, %s, %s, %s, %s, %s, %s", p.Housenumber, p.Street, p.Postcode, p.City, c.county, c.state, "France"))), " "), | ||
HouseNumber: p.Housenumber, | ||
Street: p.Street, | ||
Postcode: p.Postcode, | ||
City: p.City, | ||
State: c.state, | ||
County: c.county, | ||
Country: "France", | ||
CountryCode: "FRA", | ||
}, nil | ||
} | ||
|
||
func (r *geocodeResponse) parseContext() *context { | ||
var c context | ||
if len(r.Features) > 0 { | ||
p := r.Features[0].Properties | ||
f := strings.Split(p.Context, ",") | ||
for i := range f { | ||
switch i { | ||
case 0: | ||
c.countyCode = f[i] | ||
case 1: | ||
c.county = f[i] | ||
case 2: | ||
c.state = f[i] | ||
} | ||
} | ||
} | ||
return &c | ||
} |
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,136 @@ | ||
package frenchapigouv_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/codingsince1985/geo-golang" | ||
"github.com/codingsince1985/geo-golang/frenchapigouv" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGeocode(t *testing.T) { | ||
ts := testServer(response1) | ||
defer ts.Close() | ||
|
||
geocoder := frenchapigouv.GeocoderWithURL(ts.URL "/") | ||
location, err := geocoder.Geocode("Champ de Mars, 5 Avenue Anatole France, 75007 Paris") | ||
assert.Nil(t, err) | ||
assert.Equal(t, geo.Location{Lat: 48.859831, Lng: 2.328123}, *location) | ||
} | ||
|
||
func TestGeocodeWithNoResult(t *testing.T) { | ||
ts := testServer(response2) | ||
defer ts.Close() | ||
|
||
geocoder := frenchapigouv.GeocoderWithURL(ts.URL "/") | ||
location, err := geocoder.Geocode("nowhere") | ||
assert.Nil(t, err) | ||
assert.Nil(t, location) | ||
} | ||
|
||
func TestReverseGeocode(t *testing.T) { | ||
ts := testServer(response1) | ||
defer ts.Close() | ||
|
||
geocoder := frenchapigouv.GeocoderWithURL(ts.URL "/") | ||
address, err := geocoder.ReverseGeocode(48.859831, 2.328123) | ||
assert.Nil(t, err) | ||
assert.True(t, strings.HasPrefix(address.FormattedAddress, "5, Quai Anatole France,")) | ||
} | ||
|
||
func TestReverseGeocodeWithNoResult(t *testing.T) { | ||
ts := testServer(response2) | ||
defer ts.Close() | ||
|
||
geocoder := frenchapigouv.GeocoderWithURL(ts.URL "/") | ||
addr, err := geocoder.ReverseGeocode(0, 0.34) | ||
assert.Nil(t, addr) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestReverseGeocodeWithNoResultByDefaultPoints(t *testing.T) { | ||
ts := testServer(response3) | ||
defer ts.Close() | ||
|
||
geocoder := frenchapigouv.GeocoderWithURL(ts.URL "/") | ||
addr, err := geocoder.ReverseGeocode(0, 0) | ||
assert.Nil(t, addr) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func testServer(response string) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { | ||
resp.Write([]byte(response)) | ||
})) | ||
} | ||
|
||
const ( | ||
response1 = `[ | ||
{ | ||
"type": "FeatureCollection", | ||
"version": "draft", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [2.328123, 48.859831] | ||
}, | ||
"properties": { | ||
"label": "5 Quai Anatole France 75007 Paris", | ||
"score": 0.4882581475128644, | ||
"housenumber": "5", | ||
"citycode": "75107", | ||
"context": "75, Paris, Île-de-France", | ||
"postcode": "75007", | ||
"name": "5 Quai Anatole France", | ||
"id": "ADRNIVX_0000000270768224", | ||
"y": 6862409.3, | ||
"importance": 0.2765, | ||
"type": "housenumber", | ||
"city": "Paris", | ||
"x": 650705.5, | ||
"street": "Quai Anatole France" | ||
} | ||
}], | ||
"attribution": "BAN", | ||
"licence": "ODbL 1.0", | ||
"query": "Champ de Mars, 5 Avenue Anatole France, 75007 Paris", | ||
"limit": 10 | ||
} | ||
]` | ||
response2 = `{ | ||
"type": "FeatureCollection", | ||
"version": "draft", | ||
"features": [], | ||
"attribution": "BAN", | ||
"licence": "ODbL 1.0", | ||
"limit": 1 | ||
}` | ||
response3 = `{ | ||
"type": "FeatureCollection", | ||
"version": "draft", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [0.0, 0.0] | ||
}, | ||
"properties": { | ||
"label": "baninfo", | ||
"score": 1.0, | ||
"name": "baninfo", | ||
"id": "db", | ||
"type": "info", | ||
"context": "20181125T223127", | ||
"distance": 0 | ||
} | ||
}], | ||
"attribution": "BAN", | ||
"licence": "ODbL 1.0", | ||
"limit": 1 | ||
}` | ||
) |