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

+59
View File
@@ -0,0 +1,59 @@
//-----------------------------------------------------------------------
// <copyright file="BearingFilter.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
using System;
/// <summary>
/// Represents a bearing filter, composed of a bearing in decimal angular degrees, with a +/- range
/// also in angular degrees.
/// </summary>
public struct BearingFilter
{
/// <summary> A decimal degree between 0 and 360. </summary>
public double? Bearing;
/// <summary>
/// A decimal degree between 0 and 180. Represents the range
/// beyond bearing in both directions.
/// </summary>
public double? Range;
/// <summary> Initializes a new instance of the <see cref="BearingFilter" /> struct. </summary>
/// <param name="bearing"> A decimal degree between 0 and 360, or null. </param>
/// <param name="range"> A decimal degree between 0 and 180, or null. </param>
public BearingFilter(double? bearing, double? range)
{
if (bearing != null && (bearing > 360 || bearing < 0))
{
throw new Exception("Bearing must be greater than 0 and less than 360.");
}
if (bearing != null && (range > 180 || range < 0))
{
throw new Exception("Range must be greater than 0 and less than 180.");
}
this.Bearing = bearing;
this.Range = range;
}
/// <summary> Converts bearing to a URL snippet. </summary>
/// <returns> Returns a string for use in a Mapbox query URL. </returns>
public override string ToString()
{
if (this.Bearing != null && this.Range != null)
{
return this.Bearing.ToString() + "," + this.Range.ToString();
}
else
{
return string.Empty;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 62331eb3e8b654367ab8e0339c5d8905
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+108
View File
@@ -0,0 +1,108 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Mapbox.Utils
{
public interface ICircularBuffer<T>
{
int Count { get; }
void Add(T item);
T this[int index] { get; }
}
/// <summary>
/// http://geekswithblogs.net/blackrob/archive/2014/09/01/circular-buffer-in-c.aspx
/// https://social.msdn.microsoft.com/Forums/vstudio/en-US/416a2175-b05d-43b1-b99a-a01c56550dbe/circular-buffer-in-net?forum=netfxbcl
/// https://en.wikipedia.org/wiki/Circular_buffer
/// </summary>
/// <typeparam name="T"></typeparam>
public class CircularBuffer<T> : ICircularBuffer<T>, IEnumerable<T>
{
private T[] _buffer;
private int _head;
private int _tail;
public CircularBuffer(int capacity)
{
if (capacity < 0) { throw new ArgumentOutOfRangeException("capacity", "must be positive"); }
_buffer = new T[capacity];
_head = 0;
}
public int Count { get; private set; }
public void Add(T item)
{
_head = (_head + 1) % _buffer.Length;
_buffer[_head] = item;
if (Count == _buffer.Length)
{
_tail = (_tail + 1) % _buffer.Length;
}
else
{
++Count;
}
}
/// <summary>
/// <para>ATTENTION!!! order is flipped like in rolling window</para>
/// <para>[0] is newest value</para>
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public T this[int index]
{
get
{
if (index < 0 || index >= _buffer.Length) { throw new ArgumentOutOfRangeException("index: " + index.ToString()); }
return _buffer[mod((_head - index), _buffer.Length)];
}
}
private int mod(int x, int m) // x mod m works for both positive and negative x (unlike x % m).
{
return (x % m + m) % m;
}
public IEnumerator<T> GetEnumerator()
{
if (Count == 0 || _buffer.Length == 0)
{
yield break;
}
for (var i = 0; i < Count; ++i) { yield return this[i]; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerable<T> GetEnumerable()
{
IEnumerator<T> enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: bfee2b258d221f741a76c90caaa027db
timeCreated: 1524828267
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+102
View File
@@ -0,0 +1,102 @@
//-----------------------------------------------------------------------
// <copyright file="Compression.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
using System.IO;
using Mapbox.IO.Compression;
/// <summary> Collection of constants used across the project. </summary>
public static class Compression
{
/// <summary>
/// Decompress the specified buffer previously compressed using GZip.
/// </summary>
/// <param name="buffer">
/// The GZip'ed buffer.
/// </param>
/// <returns>
/// Returns the uncompressed buffer or the buffer in case decompression
/// is not possible.
/// </returns>
public static byte[] Decompress(byte[] buffer)
{
// Test for magic bits.
if (buffer.Length < 2 || buffer[0] != 0x1f || buffer[1] != 0x8b)
{
return buffer;
}
using (GZipStream stream = new GZipStream(new MemoryStream(buffer), CompressionMode.Decompress))
{
const int Size = 4096; // Pagesize.
byte[] buf = new byte[Size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
try
{
count = stream.Read(buf, 0, Size);
}
catch
{
// For now we return the uncompressed buffer
// on error. Assumes the magic check passed
// by luck.
return buffer;
}
if (count > 0)
{
memory.Write(buf, 0, count);
}
}
while (count > 0);
buffer = memory.ToArray();
}
}
return buffer;
}
public static byte[] Compress(byte[] raw, CompressionLevel compressionLevel)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, compressionLevel))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
public static byte[] CompressModeCompress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(memory, CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d9e429703576740d18a04c9209e91364
timeCreated: 1491243035
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+30
View File
@@ -0,0 +1,30 @@
//-----------------------------------------------------------------------
// <copyright file="Constants.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
/// <summary> Collection of constants used across the project. </summary>
public static class Constants
{
/// <summary> Base URL for all the Mapbox APIs. </summary>
public const string BaseAPI = "https://api.mapbox.com/";
public const string EventsAPI = "https://events.mapbox.com/";
/// <summary> Mercator projection max latitude limit. </summary>
public const double LatitudeMax = 85.0511;
/// <summary> Mercator projection max longitude limit. </summary>
public const double LongitudeMax = 180;
/// <summary> Mercator projection max meters</summary>
public const double WebMercMax = 20037508.342789244;
/// <summary> Epsilon to comapre floating point numbers</summary>
public const float EpsilonFloatingPoint = 1E-05f;
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8ca3d2784c960430588fbe21ea681744
timeCreated: 1491243034
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,173 @@
//-----------------------------------------------------------------------
// <copyright file="Vector2dBounds.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
/// <summary> Represents a bounding box derived from a southwest corner and a northeast corner. </summary>
public struct Vector2dBounds
{
/// <summary> Southwest corner of bounding box. </summary>
public Vector2d SouthWest;
/// <summary> Northeast corner of bounding box. </summary>
public Vector2d NorthEast;
/// <summary> Initializes a new instance of the <see cref="Vector2dBounds" /> struct. </summary>
/// <param name="sw"> Geographic coordinate representing southwest corner of bounding box. </param>
/// <param name="ne"> Geographic coordinate representing northeast corner of bounding box. </param>
public Vector2dBounds(Vector2d sw, Vector2d ne)
{
this.SouthWest = sw;
this.NorthEast = ne;
}
/// <summary> Gets the south latitude. </summary>
/// <value> The south latitude. </value>
public double South {
get {
return this.SouthWest.x;
}
}
/// <summary> Gets the west longitude. </summary>
/// <value> The west longitude. </value>
public double West {
get {
return this.SouthWest.y;
}
}
/// <summary> Gets the north latitude. </summary>
/// <value> The north latitude. </value>
public double North {
get {
return this.NorthEast.x;
}
}
/// <summary> Gets the east longitude. </summary>
/// <value> The east longitude. </value>
public double East {
get {
return this.NorthEast.y;
}
}
/// <summary>
/// Gets or sets the central coordinate of the bounding box. When
/// setting a new center, the bounding box will retain its original size.
/// </summary>
/// <value> The central coordinate. </value>
public Vector2d Center {
get {
var lat = (this.SouthWest.x + this.NorthEast.x) / 2;
var lng = (this.SouthWest.y + this.NorthEast.y) / 2;
return new Vector2d(lat, lng);
}
set {
var lat = (this.NorthEast.x - this.SouthWest.x) / 2;
this.SouthWest.x = value.x - lat;
this.NorthEast.x = value.x + lat;
var lng = (this.NorthEast.y - this.SouthWest.y) / 2;
this.SouthWest.y = value.y - lng;
this.NorthEast.y = value.y + lng;
}
}
/// <summary>
/// Creates a bound from two arbitrary points. Contrary to the constructor,
/// this method always creates a non-empty box.
/// </summary>
/// <param name="a"> The first point. </param>
/// <param name="b"> The second point. </param>
/// <returns> The convex hull. </returns>
public static Vector2dBounds FromCoordinates(Vector2d a, Vector2d b)
{
var bounds = new Vector2dBounds(a, a);
bounds.Extend(b);
return bounds;
}
/// <summary> A bounding box containing the world. </summary>
/// <returns> The world bounding box. </returns>
public static Vector2dBounds World()
{
var sw = new Vector2d(-90, -180);
var ne = new Vector2d(90, 180);
return new Vector2dBounds(sw, ne);
}
/// <summary> Extend the bounding box to contain the point. </summary>
/// <param name="point"> A geographic coordinate. </param>
public void Extend(Vector2d point)
{
if (point.x < this.SouthWest.x)
{
this.SouthWest.x = point.x;
}
if (point.x > this.NorthEast.x)
{
this.NorthEast.x = point.x;
}
if (point.y < this.SouthWest.y)
{
this.SouthWest.y = point.y;
}
if (point.y > this.NorthEast.y)
{
this.NorthEast.y = point.y;
}
}
/// <summary> Extend the bounding box to contain the bounding box. </summary>
/// <param name="bounds"> A bounding box. </param>
public void Extend(Vector2dBounds bounds)
{
this.Extend(bounds.SouthWest);
this.Extend(bounds.NorthEast);
}
/// <summary> Whenever the geographic bounding box is empty. </summary>
/// <returns> <c>true</c>, if empty, <c>false</c> otherwise. </returns>
public bool IsEmpty()
{
return this.SouthWest.x > this.NorthEast.x ||
this.SouthWest.y > this.NorthEast.y;
}
/// <summary>
/// Converts to an array of doubles.
/// </summary>
/// <returns>An array of coordinates.</returns>
public double[] ToArray()
{
double[] array =
{
this.SouthWest.x,
this.SouthWest.y,
this.NorthEast.x,
this.NorthEast.y
};
return array;
}
/// <summary> Converts the Bbox to a URL snippet. </summary>
/// <returns> Returns a string for use in a Mapbox query URL. </returns>
public override string ToString()
{
return string.Format("{0},{1}", this.SouthWest.ToString(), this.NorthEast.ToString());
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8e1bb77e9b97c4e3e942735be5df9370
timeCreated: 1493833265
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+25
View File
@@ -0,0 +1,25 @@
//-----------------------------------------------------------------------
// <copyright file="IObservable.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
/// <summary>
/// An observable interface, similar to .NET 4.0 own IObservable.
/// </summary>
/// <typeparam name="T">
/// The data being observed.
/// </typeparam>
public interface IObservable<T>
{
/// <summary> Add an <see cref="T:IObserver" /> to the observer list. </summary>
/// <param name="observer"> The object subscribing to events. </param>
void Subscribe(Mapbox.Utils.IObserver<T> observer);
/// <summary> Remove an <see cref="T:IObserver" /> to the observer list. </summary>
/// <param name="observer"> The object unsubscribing to events. </param>
void Unsubscribe(Mapbox.Utils.IObserver<T> observer);
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9a29c46baf3924f1b9d848459974aedb
timeCreated: 1491243034
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+21
View File
@@ -0,0 +1,21 @@
//-----------------------------------------------------------------------
// <copyright file="IObserver.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
/// <summary>
/// An observer interface, similar to .NET 4.0 own IObserver.
/// </summary>
/// <typeparam name="T">
/// The data being observed.
/// </typeparam>
public interface IObserver<T>
{
/// <summary> The <see cref="T:Observable" /> has updated the data. </summary>
/// <param name="next"> The data that has changed. </param>
void OnNext(T next);
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3c13777d23b1144809002930214eed99
timeCreated: 1491243034
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: bbed14c6a96bc4fba8b89885ba5f1724
folderAsset: yes
timeCreated: 1491243031
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,80 @@
//-----------------------------------------------------------------------
// <copyright file="BboxToVector2dBoundsConverter.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils.JsonConverters
{
using System;
using Mapbox.Json;
using Mapbox.Json.Converters;
using Mapbox.Json.Linq;
/// <summary>
/// Bbox to geo coordinate bounds converter.
/// </summary>
public class BboxToVector2dBoundsConverter : CustomCreationConverter<Vector2dBounds>
{
/// <summary>
/// Gets a value indicating whether this <see cref="T:Mapbox.BboxToVector2dBoundsConverter"/> can write.
/// </summary>
/// <value><c>true</c> if can write; otherwise, <c>false</c>.</value>
public override bool CanWrite {
get { return true; }
}
/// <summary>
/// Create the specified objectType.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <returns>A <see cref="Vector2dBounds"/>.</returns>
public override Vector2dBounds Create(Type objectType)
{
throw new NotImplementedException();
}
/// <summary>
/// Create the specified objectType and jArray.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <param name="val">J array.</param>
/// <returns>A <see cref="Vector2dBounds"/>.</returns>
public Vector2dBounds Create(Type objectType, JArray val)
{
return new Vector2dBounds(
new Vector2d((double)val[0], (double)val[1]),
new Vector2d((double)val[2], (double)val[3]));
}
/// <summary>
/// Reads the json.
/// </summary>
/// <returns>The serialized object.</returns>
/// <param name="reader">A reader.</param>
/// <param name="objectType">Object type.</param>
/// <param name="existingValue">Existing value.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray bbox = JArray.Load(reader);
return Create(objectType, bbox);
}
/// <summary>
/// Writes the JSON as an array.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/>.</param>
/// <param name="value">The value to serialize.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = (Vector2dBounds)value;
// TODO: This is not working correctly, and setting "bbox: [0,0,0,0]" to Vector2d properties for some reason.
System.Diagnostics.Debug.WriteLine(val);
serializer.Serialize(writer, val.ToArray());
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 382bc19f5f34f4f14985294dbd8d4f73
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,36 @@
//-----------------------------------------------------------------------
// <copyright file="JsonConverters.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils.JsonConverters
{
using Mapbox.Json;
/// <summary>
/// Custom json converters.
/// </summary>
public static class JsonConverters
{
/// <summary>
/// Array of converters.
/// </summary>
private static JsonConverter[] converters =
{
new LonLatToVector2dConverter(),
new BboxToVector2dBoundsConverter(),
new PolylineToVector2dListConverter()
};
/// <summary>
/// Gets the converters.
/// </summary>
/// <value>The converters.</value>
public static JsonConverter[] Converters {
get {
return converters;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f95c54018d5ac404bbe0b84d5dc3ab9a
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,83 @@
//-----------------------------------------------------------------------
// <copyright file="LonLatToVector2dConverter.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils.JsonConverters
{
using System;
using Mapbox.Json;
using Mapbox.Json.Converters;
using Mapbox.Json.Linq;
/// <summary>
/// Bbox to geo coordinate bounds converter.
/// </summary>
public class LonLatToVector2dConverter : CustomCreationConverter<Vector2d>
{
/// <summary>
/// Gets a value indicating whether this <see cref="T:Mapbox.LonLatToVector2dConverter"/> can write.
/// </summary>
/// <value><c>true</c> if can write; otherwise, <c>false</c>.</value>
public override bool CanWrite {
get { return true; }
}
/// <summary>
/// Create the specified objectType.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <returns>A <see cref="Vector2d"/>.</returns>
public override Vector2d Create(Type objectType)
{
throw new NotImplementedException();
}
/// <summary>
/// Create the specified objectType and jArray.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <param name="val">Jarray representing a two length array of coordinates.</param>
/// <returns>A <see cref="Vector2d"/>.</returns>
public Vector2d Create(Type objectType, JArray val)
{
// Assumes long,lat order (like in geojson)
return new Vector2d(y: (double)val[0], x: (double)val[1]);
}
/// <summary>
/// Writes the json.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/>.</param>
/// <param name="value">The value to serialize.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = (Vector2d)value;
Array valAsArray = val.ToArray();
// By default, Vector2d outputs an array with [lat, lon] order, but we want the reverse.
Array.Reverse(valAsArray);
serializer.Serialize(writer, valAsArray);
}
/// <summary>
/// Reads the json.
/// </summary>
/// <returns>The serialized object.</returns>
/// <param name="reader">A reader.</param>
/// <param name="objectType">Object type.</param>
/// <param name="existingValue">Existing value.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
/// <returns>An object.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray coordinates = JArray.Load(reader);
return Create(objectType, coordinates);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4974791f7db3740088cca9acd2104670
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,78 @@
//-----------------------------------------------------------------------
// <copyright file="PolylineToVector2dListConverter.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils.JsonConverters
{
using System;
using System.Collections.Generic;
using Mapbox.Json;
using Mapbox.Json.Converters;
using Mapbox.Json.Linq;
/// <summary>
/// Bbox to geo coordinate bounds converter.
/// </summary>
public class PolylineToVector2dListConverter : CustomCreationConverter<List<Vector2d>>
{
/// <summary>
/// Gets a value indicating whether this <see cref="T:Mapbox.PolylineToVector2dListConverter"/> can write.
/// </summary>
/// <value><c>true</c> if can write; otherwise, <c>false</c>.</value>
public override bool CanWrite {
get { return true; }
}
/// <summary>
/// Create the specified objectType.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <returns>A List of <see cref="Vector2d"/>.</returns>
public override List<Vector2d> Create(Type objectType)
{
throw new NotImplementedException();
}
/// <summary>
/// Create the specified objectType and jArray.
/// </summary>
/// <param name="objectType">Object type.</param>
/// <param name="polyLine">String representation of a polyLine.</param>
/// <returns>A List of <see cref="Vector2d"/>.</returns>
public List<Vector2d> Create(Type objectType, string polyLine)
{
return PolylineUtils.Decode(polyLine);
}
/// <summary>
/// Writes the JSON as an encoded polyline.
/// </summary>
/// <param name="writer">A <see cref="JsonWriter"/>.</param>
/// <param name="value">The original value.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var val = (List<Vector2d>)value;
serializer.Serialize(writer, PolylineUtils.Encode(val));
}
/// <summary>
/// Reads the json. Must be a linestring.
/// </summary>
/// <returns>The serialized object.</returns>
/// <param name="reader">A Reader.</param>
/// <param name="objectType">Object type.</param>
/// <param name="existingValue">Existing value.</param>
/// <param name="serializer">A <see cref="JsonSerializer"/>.</param>
/// <returns>An object.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken polyLine = JToken.Load(reader);
return Create(objectType, (string)polyLine);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 37d418b3df007437d873ac93f792c69b
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+119
View File
@@ -0,0 +1,119 @@
//-----------------------------------------------------------------------
// <copyright file="PolylineUtils.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// A set of Polyline utils.
/// </summary>
public static class PolylineUtils
{
/// <summary>Decodes an encoded path string into a sequence of Positions.</summary>
/// <remarks>
/// Adapted from <see href="https://github.com/mapbox/mapbox-java/blob/9bda93a2f84e26ad67434de1a5c73c335ecac12c/libjava/lib/src/main/java/com/mapbox/services/commons/utils/PolylineUtils.java"/>
/// </remarks>
/// <param name="encodedPath">A string representing a path.</param>
/// <param name="precision">Level of precision. OSRMv4 uses 6, OSRMv5 and Google use 5.</param>
/// <returns>List of <see cref="Vector2d"/> making up the line.</returns>
public static List<Vector2d> Decode(string encodedPath, int precision = 5)
{
int len = encodedPath.Length;
double factor = Math.Pow(10, precision);
// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
var path = new List<Vector2d>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < len)
{
int result = 1;
int shift = 0;
int b;
do
{
b = encodedPath[index++] - 63 - 1;
result += b << shift;
shift += 5;
}
while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do
{
b = encodedPath[index++] - 63 - 1;
result += b << shift;
shift += 5;
}
while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.Add(new Vector2d(y: lng / factor, x: lat / factor));
}
return path;
}
/// <summary>
/// Encodes a sequence of Positions into an encoded path string.
/// </summary>
/// <remarks>
/// Adapted from <see href="https://github.com/mapbox/mapbox-java/blob/9bda93a2f84e26ad67434de1a5c73c335ecac12c/libjava/lib/src/main/java/com/mapbox/services/commons/utils/PolylineUtils.java"/>
/// </remarks>
/// <param name="path">List of <see cref="Vector2d"/> making up the line.</param>
/// <param name="precision">Level of precision. OSRMv4 uses 6, OSRMv5 and Google use 5..</param>
/// <returns>A string representing a polyLine.</returns>
public static string Encode(List<Vector2d> path, int precision = 5)
{
long lastLat = 0;
long lastLng = 0;
var result = new StringBuilder();
double factor = Math.Pow(10, precision);
foreach (Vector2d point in path)
{
var lat = (long)Math.Round(point.x * factor);
var lng = (long)Math.Round(point.y * factor);
Encode(lat - lastLat, result);
Encode(lng - lastLng, result);
lastLat = lat;
lastLng = lng;
}
return result.ToString();
}
/// <summary>
/// Encode the latitude or longitude.
/// </summary>
/// <param name="variable">The value to encode.</param>
/// <param name="result">String representation of latitude or longitude.</param>
private static void Encode(long variable, StringBuilder result)
{
variable = variable < 0 ? ~(variable << 1) : variable << 1;
while (variable >= 0x20)
{
result.Append((char)((int)((0x20 | (variable & 0x1f)) + 63)));
variable >>= 5;
}
result.Append((char)((int)(variable + 63)));
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3b86e6a08ea6e41c3bd214de1a0b1fa2
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,65 @@
//-----------------------------------------------------------------------
// <copyright file="PolylineUtils.cs" company="Mapbox">
// Copyright (c) 2016 Mapbox. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Mapbox.Utils
{
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// A set of Unix Timestamp utils.
/// </summary>
public static class UnixTimestampUtils
{
// http://gigi.nullneuron.net/gigilabs/converting-tofrom-unix-timestamp-in-c/
/// <summary>
/// Convert from DateTime to Unix timestamp
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public static double To(DateTime date)
{
return date.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
}
/// <summary>
/// Convert from Unitx timestamp to DateTime. Uses TimeSpan.FromSeconds to caluclate offset since epoch 0
/// </summary>
/// <param name="timestamp"></param>
/// <returns></returns>
public static DateTime From(double timestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Add(TimeSpan.FromSeconds(timestamp));
}
/// <summary>
/// Convert from Unitx timestamp to DateTime. Uses TimeSpan.FromSeconds to caluclate offset since epoch 0
/// </summary>
/// <param name="timestamp"></param>
/// <returns></returns>
public static DateTime FromMilliseconds(double timestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Add(TimeSpan.FromMilliseconds(timestamp));
}
/// <summary>
/// Convert from Unitx timestamp to DateTime. Uses TimeSpan.FromTicks to caluclate offset since epoch 0
/// </summary>
/// <param name="timestamp"></param>
/// <returns></returns>
public static DateTime From(long timestamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Add(TimeSpan.FromTicks(timestamp));
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f2a29c583ae184002b8d46a92024562d
timeCreated: 1494952071
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+9
View File
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f1d1e64226b2747fea7e72a83de288ba
folderAsset: yes
timeCreated: 1491243031
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
+352
View File
@@ -0,0 +1,352 @@
namespace Mapbox.Utils
{
using System;
using System.Runtime.CompilerServices;
public struct Mathd {
public const double PI = 3.141593d;
public const double Infinity = double.PositiveInfinity;
public const double NegativeInfinity = double.NegativeInfinity;
public const double Deg2Rad = 0.01745329d;
public const double Rad2Deg = 57.29578d;
public const double Epsilon = 1.401298E-45d;
public static double Sin(double d) {
return Math.Sin(d);
}
public static double Cos(double d) {
return Math.Cos(d);
}
public static double Tan(double d) {
return Math.Tan(d);
}
public static double Asin(double d) {
return Math.Asin(d);
}
public static double Acos(double d) {
return Math.Acos(d);
}
public static double Atan(double d) {
return Math.Atan(d);
}
public static double Atan2(double y, double x) {
return Math.Atan2(y, x);
}
public static double Sqrt(double d) {
return Math.Sqrt(d);
}
public static double Abs(double d) {
return Math.Abs(d);
}
public static int Abs(int value) {
return Math.Abs(value);
}
public static double Min(double a, double b) {
if (a < b)
return a;
else
return b;
}
public static double Min(params double[] values) {
int length = values.Length;
if (length == 0)
return 0.0d;
double num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] < num)
num = values[index];
}
return num;
}
public static int Min(int a, int b) {
if (a < b)
return a;
else
return b;
}
public static int Min(params int[] values) {
int length = values.Length;
if (length == 0)
return 0;
int num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] < num)
num = values[index];
}
return num;
}
public static double Max(double a, double b) {
if (a > b)
return a;
else
return b;
}
public static double Max(params double[] values) {
int length = values.Length;
if (length == 0)
return 0d;
double num = values[0];
for (int index = 1; index < length; ++index) {
if ((double)values[index] > (double)num)
num = values[index];
}
return num;
}
public static int Max(int a, int b) {
if (a > b)
return a;
else
return b;
}
public static int Max(params int[] values) {
int length = values.Length;
if (length == 0)
return 0;
int num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] > num)
num = values[index];
}
return num;
}
public static double Pow(double d, double p) {
return Math.Pow(d, p);
}
public static double Exp(double power) {
return Math.Exp(power);
}
public static double Log(double d, double p) {
return Math.Log(d, p);
}
public static double Log(double d) {
return Math.Log(d);
}
public static double Log10(double d) {
return Math.Log10(d);
}
public static double Ceil(double d) {
return Math.Ceiling(d);
}
public static double Floor(double d) {
return Math.Floor(d);
}
public static double Round(double d) {
return Math.Round(d);
}
public static int CeilToInt(double d) {
return (int)Math.Ceiling(d);
}
public static int FloorToInt(double d) {
return (int)Math.Floor(d);
}
public static int RoundToInt(double d) {
return (int)Math.Round(d);
}
public static double Sign(double d) {
return d >= 0.0 ? 1d : -1d;
}
public static double Clamp(double value, double min, double max) {
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static int Clamp(int value, int min, int max) {
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static double Clamp01(double value) {
if (value < 0.0)
return 0.0d;
if (value > 1.0)
return 1d;
else
return value;
}
public static double Lerp(double from, double to, double t) {
return from + (to - from) * Mathd.Clamp01(t);
}
public static double LerpAngle(double a, double b, double t) {
double num = Mathd.Repeat(b - a, 360d);
if (num > 180.0d)
num -= 360d;
return a + num * Mathd.Clamp01(t);
}
public static double MoveTowards(double current, double target, double maxDelta) {
if (Mathd.Abs(target - current) <= maxDelta)
return target;
else
return current + Mathd.Sign(target - current) * maxDelta;
}
public static double MoveTowardsAngle(double current, double target, double maxDelta) {
target = current + Mathd.DeltaAngle(current, target);
return Mathd.MoveTowards(current, target, maxDelta);
}
public static double SmoothStep(double from, double to, double t) {
t = Mathd.Clamp01(t);
t = (-2.0 * t * t * t + 3.0 * t * t);
return to * t + from * (1.0 - t);
}
public static double Gamma(double value, double absmax, double gamma) {
bool flag = false;
if (value < 0.0)
flag = true;
double num1 = Mathd.Abs(value);
if (num1 > absmax) {
if (flag)
return -num1;
else
return num1;
} else {
double num2 = Mathd.Pow(num1 / absmax, gamma) * absmax;
if (flag)
return -num2;
else
return num2;
}
}
public static bool Approximately(double a, double b) {
return Mathd.Abs(b - a) < Mathd.Max(1E-06d * Mathd.Max(Mathd.Abs(a), Mathd.Abs(b)), 1.121039E-44d);
}
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime) {
smoothTime = Mathd.Max(0.0001d, smoothTime);
double num1 = 2d / smoothTime;
double num2 = num1 * deltaTime;
double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
double num4 = current - target;
double num5 = target;
double max = maxSpeed * smoothTime;
double num6 = Mathd.Clamp(num4, -max, max);
target = current - num6;
double num7 = (currentVelocity + num1 * num6) * deltaTime;
currentVelocity = (currentVelocity - num1 * num7) * num3;
double num8 = target + (num6 + num7) * num3;
if (num5 - current > 0.0 == num8 > num5) {
num8 = num5;
currentVelocity = (num8 - num5) / deltaTime;
}
return num8;
}
public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime) {
target = current + Mathd.DeltaAngle(current, target);
return Mathd.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double Repeat(double t, double length) {
return t - Mathd.Floor(t / length) * length;
}
public static double PingPong(double t, double length) {
t = Mathd.Repeat(t, length * 2d);
return length - Mathd.Abs(t - length);
}
public static double InverseLerp(double from, double to, double value) {
if (from < to) {
if (value < from)
return 0d;
if (value > to)
return 1d;
value -= from;
value /= to - from;
return value;
} else {
if (from <= to)
return 0d;
if (value < to)
return 1d;
if (value > from)
return 0d;
else
return (1.0d - (value - to) / (from - to));
}
}
public static double DeltaAngle(double current, double target) {
double num = Mathd.Repeat(target - current, 360d);
if (num > 180.0d)
num -= 360d;
return num;
}
internal static bool LineIntersection(Vector2d p1, Vector2d p2, Vector2d p3, Vector2d p4, ref Vector2d result) {
double num1 = p2.x - p1.x;
double num2 = p2.y - p1.y;
double num3 = p4.x - p3.x;
double num4 = p4.y - p3.y;
double num5 = num1 * num4 - num2 * num3;
if (num5 == 0.0d)
return false;
double num6 = p3.x - p1.x;
double num7 = p3.y - p1.y;
double num8 = (num6 * num4 - num7 * num3) / num5;
result = new Vector2d(p1.x + num8 * num1, p1.y + num8 * num2);
return true;
}
internal static bool LineSegmentIntersection(Vector2d p1, Vector2d p2, Vector2d p3, Vector2d p4, ref Vector2d result) {
double num1 = p2.x - p1.x;
double num2 = p2.y - p1.y;
double num3 = p4.x - p3.x;
double num4 = p4.y - p3.y;
double num5 = (num1 * num4 - num2 * num3);
if (num5 == 0.0d)
return false;
double num6 = p3.x - p1.x;
double num7 = p3.y - p1.y;
double num8 = (num6 * num4 - num7 * num3) / num5;
if (num8 < 0.0d || num8 > 1.0d)
return false;
double num9 = (num6 * num2 - num7 * num1) / num5;
if (num9 < 0.0d || num9 > 1.0d)
return false;
result = new Vector2d(p1.x + num8 * num1, p1.y + num8 * num2);
return true;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 52ad69ec3e74044aeb3f5ae61b5069f3
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,25 @@
namespace Mapbox.Utils
{
public struct RectD
{
public Vector2d Min { get; private set; }
public Vector2d Max { get; private set; }
//size is absolute width&height so Min+size != max
public Vector2d Size { get; private set; }
public Vector2d Center { get; private set; }
public RectD(Vector2d min, Vector2d size)
{
Min = min;
Max = min + size;
Center = new Vector2d(Min.x + size.x / 2, Min.y + size.y / 2);
Size = new Vector2d(Mathd.Abs(size.x), Mathd.Abs(size.y));
}
public bool Contains(Vector2d point)
{
bool flag = Size.x < 0.0 && point.x <= Min.x && point.x > (Min.x + Size.x) || Size.x >= 0.0 && point.x >= Min.x && point.x < (Min.x + Size.x);
return flag && (Size.y < 0.0 && point.y <= Min.y && point.y > (Min.y + Size.y) || Size.y >= 0.0 && point.y >= Min.y && point.y < (Min.y + Size.y));
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f922c015d33a448f4b51ecc8e0153dd8
timeCreated: 1493218361
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,268 @@
namespace Mapbox.Utils
{
using Mapbox.Json;
using System;
using System.Globalization;
[Serializable]
public struct Vector2d
{
public const double kEpsilon = 1E-05d;
public double x;
public double y;
public double this[int index]
{
get
{
switch (index)
{
case 0:
return this.x;
case 1:
return this.y;
default:
throw new IndexOutOfRangeException("Invalid Vector2d index!");
}
}
set
{
switch (index)
{
case 0:
this.x = value;
break;
case 1:
this.y = value;
break;
default:
throw new IndexOutOfRangeException("Invalid Vector2d index!");
}
}
}
[JsonIgnore]
public Vector2d normalized
{
get
{
Vector2d vector2d = new Vector2d(this.x, this.y);
vector2d.Normalize();
return vector2d;
}
}
[JsonIgnore]
public double magnitude
{
get
{
return Mathd.Sqrt(this.x * this.x + this.y * this.y);
}
}
[JsonIgnore]
public double sqrMagnitude
{
get
{
return this.x * this.x + this.y * this.y;
}
}
public static Vector2d zero
{
get
{
return new Vector2d(0.0d, 0.0d);
}
}
public static Vector2d one
{
get
{
return new Vector2d(1d, 1d);
}
}
public static Vector2d up
{
get
{
return new Vector2d(0.0d, 1d);
}
}
public static Vector2d right
{
get
{
return new Vector2d(1d, 0.0d);
}
}
public Vector2d(double x, double y)
{
this.x = x;
this.y = y;
}
public static Vector2d operator +(Vector2d a, Vector2d b)
{
return new Vector2d(a.x + b.x, a.y + b.y);
}
public static Vector2d operator -(Vector2d a, Vector2d b)
{
return new Vector2d(a.x - b.x, a.y - b.y);
}
public static Vector2d operator -(Vector2d a)
{
return new Vector2d(-a.x, -a.y);
}
public static Vector2d operator *(Vector2d a, double d)
{
return new Vector2d(a.x * d, a.y * d);
}
public static Vector2d operator *(float d, Vector2d a)
{
return new Vector2d(a.x * d, a.y * d);
}
public static Vector2d operator /(Vector2d a, double d)
{
return new Vector2d(a.x / d, a.y / d);
}
public static bool operator ==(Vector2d lhs, Vector2d rhs)
{
return Vector2d.SqrMagnitude(lhs - rhs) < 0.0 / 1.0;
}
public static bool operator !=(Vector2d lhs, Vector2d rhs)
{
return (double)Vector2d.SqrMagnitude(lhs - rhs) >= 0.0 / 1.0;
}
public void Set(double new_x, double new_y)
{
this.x = new_x;
this.y = new_y;
}
public static Vector2d Lerp(Vector2d from, Vector2d to, double t)
{
t = Mathd.Clamp01(t);
return new Vector2d(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t);
}
public static Vector2d MoveTowards(Vector2d current, Vector2d target, double maxDistanceDelta)
{
Vector2d vector2 = target - current;
double magnitude = vector2.magnitude;
if (magnitude <= maxDistanceDelta || magnitude == 0.0d)
return target;
else
return current + vector2 / magnitude * maxDistanceDelta;
}
public static Vector2d Scale(Vector2d a, Vector2d b)
{
return new Vector2d(a.x * b.x, a.y * b.y);
}
public void Scale(Vector2d scale)
{
this.x *= scale.x;
this.y *= scale.y;
}
public void Normalize()
{
double magnitude = this.magnitude;
if (magnitude > 9.99999974737875E-06)
this = this / magnitude;
else
this = Vector2d.zero;
}
public override string ToString()
{
return string.Format(NumberFormatInfo.InvariantInfo, "{0:F5},{1:F5}", this.y, this.x);
}
public override int GetHashCode()
{
return this.x.GetHashCode() ^ this.y.GetHashCode() << 2;
}
public override bool Equals(object other)
{
if (!(other is Vector2d))
return false;
Vector2d vector2d = (Vector2d)other;
if (this.x.Equals(vector2d.x))
return this.y.Equals(vector2d.y);
else
return false;
}
public static double Dot(Vector2d lhs, Vector2d rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
public static double Angle(Vector2d from, Vector2d to)
{
return Mathd.Acos(Mathd.Clamp(Vector2d.Dot(from.normalized, to.normalized), -1d, 1d)) * 57.29578d;
}
public static double Distance(Vector2d a, Vector2d b)
{
return (a - b).magnitude;
}
public static Vector2d ClampMagnitude(Vector2d vector, double maxLength)
{
if (vector.sqrMagnitude > maxLength * maxLength)
return vector.normalized * maxLength;
else
return vector;
}
public static double SqrMagnitude(Vector2d a)
{
return (a.x * a.x + a.y * a.y);
}
public double SqrMagnitude()
{
return (this.x * this.x + this.y * this.y);
}
public static Vector2d Min(Vector2d lhs, Vector2d rhs)
{
return new Vector2d(Mathd.Min(lhs.x, rhs.x), Mathd.Min(lhs.y, rhs.y));
}
public static Vector2d Max(Vector2d lhs, Vector2d rhs)
{
return new Vector2d(Mathd.Max(lhs.x, rhs.x), Mathd.Max(lhs.y, rhs.y));
}
public double[] ToArray()
{
double[] array =
{
this.x,
this.y
};
return array;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 65e0c0d8a33eb4adea4665f24e55962a
timeCreated: 1493833265
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: