public async Task LimitNumberOfReturnedGeoObjects() { var client = new GeocoderClient(apiKey); var unlimitedResponse = await client.Geocode(new GeocoderRequest { Request = "Серова" }); var unlimitedGeoObjectCount = unlimitedResponse.GeoObjectCollection.FeatureMember.Count; var limitedResponse = await client.Geocode(new GeocoderRequest { Request = "Серова", MaxCount = 1 }); var limitedGeoObjectCount = limitedResponse.GeoObjectCollection.FeatureMember.Count; Assert.True(unlimitedGeoObjectCount > limitedGeoObjectCount); }
public async Task GeocodeSettlementWithoutStreet() { var request = new GeocoderRequest { Request = "Ярославская область Октябрьский 12" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var coordinate = firstGeoObject.GeoObject.Point.Pos; var addressComponents = firstGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var country = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Country)); var province = addressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var area = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Area)); var city = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var street = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Street)); var house = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.House)); Assert.Equal("Россия", country.Name); Assert.Equal("Ярославская область", province.Name); Assert.Equal("Рыбинский район", area.Name); Assert.Equal("поселок Октябрьский", city.Name); Assert.Null(street); Assert.Equal("12", house.Name); Assert.Equal("39.110177 57.984794", coordinate); }
public async Task GeocodeCity() { var request = new GeocoderRequest { Request = "Ярославль серова 13" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var coordinate = firstGeoObject.GeoObject.Point.Pos; var addressComponents = firstGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var country = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Country)); var province = addressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var area = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Area)); var city = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var street = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Street)); var house = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.House)); Assert.Equal("Россия", country.Name); Assert.Equal("Ярославская область", province.Name); Assert.Equal("городской округ Ярославль", area.Name); Assert.Equal("Ярославль", city.Name); Assert.Equal("улица Серова", street.Name); Assert.Equal("13", house.Name); Assert.Equal("39.802311 57.608719", coordinate); }
public async Task RestrictSearchArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", SearchArea = new Area { Latitude = 59.300000, Longitude = 39.500000, LatitudeSpan = 0.500000, LongitudeSpan = 0.500000 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var firstResponseAddressComponents = firstResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var firstResponseProvince = firstResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var firstResponseLocality = firstResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var secondRequest = new GeocoderRequest { Request = "Песочное", SearchArea = new Area { Latitude = 58.00400, Longitude = 39.200000, LatitudeSpan = 0.500000, LongitudeSpan = 0.500000 }, IsRestrictArea = true }; var secondResponse = await client.Geocode(secondRequest); var secondResponseGeoObject = secondResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var secondResponseAddressComponents = secondResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var secondResponseProvince = secondResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var secondResponseLocality = secondResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); Assert.Equal("Вологодская область", firstResponseProvince.Name); Assert.Equal("поселок Песочное", firstResponseLocality.Name); Assert.Equal("Ярославская область", secondResponseProvince.Name); Assert.Equal("поселок Песочное", secondResponseLocality.Name); }
private async Task <bool> UpdateCoordinate(Pharmacy pharmacy) { var query = YekaterinburgRegionPrefix + pharmacy.City + ", " + pharmacy.Address; var request = new GeocoderRequest { Request = query }; var apiKey = _configuration["GeoCoderApiKey"]; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var geoObjects = response.GeoObjectCollection.FeatureMember; if (geoObjects.Count == 0) { _log.Error($"No geoobjects by query {query} - Id {pharmacy.Id}"); return(false); } var geoObjectFromSelectedCity = geoObjects .FirstOrDefault(e => e.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components .FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)) ?.Name == pharmacy.City); var geoObject = geoObjectFromSelectedCity ?? geoObjects.First(); if (geoObjects.Count > 1 && geoObjectFromSelectedCity == null) { _log.Warn($"Multiple geoobjects ({geoObjects.Count}) and No one from city {pharmacy.City} by query {query} - Id {pharmacy.Id}."); } var coordinate = geoObject.GeoObject.Point.Pos; var formattedAddress = geoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Formatted; pharmacy.FormattedAddress = formattedAddress.Replace("Россия, Свердловская область, ", string.Empty); try { UpdateCoordinates(coordinate, pharmacy); return(true); } catch (Exception exception) { _log.Error(exception, $"query {query} - Id {pharmacy.Id}"); return(false); } }
public async Task ParseCoordinates() { var request = new GeocoderRequest { Request = "Ярославская область Октябрьский 12" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var strCoordinate = firstGeoObject.GeoObject.Point.Pos; var coordinate = strCoordinate.ToCoordinate(); Assert.Equal("39.110177 57.984794", strCoordinate); Assert.Equal(57.984794, coordinate.Latitude); Assert.Equal(39.110177, coordinate.Longitude); }
public async Task NotFindGeoObjectInBordersArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", BordersArea = new BoxArea { LowerLatitude = 59.600000, LowerLongitude = 41.641063, UpperLatitude = 59.700000, UpperLongitude = 41.666459 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); Assert.Null(firstResponseGeoObject); }
public async Task FindGeoObjectInBordersArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", BordersArea = new BoxArea { LowerLatitude = 59.440733, LowerLongitude = 39.641063, UpperLatitude = 59.457845, UpperLongitude = 39.666459 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var firstResponseAddressComponents = firstResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var firstResponseProvince = firstResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var firstResponseLocality = firstResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); Assert.Equal("Вологодская область", firstResponseProvince.Name); Assert.Equal("поселок Песочное", firstResponseLocality.Name); }
public async Task <ICollection <AddressEM> > Geocode(string request, int maxResultCount = 5) { var result = new List <AddressEM>(); var clientRequest = new GeocoderRequest { Request = request, MaxCount = maxResultCount }; var client = new GeocoderClient(); var clientResponse = await client.Geocode(clientRequest); foreach (var geoFeatureMember in clientResponse.GeoObjectCollection.FeatureMember) { var geoObject = geoFeatureMember.GeoObject; var address = CreateAddressByGeoObject(geoObject); result.Add(address); } return(result); }