using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using ArcGIS.Core.Geometry; using ArcGIS.Desktop.Core.Geoprocessing; using ArcGIS.Desktop.Mapping; using ArcGIS.Desktop.Framework.Threading.Tasks; using ArcGIS.Core.Data; using ArcGIS.Desktop.Core; using ArcGIS.Desktop.Editing; using ArcGIS.Desktop.Internal.Layouts.Utilities; namespace PruebaAddIn { /// /// Interaction logic for ProWindow1.xaml /// public partial class ProWindow1 : ArcGIS.Desktop.Framework.Controls.ProWindow { public ProWindow1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { string layerpath, outpath; layerpath = "D:\\datos_olivia_gdb\\Soria.gdb"; outpath = "D:\\datos_olivia_gdb\\Pruebas"; string nameFeatrue = "prueba"; string whereClause = "NOM_TIPO_ENTIDAD = 'Aceras'"; //layerpath = @"C:\Users\narvaling_epareja\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\SanLorenzo"; /* outpath = "C:\\Users\\narvaling_epareja\\Documents\\PruebasOlivia"; layerpath = "Z:\\Europe.gdb"; string nameFeatrue = "streets"; string whereClause = "LEFT_POSTAL_CODE = '282000'";*/ var tas = ConvertShp(layerpath, outpath, nameFeatrue, whereClause); } protected string PritnE(RowCursor rowCursorSource, string pathGeo, string pathTable) { try { var tabla = new System.IO.StreamWriter(pathTable, false); var entid = new System.IO.StreamWriter(pathGeo, false); string stdata; string stEnt; string colmn=""; List tipos = new List(); int i = -1; int siz = -1; Row row; Feature feat; Task escribe = null; Task escribeEnt = null; while (rowCursorSource.MoveNext()) { using (row = rowCursorSource.Current) { stEnt = ""; stdata = ""; feat = row as Feature; if (siz < 0)//pilla cabeceras la primera vez { var fields = row.GetFields(); siz = fields.Count; tipos = new List(siz); int ii = 0; foreach (var f in fields) { ii++; colmn = colmn + f.Name + ";"; if (ii > 10) break; } } //pilla entidad geometrica var geom = feat.GetShape(); if (geom is ArcGIS.Core.Geometry.Polyline pline) { //tratamiento de polilinea var lisCoord = pline.Copy3DCoordinatesToList(); stEnt = lisCoord.Count.ToString() + ";"; foreach (var coor in lisCoord) stEnt = stEnt + coor.X.ToString() + ";" + coor.Y.ToString() + ";" + coor.Z.ToString() + ";"; } else if (geom is ArcGIS.Core.Geometry.MapPoint point) { //tratamiento de puntos var coor = point.Coordinate3D; stEnt = "1;" + coor.X.ToString() + ";" + coor.Y.ToString() + ";" + coor.Z.ToString() + ";"; } i = 0; while (i < siz && i < 10) { stdata = stdata + row[i].ToString() + ";"; i++; } MessageBox.Show(colmn + "\n" + stdata + "\n" + stEnt); break; } } } catch (Exception e) { return e.Message; } //generalizar--- return ""; } protected string Carto2Csv(RowCursor rowCursorSource, string pathGeo, string pathTable) { try { var tabla = new System.IO.StreamWriter(pathTable, false); var entid = new System.IO.StreamWriter(pathGeo, false); string stline; string stEnt; List tipos = new List(); int i = -1; int siz = -1; Row row; Feature feat; Task escribe = null; Task escribeEnt = null; while (rowCursorSource.MoveNext()) { using (row = rowCursorSource.Current) { stline = ""; stEnt = ""; feat = row as Feature; if (siz < 0)//pilla cabeceras la primera vez { var fields = row.GetFields(); siz = fields.Count; tipos = new List(siz); foreach (var f in fields) { stline = stline + f.Name + ";"; tipos.Add(f.FieldType); } if (escribe != null) { escribe.Wait(); } escribe = tabla.WriteLineAsync(stline); //tabla.WriteLine(stline); stline = ""; } //pilla entidad geometrica var geom = feat.GetShape(); if (geom is ArcGIS.Core.Geometry.Polyline pline) { //tratamiento de polilinea var lisCoord = pline.Copy3DCoordinatesToList(); stEnt = lisCoord.Count.ToString() + ";"; foreach (var coor in lisCoord) stEnt = stEnt + coor.X.ToString() + ";" + coor.Y.ToString() + ";" + coor.Z.ToString() + ";"; if (escribeEnt != null) { escribeEnt.Wait(); } escribeEnt = entid.WriteLineAsync(stEnt); } else if (geom is ArcGIS.Core.Geometry.MapPoint point) { //tratamiento de puntos var coor = point.Coordinate3D; stEnt = "1;"+ coor.X.ToString() + ";" + coor.Y.ToString() + ";" + coor.Z.ToString() + ";"; if (escribeEnt != null) { escribeEnt.Wait(); } escribeEnt = entid.WriteLineAsync(stEnt); } i = 0; while (i < siz) { switch (tipos[i]) { case FieldType.String: //var st =a. as string; stline = stline + row[i] as string + ";"; break; case FieldType.Integer: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; case FieldType.Double: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; case FieldType.SmallInteger: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; default: stline = stline + ";"; break; } i++; } if (escribe != null) { escribe.Wait(); } escribe = tabla.WriteLineAsync(stline); } } if (escribe != null) { escribe.Wait(); } if (escribeEnt != null) { escribeEnt.Wait(); } tabla.Close(); entid.Close(); } catch (Exception e) { return e.Message; } //generalizar--- return ""; } protected Task ConvertShp(string layerpath, string outpath, string nameFeatureClass, string whereClause) { return QueuedTask.Run(() => { if (!System.IO.Directory.Exists(outpath)) System.IO.Directory.CreateDirectory(outpath); try { var path = new Uri(layerpath); using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(path))) { using (FeatureClass featureClass = fileGeodatabase.OpenDataset(nameFeatureClass)) { using (RowCursor rowCursor = featureClass.Search(new QueryFilter { WhereClause = whereClause })) { var res = PritnE(rowCursor, outpath+"\\entd.txt", outpath+"\\tabla.txt"); } } } } catch (Exception e) { MessageBox.Show(e.ToString() + e.Message); throw e; } MessageBox.Show("fin"); return "hola"; }); } /* protected Task ConvertShp(string layerpath, string outpath) { return QueuedTask.Run(() => { if (!System.IO.Directory.Exists(outpath)) System.IO.Directory.CreateDirectory(outpath); try { var tabla = new System.IO.StreamWriter("D:\\datos_olivia_gdb\\tabla.txt"); var entid = new System.IO.StreamWriter("D:\\datos_olivia_gdb\\entd.txt"); string stline; string stEnt; List tipos = new List(); int i = -1; int siz = -1; EditOperation op = new EditOperation(); var path = new Uri("D:\\datos_olivia_gdb\\Soria.gdb"); using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(path))) { //fileGeodatabase var resul = fileGeodatabase.OpenDataset("copia"); //management.CreateFeatureClass //FeatureClass featureClass_out= fileGeodatabase. using (FeatureClass featureClass = fileGeodatabase.OpenDataset("prueba")) { FeatureClassDefinition facilitySiteDefinition = featureClass.GetDefinition(); resul.CopyFrom(featureClass); resul.DeleteRows(new QueryFilter()); //featureClass_out.CopyFrom(featureClass); //featureClass_out.DeleteRows(null); //int facilityIdIndex = facilitySiteDefinition.FindField("TIPO_ENT"); string whereClause = "NOM_TIPO_ENTIDAD = 'Aceras'"; //Selection sel = featureClass.Select(new QueryFilter { WhereClause = whereClause }, SelectionType.ObjectID, SelectionOption.Normal); //FeatureClass featSel = sel as FeatureClass; //IReadOnlyList selectedOIDs = sel.GetObjectIDs(); Row row; Feature feat; //var buf = new RowBuffer(); //var insertCursor = resul.CreateInsertCursor(); using (RowCursor rowCursor = featureClass.Search(new QueryFilter { WhereClause = whereClause })) { while (rowCursor.MoveNext()) { using (row = rowCursor.Current) { stline = ""; stEnt = ""; feat = row as Feature; if (siz < 0) { var fields = row.GetFields(); siz = fields.Count; tipos = new List(siz); foreach (var f in fields) { stline = stline + f.Name + ";"; tipos.Add(f.FieldType); } tabla.WriteLine(stline); stline = ""; } resul.CopyFrom(feat); //pilla entidad geometrica var geom = feat.GetShape(); if (geom is ArcGIS.Core.Geometry.Polyline pline) { //tratamiento de polilinea var lisCoord = pline.Copy3DCoordinatesToList(); stEnt = lisCoord.Count.ToString() + ";"; foreach (var coor in lisCoord) { stEnt = stEnt + coor.X.ToString() + ";" + coor.Y.ToString() + ";" + coor.Z.ToString() + ";"; /*double x = coor.X; double y = coor.Y; double z = coor.Z; st* / } entid.WriteLine(stEnt); } else if (geom is ArcGIS.Core.Geometry.MapPoint point) { //tratamiento de puntos var coor = point.Coordinate3D; double x = coor.X; double y = coor.Y; double z = coor.Z; } //pilla atributos (falta) //notas /* //int assetNameIndex = tableDefinition.FindField("ASSETNA"); var aa = row[assetNameIndex] as ... * / //var atri = row.GetFields(); i = 0; while (i < siz) { switch (tipos[i]) { case FieldType.String: //var st =a. as string; stline = stline + row[i] as string + ";"; break; case FieldType.Integer: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; case FieldType.Double: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; case FieldType.SmallInteger: //var st =a. as string; stline = stline + row[i].ToString() + ";"; break; default: stline = stline + ";"; break; } i++; } tabla.WriteLine(stline); /*foreach (var a in atri) { switch(a.FieldType) { case FieldType.String: //var st =a. as string; break; default: stline = stline + ";" break; } }* / //featureClass_out } } } tabla.Close(); entid.Close(); } } } catch (Exception e) { string m = e.Message; m = m + " "; } var progDlg = new ProgressDialog("Running Geoprocessing Tool", "Cancel", 100, true); progDlg.Show(); var progSrc = new CancelableProgressorSource(progDlg); // prepare input parameter values to CopyFeatures tool string input_data = layerpath; //string out_workspace = ArcGIS.Desktop.Core.Project.Current.DefaultGeodatabasePath; string out_data = outpath; // make a value array of strings to be passed to ExecuteToolAsync var parameters = Geoprocessing.MakeValueArray(input_data, out_data); /* // execute the tool IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", parameters, null, new CancelableProgressorSource(progDlg).Progressor, GPExecuteToolFlags.Default); */ // dialog hides itself once the execution is complete //progDlg.Hide(); /*//input layers list should contain the path of each layer name, i.e. if the root node is "Mapping" and the layer name is "gs_points", path is "Mapping\\gs_points". List inlayers = new List(); inlayers.Add(layerpath); //outpath is just the folder to save the shapefiles in var valueArray = Geoprocessing.MakeValueArray(inlayers, outpath); // to let the GP tool run asynchronously without blocking the main thread // use the GPThread option of GPExecuteToolFlasgs // GPExecuteToolFlags flags = GPExecuteToolFlags.GPThread; // instruct the tool run non-blocking GPThread IGPResult gpResult = await Geoprocessing.ExecuteToolAsync("FeatureClassToShapefile_conversion", valueArray, null, null, null, flags);*/ /*string res = string.IsNullOrEmpty(gpResult.ReturnValue) ? $@"Error in gp tool: {gpResult.ErrorMessages}" : $@"Ok: {gpResult.ReturnValue}";* / return "hola"; }); }*/ } }