149 lines
5.7 KiB
C#
149 lines
5.7 KiB
C#
using AgroBase.Models;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AgroBase.Services
|
|
{
|
|
public static class MapasVariaveisModel
|
|
{
|
|
public static string NomeArquivoMapaInterativo = "mapa_interativo.html";
|
|
public static string NomeArquivoMapaInterativoSaida = "GeoJson.json";
|
|
public static string NomeArquivoMapaGPS = "mapa_gps.html";
|
|
public static string TopicoCoordenadasGPS = "coordenadas_gps";
|
|
public static string TopicoSelecaoRuasMapa = "selecao_ruas_mapa";
|
|
public static string TopicoTrajetoriaDinamica = "trajetoria_dinamica";
|
|
}
|
|
|
|
public class MapasService
|
|
{
|
|
OpenFileDialog opfMapa;
|
|
public Process pythonProcess;
|
|
private string CaminhoSaida = Variaveis.CaminhoSistema + PythonService.CaminhoGeral + PythonService.CaminhoLeitura;
|
|
public string NomeArquivoMapa = "";
|
|
public string NomeArquivos = "";
|
|
public string CaminhoMapa = "";
|
|
public string ArquivoSaida = "";
|
|
public string MapaUrl = "";
|
|
public string CaminhoDadosMapa = "";
|
|
public MapaFeatureCollectionModel DadosMapa;
|
|
public bool Carregado = false;
|
|
|
|
|
|
public void CarregarMapa(string script, string nomeArquivoMapa, string nomeArquivoSaida, string caminhoArquivo = "")
|
|
{
|
|
NomeArquivoMapa = nomeArquivoMapa;
|
|
ArquivoSaida = nomeArquivoSaida;
|
|
|
|
opfMapa = new OpenFileDialog
|
|
{
|
|
DefaultExt = ".shp",
|
|
Filter = "Shapefile (*.shp)|*.shp|JSON Files (*.json)|*.json",
|
|
Title = "Selecionar arquivo de mapa"
|
|
};
|
|
|
|
if (!string.IsNullOrEmpty(caminhoArquivo) || opfMapa.ShowDialog() == DialogResult.OK)
|
|
{
|
|
caminhoArquivo = string.IsNullOrEmpty(caminhoArquivo) ? opfMapa.FileName : caminhoArquivo;
|
|
string extensao = "." + caminhoArquivo.Split('.')[caminhoArquivo.Split('.').Length - 1];
|
|
NomeArquivos = caminhoArquivo.Split('\\')[caminhoArquivo.Split('\\').Length - 1].Replace(extensao, "");
|
|
CaminhoMapa = caminhoArquivo.Replace(NomeArquivos, "").Replace(extensao, "").Replace("\\", "/");
|
|
|
|
pythonProcess = PythonService.RunScript(script, new string[]
|
|
{
|
|
CaminhoMapa,
|
|
NomeArquivos,
|
|
ArquivoSaida,
|
|
NomeArquivoMapa,
|
|
MapasVariaveisModel.TopicoCoordenadasGPS,
|
|
MapasVariaveisModel.TopicoSelecaoRuasMapa,
|
|
MapasVariaveisModel.TopicoTrajetoriaDinamica,
|
|
extensao == ".json" ? "1" : "0"
|
|
});
|
|
|
|
MapaUrl = new Uri(CaminhoSaida + NomeArquivoMapa).AbsoluteUri;
|
|
CaminhoDadosMapa = CaminhoSaida + ArquivoSaida;
|
|
}
|
|
}
|
|
|
|
public void CarregarMapaGPS(string nomeArquivoMapa)
|
|
{
|
|
NomeArquivoMapa = nomeArquivoMapa;
|
|
|
|
pythonProcess = PythonService.RunScript(PythonService.ScriptMapGPS, new string[]
|
|
{
|
|
CaminhoMapa,
|
|
NomeArquivos,
|
|
ArquivoSaida,
|
|
NomeArquivoMapa,
|
|
MapasVariaveisModel.TopicoCoordenadasGPS
|
|
});
|
|
|
|
MapaUrl = new Uri(CaminhoSaida + NomeArquivoMapa).AbsoluteUri;
|
|
CaminhoDadosMapa = CaminhoSaida + ArquivoSaida;
|
|
}
|
|
|
|
public void CarregarDadosMapa()
|
|
{
|
|
var dados = File.ReadAllText(CaminhoDadosMapa);
|
|
DadosMapa = JsonConvert.DeserializeObject<MapaFeatureCollectionModel>(dados);
|
|
int i = 0;
|
|
DadosMapa.features.ForEach(feature =>
|
|
{
|
|
feature.geometry.id = i.ToString();
|
|
i++;
|
|
});
|
|
}
|
|
|
|
public static void CriarArquivoMapa(List<List<GPSModel>> Trajetoria, int id = 1517, string name = "Projeto", double dist1 = 0.0, double dist2 = 0.0)
|
|
{
|
|
var featureCollection = new MapaFeatureCollectionModel
|
|
{
|
|
type = "FeatureCollection",
|
|
features = new List<MapaFeatureModel>()
|
|
};
|
|
|
|
for (int i = 0; i < Trajetoria.Count; i++)
|
|
{
|
|
var rua = Trajetoria[i];
|
|
double length = GPSService.DistanciaDoTrecho(rua);
|
|
featureCollection.features.Add(new MapaFeatureModel()
|
|
{
|
|
type = "Feature",
|
|
properties = new MapaFeaturePropertiesModel()
|
|
{
|
|
Id = (i + 1).ToString(),
|
|
Name = name,
|
|
Length = length,
|
|
Dist1 = dist1,
|
|
Dist2 = dist2
|
|
},
|
|
geometry = new MapaFeatureGeometryModel()
|
|
{
|
|
type = "LineString",
|
|
coordinates = new List<List<double>>()
|
|
}
|
|
});
|
|
|
|
// Adicionando coordenadas aos recursos
|
|
var coordinates = featureCollection.features[i].geometry.coordinates;
|
|
foreach (var gpsModel in rua)
|
|
{
|
|
var coordinate = new List<double> { gpsModel.Longitude, gpsModel.Latitude };
|
|
coordinates.Add(coordinate);
|
|
}
|
|
}
|
|
|
|
// Serializando o objeto para JSON
|
|
string jsonString = JsonConvert.SerializeObject(featureCollection);
|
|
|
|
File.WriteAllText(Variaveis.CaminhoMapasConvertidos + name + ".json", jsonString, Encoding.UTF8);
|
|
}
|
|
|
|
}
|
|
}
|