public void Export(int?pkValue = null) { try { cti.ThreadProgress.Close(progressKey); // Выбор проекции файла int?srid = SelectProjectionDialog.Select(_pgTable.Srid); if (srid == null) { return; } cti.ThreadProgress.ShowWait(progressKey); string whereSql = " where 1=1"; if (pkValue != null) { whereSql += string.Format(" and \"{0}\"={1}", _pgTable.PrimaryKey, pkValue); } int rowCount; using (var sw = new SqlWork(_connect)) { sw.sql = string.Format("select count(*) from {0}.{1} {2}", _pgTable.SchemeName, _pgTable.Name, whereSql); sw.ExecuteReader(); rowCount = sw.CanRead() ? sw.GetInt32(0) : 0; sw.Close(); } string sridWkt = ""; using (var sw = new SqlWork()) { sw.sql = "SELECT srtext FROM spatial_ref_sys where srid=" + srid; sridWkt = sw.ExecuteScalar <String>(); } var shpWork = new SHPWork(_fileInfo); List <OGRFramework.SHPWork.TypeAndName> fields; string selString = FormShapeFields(out fields); string errorString = ""; shpWork.OpenWrite(new[] { "" }, new[] { "" }, fields, out errorString, GetGeometryType(), sridWkt); if (errorString != "") { throw new Exception(errorString); } var errors = new DataTable(); errors.Columns.Add("ERROR"); using (SqlWork sw = new SqlWork(_connect)) { sw.sql = string.Format( "SELECT {3} st_astext(st_transform({2}, {5})) as sys_geom_column_for_export FROM {0}.{1} {4}", _pgTable.SchemeName, _pgTable.Name, _pgTable.GeomField, selString, whereSql, srid); sw.ExecuteReader(); List <object> values = new List <object>(); for (int rCount = 1; sw.CanRead(); rCount++) { try { values.Clear(); foreach (var f in fields) { values.Add(sw.GetValue(f.Name)); } string result = shpWork.AddObject(values, sw.GetString("sys_geom_column_for_export")); if (result != "" && errors.Rows.Count < 100) { errors.Rows.Add(string.Format("ID={0} Ошибка={1} Геометрия={2}", sw.GetInt32(_pgTable.PrimaryKey), result, sw.GetString("sys_geom_column_for_export"))); } cti.ThreadProgress.SetText(string.Format("Обработано объектов {0} из {1}", rCount, rowCount)); } catch (AccessViolationException ex) { } } if (LoadBadObjects(shpWork, errors)) { shpWork.EndWrite(out errorString, SHPWork.EndWriteOption.AppendBadGeometries); } else { shpWork.EndWrite(out errorString); } if (errorString != "") { throw new Exception(errorString); } } } catch (Exception ex) { cti.ThreadProgress.Close(progressKey); Classes.workLogFile.writeLogFile(ex, true, true); } cti.ThreadProgress.Close(progressKey); }