override public void Init(FileInfo inputFile, PgM.PgTableBaseM pgTable) { _inputFile = inputFile; _pgTable = pgTable; if (pgTable != null) { _connect = (pgTable.Source as PgVM.PgDataRepositoryVM).Connect; } _shpWork = new SHPWork(inputFile); _rowsCount = _shpWork.GetFeatureCount(); WorkerReportsProgress = true; try { _srid = _shpWork.getSRID(); } catch (Exception ex) { SelectProjectionV frmProj = new SelectProjectionV(); SelectProjectionVM datacontext = new SelectProjectionVM(frmProj); frmProj.DataContext = datacontext; if (frmProj.ShowDialog() == true) { var proj = datacontext.SelectedProj; if (proj != null) { _srid = proj.Srid; } } else { throw new Exception("Не указана проекция слоя!"); } } }
/// <summary> /// Загружать ли объекты с некорректной геометрией /// </summary> /// <param name="errors">Ошибки при выгрузке</param> /// <returns>True - если выгрузить без геометрии, False - не загружать объекты</returns> private bool LoadBadObjects(SHPWork shpWork, DataTable errors) { if (errors.Rows.Count > 0) { cti.ThreadProgress.Close(progressKey); ImportExport.ExportQuestion eq = new ImportExport.ExportQuestion(errors); eq.countsTB.Text = string.Format(Rekod.Properties.Resources.SHPExporter_LoadForPreview, errors.Rows.Count); eq.ShowDialog(); var ans = eq.Answer; cti.ThreadProgress.ShowWait(progressKey); if (ans == 0) { var bads = shpWork.BadObjectsCount.ToString(); //cti.ThreadProgress.SetText("Дозапись объектов: " + bads); форма не успевает так быстро return(true); } } return(false); }
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); }