Created
November 21, 2012 01:55
-
-
Save vadimii/4122572 to your computer and use it in GitHub Desktop.
Geo coords for address line via Yandex API
This file contains hidden or 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
const string cs = "<CONNECTION_STRING>"; | |
const string key = "<YANDEX_API_CODE>"; | |
const string requestUrlTemplate = | |
"http://geocode-maps.yandex.ru/1.x/" + | |
"?geocode={0}&key={1}&&results=1"; | |
var addresses = new List<string>(); | |
using (var connection = new SqlConnection(cs)) | |
using (var select = connection.CreateCommand()) | |
{ | |
select.CommandText = | |
"select distinct AddressLine from FacilityCoords"; | |
connection.Open(); | |
using (var reader = select.ExecuteReader()) | |
while (reader.Read()) | |
{ | |
addresses.Add((string)reader["AddressLine"]); | |
} | |
} | |
using (var browser = new WebClient()) | |
{ | |
browser.Encoding = Encoding.UTF8; | |
foreach(var address in addresses) | |
{ | |
var requestUrl = string.Format( | |
requestUrlTemplate, | |
HttpUtility.UrlPathEncode(address), | |
HttpUtility.UrlPathEncode(key)); | |
var geo = browser.DownloadString(requestUrl); | |
using (var sr = new StringReader(geo)) | |
{ | |
sr.ReadLine(); // skip xml declaration | |
geo = sr.ReadToEnd(); | |
} | |
using (var connection = new SqlConnection(cs)) | |
using (var update = connection.CreateCommand()) | |
{ | |
connection.Open(); | |
update.CommandText = | |
"update FacilityCoords set GeoObjectXML = @GeoObjectXML " + | |
"where AddressLine = @AddressLine"; | |
update.Parameters.Add("@GeoObjectXML", geo); | |
update.Parameters.Add("@AddressLine", address); | |
update.ExecuteNonQuery(); | |
} | |
} | |
} |
This file contains hidden or 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
create view FacilityGeoPoint | |
as | |
select FacilityID, Point = GeoObjectXML.value(N' | |
declare namespace gml="http://www.opengis.net/gml"; | |
(//gml:pos/text())[1]', 'nvarchar(50)') from FacilityCoords |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment