start polygon

This commit is contained in:
Riccardo Usardi committed 2025-07-10 21:52:06 +02:00
1 parent 5aca82244e
commit 6e5e25a141
22 files changed
+431 -210

No files matched your search

@@ -0,0 +1,35 @@
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}))";
}
}
}
@@ -7,6 +7,7 @@ using System.Text;
using System.Threading.Tasks;
using uWine.Core.Domain.Entities;
using uWine.Core.Domain.Repositories;
using uWine.Core.Infrastructure.Helpers;
namespace uWine.Core.Infrastructure.Repository
{
@@ -38,8 +39,8 @@ namespace uWine.Core.Infrastructure.Repository
command.Parameters.AddWithValue("@name", vineyard.Name);
command.Parameters.AddWithValue("@location", vineyard.Location);
command.Parameters.AddWithValue("@hectares", vineyard.Hectares);
command.Parameters.AddWithValue("@latitude", vineyard.Latitude);
command.Parameters.AddWithValue("@longitude", vineyard.Longitude);
command.Parameters.AddWithValue("@latitude", vineyard.Coordinates.Latitude);
command.Parameters.AddWithValue("@longitude", vineyard.Coordinates.Longitude);
using (var reader = command.ExecuteReader())
{
@@ -55,6 +56,20 @@ namespace uWine.Core.Infrastructure.Repository
}
}
public void InsertArea(Dictionary<int,Coordinates> coordinates, int vineyardId)
{
var polygon = CoordinatesHelper.ToWktPolygon(coordinates);
using (var connection = Connection())
{
connection.Open();
var command = new MySqlCommand("UPDATE t_vineyard SET area = ST_GeomFromText(@polygon, 4326) where id = @id", connection);
command.Parameters.AddWithValue("@id", vineyardId);
command.Parameters.AddWithValue("@polygon", polygon);
command.ExecuteNonQuery();
}
}
public List<Vineyard> GetOwnerVineyards(int ownerId)
{
var vineyards = new List<Vineyard>();
@@ -71,16 +86,7 @@ namespace uWine.Core.Infrastructure.Repository
{
while (reader.Read())
{
var vineyard = new Vineyard
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
Location = reader.GetString(2),
Hectares = reader.GetDouble(3),
Latitude = reader.GetDouble(4),
Longitude = reader.GetDouble(5)
};
vineyards.Add(vineyard);
vineyards.Add(Vineyard.FromRecord(reader));
}
}
}
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using uWine.Core.Domain.Entities;
using uWine.Core.Infrastructure.Repository;
namespace uWine.Core.Infrastructure.Services
{
public class CoordinatesService
{
public MariaDbVineyardRepository _vineyardRepository;
public CoordinatesService(MariaDbVineyardRepository vineyardRepository)
{
_vineyardRepository = vineyardRepository;
}
public async Task<Coordinates> GetCoordinatesAsync(int vineyardId)
{
var vineyard = await _vineyardRepository.GetVineyardByIdAsync(vineyardId);
if (vineyard == null)
{
throw new ArgumentException("Vineyard not found");
}
return new Coordinates(vineyard.Latitude, vineyard.Longitude);
}
public async Task<Dictionary<int,Coordinates>> VineyardCoordinates(int vineyardId)
{
}
}
}