339 lines
13 KiB
C#
339 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using ArcGIS.Desktop.Catalog;
|
|
using ArcGIS.Desktop.Core;
|
|
using ArcGIS.Desktop.Framework;
|
|
using System.Collections.ObjectModel;
|
|
using ArcGIS.Core.Geometry;
|
|
using ArcGIS.Core.Data;
|
|
using ArcGIS.Desktop.Mapping;
|
|
using ArcGIS.Core.Internal.CIM;
|
|
|
|
namespace OliviaAddInPro.Helper
|
|
{
|
|
public static class HelperGdb
|
|
{
|
|
public static string out_st = string.Empty;
|
|
|
|
[Flags]
|
|
public enum TiposOpenFileDlg
|
|
{
|
|
OpenFtrClassLine=1,
|
|
OpenFtrClassPoint=2,
|
|
OpenFtrClassPolygon=4,
|
|
OpenGdb=8,
|
|
}
|
|
|
|
//Dado el tipo de FtClass y una posición inicial abre un diálogo de búsqueda de ftclass
|
|
//si se cancela o no es una feature class lo que se ha abierto devuelve null
|
|
//si no, devuelve la featureclass directamente abierta
|
|
public static FeatureClass OpenFtClassDialog(TiposOpenFileDlg tipo, string initialLoc = "")
|
|
{
|
|
FeatureClass fc = null;
|
|
string path = OpenFileDialog( tipo, initialLoc);
|
|
if(!string.IsNullOrEmpty(path))
|
|
{
|
|
fc = GetFtClass(path);
|
|
}
|
|
return fc;
|
|
}
|
|
|
|
//Libera el objeto
|
|
public static void Free(IDisposable obj)
|
|
{
|
|
obj.Dispose();
|
|
}
|
|
|
|
//Devuelve el Path del archivo seleccionado o un string vacío si se ha cancelado
|
|
public static string OpenFileDialog(TiposOpenFileDlg tipo, string initialLoc="")
|
|
{
|
|
string titulo;
|
|
titulo = "Abrir Archivo";
|
|
//Create a browse filter that uses Pro's "esri_browseDialogFilters_geodatabases" filter.
|
|
//The browse filter is used in an OpenItemDialog.
|
|
//fuentes filtros
|
|
//https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Map-Exploration/IdentifyWindow/Daml.cs
|
|
BrowseProjectFilter filtro = new BrowseProjectFilter();
|
|
if ((tipo & TiposOpenFileDlg.OpenFtrClassLine)== TiposOpenFileDlg.OpenFtrClassLine)
|
|
{
|
|
filtro.AddFilter(BrowseProjectFilter.GetFilter("esri_browseDialogFilters_featureClasses_line"));
|
|
titulo = "Abrir Feature Class";
|
|
}
|
|
if ((tipo & TiposOpenFileDlg.OpenFtrClassPoint) == TiposOpenFileDlg.OpenFtrClassPoint)
|
|
{
|
|
filtro.AddFilter(BrowseProjectFilter.GetFilter("esri_browseDialogFilters_featureClasses_point"));
|
|
titulo = "Abrir Feature Class";
|
|
}
|
|
if ((tipo & TiposOpenFileDlg.OpenFtrClassPolygon) == TiposOpenFileDlg.OpenFtrClassPolygon)
|
|
{
|
|
filtro.AddFilter(BrowseProjectFilter.GetFilter("esri_browseDialogFilters_featureClasses_polygon"));
|
|
titulo = "Abrir Feature Class";
|
|
}
|
|
if ((tipo & TiposOpenFileDlg.OpenGdb) == TiposOpenFileDlg.OpenGdb)
|
|
{
|
|
filtro.AddFilter(BrowseProjectFilter.GetFilter("esri_browseDialogFilters_geodatabases"));
|
|
titulo = "Abrir Geodatabase";
|
|
}
|
|
if(tipo==0)
|
|
{
|
|
filtro.AddFilter(BrowseProjectFilter.GetFilter(""));
|
|
}
|
|
//Display the filter in an Open Item dialog
|
|
OpenItemDialog aNewFilter = new OpenItemDialog
|
|
{
|
|
Title = titulo,
|
|
InitialLocation = initialLoc,
|
|
MultiSelect = false,
|
|
//Set the BrowseFilter property to Pro's Geodatabase filter.
|
|
BrowseFilter = filtro
|
|
};
|
|
bool? ok = aNewFilter.ShowDialog();
|
|
if ((ok ?? true) && aNewFilter.Items.Count() > 0)
|
|
return aNewFilter.Items.First().Path;
|
|
else
|
|
return string.Empty;
|
|
}
|
|
|
|
/*public static bool SelecPolig(string title, int wnd_handle, out string text_sal, out IGeometry geom_sal)
|
|
{
|
|
|
|
}
|
|
public static bool SelecInstal(string title, int wnd_handle, out string text_sal, out double x, out double y)
|
|
{
|
|
|
|
}*/
|
|
|
|
//Dado un path comprueba que sea de una gdb, termina en .gdb, o si tiene .gdb en medio lo corta ahí
|
|
//y si no tiene devuelve vacío
|
|
public static string GetPathGdb(string path)
|
|
{
|
|
string pathGdb = string.Empty;
|
|
int i = 0;
|
|
string gdbext = ".gdb";
|
|
if(path.Contains(gdbext))
|
|
{
|
|
i = path.IndexOf(gdbext, 0, path.Length);
|
|
pathGdb = path.Substring(0, i + 4);
|
|
}
|
|
|
|
return pathGdb;
|
|
|
|
}
|
|
//Dado un path aunque sea de una feature class, devuelve el path de la gdb que la contiene, o si
|
|
//es de gdb directamente, y si no tiene .gdb, devuelve null
|
|
public static Task<Geodatabase> GetGdb(string pathGdb)
|
|
{
|
|
Geodatabase fileGeodatabase = null;
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
string path = GetPathGdb(pathGdb);
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
try
|
|
{
|
|
fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(path)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "Error al abrir Geodatabase " + path + ": " + ex.Message;
|
|
return null;
|
|
}
|
|
}
|
|
return fileGeodatabase;
|
|
});
|
|
//return fileGeodatabase;
|
|
}
|
|
|
|
//Dado un path de una feature class devuelve la ftclass abierta directamente,
|
|
//o null si ha habido algún problema o no lo ha encontrado
|
|
public static FeatureClass GetFtClass(string pathFtClss)
|
|
{
|
|
FeatureClass ftclss = null;
|
|
Geodatabase gdb = GetGdb(pathFtClss).Result;
|
|
if (gdb != null)
|
|
{
|
|
ftclss = GetFtClass(System.IO.Path.GetFileNameWithoutExtension(pathFtClss), gdb).Result;
|
|
}
|
|
else //mira a ver si es shapefile
|
|
{
|
|
ftclss = GetFtClassFromShp(pathFtClss).Result;
|
|
}
|
|
|
|
return ftclss;
|
|
}
|
|
|
|
//Dado el path de una gdb y el nombre de una feature class, devuelve la
|
|
//feature class abierta, o null si hay algun problema
|
|
public static Task<FeatureClass> GetFtClass(string nameFtclss, Geodatabase gdb)
|
|
{
|
|
FeatureClass ftclss = null;
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
ftclss = gdb.OpenDataset<FeatureClass>(nameFtclss);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "Error al abrir Feature Class " + nameFtclss + ": " + ex.Message;
|
|
return null;
|
|
}
|
|
return ftclss;
|
|
});
|
|
}
|
|
|
|
//Abre una feature class cuando es un shapefile
|
|
public static Task<FeatureClass> GetFtClassFromShp(string pathShp)
|
|
{
|
|
FeatureClass ftclss = null;
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
if (pathShp.Contains(".shp"))
|
|
{
|
|
try
|
|
{
|
|
string shpname = System.IO.Path.GetFileNameWithoutExtension(pathShp);
|
|
var shapeFileConnPath = new FileSystemConnectionPath(new Uri(pathShp), FileSystemDatastoreType.Shapefile);
|
|
var shapefile = new FileSystemDatastore(shapeFileConnPath);
|
|
ftclss = shapefile.OpenDataset<FeatureClass>(shpname);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "Error al abrir Shapefile " + pathShp + ": " + ex.Message;
|
|
return null;
|
|
}
|
|
}
|
|
return ftclss;
|
|
});
|
|
}
|
|
|
|
//devuelve el campo dado el nombre
|
|
private static Task<ArcGIS.Core.Data.Field> GetFieldByName(FeatureClass ftClss, string fieldName)
|
|
{
|
|
FeatureClassDefinition ftcldef = ftClss.GetDefinition();
|
|
ArcGIS.Core.Data.Field field=null;
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
field = ftcldef.GetFields().First(x => x.Name.Equals(fieldName));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "No se encuentra el campo " + fieldName + ": " + ex.Message;
|
|
return null;
|
|
}
|
|
return field;
|
|
});
|
|
|
|
}
|
|
|
|
//Devuelve una lista con los campos de una feature class
|
|
public static Task<ObservableCollection<string>> GetFields(FeatureClass fc)
|
|
{
|
|
FeatureClassDefinition ftcldef=null;
|
|
IReadOnlyList<ArcGIS.Core.Data.Field> fields = null;
|
|
ObservableCollection<string> fields_st = new ObservableCollection<string>();
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
ftcldef = fc.GetDefinition();
|
|
fields = ftcldef.GetFields();
|
|
foreach (ArcGIS.Core.Data.Field f in fields)
|
|
{
|
|
fields_st.Add(f.Name);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "Error al leer los campos " + fc.GetName() + ": " + ex.Message;
|
|
return fields_st;
|
|
}
|
|
return fields_st;
|
|
});
|
|
}
|
|
|
|
//Devuelve comilla simple si el campo es de texto, o nada si es númerico
|
|
//var whereClause = $"{SelectedField} = {Quote(f)}{FieldValue}{Quote(f)}";
|
|
public static string Quote(ArcGIS.Core.Data.Field f)
|
|
{
|
|
return f.FieldType == FieldType.String ? "'" : "";
|
|
}
|
|
|
|
//Dado un nombre de campo, devuelve los atributos que aparecen distintos
|
|
public static Task<ObservableCollection<string>> GetAttributes(FeatureClass fc, string fieldName)
|
|
{
|
|
ObservableCollection<string> attribs_st = new ObservableCollection<string>();
|
|
return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
FeatureClassDefinition ftcldef = fc.GetDefinition();
|
|
ArcGIS.Core.Data.Field field = ftcldef.GetFields().First(x => x.Name.Equals(fieldName));
|
|
string whereClause = $"";
|
|
ArcGIS.Core.Data.QueryFilter queryFilter = new ArcGIS.Core.Data.QueryFilter { WhereClause = whereClause };
|
|
using (RowCursor rowCursor = fc.Search(queryFilter))
|
|
{
|
|
while (rowCursor.MoveNext())
|
|
{
|
|
using (Row row = rowCursor.Current)
|
|
{
|
|
attribs_st.Add(Convert.ToString(row[fieldName]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
out_st = "Error al leer los campos " + fc.GetName() + ": " + ex.Message;
|
|
return attribs_st;
|
|
|
|
}
|
|
return attribs_st;
|
|
});
|
|
}
|
|
|
|
/*public static Geometry GetGeomSel(string pathLyr, string selField, ObservableCollection<string> selAttributes, out string textoSal)
|
|
{
|
|
string _textosal = "";
|
|
Geometry geomsal = null;
|
|
|
|
/*is_str = FunGDB.is_str_field(clase_path, selField);
|
|
nval = list.selec_valores_campo(selField, out val_ids, out val_st);//valores únicos
|
|
if (nval > 0)
|
|
{
|
|
//se embucla para unir las geoms
|
|
for (int i = 0; i < nval; i++)
|
|
{
|
|
if (is_str)
|
|
consulta = selField + " = '" + val_st[i] + "'";
|
|
else
|
|
consulta = selField + " = " + val_st[i];
|
|
ids = FunGDB.dame_ids_consulta(clase_path, consulta);
|
|
fc = FunGDB.abre_ftclass(clase_path);
|
|
if ((ids != null) && (fc != null))
|
|
{
|
|
for (j = 0; j < ids.Length; j++)
|
|
{
|
|
f = fc.GetFeature(ids[j]);
|
|
geom = f.Shape;
|
|
geom_sal = FunGDB.une_geoms(geom_sal, geom);
|
|
FunGDB.libera(f);
|
|
}
|
|
}
|
|
FunGDB.libera(fc);
|
|
//Actualiza el texto de salida
|
|
_textosal = _textosal + HelperGlobal.RevisaText(val_st[i]);
|
|
}
|
|
}
|
|
textoSal = _textosal;
|
|
return geomsal;
|
|
}*/
|
|
}
|
|
}
|