diff --git a/Helper/HelperGdb.cs b/Helper/HelperGdb.cs index 3fd4980..f7e6e93 100644 --- a/Helper/HelperGdb.cs +++ b/Helper/HelperGdb.cs @@ -11,11 +11,14 @@ 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 { @@ -25,38 +28,28 @@ namespace OliviaAddInPro.Helper 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 = "") { - Item it = OpenFileDialog( tipo, initialLoc); - //FeatureClass fc = new FeatureClass(); - // return fc; - return null; - } + FeatureClass fc = null; + string path = OpenFileDialog( tipo, initialLoc); + if(!string.IsNullOrEmpty(path)) + { + fc = GetFtClass(path); + } + return fc; + } - public static async Task OpenGdb(string path) + //Libera el objeto + public static void Free(IDisposable obj) { - try - { - await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => { - // Opens a file geodatabase. This will open the geodatabase if the folder exists and contains a valid geodatabase. - using ( - Geodatabase geodatabase = - new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(path)))) - { - return geodatabase; - } - }); - } - catch (GeodatabaseNotFoundOrOpenedException exception) - { - // Handle Exception. - } - return null; - + obj.Dispose(); } //Devuelve el Path del archivo seleccionado o un string vacío si se ha cancelado - public static Item OpenFileDialog(TiposOpenFileDlg tipo, string initialLoc="") + public static string OpenFileDialog(TiposOpenFileDlg tipo, string initialLoc="") { string titulo; titulo = "Abrir Archivo"; @@ -100,9 +93,9 @@ namespace OliviaAddInPro.Helper }; bool? ok = aNewFilter.ShowDialog(); if ((ok ?? true) && aNewFilter.Items.Count() > 0) - return aNewFilter.Items.First(); + return aNewFilter.Items.First().Path; else - return null; + return string.Empty; } /*public static bool SelecPolig(string title, int wnd_handle, out string text_sal, out IGeometry geom_sal) @@ -114,31 +107,198 @@ namespace OliviaAddInPro.Helper }*/ - public static Table GetTable(string pathLyr) + //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) { - Table tb=null; - Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(pathLyr))); - // FeatureLayer lyr = new FeatureLayer(); + 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 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 GetFtClass(string nameFtclss, Geodatabase gdb) + { + FeatureClass ftclss = null; + return ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() => + { + try + { + ftclss = gdb.OpenDataset(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 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(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 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; + }); - return tb; } - public static ObservableCollection GetFields(FeatureClass fc) + //Devuelve una lista con los campos de una feature class + public static Task> GetFields(FeatureClass fc) + { + FeatureClassDefinition ftcldef=null; + IReadOnlyList fields = null; + ObservableCollection fields_st = new ObservableCollection(); + 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) { - ObservableCollection fields = new ObservableCollection(); - - - return fields; + return f.FieldType == FieldType.String ? "'" : ""; } - public static ObservableCollection GetAttributes(FeatureClass fc, string fieldName) + //Dado un nombre de campo, devuelve los atributos que aparecen distintos + public static Task> GetAttributes(FeatureClass fc, string fieldName) { - ObservableCollection attribs = new ObservableCollection(); + ObservableCollection attribs_st = new ObservableCollection(); + 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; + } + return attribs_st; + }); } - public static Geometry GetGeomSel(string pathLyr, string selField, ObservableCollection selAttributes, out string textoSal) + /*public static Geometry GetGeomSel(string pathLyr, string selField, ObservableCollection selAttributes, out string textoSal) { string _textosal = ""; Geometry geomsal = null; @@ -170,9 +330,9 @@ namespace OliviaAddInPro.Helper //Actualiza el texto de salida _textosal = _textosal + HelperGlobal.RevisaText(val_st[i]); } - }*/ + } textoSal = _textosal; return geomsal; - } + }*/ } } diff --git a/OptionsMenuItem_.cs b/OptionsMenuItem_.cs deleted file mode 100644 index 95384dc..0000000 --- a/OptionsMenuItem_.cs +++ /dev/null @@ -1,69 +0,0 @@ -/* - - Copyright 2019 Esri - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - - See the License for the specific language governing permissions and - limitations under the License. - -*/ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Media; -using System.Windows.Media.Imaging; - -namespace OliviaAddInPro -{ - public class OptionsMenuItem : INotifyPropertyChanged - { - public OptionsMenuItem(BitmapImage imageUri, string optionString, PaneLimpiezaSubBase subPanelViewModel) - { - _imageSource = imageUri; - _optionString = optionString; - _subPanelViewModelBase = subPanelViewModel; - } - - public event PropertyChangedEventHandler PropertyChanged; - private void OnNotifyPropertyChanged(string propName) - { - if (PropertyChanged != null) - PropertyChanged(this, new PropertyChangedEventArgs(propName)); - } - private PaneLimpiezaSubBase _subPanelViewModelBase; - - public PaneLimpiezaSubBase SubPanelViewModel - { - get { return _subPanelViewModelBase; } - set { - _subPanelViewModelBase = value; - OnNotifyPropertyChanged("SubPanelViewModel"); - } - } - private ImageSource _imageSource; - public ImageSource ImageSource - { - get { return _imageSource; } - set { _imageSource = value; } - } - - private string _optionString; - public string OptionString - { - get { return _optionString; } - set { _optionString = value; } - } - } -} diff --git a/View/PaneLimpiezaSub1.xaml.cs b/View/PaneLimpiezaSub1.xaml.cs index 718cc4c..e64dc20 100644 --- a/View/PaneLimpiezaSub1.xaml.cs +++ b/View/PaneLimpiezaSub1.xaml.cs @@ -43,9 +43,9 @@ namespace OliviaAddInPro private void Button_Click(object sender, RoutedEventArgs e) { - Item it=HelperGdb.OpenFileDialog(HelperGdb.TiposOpenFileDlg.OpenFtrClassLine | HelperGdb.TiposOpenFileDlg.OpenFtrClassPoint); - if(it!=null && it.Path.Length > 0) - label_capalimp.Content = it.Name; + string capa=HelperGdb.OpenFileDialog(HelperGdb.TiposOpenFileDlg.OpenFtrClassLine | HelperGdb.TiposOpenFileDlg.OpenFtrClassPoint); + if(!string.IsNullOrEmpty(capa)) + label_capalimp.Content = System.IO.Path.GetFileNameWithoutExtension(capa); else label_capalimp.Content = Resource1.String_selec_capa; } diff --git a/View/PaneLimpiezaSub2.xaml.cs b/View/PaneLimpiezaSub2.xaml.cs index 4818e71..c432956 100644 --- a/View/PaneLimpiezaSub2.xaml.cs +++ b/View/PaneLimpiezaSub2.xaml.cs @@ -42,7 +42,7 @@ namespace OliviaAddInPro string texto=""; if (fc != null) { - texto = System.IO.Path.GetFileName(fc.GetName()); + //texto = fc.GetName(); //saca la ventana de selección de campo ShowProWndSelectFields selfwnd = new ShowProWndSelectFields(fc, true); if(selfwnd.SelAttributes.Count>0) @@ -51,11 +51,12 @@ namespace OliviaAddInPro //HelperGdb.GetGeomSel(it.Path, selfwnd.SelField, selfwnd.SelAttributes, out texto); } } - if(!ok) - texto= Resource1.String_selec_capa; + if (!ok) + { + texto = Resource1.String_selec_capa; + } label_caparestr.Content = texto; - - + HelperGdb.Free(fc); } private void button_capaniv_Click(object sender, RoutedEventArgs e) diff --git a/View/ProWndSelectFields.xaml b/View/ProWndSelectFields.xaml index dcf5102..c18c16f 100644 --- a/View/ProWndSelectFields.xaml +++ b/View/ProWndSelectFields.xaml @@ -7,6 +7,7 @@ xmlns:extensions="clr-namespace:ArcGIS.Desktop.Extensions;assembly=ArcGIS.Desktop.Extensions" mc:Ignorable="d" Height="300" Width="243.123" WindowStartupLocation="CenterOwner" Icon="OliviaIconPro.ico" ResizeMode="NoResize" Closed="ProWindow_Closed" Closing="ProWindow_Closing" + d:DataContext="{Binding Path=ui.ShowProWndSelectFields}" > @@ -18,9 +19,9 @@