36 lines
1.3 KiB
C#
36 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using uWine.Core.Domain.Entities;
|
|
|
|
namespace uWine.Core.Infrastructure.Helpers
|
|
{
|
|
public class CoordinatesHelper
|
|
{
|
|
public static string ToWktPolygon(Dictionary<int, Coordinates> coordinatesDict)
|
|
{
|
|
if (coordinatesDict == null || !coordinatesDict.Any())
|
|
{
|
|
throw new ArgumentException("Il dictionary di coordinate non può essere nullo o vuoto.");
|
|
}
|
|
|
|
// Ordina le coordinate in base alla chiave (indice)
|
|
var orderedCoordinates = coordinatesDict.OrderBy(kv => kv.Key)
|
|
.Select(kv => kv.Value)
|
|
.ToList();
|
|
|
|
// Costruisci la stringa dei punti "longitude latitude"
|
|
// Ricorda che i sistemi GIS spesso usano (longitude, latitude) ovvero (x, y)
|
|
var points = orderedCoordinates.Select(c => $"{c.Longitude} {c.Latitude}");
|
|
|
|
// Aggiungi il primo punto alla fine per chiudere il poligono
|
|
var firstPoint = orderedCoordinates.First();
|
|
var closedPoints = string.Join(",", points) + $",{firstPoint.Longitude} {firstPoint.Latitude}";
|
|
|
|
return $"POLYGON(({closedPoints}))";
|
|
}
|
|
}
|
|
}
|