134 lines
5.0 KiB
C#
134 lines
5.0 KiB
C#
|
using SafeMobileLib;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
|
|||
|
namespace Dispatcher
|
|||
|
{
|
|||
|
public class ReverseGeocodingModule
|
|||
|
{
|
|||
|
public static String osnServer = "192.168.65.130";
|
|||
|
|
|||
|
|
|||
|
public static List<String> CalculateAddressForPositions(List<PositionData> positions)
|
|||
|
{
|
|||
|
List<String> results = new List<string>();
|
|||
|
foreach(PositionData pos in positions)
|
|||
|
{
|
|||
|
String address = GetAddressFromOSM(pos.Lat, pos.Lng);
|
|||
|
if (address == null)
|
|||
|
address = GetAddressFromGoogle(pos.Lat, pos.Lng);
|
|||
|
|
|||
|
if(address == null)
|
|||
|
address = Math.Round(pos.Lat, 5).ToString() + " , " + Math.Round(pos.Lng, 5).ToString();
|
|||
|
|
|||
|
results.Add(address);
|
|||
|
pos.Address = address;
|
|||
|
}
|
|||
|
|
|||
|
return results;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static async Task<List<String>> CalculateAddressForPositionsAsync(List<PositionData> positions)
|
|||
|
{
|
|||
|
List<String> results = new List<string>();
|
|||
|
|
|||
|
await Task.Factory.StartNew(() =>
|
|||
|
{
|
|||
|
foreach (PositionData pos in positions)
|
|||
|
{
|
|||
|
String address = GetAddressFromOSM(pos.Lat, pos.Lng);
|
|||
|
if (address == null)
|
|||
|
address = GetAddressFromGoogle(pos.Lat, pos.Lng);
|
|||
|
|
|||
|
if (address == null)
|
|||
|
address = Math.Round(pos.Lat, 5).ToString() + " , " + Math.Round(pos.Lng, 5).ToString();
|
|||
|
|
|||
|
results.Add(address);
|
|||
|
pos.Address = address;
|
|||
|
}
|
|||
|
|
|||
|
});
|
|||
|
|
|||
|
return results;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public static string GetAddressFromGoogle(Double LAT, Double LNG)
|
|||
|
{
|
|||
|
String addr = null;
|
|||
|
try
|
|||
|
{
|
|||
|
XmlDocument doc = new XmlDocument();
|
|||
|
XmlNode element = null;
|
|||
|
|
|||
|
doc.Load("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" + (LAT.ToString()).Replace(',', '.')
|
|||
|
+ "," + (LNG.ToString()).Replace(',', '.') + "&sensor=false");
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/status");
|
|||
|
if (element.InnerText == "OK")
|
|||
|
{
|
|||
|
element = doc.SelectSingleNode("//GeocodeResponse/result/formatted_address");
|
|||
|
addr = element.InnerText.Replace("'", "`");
|
|||
|
Utils.WriteLine($"Google Reverse Geocoding for [{LAT},{LNG}] is {addr}", ConsoleColor.Green);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from Google for [{LAT},{LNG}]. Response is {element.InnerText}", ConsoleColor.Yellow);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from Google for [{LAT},{LNG}]", ConsoleColor.Yellow);
|
|||
|
//SM.Debug("Error on address calc:"+ex.ToString());
|
|||
|
}
|
|||
|
//SM.Debug("Prin google:" + addr);
|
|||
|
return addr;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public static string GetAddressFromOSM(Double LAT, Double LNG)
|
|||
|
{
|
|||
|
String addr = null;
|
|||
|
try
|
|||
|
{
|
|||
|
XmlDocument doc = new XmlDocument();
|
|||
|
XmlNode element = null;
|
|||
|
|
|||
|
string address = $"http://{osnServer}/nominatim/reverse?format=xml&lat=" + (LAT.ToString()).Replace(',', '.') + "&lon=" + (LNG.ToString()).Replace(',', '.') + "&zoom=18&addressdetails=1";
|
|||
|
doc.Load(address);
|
|||
|
//doc.Load("http://nominatim.openstreetmap.org/reverse?format=xml&lat=" + (LAT.ToString()).Replace(',', '.') + "&lon=" + (LNG.ToString()).Replace(',', '.') + "&zoom=18&addressdetails=1");
|
|||
|
//doc.Load("http://89.33.124.4:1142/nominatim/reverse?format=xml&lat=" + (LAT.ToString()).Replace(',', '.') + "&lon=" + (LNG.ToString()).Replace(',', '.') + "&zoom=18&addressdetails=1");
|
|||
|
element = doc.SelectSingleNode("//reversegeocode/result");
|
|||
|
addr = element?.InnerText.Replace("'", "`");
|
|||
|
if (addr == null)
|
|||
|
{
|
|||
|
element = doc.SelectSingleNode("//reversegeocode/error");
|
|||
|
addr = element?.InnerText.Replace("'", "`");
|
|||
|
}
|
|||
|
|
|||
|
if (addr.Contains("Unable to geocode") || addr.StartsWith("Fail"))
|
|||
|
{
|
|||
|
addr = null;
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from OSM for [{LAT},{LNG}]. Response is {addr}", ConsoleColor.Yellow);
|
|||
|
}
|
|||
|
else
|
|||
|
Utils.WriteLine($"OSM Reverse Geocoding for [{LAT},{LNG}] is {addr}", ConsoleColor.Green);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Utils.WriteLine($"Unable to do Reverse Geocoding from OSM for [{LAT},{LNG}]", ConsoleColor.Yellow);
|
|||
|
}
|
|||
|
return addr;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|