Skip to content

Commit

Permalink
Nominatim location schema (#122)
Browse files Browse the repository at this point in the history
* nominatim location schema

* missing params, rename classes (it's related to nominatim only)
  • Loading branch information
lenisko authored Mar 6, 2021
1 parent feb65dd commit 16f8232
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
},
"gmapsKey": "",
"nominatim": "",
"nominatimSchema": "{DisplayName}",
"despawnTimeMinimumMinutes": 5,
"reloadSubscriptionChangesMinutes": 1,
"maxNotificationsPerMinute": 10,
Expand Down
6 changes: 6 additions & 0 deletions src/Configuration/WhConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public class WhConfig
[JsonProperty("nominatim")]
public string NominatimEndpoint { get; set; }

/// <summary>
/// Gets or sets the OpenStreetMaps Nominatim location string schema
/// </summary>
[JsonProperty("nominatimSchema")]
public string NominatimSchema { 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
99 changes: 95 additions & 4 deletions src/Geofence/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,98 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using SmartFormat;

using WhMgr.Configuration;
using WhMgr.Diagnostics;

/// <summary>
/// NominatimReverseLookup class
/// </summary>
public partial class NominatimReverseLookup
{
[JsonProperty("place_id")]
public long PlaceId { get; set; }

[JsonProperty("licence")]
public string Licence { get; set; }

[JsonProperty("osm_type")]
public string OsmType { get; set; }

[JsonProperty("osm_id")]
public long OsmId { get; set; }

[JsonProperty("lat")]
public decimal Lat { get; set; }

[JsonProperty("lon")]
public decimal Lon { get; set; }

[JsonProperty("place_rank")]
public long PlaceRank { get; set; }

[JsonProperty("category")]
public string Category { get; set; }

[JsonProperty("type")]
public string Type { get; set; }

[JsonProperty("importance")]
public long Importance { get; set; }

[JsonProperty("addresstype")]
public string Addresstype { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("display_name")]
public string DisplayName { get; set; }

[JsonProperty("address")]
public NominatimAddress Address { get; set; }

[JsonProperty("boundingbox")]
public decimal[] Boundingbox { get; set; }
}

/// <summary>
/// NominatimAddress class
/// </summary>
public partial class NominatimAddress
{
[JsonProperty("house_number")]
public string HouseNumber { get; set; }

[JsonProperty("road")]
public string Road { get; set; }

[JsonProperty("neighbourhood")]
public string Neighbourhood { get; set; }

[JsonProperty("suburb")]
public string Suburb { get; set; }

[JsonProperty("town")]
private string Town { set { City = value; } }

[JsonProperty("city")]
public string City { get; set; }

[JsonProperty("state")]
public string State { get; set; }

[JsonProperty("postcode")]
public string Postcode { get; set; }

[JsonProperty("country")]
public string Country { get; set; }

[JsonProperty("country_code")]
public string CountryCode { get; set; }
}

/// <summary>
/// Geocoordinate location
/// </summary>
Expand Down Expand Up @@ -77,7 +166,7 @@ public Location GetAddress(WhConfig config)
return GetGoogleAddress(City, Latitude, Longitude, config.GoogleMapsKey);

if (!string.IsNullOrEmpty(config.NominatimEndpoint))
return GetNominatimAddress(City, Latitude, Longitude, config.NominatimEndpoint);
return GetNominatimAddress(City, Latitude, Longitude, config.NominatimEndpoint, config.NominatimSchema);

return null;
}
Expand Down Expand Up @@ -141,8 +230,9 @@ private Location GetGoogleAddress(string city, double lat, double lng, string gm
/// <param name="lat">Latitude to lookup</param>
/// <param name="lng">Longitude to lookup</param>
/// <param name="endpoint">Nominatim endpoint</param>
/// <param name="nominatimSchema">Nominatim schema</param>
/// <returns></returns>
private Location GetNominatimAddress(string city, double lat, double lng, string endpoint)
private Location GetNominatimAddress(string city, double lat, double lng, string endpoint, string nominatimSchema)
{
var unknown = "Unknown";
var url = $"{endpoint}/reverse?format=jsonv2&lat={lat}&lon={lng}";
Expand All @@ -153,8 +243,9 @@ private Location GetNominatimAddress(string city, double lat, double lng, string
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));
dynamic obj = JsonConvert.DeserializeObject<NominatimReverseLookup>(json);
var location_string = Smart.Format(nominatimSchema, obj);
return new Location(location_string, city ?? unknown, Convert.ToDouble(obj.Lat), Convert.ToDouble(obj.Lon));
}
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions src/WhMgr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<PackageReference Include="Stripe.net" Version="39.29.0" />
<PackageReference Include="TimeZoneConverter" Version="3.3.0" />
<PackageReference Include="Twilio" Version="5.51.0" />
<PackageReference Include="SmartFormat.NET" Version="2.5.3" />
</ItemGroup>

</Project>

0 comments on commit 16f8232

Please sign in to comment.