373 lines
16 KiB
C#
373 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using OliviaAddInPro.Model;
|
|
using ArcGIS.Core.Data;
|
|
using ArcGIS.Core.Geometry;
|
|
using OliviaAddInPro.Helper;
|
|
using ArcGIS.Core.Internal.Data;
|
|
using ArcGIS.Desktop.Framework.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace OliviaAddInPro.Services
|
|
{
|
|
public enum ModosEjec
|
|
{
|
|
Sectoriza,
|
|
Planifica,
|
|
SoloPlanifica
|
|
|
|
}
|
|
//Clase que realiza las funciones de la ejecución
|
|
public class EjecServ
|
|
{
|
|
//Cadenas de nombres internos para la exportación/importación de los archivos
|
|
public string prefNameExport = "data_";
|
|
public string extShp = ".shp";
|
|
public string prefNameExportNw = "nw_";
|
|
|
|
public string ErrStr = "";
|
|
SpatialQueryFilter filtroEspacial = null;
|
|
SpatialReference spatRef = null;
|
|
SpatialReference spatRefData = null;
|
|
|
|
public TratamientoComun com;
|
|
/**
|
|
* Acciones para comenzar ejecución
|
|
* Modo 0, sectorizar
|
|
* Modo 1, planificar
|
|
*/
|
|
public bool ExportaEjec(ModosEjec modo, out string ErrStr)
|
|
{
|
|
ErrStr = string.Empty;
|
|
try
|
|
{
|
|
com.ProgrSrc.Init("Exportando datos");
|
|
//Comprueba que tiene las columnas necesarias para planificar
|
|
if ((modo == ModosEjec.Planifica) && !CompruebaPlanif())
|
|
{
|
|
ErrStr = "En la capa de ámbitos seleccionada no se encuentran columnas de SECTOR y/o SECUENCIA, necesarias para planificar";
|
|
return false;
|
|
}
|
|
|
|
//Cuenta las filas que cumplen la consulta
|
|
int nelems = HelperGdb.GetNumElems(com.CapaElems, com.ConsultaAmbs);
|
|
if (nelems <= 0)
|
|
{
|
|
ErrStr = "No existen ámbitos que cumplan las condiciones introducidas para la exportación " + com.ConsultaAmbs;
|
|
return false;
|
|
}
|
|
|
|
com.ProgrSrc.IncMessage(0, "Exportando geometria");
|
|
|
|
//Obtiene la geometría que envuelve a los ámbitos
|
|
Geometry geom_export = null;
|
|
geom_export = GetGeomAmbitsExport();
|
|
if (geom_export == null || geom_export.IsEmpty)
|
|
{
|
|
ErrStr = "No se ha podido generar geometría de los ámbitos" + com.ConsultaAmbs + ErrStr;
|
|
return false;
|
|
}
|
|
|
|
//mira spatialreference de los datos de entrada
|
|
spatRefData = geom_export.SpatialReference;
|
|
//AQUÍ COMPARAR SI ES IGUAL QUE SPATREFDEF Y SI NO, REPROYECTAR
|
|
/*if(spatRef.Wkid!=GeneralDef.SpatRefDef)
|
|
{
|
|
/*
|
|
* FALTA HACER
|
|
* dame_geom_coords en v2010
|
|
*/
|
|
//}
|
|
|
|
//crea el filtro de exportación
|
|
filtroEspacial = HelperGdb.CreateFiler(com.ConsultaAmbs, geom_export);
|
|
if (filtroEspacial == null)
|
|
{
|
|
ErrStr = "Error al crear el filtro de exportacion de los ámbitos";
|
|
return false;
|
|
}
|
|
//Termina de preparar nombre
|
|
//Prepara nombre
|
|
string fechaHora = string.Empty;
|
|
//se consigue el tiempo en este instante para añadirlo a los nombres de los archivos de salida (shapefiles)
|
|
fechaHora = DateTime.Now.ToString("yyyyMMdd_Hmmss");
|
|
//Pone nombre al shape en función de los ámbitos, el tratamiento, y los polígonos + timestamp
|
|
com.NombreShpExport = prefNameExport + com.NombreShpExp_PrefTto + "_" + fechaHora + extShp;
|
|
|
|
com.ProgrSrc.IncMessage(10, "Exportando ámbitos de trabajo");
|
|
|
|
//exporta los datos de entrada
|
|
if (!HelperGdb.ExportShp2(com.CapaElems, filtroEspacial, com.NombreShpExport, OliviaGlob.Paths.DirData, com.ProgrSrc._ProgrSrc, null, 40))
|
|
{
|
|
ErrStr = "Error al exportar los ámbitos: " + HelperGdb.OutStr;
|
|
return false;
|
|
}
|
|
if (com.ProgrSrc._ProgrSrc.Getcancelled())
|
|
{
|
|
ErrStr = Resource1.String_cancel_progreso;
|
|
return false;
|
|
}
|
|
//Guarda el nombre
|
|
OliviaGlob.Paths.PathData = OliviaGlob.Paths.DirData + com.NombreShpExport;
|
|
|
|
com.ProgrSrc.IncMessage(10, "Preparando exportación de red navegable");//50%
|
|
|
|
//hace intersecciones espaciales en caso de ámbitos lineales para quitar los que tienen más parte fuera de la zona que dentro
|
|
//REVISAR
|
|
/*if (((com.GeomNiv != null) || (com.GeomZon != null)) &&
|
|
(OliviaGlob.IsReco() || (OliviaGlob.IsLimp() && (com.TipoTto < (int)LimpiezaDef.TiposTto.TtoPapeVaci))))
|
|
{
|
|
if (!HelperGdb.RemoveRowsGeom(OliviaGlob.Paths.PathData, geom_export, 0.4))
|
|
{
|
|
ErrStr = "Error al quitar los ámbitos que sobresalen: " + HelperGdb.OutStr;
|
|
return false;
|
|
}
|
|
}*/
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
//comprueba, si hay restricciones de circulación y hay instalación, que la instalación no está en ellas
|
|
if (!com.CoordsInstal.IsEmpty && (com.CoordsInstal.X != 0) && (com.GeomRestr != null))
|
|
{
|
|
Respuesta<bool> resp = HelperGdb.IsPtoInGeom(com.CoordsInstal, com.GeomRestr);
|
|
if (!resp.Value && resp.HasError)
|
|
{
|
|
ErrStr = "Error al comprobar si la instalación está contenida en el polígono de restricciones: " + resp.Error.ElementAt(0);
|
|
return false;
|
|
}
|
|
else if (resp.Value) //sí está contenido en las restricciones
|
|
{
|
|
ErrStr = "Error, la instalación está en la zona restringida a la circulación ";
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
//exporta la red navegable (buffer, le quita las restr...)
|
|
//se obtiene la geometría a intersecar con la red, que será la que contiene
|
|
//a todos los ámbitos y la instalación, ampliada un buffer, mayor si hay
|
|
//restricciones de circulación
|
|
geom_export = HelperGdb.BufferGeom(geom_export, com.BuffExport).Value;
|
|
if (geom_export == null)
|
|
{
|
|
ErrStr = "Error al añadir buffer a la geometría";
|
|
return false;
|
|
}
|
|
//quita las restricciones
|
|
if (com.GeomRestr != null)
|
|
{
|
|
geom_export = HelperGdb.QuitaGeom(geom_export, com.GeomRestr);
|
|
if (geom_export == null)
|
|
{
|
|
ErrStr = "Error al intersecar con las restricciones.";
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
//comprueba si la geometría de exportación contiene a la instalación y a la planta de descarga
|
|
Coordinate2D[] coords = { com.CoordsInstal, com.CoordsPlanta };
|
|
for (int i = 0; i < coords.Length; i++)
|
|
{
|
|
if (!coords[i].IsEmpty && (coords[i].X != 0))
|
|
{
|
|
Respuesta<bool> resp = HelperGdb.IsPtoInGeom(coords[i], geom_export);
|
|
if (!resp.Value) //si no lo contiene
|
|
{
|
|
//ha ido mal
|
|
if (resp.HasError)
|
|
{
|
|
ErrStr = "Error al comprobar si la instalación está contenida en el polígono de exportación: " + resp.Error.ElementAt(0);
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
//amplía la geom
|
|
Respuesta<ArcGIS.Core.Geometry.Geometry> respGeom = HelperGdb.AddPtoInGeom(coords[i], geom_export);
|
|
if (respGeom.Value == null)
|
|
{
|
|
//ha ido mal
|
|
if (resp.HasError)
|
|
ErrStr = resp.Error.ElementAt(0);
|
|
else
|
|
ErrStr = "Error al incluir punto de instalación en polígono de exportación.";
|
|
return false;
|
|
}
|
|
//actualiza la geometría
|
|
geom_export = respGeom.Value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//mira spatialreference del nw
|
|
FeatureClass fc = HelperGdb.GetFtClass(OliviaGlob.Paths.PathGdbNw);
|
|
/*
|
|
if (fc != null)
|
|
{
|
|
spatRef = fc.GetDefinition().GetSpatialReference();
|
|
//AQUÍ COMPARAR SI ES IGUAL QUE SPATREFDEF Y SI NO, REPROYECTAR
|
|
if (spatRef.Wkid != spatRefData.Wkid)
|
|
{
|
|
var sp_ref = SpatialReferenceBuilder.CreateSpatialReference(102629); // NAD83 SP AL E FIPS 0101 Feet
|
|
|
|
var envi = Geoprocessing.MakeEnvironmentArray(outputCoordinateSystem: sp_ref);
|
|
//https://pro.arcgis.com/es/pro-app/2.8/tool-reference/environment-settings/output-coordinate-system.htm
|
|
//ProjectionTransformation transf_forTarget = ProjectionTransformation.Create(spatRef, spatRefData);
|
|
|
|
//geom_export=GeometryEngine.Instance.ProjectEx(geom_export, transf_forTarget);
|
|
//transf_forTarget.Transformation
|
|
/*
|
|
* FALTA HACER
|
|
* /
|
|
}
|
|
}*/
|
|
|
|
//Hace el filtro con la geometría final
|
|
filtroEspacial = HelperGdb.CreateFiler(String.Empty, geom_export);
|
|
if (filtroEspacial == null)
|
|
{
|
|
ErrStr = "Error al crear el filtro de exportacion de la red navegable";
|
|
return false;
|
|
|
|
}
|
|
com.ProgrSrc.IncMessage(10, "Exportando red navegable");//60%
|
|
|
|
//Prepara nombre de exportación
|
|
com.NombreShpExportNw = prefNameExportNw + fechaHora + extShp;
|
|
//exporta los datos de entrada
|
|
if (!HelperGdb.ExportShp2(OliviaGlob.Paths.PathGdbNw, filtroEspacial, com.NombreShpExportNw, OliviaGlob.Paths.DirData, com.ProgrSrc._ProgrSrc, spatRefData,100))
|
|
{
|
|
ErrStr = "Error al exportar la red navegable: " + HelperGdb.OutStr;
|
|
return false;
|
|
}
|
|
//guarda los nombres del shape
|
|
OliviaGlob.Paths.PathNW = OliviaGlob.Paths.DirData + com.NombreShpExportNw;
|
|
if (com.ProgrSrc._ProgrSrc.Getcancelled())
|
|
{
|
|
ErrStr = Resource1.String_cancel_progreso;
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrStr = "Errores al exportar para comenzar la ejecución: " + ex.Message;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Prepara la geometría para exportar los ámbitos
|
|
*/
|
|
public Geometry GetGeomAmbitsExport()
|
|
{
|
|
Geometry geomAux = null;
|
|
Geometry geomAmbits = null;
|
|
|
|
ErrStr = string.Empty;
|
|
FeatureClass fc = HelperGdb.GetFtClass(com.CapaElems);
|
|
if (fc == null)
|
|
{
|
|
ErrStr = "No se ha podido abrir la clase " + com.CapaElems;
|
|
return null;
|
|
}
|
|
|
|
//Hace la intersección de zonas y niveles
|
|
geomAux = null;
|
|
if (com.GeomNiv != null || com.GeomZon != null)
|
|
{
|
|
geomAux = HelperGdb.IntersectGeom(com.GeomNiv, com.GeomZon);
|
|
if (geomAux == null)
|
|
{
|
|
ErrStr = "Error al intersecar zonas y/o niveles.";
|
|
return null;
|
|
}
|
|
if (geomAux.IsEmpty)
|
|
{
|
|
ErrStr = "No existen ámbitos en la intersección entre zonas y/o niveles.";
|
|
return null;
|
|
}
|
|
}
|
|
//comprueba que, en el caso de ejes de vía, hayan metido polígono de exportación
|
|
if ((geomAux == null) && (com.CapaElems == OliviaGlob.Paths.PathGdbNw))
|
|
{
|
|
ErrStr = "Al emplear ejes de calle como ámbitos es necesario indicar polígono de exportación";
|
|
return null;
|
|
}
|
|
|
|
//prepara el filtro con consulta y espacial
|
|
SpatialQueryFilter filtro = HelperGdb.CreateFiler(com.ConsultaAmbs, geomAux);
|
|
if (geomAux == null)
|
|
{
|
|
//Ahora hace la geometría de los ámbitos que cumplen la consulta, si no hay ya geometría
|
|
geomAmbits = HelperGdb.GetGeomConvexHull(fc, filtro).Result;
|
|
if (geomAmbits == null || geomAmbits.IsEmpty)
|
|
{
|
|
ErrStr = "No se ha podido generar geometría de los ámbitos" + com.ConsultaAmbs + HelperGdb.OutStr;
|
|
return null;
|
|
}
|
|
geomAux = geomAmbits;
|
|
}
|
|
//le quita las restricciones
|
|
if (com.GeomRestr != null)
|
|
{
|
|
geomAux = HelperGdb.QuitaGeom(geomAux, com.GeomRestr);
|
|
if (geomAux == null)
|
|
{
|
|
ErrStr = "Error al intersecar con las restricciones.";
|
|
return null;
|
|
}
|
|
}
|
|
HelperGdb.Free(fc);
|
|
return geomAux;
|
|
}
|
|
|
|
/**
|
|
* Devuelve el string a concatenar en el nombre del path dependiendo de los polígonos seleccionados (zonas, turnos... etc)
|
|
*/
|
|
public string DameStrPoligs()
|
|
{
|
|
string str = "";
|
|
|
|
if (com.TextGeomNiv != "" && com.GeomNiv != null)
|
|
str += "_N" + com.TextGeomNiv;
|
|
if (com.TextGeomZon != "" && com.GeomZon != null)
|
|
str += "_Z" + com.TextGeomZon;
|
|
if (com.TextGeomRestr != "" && com.GeomRestr != null)
|
|
str += "_R" + com.TextGeomRestr;
|
|
|
|
return str;
|
|
}
|
|
|
|
public bool ComprCamposPlanif(string pathCapa)
|
|
{
|
|
int NCAMPS = 2;
|
|
string[] camps;
|
|
camps = new string[NCAMPS];
|
|
camps[0] = LimpiezaDef.Campos.consulta_sector;
|
|
camps[1] = LimpiezaDef.Campos.consulta_secuen;
|
|
return HelperGdb.CheckFileds(pathCapa, camps) == 0;
|
|
}
|
|
|
|
/**
|
|
* Comprueba lo necesario para ver si hay campos para la planificación
|
|
*/
|
|
public bool CompruebaPlanif()
|
|
{
|
|
//si ha importado no hace falta que compruebe, seguro que las tiene
|
|
if (OliviaGlob.IsConfig2())
|
|
return true;
|
|
//no ha importado, comprueba capa
|
|
if (ComprCamposPlanif(com.CapaElems))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|