Skip to content

Commit

Permalink
Merge pull request #28 from versx/nominatim
Browse files Browse the repository at this point in the history
OSM Nominatim Support
  • Loading branch information
versx authored Oct 22, 2020
2 parents 2ca60bf + 72acfae commit 5415f64
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ bitsadmin /transfer dotnet-install-job /download /priority FOREGROUND https://ra
8.) Build executable `dotnet build ../../..` (if dotnet is in your path) otherwise `~/.dotnet/dotnet build ../../..`
9.) Start WhMgr `dotnet WhMgr.dll` (if dotnet is in your path) otherwise `~/.dotnet/dotnet WhMgr.dll` (If Windows, run as Administrator)
10.) Optional User Interface for members to create subscriptions from a website instead of using Discord commands. (Still WIP but mostly done) [WhMgr UI](https://github.com/versx/WhMgr-UI)
11.) Optional reverse location lookup with OpenStreetMaps Nominatim instead of Google Maps, install instructions [here](https://nominatim.org/release-docs/develop/admin/Installation/)

## Updating
1.) Pull latest changes in root folder
Expand Down
1 change: 1 addition & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"minIV": 100
},
"gmapsKey": "",
"nominatim": "https://nominatim.openstreetmap.org",
"despawnTimeMinimumMinutes": 5,
"reloadSubscriptionChangesMinutes": 1,
"debug": false,
Expand Down
12 changes: 10 additions & 2 deletions src/Commands/Nests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,15 @@ public IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Nes
var geofences = _dep.Whm.Geofences.Values.ToList();
var geofence = GeofenceService.GetGeofence(geofences, new Location(nest.Latitude, nest.Longitude));
var city = geofence?.Name ?? "Unknown";
var googleAddress = Utils.GetGoogleAddress(city, nest.Latitude, nest.Longitude, _dep.WhConfig.GoogleMapsKey);
Location address;
if (!string.IsNullOrEmpty(_dep.WhConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, nest.Latitude, nest.Longitude, _dep.WhConfig.GoogleMapsKey);
}
else
{
address = Utils.GetNominatimAddress(city, nest.Latitude, nest.Longitude, _dep.WhConfig.NominatimEndpoint);
}

var dict = new Dictionary<string, string>
{
Expand Down Expand Up @@ -177,7 +185,7 @@ public IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Nes
{ "wazemaps_url", wazeMapsLink },
{ "scanmaps_url", scannerMapsLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

// Discord Guild properties
{ "guild_name", guild?.Name },
Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/WhConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public class WhConfig
[JsonProperty("gmapsKey")]
public string GoogleMapsKey { get; set; }

/// <summary>
/// Gets or sets the OpenStreetMaps Nominatim endpoint to use for reverse location lookup
/// </summary>
[JsonProperty("nominatim")]
public string NominatimEndpoint { get; set; }

/// <summary>
/// Gets or sets the minimum despawn time in minutes a Pokemon must have in order to send the alarm
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions src/Net/Models/GymDetailsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,15 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);

const string defaultMissingValue = "?";
Expand Down Expand Up @@ -185,7 +193,7 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
{ "wazemaps_url", wazeMapsLocationLink },
{ "scanmaps_url", scannerMapsLocationLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

// Discord Guild properties
{ "guild_name", guild?.Name },
Expand Down
12 changes: 10 additions & 2 deletions src/Net/Models/PokemonData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,15 @@ private async Task<IReadOnlyDictionary<string, string>> GetProperties(DiscordGui
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);
var pokestop = Pokestop.Pokestops.ContainsKey(PokestopId) ? Pokestop.Pokestops[PokestopId] : null;

Expand Down Expand Up @@ -579,7 +587,7 @@ private async Task<IReadOnlyDictionary<string, string>> GetProperties(DiscordGui
{ "wazemaps_url", wazeMapsLocationLink },
{ "scanmaps_url", scannerMapsLocationLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

// Pokestop properties
{ "near_pokestop", Convert.ToString(pokestop != null) },
Expand Down
12 changes: 10 additions & 2 deletions src/Net/Models/PokestopData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,15 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);
var invasion = MasterFile.Instance.GruntTypes.ContainsKey(GruntType) ? MasterFile.Instance.GruntTypes[GruntType] : null;
var leaderString = Translator.Instance.Translate("grunt_" + Convert.ToInt32(GruntType));
Expand Down Expand Up @@ -204,7 +212,7 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
{ "lure_img_url", lureImageUrl },
{ "invasion_img_url", invasionImageUrl },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

// Discord Guild properties
{ "guild_name", guild?.Name },
Expand Down
12 changes: 10 additions & 2 deletions src/Net/Models/QuestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);

const string defaultMissingValue = "?";
Expand Down Expand Up @@ -148,7 +156,7 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
{ "wazemaps_url", wazeMapsLocationLink },
{ "scanmaps_url", scannerMapsLocationLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

//Pokestop properties
{ "pokestop_id", PokestopId ?? defaultMissingValue },
Expand Down
12 changes: 10 additions & 2 deletions src/Net/Models/RaidData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,15 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);

const string defaultMissingValue = "?";
Expand Down Expand Up @@ -275,7 +283,7 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
{ "wazemaps_url", wazeMapsLocationLink },
{ "scanmaps_url", scannerMapsLocationLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

//Gym properties
{ "gym_id", GymId },
Expand Down
16 changes: 14 additions & 2 deletions src/Net/Models/WeatherData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public DiscordEmbedNotification GenerateWeatherMessage(ulong guildId, DiscordCli
return new DiscordEmbedNotification(username, iconUrl, description, new List<DiscordEmbed> { eb.Build() });
}

#endregion

#region Private Methods

private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, WhConfig whConfig, string city, string weatherImageUrl)
{
var weather = Translator.Instance.GetWeather(GameplayCondition);
Expand All @@ -150,7 +154,15 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
var appleMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? appleMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, appleMapsLink);
var wazeMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? wazeMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, wazeMapsLink);
var scannerMapsLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? scannerMapsLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, scannerMapsLink);
var googleAddress = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
Geofence.Location address = null;
if (!string.IsNullOrEmpty(whConfig.GoogleMapsKey))
{
address = Utils.GetGoogleAddress(city, Latitude, Longitude, whConfig.GoogleMapsKey);
}
else if (!string.IsNullOrEmpty(whConfig.NominatimEndpoint))
{
address = Utils.GetNominatimAddress(city, Latitude, Longitude, whConfig.NominatimEndpoint);
}
//var staticMapLocationLink = string.IsNullOrEmpty(whConfig.ShortUrlApiUrl) ? staticMapLink : NetUtil.CreateShortUrl(whConfig.ShortUrlApiUrl, staticMapLink);

const string defaultMissingValue = "?";
Expand Down Expand Up @@ -188,7 +200,7 @@ private IReadOnlyDictionary<string, string> GetProperties(DiscordGuild guild, Wh
{ "wazemaps_url", wazeMapsLocationLink },
{ "scanmaps_url", scannerMapsLocationLink },

{ "address", googleAddress?.Address },
{ "address", address?.Address },

// Discord Guild properties
{ "guild_name", guild?.Name },
Expand Down
22 changes: 22 additions & 0 deletions src/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,27 @@ public static Location GetGoogleAddress(string city, double lat, double lng, str
}
return null;
}

public static Location GetNominatimAddress(string city, double lat, double lng, string endpoint)
{
var unknown = "Unknown";
var url = $"{endpoint}/reverse?format=jsonv2&lat={lat}&lon={lng}";
try
{
using (var wc = new WebClient())
{
wc.Proxy = null;
wc.Headers.Add("User-Agent", Strings.BotName);
var json = wc.DownloadString(url);
dynamic obj = JsonConvert.DeserializeObject(json);
return new Location(Convert.ToString(obj.display_name), city ?? unknown, Convert.ToDouble(obj.lat), Convert.ToDouble(obj.lon));
}
}
catch (Exception ex)
{
_logger.Error(ex);
}
return null;
}
}
}

0 comments on commit 5415f64

Please sign in to comment.