Reputation: 11628
I need to find out the name of the city given a pair of lat
,lon
coordinates
I'm using Nominatim API https://wiki.openstreetmap.org/wiki/Nominatim
This is an example query:
https://nominatim.openstreetmap.org/reverse?format=xml&lat=40&lon=30&zoom=18&addressdetails=1
And it works from the Browser, however it fails from this C# code:
ServicePointManager.Expect100Continue = true;
ServicePointManager.DefaultConnectionLimit = 9999;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
var url = "https://nominatim.openstreetmap.org/reverse?format=xml&lat=40&lon=30&zoom=18&addressdetails=1";
var request = (HttpWebRequest)WebRequest.Create(url);
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
I get the following exception:
The underlying connection was closed: The connection was closed unexpectedly.
No idea why as the code above works perfectly fine for other APIs and the query works on a Browser
Upvotes: 0
Views: 3010
Reputation: 1
public async Task ForwardGeocoderTests_SuccessfulForwardGeocode() {
// arrange
var baseUrl = @"https://nominatim.openstreetmap.org/search";
var responseJson = "[{\"place_id\":321574407,\"licence\":\"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright\",\"osm_type\":\"way\",\"osm_id\":995417378,\"boundingbox\":[\"38.8637386\",\"38.8637409\",\"-76.9467576\",\"-76.9467515\"],\"lat\":\"38.8637409\",\"lon\":\"-76.9467576\",\"display_name\":\"Pennsylvania Avenue, Washington, District of Columbia, 20746-8001, United States\",\"place_rank\":26,\"category\":\"highway\",\"type\":\"trunk\",\"importance\":0.41000000000000003,\"address\":{\"road\":\"Pennsylvania Avenue\",\"city\":\"Washington\",\"state\":\"District of Columbia\",\"postcode\":\"20746-8001\",\"country\":\"United States\",\"country_code\":\"us\"},\"geojson\":{\"type\":\"LineString\",\"coordinates\":[[-76.9467515,38.8637386],[-76.9467576,38.8637409]]},\"extratags\":{},\"namedetails\":{\"ref\":\"MD 4\",\"name\":\"Pennsylvania Avenue\",\"name:en\":\"Pennsylvania Avenue\"}},{\"place_id\":270072998,\"licence\":\"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright\",\"osm_type\":\"way\",\"osm_id\":899927559,\"boundingbox\":[\"38.8958906\",\"38.8959158\",\"-77.030956\",\"-77.0308642\"],\"lat\":\"38.8959025\",\"lon\":\"-77.0309076\",\"display_name\":\"Pennsylvania Avenue, Washington, District of Columbia, 20045, United States\",\"place_rank\":27,\"category\":\"highway\",\"type\":\"path\",\"importance\":0.385,\"address\":{\"road\":\"Pennsylvania Avenue\",\"city\":\"Washington\",\"state\":\"District of Columbia\",\"postcode\":\"20045\",\"country\":\"United States\",\"country_code\":\"us\"},\"geojson\":{\"type\":\"LineString\",\"coordinates\":[[-77.0308642,38.8958906],[-77.0309076,38.8959025],[-77.030956,38.8959158]]},\"extratags\":{\"surface\":\"paved\"},\"namedetails\":{\"name\":\"Pennsylvania Avenue\"}}]";
var geocodeRequest = new ForwardGeocodeRequest {
queryString = "1600 Pennsylvania Avenue, Washington, DC",
BreakdownAddressElements = true,
ShowExtraTags = true,
ShowAlternativeNames = true,
ShowGeoJSON = true
};
var expectedDict = new Dictionary<string, string> {
{ "format", "jsonv2" },
{ "q", geocodeRequest.queryString },
{ "addressdetails", "1" },
{ "namedetails", "1" },
{ "polygon_geojson", "1"},
{ "extratags", "1" },
};
var nominatimWebInterface = Substitute.For<INominatimWebInterface>();
var forwardGeocoder = new ForwardGeocoder(nominatimWebInterface);
nominatimWebInterface
.GetRequest<GeocodeResponse[]>(
Arg.Is(baseUrl),
Arg.Is<Dictionary<string, string>>(x => x.IsEquivalentTo(expectedDict)))
.Returns(JsonConvert.DeserializeObject<GeocodeResponse[]>(responseJson));
// act
var r = await forwardGeocoder.Geocode(geocodeRequest);
// assert
Assert.AreEqual(2, r.Length);
Assert.AreEqual(321574407, r[0].PlaceID);
}
Upvotes: 0
Reputation: 53
For me I was testing with angular
http://nominatim.openstreetmap.org/reverse?format=json&lon= lon &lat= lag
worked for localhost but it does not work anymore when put online. only by changing to
https://nominatim.openstreetmap.org/reverse?format=json&lon= lon &lat= lag
works.
http -> https
Upvotes: 0
Reputation: 21469
Does it always fail or just sometimes?
Currently some OSM servers are moved from London to Amsterdam. The API and possible other services (such as Nominatim) run on backup hardware until the primary servers are up again. This will degrade performance and can also lead to sporadic problems. See https://lists.openstreetmap.org/pipermail/talk/2018-July/081009.html . You can check the current state at https://twitter.com/osm_tech .
Upvotes: 1