//Author Domenico Ciavarella
//www.studioat.it
namespace Studioat.GIS.Yahoo.Geocode.waGeocodeYahoo
{
public partial class frmGeocodeYahoo : Form
{
private readonly static string strTblIndirizzi = "tblIndirizzi";
private readonly static string appID = ""; //<----------- set here your appID!!!!
private readonly static string geocodeURI = "http://local.yahooapis.com/MapsService/V1/geocode?appid={0}&street={1}&city={2}&country={3}&zip={4}";
private readonly static string country = "IT";
class AddressObject
{
public string Address;
public string City;
public string State;
public string Zip;
public string Country;
public double Latitude;
public double Longitude;
public string precision;
public string warning;
}
public frmGeocodeYahoo()
{
InitializeComponent();
}
private void btnBrowser_Click(object sender, EventArgs e)
{
try
{
OFDDB.CheckFileExists = true;
OFDDB.CheckPathExists = true;
OFDDB.DefaultExt = "mdb";
OFDDB.DereferenceLinks = true;
OFDDB.Filter = "File Access (*.mdb)|*.mdb";
OFDDB.Multiselect = false;
OFDDB.RestoreDirectory = true;
OFDDB.ShowHelp = false;
OFDDB.ShowReadOnly = false;
OFDDB.Title = "Selezionare il db indirizzi";
OFDDB.ValidateNames = true;
if (OFDDB.ShowDialog() == DialogResult.OK)
txtDB.Text = OFDDB.FileName;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
string StringUrl(string s)
{
string result = s.Replace(" ", "+");
return result;
}
AddressObject GetAddress(string street, string city, string country)
{
return GetAddress(street,city,country,string.Empty);
}
AddressObject GetAddress(string street, string city,string country,string zip)
{
try
{
AddressObject newAddress = new AddressObject();
NumberFormatInfo nfi = new NumberFormatInfo();
string url = string.Format(geocodeURI, appID, StringUrl(street), StringUrl(city), country, zip);
using (XmlTextReader xmlReader = new XmlTextReader(url))
{
xmlReader.WhitespaceHandling = WhitespaceHandling.Significant;
while (xmlReader.Read())
{
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Result"))
{
if (xmlReader.GetAttribute("precision") != null)
newAddress.warning = xmlReader.GetAttribute("precision");
if (xmlReader.GetAttribute("warning") != null)
newAddress.precision = xmlReader.GetAttribute("warning");
XmlReader xmlResultReader = xmlReader.ReadSubtree();
while (xmlResultReader.Read())
{
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "Latitude"))
newAddress.Latitude = Convert.ToDouble(xmlResultReader.ReadInnerXml(),nfi);
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "Longitude"))
newAddress.Longitude = Convert.ToDouble(xmlResultReader.ReadInnerXml(), nfi);
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "Address"))
newAddress.Address = xmlResultReader.ReadInnerXml();
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "City"))
newAddress.City = xmlResultReader.ReadInnerXml();
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "State"))
newAddress.State = xmlResultReader.ReadInnerXml();
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "Zip"))
newAddress.Zip = xmlResultReader.ReadInnerXml();
if ((xmlResultReader.NodeType == XmlNodeType.Element) && (xmlResultReader.Name == "Country"))
newAddress.Country = xmlResultReader.ReadInnerXml();
}
}
}
}
return newAddress;
}
catch
{
return null;
}
}
private void btnOK_Click(object sender, EventArgs e)
{
OleDbConnection cnn = null;
OleDbDataAdapter da = null;
DataSet ds = null;
try
{
txtError.Text = string.Empty;
cnn = new OleDbConnection();
cnn.ConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", txtDB.Text);
da = new OleDbDataAdapter();
if (chkOnlyWithoutXY.Checked)
da.SelectCommand = new OleDbCommand(string.Format("SELECT * FROM {0} WHERE (((Lon) Is Null)) OR (((Lat) Is Null))", strTblIndirizzi), cnn);
else
da.SelectCommand = new OleDbCommand(string.Format("SELECT * FROM {0}", strTblIndirizzi), cnn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(da);
cnn.Open();
ds = new DataSet();
da.Fill(ds, strTblIndirizzi);
PB.Visible = true;
PB.Minimum = 0;
PB.Maximum = ds.Tables[0].Rows.Count;
PB.Value = 0;
PB.Step = 1;
txtError.Text = string.Format("Numero di indirizzi sul quale verrà effettuata la geocodifica: {0}", ds.Tables[0].Rows.Count);
string strNameFile = System.IO.Path.Combine(Application.StartupPath, System.Guid.NewGuid().ToString());
foreach (DataRow r in ds.Tables[0].Rows)
{
if ((!System.Convert.IsDBNull(r["Citta"])) && (!System.Convert.IsDBNull(r["Indirizzo"])))
{
AddressObject address = null;
if (System.Convert.IsDBNull(r["CAP"]))
address = GetAddress(r["Indirizzo"].ToString(), r["Citta"].ToString(), country);
else
address = GetAddress(r["Indirizzo"].ToString(), r["Citta"].ToString(), country, r["CAP"].ToString());
if (address != null)
{
r["Lon"] = address.Longitude;
r["Lat"] = address.Latitude;
r["CittaY"] = address.City;
r["IndirizzoY"] = address.Address;
r["CAPY"] = address.Zip;
r["PrecisionY"] = address.precision;
r["WarningY"] = address.warning;
r["StateY"] = address.State;
r["CountryY"] = address.Country;
}
}
ds.WriteXml(System.IO.Path.ChangeExtension(strNameFile, "xml"));
Application.DoEvents();
PB.PerformStep();
}
}
catch (Exception ex)
{
if (txtError.Text == string.Empty)
txtError.Text = ex.Message;
else
txtError.Text += System.Environment.NewLine + ex.Message;
}
finally
{
PB.Visible = false;
if (da != null)
{
if (ds != null)
{
Int32 intNumeroAggiornati = da.Update(ds, strTblIndirizzi);
ds.Dispose();
string strMessage = string.Format("Numero di indirizzi aggiornati: {0}{1}{2}", intNumeroAggiornati, System.Environment.NewLine, "Operazione completata");
if (txtError.Text == string.Empty)
txtError.Text = strMessage;
else
txtError.Text += System.Environment.NewLine + strMessage;
}
da.Dispose();
}
if ((cnn != null) && (cnn.State == ConnectionState.Open))
cnn.Close();
}
}
}
}
Qui puoi scaricare la soluzione in VS2008 waGeocodeYahoo
Nessun commento:
Posta un commento