267 lines
9.1 KiB
C#
267 lines
9.1 KiB
C#
using AgroBase.Models;
|
|
using AgroBase.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Forms
|
|
{
|
|
public partial class frmMapa : Form
|
|
{
|
|
private MapasModel Mapa;
|
|
|
|
int idxRua = 0;
|
|
int idxDelRow = -1;
|
|
int idxDelRowRua = -1;
|
|
List<List<GPSModel>> Pontos = new List<List<GPSModel>>() { new List<GPSModel>() };
|
|
AsyncTaskTimerModel tmrLeitura;
|
|
|
|
public frmMapa()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void frmMapa_Load(object sender, EventArgs e)
|
|
{
|
|
Mapa = new MapasModel()
|
|
{
|
|
pnlMapa = pnlMapa,
|
|
lblMapa = lblMapa,
|
|
};
|
|
|
|
tmrLeitura = new AsyncTaskTimerModel("tmrLeitura", tmrLeitura_Tick, 500, this);
|
|
tmrLeitura.Start();
|
|
|
|
|
|
dgPontos.Columns.Clear();
|
|
dgPontos.Columns.Add("clRua", "Rua");
|
|
dgPontos.Columns.Add("clPonto", "Ponto");
|
|
dgPontos.Columns.Add("clLatitude", "Latitude");
|
|
dgPontos.Columns.Add("clLongitude", "Longitude");
|
|
dgPontos.Columns.Add("clDistancia", "Distancia (m)");
|
|
dgPontos.Columns.Add("clOrientacao", "Orientação (º)");
|
|
dgPontos.Columns.Add("clSatelites", "N Satélites");
|
|
dgPontos.Columns.Add("clCorrecao", "Correção");
|
|
dgPontos.Columns.Add("clPrecisao", "Precisão (cm)");
|
|
|
|
dgRuas.Columns.Clear();
|
|
dgRuas.Columns.Add("clRua", "Rua");
|
|
dgRuas.Columns.Add("clPontos", "Pontos");
|
|
dgRuas.Columns.Add("clDistancia", "Distância (m)");
|
|
dgRuas.Columns.Add("clOrientacao", "Orientação (º)");
|
|
|
|
AtualizarGrid();
|
|
}
|
|
|
|
private void btnCarregarMapa_Click(object sender, EventArgs e)
|
|
{
|
|
Mapa.btnCarregar_Click(sender, e);
|
|
}
|
|
|
|
private void btnRegistrarPontos_Click(object sender, EventArgs e)
|
|
{
|
|
GPSModel P1 = Variaveis.OperacaoEmAndamento.Sensoriamento.Gps.Clone();
|
|
|
|
if (Pontos[idxRua].Count == 0)
|
|
{
|
|
P1.Distancia = 0;
|
|
P1.OrientacaoMovimento = 0;
|
|
}
|
|
else
|
|
{
|
|
P1.Distancia = GPSUtils.DistanciaEntrePontos(Pontos[idxRua][Pontos[idxRua].Count - 1], P1);
|
|
P1.OrientacaoMovimento = GPSUtils.CalcularOrientacao(Pontos[idxRua][Pontos[idxRua].Count - 1], P1);
|
|
}
|
|
|
|
P1.Altitude = idxRua + 1;
|
|
P1.VariacaoMagnetica = Pontos[idxRua].Count + 1;
|
|
|
|
Pontos[idxRua].Add(P1);
|
|
|
|
AtualizarGrid();
|
|
}
|
|
|
|
private void btnNovaRua_Click(object sender, EventArgs e)
|
|
{
|
|
if (!Pontos.Any() || Pontos[idxRua].Count < 2)
|
|
{
|
|
MessageBox.Show("Selecione pelo menos 2 pontos para a rua " + (idxRua + 1));
|
|
return;
|
|
}
|
|
Pontos.Add(new List<GPSModel>());
|
|
idxRua = Pontos.Count - 1;
|
|
AtualizarGrid();
|
|
}
|
|
|
|
private void btnLimpar_Click(object sender, EventArgs e)
|
|
{
|
|
if (MessageBox.Show("Limpar pontos registrados?", "Limpar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
Pontos = new List<List<GPSModel>>() { new List<GPSModel>() };
|
|
idxRua = 0;
|
|
|
|
AtualizarGrid();
|
|
}
|
|
}
|
|
|
|
private void btnGerarMapa_Click(object sender, EventArgs e)
|
|
{
|
|
if (!Pontos.Any() || Pontos[idxRua].Count < 2)
|
|
{
|
|
MessageBox.Show("Selecione os pontos da rua " + (idxRua + 1) + " para gerar o mapa!");
|
|
return;
|
|
}
|
|
if (txtDescricao.Text == "")
|
|
{
|
|
MessageBox.Show("Preencha o nome do arquivo de mapa!");
|
|
txtDescricao.Focus();
|
|
return;
|
|
}
|
|
MapasService.CriarArquivoMapa(Pontos, 1517, txtDescricao.Text, GPSUtils.DistanciaDoTrecho(Pontos[0]));
|
|
MessageBox.Show("Mapa gerado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
private void pnlOrientacaoGps_Paint(object sender, PaintEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
double angulo = Variaveis.OperacaoEmAndamento.Sensoriamento.Gps.AnguloCarroDefinido;
|
|
double anguloCorredor = Variaveis.OperacaoEmAndamento.Sensoriamento.Gps.OrientacaoMovimento;
|
|
FuncoesGlobais.DesenharInclinacao((Panel)sender, e, (float)angulo, 90, (float)anguloCorredor);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void AtualizarGrid()
|
|
{
|
|
dgPontos.Rows.Clear();
|
|
foreach (var ponto in Pontos.SelectMany(x => x))
|
|
{
|
|
dgPontos.Rows.Add(new string[] {
|
|
ponto.Altitude.ToString(),
|
|
ponto.VariacaoMagnetica.ToString(),
|
|
ponto.Latitude.ToString(),
|
|
ponto.Longitude.ToString(),
|
|
ponto.Distancia.ToString("0.00"),
|
|
ponto.OrientacaoMovimento.ToString("0.00"),
|
|
ponto.NumeroSatelites.ToString(),
|
|
ponto.QualidadeFix.ToString(),
|
|
ponto.PrecisaoCm.ToString("0.00")
|
|
});
|
|
}
|
|
dgPontos.Refresh();
|
|
|
|
dgRuas.Rows.Clear();
|
|
foreach (var rua in Pontos)
|
|
{
|
|
int ruaAtual = Pontos.IndexOf(rua) + 1;
|
|
dgRuas.Rows.Add(new string[] {
|
|
ruaAtual.ToString(),
|
|
rua.Count().ToString(),
|
|
GPSUtils.DistanciaDoTrecho(rua).ToString("0.00"),
|
|
(rua.Count < 2 ? 0 : GPSUtils.CalcularOrientacao(rua[0], rua[rua.Count() - 1])).ToString("0.00")
|
|
});
|
|
}
|
|
|
|
lblRuaAtual.Text = "Rua Atual: " + (idxRua + 1).ToString();
|
|
}
|
|
|
|
private async Task tmrLeitura_Tick()
|
|
{
|
|
bool add = Pontos.Any(x => x?.Any() ?? false);
|
|
GPSModel ultimoPonto = new GPSModel();
|
|
if (add)
|
|
{
|
|
if (Pontos[Pontos.Count - 1].Count > 0)
|
|
ultimoPonto = Pontos[Pontos.Count - 1][Pontos[Pontos.Count - 1].Count - 1];
|
|
else
|
|
ultimoPonto = Pontos[Pontos.Count - 2][Pontos[Pontos.Count - 2].Count - 1];
|
|
}
|
|
|
|
var Gps = Variaveis.OperacaoEmAndamento.Sensoriamento.Gps;
|
|
|
|
pnlOrientacaoGps.Invalidate();
|
|
lblStatusGPS.Text = $"GPS - Precisão: {Gps.PrecisaoCm} cm, " +
|
|
$"Coreção: {Gps.QualidadeFix}, " +
|
|
$"Satélites: {Gps.NumeroSatelites}, " +
|
|
$"Distância: {(!add ? 0 : GPSUtils.DistanciaEntrePontos(ultimoPonto, Gps) ).ToString("0.00")} m, " +
|
|
$"Orientação: {(!add ? 0 : GPSUtils.CalcularOrientacao(ultimoPonto, Gps)).ToString("0.00")}º";
|
|
}
|
|
|
|
private void dgRuas_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
idxRua = dgRuas.CurrentRow.Index;
|
|
AtualizarGrid();
|
|
}
|
|
|
|
private void dgPontos_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
|
|
{
|
|
if (MessageBox.Show("Remover o ponto selecionado?", "Remover", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
idxDelRow = Convert.ToInt32(e.Row.Cells[1].Value.ToString());
|
|
idxDelRowRua = Convert.ToInt32(e.Row.Cells[0].Value.ToString()) - 1;
|
|
}
|
|
else
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
|
|
private void dgPontos_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
|
|
{
|
|
if (idxDelRow >= 0)
|
|
{
|
|
var ponto = Pontos[idxDelRowRua].FirstOrDefault(x => x.VariacaoMagnetica == idxDelRow);
|
|
Pontos[idxDelRowRua].Remove(ponto);
|
|
|
|
int i = 1;
|
|
foreach (var _ponto in Pontos[idxDelRowRua])
|
|
{
|
|
_ponto.VariacaoMagnetica = i;
|
|
i++;
|
|
}
|
|
|
|
AtualizarGrid();
|
|
|
|
idxDelRow = -1;
|
|
idxDelRowRua = -1;
|
|
}
|
|
}
|
|
|
|
private void dgRuas_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
|
|
{
|
|
if (MessageBox.Show("Remover a rua selecionada?", "Remover", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
{
|
|
idxDelRow = e.Row.Index;
|
|
}
|
|
else
|
|
{
|
|
e.Cancel = true;
|
|
}
|
|
}
|
|
|
|
private void dgRuas_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
|
|
{
|
|
if (idxDelRow >= 0 && idxDelRow < Pontos.Count)
|
|
{
|
|
Pontos.RemoveAt(idxDelRow);
|
|
|
|
int i = 1;
|
|
foreach (var rua in Pontos)
|
|
{
|
|
rua.ForEach(x => x.Altitude = i);
|
|
i++;
|
|
}
|
|
|
|
AtualizarGrid();
|
|
|
|
idxDelRow = -1;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|