This commit is contained in:
runette committed 2024-04-22 15:52:51 +01:00
commit e0afef912e
1285 files changed
+83219

No files matched your search

@@ -0,0 +1,206 @@
//-----------------------------------------------------------------------
// <copyright file="ForwardGeocodeResource.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding
{
using System;
using System.Collections.Generic;
using Mapbox.Utils;
using UnityEngine;
/// <summary> A forward geocode request. </summary>
public sealed class ForwardGeocodeResource : GeocodeResource<string>
{
/// <summary>
/// ISO 3166-1 alpha-2 country codes.
/// See <see href="https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2">for all options</see>.
/// </summary>
private static readonly List<string> CountryCodes = new List<string>()
{
"ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bl", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mf", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw"
};
// Required
private string query;
// Optional
private bool? autocomplete;
// Optional
private string[] country;
// Optional
private Vector2d? proximity;
// Optional
private Vector2dBounds? bbox;
/// <summary> Initializes a new instance of the <see cref="ForwardGeocodeResource" /> class.</summary>
/// <param name="query"> Place name for forward geocoding. </param>
public ForwardGeocodeResource(string query)
{
this.Query = query;
}
/// <summary> Gets or sets the place name for forward geocoding. </summary>
public override string Query
{
get
{
return this.query;
}
set
{
this.query = value;
}
}
/// <summary> Gets or sets the autocomplete option. </summary>
public bool? Autocomplete
{
get
{
return this.autocomplete;
}
set
{
this.autocomplete = value;
}
}
/// <summary>
/// Gets or sets the bounding box option. Bounding box is a rectangle within which to
/// limit results, given as <see cref="Bbox"/>.
/// </summary>
public Vector2dBounds? Bbox
{
get
{
return this.bbox;
}
set
{
this.bbox = value;
}
}
/// <summary>
/// Gets or sets the country option. Country is an Array of ISO 3166 alpha 2 country codes.
/// For all possible values, <see cref="CountryCodes"/>.
/// </summary>
public string[] Country
{
get
{
return this.country;
}
set
{
if (value == null)
{
this.country = value;
return;
}
for (int i = 0; i < value.Length; i++)
{
// Validate that provided countries exist
if (!CountryCodes.Contains(value[i]))
{
throw new Exception("Invalid country shortcode. See https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2.");
}
}
this.country = value;
}
}
/// <summary>
/// Gets or sets the proximity option, which is a location around which to bias results,
/// given as <see cref="Vector2d"/>.
/// </summary>
public Vector2d? Proximity
{
get
{
return this.proximity;
}
set
{
this.proximity = value;
}
}
/// <summary> Builds a forward geocode URL string. </summary>
/// <returns> A complete, valid forward geocode URL. </returns>
public override string GetUrl()
{
Dictionary<string, string> opts = new Dictionary<string, string>();
if (this.Autocomplete != null)
{
opts.Add("autocomplete", this.Autocomplete.ToString().ToLower());
}
if (this.Bbox != null)
{
var nonNullableBbox = (Vector2dBounds)this.Bbox;
opts.Add("bbox", nonNullableBbox.ToString());
}
if (this.Country != null)
{
opts.Add("country", ForwardGeocodeResource.GetUrlQueryFromArray<string>(this.Country));
}
if (this.Proximity != null)
{
var nonNullableProx = (Vector2d)this.Proximity;
opts.Add("proximity", nonNullableProx.ToString());
}
if (this.Types != null)
{
opts.Add("types", GetUrlQueryFromArray(this.Types));
}
// !!!!!!!!!! HACK !!!!!!!
// we are seeing super weird behaviour on some iOS devices:
// crashes with properly escaped whitespaces %20 and commas %2C - and other special characters
// 'NSAllowsArbitraryLoads' and 'NSURLConnection finished with error - code - 1002'
// Use 'CFNETWORK_DIAGNOSTICS=1' in XCode to get more details https://stackoverflow.com/a/46748461
// trying to get rid of at least the most common characters - other will still crash
#if UNITY_IOS
Query = Query
.Replace(",", " ")
.Replace(".", " ")
.Replace("-", " ");
#endif
return
Constants.BaseAPI +
ApiEndpoint +
Mode +
#if UNITY_IOS
#if UNITY_2017_1_OR_NEWER
UnityEngine.Networking.UnityWebRequest.EscapeURL(Query) +
#else
WWW.EscapeURL(Query) +
#endif
#else
Uri.EscapeDataString(Query) +
#endif
".json" +
EncodeQueryString(opts);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 33cb5b2bc4f6f4397a21fdd87dfa0048
timeCreated: 1493833265
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,73 @@
//-----------------------------------------------------------------------
// <copyright file="GeocodeResource.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding
{
using System;
using System.Collections.Generic;
using Mapbox.Platform;
/// <summary> Base geocode class. </summary>
/// <typeparam name="T"> Type of Query field (either string or LatLng). </typeparam>
public abstract class GeocodeResource<T> : Resource
{
/// <summary> A List of all possible geocoding feature types. </summary>
public static readonly List<string> FeatureTypes = new List<string>
{
"country", "region", "postcode", "place", "locality", "neighborhood", "address", "poi"
};
private readonly string apiEndpoint = "geocoding/v5/";
private readonly string mode = "mapbox.places/";
// Optional
private string[] types;
/// <summary> Gets or sets the query. </summary>
public abstract T Query { get; set; }
/// <summary> Gets the API endpoint as a partial URL path. </summary>
public override string ApiEndpoint {
get {
return this.apiEndpoint;
}
}
/// <summary> Gets the mode. </summary>
public string Mode {
get {
return this.mode;
}
}
/// <summary> Gets or sets which feature types to return results for. </summary>
public string[] Types {
get {
return this.types;
}
set {
if (value == null)
{
this.types = value;
return;
}
for (int i = 0; i < value.Length; i++)
{
// Validate provided types
if (!FeatureTypes.Contains(value[i]))
{
throw new Exception("Invalid type. Must be \"country\", \"region\", \"postcode\", \"place\", \"locality\", \"neighborhood\", \"address\", or \"poi\".");
}
}
this.types = value;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: aba88323b8d91474fb3b991eb0bd9349
timeCreated: 1491243034
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+87
View File
@@ -0,0 +1,87 @@
//-----------------------------------------------------------------------
// <copyright file="Geocoder.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding
{
using System;
using System.Text;
using Mapbox.Json;
using Mapbox.Platform;
using Mapbox.Utils.JsonConverters;
/// <summary>
/// Wrapper around the <see href="https://www.mapbox.com/api-documentation/search/#geocoding">
/// Mapbox Geocoding API</see>. The Geocoder does two things: geocoding and reverse geocoding.
/// </summary>
public sealed class Geocoder
{
private readonly IFileSource fileSource;
/// <summary> Initializes a new instance of the <see cref="Geocoder" /> class. </summary>
/// <param name="fileSource"> Network access abstraction. </param>
public Geocoder(IFileSource fileSource)
{
this.fileSource = fileSource;
}
/// <summary> Performs asynchronously a geocoding lookup. </summary>
/// <param name="geocode"> Geocode resource. </param>
/// <param name="callback"> Callback to be called after the request is completed. </param>
/// <typeparam name="T"> String or LngLat. Should be automatically inferred. </typeparam>
/// <returns>
/// Returns a <see cref="IAsyncRequest" /> that can be used for canceling a pending
/// request. This handle can be completely ignored if there is no intention of ever
/// canceling the request.
/// </returns>
public IAsyncRequest Geocode<T>(GeocodeResource<T> geocode, Action<ReverseGeocodeResponse> callback)
{
return this.fileSource.Request(
geocode.GetUrl(),
(Response response) =>
{
var str = Encoding.UTF8.GetString(response.Data);
var data = Deserialize<ReverseGeocodeResponse>(str);
callback(data);
});
}
/// <summary> Performs asynchronously a geocoding lookup. </summary>
/// <param name="geocode"> Geocode resource. </param>
/// <param name="callback"> Callback to be called after the request is completed. </param>
/// <typeparam name="T"> String or LngLat. Should be automatically inferred. </typeparam>
/// <returns>
/// Returns a <see cref="IAsyncRequest" /> that can be used for canceling a pending
/// request. This handle can be completely ignored if there is no intention of ever
/// canceling the request.
/// </returns>
public IAsyncRequest Geocode<T>(GeocodeResource<T> geocode, Action<ForwardGeocodeResponse> callback)
{
return this.fileSource.Request(
geocode.GetUrl(),
(Response response) =>
{
var str = Encoding.UTF8.GetString(response.Data);
var data = Deserialize<ForwardGeocodeResponse>(str);
callback(data);
});
}
/// <summary>
/// Deserialize the geocode response string into a <see cref="GeocodeResponse"/>.
/// </summary>
/// <param name="str">JSON String.</param>
/// <returns>A <see cref="GeocodeResponse"/>.</returns>
/// <typeparam name="T">Forward or reverse geocode. </typeparam>
public T Deserialize<T>(string str)
{
return JsonConvert.DeserializeObject<T>(str, JsonConverters.Converters);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d304b174589dd496993060116409b23c
timeCreated: 1491336250
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 5ef566ef707b848c4bac9b1f13fccf17
folderAsset: yes
timeCreated: 1491243031
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,96 @@
//-----------------------------------------------------------------------
// <copyright file="Feature.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding {
using System;
using System.Collections.Generic;
using Mapbox.Json;
using Mapbox.Utils;
using Mapbox.Utils.JsonConverters;
/// <summary> A GeoJSON FeatureCollection of points returned by geocoding API.</summary>
#if !WINDOWS_UWP
//http://stackoverflow.com/a/12903628
[Serializable]
#endif
public class Feature {
/// <summary> Gets or sets the id. Ids are unique in the Mapbox geocoder. </summary>
/// <value>The id.</value>
[JsonProperty("id")]
public string Id { get; set; }
/// <summary>
/// Gets or sets feature type. One of country, region, postcode, place, locality, neighborhood, address, poi.
/// </summary>
/// <value>The type.</value>
[JsonProperty("type")]
public string Type { get; set; }
/// <summary>
/// Gets or sets the text.
/// </summary>
/// <value>The text.</value>
[JsonProperty("text")]
public string Text { get; set; }
/// <summary>
/// Gets or sets the name of the place.
/// </summary>
/// <value>The name of the place.</value>
[JsonProperty("place_name")]
public string PlaceName { get; set; }
/// <summary>
/// Gets or sets the relevance.
/// </summary>
/// <value>The relevance.</value>
[JsonProperty("relevance")]
public double Relevance { get; set; }
/// <summary>
/// Gets or sets the properties.
/// </summary>
/// <value>The properties.</value>
[JsonProperty("properties")]
public Dictionary<string, object> Properties { get; set; }
/// <summary>
/// Gets or sets the bbox.
/// </summary>
/// <value>The bbox.</value>
[JsonProperty("bbox", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(BboxToVector2dBoundsConverter))]
public Vector2dBounds? Bbox { get; set; }
/// <summary>
/// Gets or sets the center.
/// </summary>
/// <value>The center.</value>
[JsonProperty("center")]
[JsonConverter(typeof(LonLatToVector2dConverter))]
public Vector2d Center { get; set; }
/// <summary>
/// Gets or sets the geometry.
/// </summary>
/// <value>The geometry.</value>
[JsonProperty("geometry")]
public Geometry Geometry { get; set; }
/// <summary>
/// Gets or sets the address.
/// </summary>
[JsonProperty("address", NullValueHandling = NullValueHandling.Ignore)]
public string Address { get; set; }
/// <summary>
/// Gets or sets the context.
/// </summary>
/// <value>The context.</value>
[JsonProperty("context", NullValueHandling = NullValueHandling.Ignore)]
public List<Dictionary<string, string>> Context { get; set; }
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f0645486f47864cd390751dd94d0302f
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,71 @@
//-----------------------------------------------------------------------
// <copyright file="GeocodeResponse.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding {
using System;
using System.Collections.Generic;
using Mapbox.Json;
/// <summary> Base geocode response. </summary>
#if !WINDOWS_UWP
//http://stackoverflow.com/a/12903628
[Serializable]
#endif
public abstract class GeocodeResponse {
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>The type.</value>
[JsonProperty("type", Order = 0)]
public string Type { get; set; }
/// <summary>
/// Gets or sets the features.
/// </summary>
/// <value>The features.</value>
[JsonProperty("features", Order = 2)]
public List<Feature> Features { get; set; }
/// <summary>
/// Gets or sets the attribution.
/// </summary>
/// <value>The attribution.</value>
[JsonProperty("attribution", Order = 3)]
public string Attribution { get; set; }
}
/// <summary>
/// Reverse Geocode response.
/// </summary>
#if !WINDOWS_UWP
//http://stackoverflow.com/a/12903628
[Serializable]
#endif
public class ReverseGeocodeResponse : GeocodeResponse {
/// <summary>
/// Gets or sets the query.
/// </summary>
/// <value>The query.</value>
[JsonProperty("query", Order = 1)]
public List<double> Query { get; set; }
}
/// <summary>
/// Forward geocode response.
/// </summary>
#if !WINDOWS_UWP
//http://stackoverflow.com/a/12903628
[Serializable]
#endif
public class ForwardGeocodeResponse : GeocodeResponse {
/// <summary>
/// Gets or sets the query.
/// </summary>
/// <value>The query.</value>
[JsonProperty("query", Order = 1)]
public List<string> Query { get; set; }
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f1cccf508c40144308be9518f5085a69
timeCreated: 1491243035
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------
// <copyright file="Geometry.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding {
using System;
using Mapbox.Json;
using Mapbox.Utils;
using Mapbox.Utils.JsonConverters;
/// <summary> Point geometry representing location of geocode result. </summary>
#if !WINDOWS_UWP
//http://stackoverflow.com/a/12903628
[Serializable]
#endif
public class Geometry {
/// <summary>
/// Gets or sets type. Geocode results will always be type: point.
/// </summary>
/// <value>The GeoJSON geometry type.</value>
[JsonProperty("type")]
public string Type { get; set; }
/// <summary>
/// Gets or sets coordinates. Because they are points, Geocode results will always be a single Geocoordinate.
/// </summary>
/// <value>The coordinates.</value>
[JsonConverter(typeof(LonLatToVector2dConverter))]
[JsonProperty("coordinates")]
public Vector2d Coordinates { get; set; }
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b599e78a14b3d42c8822cec5d5775397
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,55 @@
//-----------------------------------------------------------------------
// <copyright file="ReverseGeocodeResource.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Geocoding
{
using System.Collections.Generic;
using Mapbox.Utils;
/// <summary> A reverse geocode request. </summary>
public sealed class ReverseGeocodeResource : GeocodeResource<Vector2d>
{
// Required
private Vector2d query;
/// <summary> Initializes a new instance of the <see cref="ReverseGeocodeResource" /> class.</summary>
/// <param name="query"> Location to reverse geocode. </param>
public ReverseGeocodeResource(Vector2d query)
{
this.Query = query;
}
/// <summary> Gets or sets the location. </summary>
public override Vector2d Query {
get {
return this.query;
}
set {
this.query = value;
}
}
/// <summary> Builds a complete reverse geocode URL string. </summary>
/// <returns> A complete, valid reverse geocode URL string. </returns>
public override string GetUrl()
{
Dictionary<string, string> opts = new Dictionary<string, string>();
if (this.Types != null)
{
opts.Add("types", GetUrlQueryFromArray(this.Types));
}
return Constants.BaseAPI +
this.ApiEndpoint +
this.Mode +
this.Query.ToString() +
".json" +
EncodeQueryString(opts);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 23c6d0bd3662648d3859495c35635b64
timeCreated: 1493833265
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: