/// <summary> /// 显示报告窗口为模态窗口 /// </summary> /// <param name="proj">投影的目标工程</param> /// <param name="message"></param> /// <param name="type">报告类型</param> public void ShowReport(MapWinGIS.GeoProjection proj, string message, ReportType type) { this.Text = type == ReportType.Loading ? "投影检查结果" : "投影分配结果"; this.SetProjectProjection(proj, type); this.ShowReportCore(proj, message); this.ShowDialog(); }
/// <summary> /// Checks user input to start transformation /// </summary> private void btnOk_Click(object sender, EventArgs e) { CoordinateSystem cs = this.ProjectionTreeView1.SelectedCoordinateSystem; if (cs == null) { MessageBox.Show("No projection is selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } IEnumerable <string> filenames = LayersControl1.Filenames; if (filenames.Count() == 0) { MessageBox.Show("No files are selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (!proj.ImportFromEPSG(cs.Code)) { MessageBox.Show("Failed to initialize the selected projection", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } this.DoReprojection(filenames, proj, false); }
/// <summary> /// 查找坐标系统 /// </summary> private CoordinateSystem GetCoordinateSystemCore(MapWinGIS.GeoProjection proj) { string name = proj.Name.ToLower();//获得Proj的名字 if (proj.IsEmpty) { return(null); } else if (proj.IsGeographic)//地理坐标系统 { foreach (GeographicCS gcs in this.m_listGCS) { if (gcs.Name.ToLower() == name) { return(gcs); } } } else if (proj.IsProjected)//投影坐标系统 { foreach (ProjectedCS pcs in this.m_listPCS) { if (pcs.Name.ToLower() == name) { return(pcs); } } } return(null); }
/// <summary> /// Tries to assign the projection and reproject the data back to WGS 84 to make sure /// that it is placed right on the workld map /// </summary> private void btnTest_Click(object sender, EventArgs e) { string filename = LayersControl1.SelectedFilename; if (filename == "") { MessageBox.Show("No file is selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // initializing target projection CoordinateSystem cs = this.ProjectionTreeView1.SelectedCoordinateSystem; if (cs == null) { MessageBox.Show("No projection is selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (!proj.ImportFromEPSG(cs.Code)) { MessageBox.Show("Failed to initialize projection: " + cs.Name, "Unsupported projection", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { frmProjectionResults form = new frmProjectionResults(m_mapWin); if (form.Assign(filename, proj)) { form.ShowDialog(this); } form.Dispose(); } }
/// <summary> /// 显示选择的投影的属性 /// </summary> /// <param name="proj">选择的投影</param> private void ShowProjectionProperties(MapWinGIS.GeoProjection proj) { if (proj == null || proj.IsEmpty) { return; } CoordinateSystem cs = null; if (m_database != null) { cs = m_database.GetCoordinateSystem(proj, ProjectionSearchType.Enhanced); } if (cs != null) { frmProjectionProperties form = new frmProjectionProperties(cs, m_database); form.ShowDialog(this); form.Dispose(); } else { frmProjectionProperties form = new frmProjectionProperties(proj); form.ShowDialog(this); form.Dispose(); } }
/// <summary> /// 显示报告窗口为非模态窗口 /// 初始化进度 /// </summary> /// <param name="proj">投影的目标工程</param> public void InitProgress(MapWinGIS.GeoProjection proj) { this.Text = "重新投影"; this.label1.Text = "进行文件重新投影"; this.SetProjectProjection(proj, ReportType.Loading); this.lblProjection.Visible = false; this.Show(); }
/// <summary> /// 显示对话框,选择投影,提示是否将投影应用于项目,并返回用户选择的投影 /// 在状态栏和项目属性中可以调用 /// </summary> public MapWinGIS.GeoProjection SetProjectProjectionByDialog() { MapWinGIS.GeoProjection proj = this.GetProjectionFromUser(); //从对话框中获取到选择的投影 bool needsReloading = false; if (proj != null) { //检测是否需要重新加载项目,来重新设置投影 if (Program.frmMain.Layers.NumLayers > 0 && !Program.frmMain.Project.GeoProjection.IsEmpty) { MapWinGIS.Extents ext = Program.frmMain.MapMain.MaxExtents; if (!proj.IsSameExt[Program.frmMain.Project.GeoProjection, ext]) //检测当前投影和项目投影是否一致 { //不一致,则重投 if (MessageBox.Show("此操作需要重新加载项目,来在图层上重新投影。继续?", Program.frmMain.ApplicationInfo.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { needsReloading = true; } else { return(null); } } } //设置新的投影 MapWinGIS.GeoProjection projOld = Program.frmMain.Project.GeoProjection; Program.frmMain.Project.GeoProjection = proj; if (needsReloading) //需要重新加载,则保存加载 { // 保存原来的 if (this.Modified) { Program.frmMain.DoSave(); } // 取消保存 if (this.Modified) { Program.frmMain.Project.GeoProjection = projOld; return(null); } else { // 确保用户在重新加载时看见必要的对话框 Program.frmMain.ApplicationInfo.ShowLoadingReport = true; Program.frmMain.ApplicationInfo.NeverShowProjectionDialog = false; Program.appInfo.ProjectReloading = true; return(proj); } } } return(null); }
/// <summary> /// Runs the operation /// </summary> private void btnOk_Click(object sender, EventArgs e) { CoordinateSystem cs = this.ProjectionTreeView1.SelectedCoordinateSystem; if (cs == null) { MessageBox.Show("No projection is selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (this.LayersControl1.Filenames.Count() == 0) { MessageBox.Show("No files are selected", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MapWinGIS.GeoProjection projection = new MapWinGIS.GeoProjection(); if (projection.ImportFromEPSG(cs.Code)) { frmTesterReport report = new frmTesterReport(); int count = 0; // number of successfully processed files foreach (string name in this.LayersControl1.Filenames) { LayerSource layer = new LayerSource(name); string projName = layer.Projection != null ? layer.Projection.Name : ""; if (layer.Type != LayerSourceType.Undefined) { layer.Projection = projection; count++; } else { report.AddFile(name, projName, ProjectionOperaion.Skipped, ""); } } if (count > 0) { MessageBox.Show(string.Format("The projection was successfully assigned to the files: {0}", count), m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } if (report.MismatchedCount > 0) { report.ShowReport(projection, "The following files were not processed:", ReportType.Assignment); } this.LayersControl1.UpdateProjections(); } else { MessageBox.Show("Failed to initialize the selected projection", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } }
/// <summary> /// 更新坐标参考系表格Proj4行的Proj4字段 /// 假设在数据库中坐标参考系统表格(从Sqlite中移除) /// </summary> /// <returns></returns> public void UpdateProj4Strings(string dbName) { DataSet ds = new DataSet(); DbDataAdapter adapter = m_provider.CreateDataAdapter(); DbConnection conn = m_provider.CreateConnection(dbName); try { DataTable dt = new DataTable(); DbCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT [COORD_REF_SYS_CODE], [proj4] FROM [Coordinate Reference System]"; adapter.SelectCommand = cmd; adapter.Fill(dt); DbCommand cmdUpdate = conn.CreateCommand(); cmdUpdate.CommandText = "UPDATE [Coordinate Reference System] SET proj4 = @proj4 WHERE COORD_REF_SYS_CODE = @cs"; DbParameter param = m_provider.CreateParameter(); param.ParameterName = "@proj4"; param.DbType = DbType.String; param.Size = 254; param.SourceColumn = "proj4"; cmdUpdate.Parameters.Add(param); param = m_provider.CreateParameter(); param.ParameterName = "@cs"; param.DbType = DbType.Int32; param.Size = 10; param.SourceColumn = "COORD_REF_SYS_CODE"; cmdUpdate.Parameters.Add(param); //cmdUpdate.Parameters.Add("@proj4", OleDbType.Char, 254, "proj4"); //cmdUpdate.Parameters.Add("@cs", OleDbType.Integer, 10, "COORD_REF_SYS_CODE"); adapter.UpdateCommand = cmdUpdate; int count = 0; MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); for (int i = 0; i < dt.Rows.Count; i++) { int code = (int)dt.Rows[i][0]; if (proj.ImportFromEPSG(code)) { string proj4 = proj.ExportToProj4(); dt.Rows[i][1] = proj4; count++; } } adapter.Update(dt); MessageBox.Show(string.Format("Records updated: {0}", count)); } finally { ds.Clear(); } }
/// <summary> /// 返回对应于给定的方言字段的代码 /// </summary> /// <param name="proj">投影对象:WKT,proj4格式将被测试</param> /// <returns>EPSG 代码</returns> public int EpsgCodeByDialectString(MapWinGIS.GeoProjection proj) { int code = this.EpsgCodeByDialectString(proj.ExportToProj4()); if (code == -1) { code = this.EpsgCodeByDialectString(proj.ExportToWKT()); } return(code); }
/// <summary> /// 从数据库中指定的投影字段得到坐标系统. 任何投影格式都能被使用. /// </summary> public CoordinateSystem GetCoordinateSystem(string str, ProjectionSearchType searchType) { CoordinateSystem cs = null; MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (proj.ImportFromAutoDetect(str)) { cs = this.GetCoordinateSystem(proj, searchType);//返回proj相关的坐标系统 } return(cs); }
/// <summary> /// Assignes selected projection and displays results /// </summary> public bool Assign(string filename, MapWinGIS.GeoProjection proj) { MapWinGIS.GeoProjection projWGS84 = new MapWinGIS.GeoProjection(); if (!projWGS84.ImportFromEPSG(4326)) { MessageBox.Show("Failed to initialize WGS84 coordinate system.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } bool sameProj = proj.get_IsSame(projWGS84); int count = 0; bool success = false; int rowIndex = dgv.Rows.Add(); m_shapefile = new MapWinGIS.Shapefile(); if (m_shapefile.Open(filename, m_mapWin.Layers as MapWinGIS.ICallback)) { this.Text = "Assigning projection: " + System.IO.Path.GetFileName(filename); this.lblProjection.Text = "Projection: " + proj.Name; // it will be faster to assing new instance of class // as ImportFromEPSG() is slow according to GDAL documentation m_shapefile.GeoProjection = proj; if (!sameProj) { // we can't show preview on map without reprojection if ((m_shapefile.StartEditingShapes(true, null))) { if (m_shapefile.ReprojectInPlace(projWGS84, ref count)) { success = true; } } } else { success = true; } } if (success) { this.AddShapefile(m_shapefile); return(true); } else { // no success in reprojection m_shapefile.Close(); return(false); } }
/// <summary> /// 投影显示的项目 /// </summary> /// <param name="proj">投影的目标工程</param> /// <param name="reportType"></param> private void SetProjectProjection(MapWinGIS.GeoProjection proj, ReportType reportType) { string suffix = "目标投影: "; if (proj == null || proj.IsEmpty)//投影目标为空 { lblProjection.Text = suffix + "没有定义"; } else { lblProjection.Text = suffix + proj.Name; } }
/// <summary> /// Shows list of options for projection mismatch situations /// </summary> /// <param name="dontShow">User has marked 'never show the dialog' checkbox</param> public int ShowProjectionMismatch(ArrayList list, int selectedIndex, MapWinGIS.GeoProjection projectProj, MapWinGIS.GeoProjection layerProj, out bool useForOthers, out bool dontShow) { if (projectProj == null || layerProj == null) { throw new ArgumentException("No projections for mismatch dialog specified"); } m_projectProj = projectProj; m_layerProj = layerProj; this.Text = "Projection mismatch"; // PM 2013-05-03: // this.lblMessage.Text = "Layer projection is different from project one." + Environment.NewLine + "Choose the way how to handle it:"; this.lblMessage.Text = "Layer projection is different from project one." + Environment.NewLine + "How do you want to handle this?"; return(this.ShowProjectionDialog(list, selectedIndex, out useForOthers, out dontShow)); }
/// <summary> /// 执行相同行动的报告显示,它不依赖于报告的类型(层加载或分配投影) /// </summary> /// <param name="proj"></param> /// <param name="message"></param> private void ShowReportCore(MapWinGIS.GeoProjection proj, string message) { if (message == "") { message = "下列文件是由于投影的不匹配或不存在的影响:"; } this.label1.Text = message; this.lblProjection.Visible = true; this.lblFile.Visible = false; this.progressBar1.Visible = false; this.button1.Visible = true; if (this.Visible) { this.Visible = false; } }
/// <summary> /// Shows projection absence dialog /// </summary> public int ShowProjectionAbsence(ArrayList list, int selectedIndex, MapWinGIS.GeoProjection projectProj, out bool useForOthers, out bool dontShow) { if (projectProj == null) { throw new ArgumentException("No projections for mismatch dialog specified"); } m_projectProj = projectProj; // PM 2013-05-03: // this.Text = "Projection absence"; // this.lblMessage.Text = "Layer projection isn't specified." + Environment.NewLine + "Choose the way how to handle it:"; this.Text = "Missing projection file"; this.lblMessage.Text = "This layer's projection is unknown" + Environment.NewLine + "How do you want to handle this?"; btnLayer.Visible = false; return(this.ShowProjectionDialog(list, selectedIndex, out useForOthers, out dontShow)); }
/// <summary> /// 显示“选择投影”对话框,并且返回用户选择的投影 /// </summary> public MapWinGIS.GeoProjection GetProjectionFromUser() { //frmChooseProjection form = new frmChooseProjection(Program.ProjectionDB, Program.frmMain); MapWinGIS.GeoProjection proj = null; //if (form.ShowDialog(Program.frmMain) == DialogResult.OK) //{ // if (form.projectionTreeView1.SelectedProjection == null && form.projectionTreeView1.SelectedCoordinateSystem != null) // { // MessageBox.Show("初始化选择的投影出错!", Program.appInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error); // } // else // { // proj = form.projectionTreeView1.SelectedProjection; // } //} //form.Dispose(); return(proj); }
/// <summary> /// 创建一个frmProjectionCompare类的新实例 /// 构造函数 /// </summary> /// <param name="projectProj">项目投影</param> /// <param name="layerProj">图层投影</param> /// <param name="database">数据库</param> public frmProjectionCompare(MapWinGIS.GeoProjection projectProj, MapWinGIS.GeoProjection layerProj, ProjectionDatabase database) { InitializeComponent(); m_projectProj = projectProj; m_layerProj = layerProj; m_database = database; this.lblProject.Text = "项目: " + projectProj.Name; this.lblLayer.Text = "图层: " + layerProj.Name; this.txtProject.Text = projectProj.ExportToProj4(); this.txtLayer.Text = layerProj.ExportToProj4(); this.btnLayer.Click += delegate(object sender, EventArgs e) { this.ShowProjectionProperties(m_layerProj); }; this.btnProject.Click += delegate(object sender, EventArgs e) { this.ShowProjectionProperties(m_projectProj); }; }
/// <summary> /// 返回与给定的GeoProjection相关的坐标系统对象. /// 这个属性的计算量很大. /// </summary> public CoordinateSystem GetCoordinateSystem(MapWinGIS.GeoProjection projection, ProjectionSearchType searchType) { if (m_dbname == "") { return(null); } // standard CoordinateSystem cs = this.GetCoordinateSystemCore(projection); if (searchType == ProjectionSearchType.Standard || cs != null) { return(cs); } // enhanced MapWinGIS.GeoProjection projTest = new MapWinGIS.GeoProjection(); if (projTest.ImportFromProj4(projection.ExportToProj4())) { cs = this.GetCoordinateSystemCore(projection); } if (searchType == ProjectionSearchType.Enhanced || cs != null) { return(cs); } // dialects this.RefreshDialects(); int code = this.EpsgCodeByDialectString(projection); if (code != -1) { return(this.GetCoordinateSystem(code)); } return(null); }
/// <summary> /// 显示投影进度 /// </summary> /// <param name="proj">投影的目标工程</param> public void ShowReprojectionProgress(MapWinGIS.GeoProjection proj) { m_report.InitProgress(proj); }
/// <summary> /// 投影改变时,修改状态栏名字改 /// </summary> internal void HandleProjectionChanged(MapWinGIS.GeoProjection oldProjection, MapWinGIS.GeoProjection newProjection) { this.StatusBarPanelProjection.Text = !newProjection.IsEmpty ? newProjection.Name : "Not defined"; }
/// <summary> /// Starts identification /// </summary> private void btnStart_Click(object sender, EventArgs e) { if (textBox1.Text == "") { Globals.MessageBoxInformation("No input projection is specified", "Enter projection"); return; } MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (!proj.ImportFromProj4(textBox1.Text)) { if (!proj.ImportFromWKT(textBox1.Text)) { if (!proj.ImportFromESRI(textBox1.Text)) { Globals.MessageBoxInformation("The string can't be identified as one of the following formats: proj4, OGC WKT, ESRI WKT", "Invalid projection"); return; } } } ProjectionDatabase db = m_mapWin.ProjectionDatabase as ProjectionDatabase; if (db == null) { Globals.MessageBoxInformation("Projection database wasn't loaded", "Database inaccessible"); return; } listBox1.Items.Clear(); CoordinateSystem cs = db.GetCoordinateSystem(proj, ProjectionSearchType.UseDialects); if (cs != null) { // easy case - it was found by name listBox1.Items.Add(cs); Globals.MessageBoxExlamation("Projection was identified.", "Success"); return; } if (listBox1.Items.Count == 0 || !chkBreak.Checked) { Cursor oldCursor = this.Cursor; this.Cursor = Cursors.WaitCursor; try { // difficult one MapWinGIS.GeoProjection projTest = new MapWinGIS.GeoProjection(); bool isSame = false; if (proj.IsGeographic) { foreach (GeographicCS gcs in db.GeographicCS) { if (projTest.ImportFromProj4(gcs.proj4)) { if (m_bounds != null) { isSame = projTest.get_IsSameExt(proj, m_bounds, 6); } else { isSame = projTest.get_IsSame(proj); } if (isSame) { listBox1.Items.Add(gcs); if (chkBreak.Checked) { break; } } } } } else if (proj.IsProjected) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); int count = 0; foreach (ProjectedCS pcs in db.ProjectedCS) { if (projTest.ImportFromProj4(pcs.proj4)) { count++; if (m_bounds != null) { isSame = projTest.get_IsSameExt(proj, m_bounds, 6); } else { isSame = projTest.get_IsSame(proj); } if (isSame) { listBox1.Items.Add(pcs); if (chkBreak.Checked) { break; } } } } } } finally { this.Cursor = oldCursor; } } if (listBox1.Items.Count == 0) { Globals.MessageBoxExlamation("Projection isn't present in the database", "Unknown projection"); } else { // Globals.MessageBoxExlamation("Projection wasn't identified. One of the listed projections could be the right one", "Results"); Globals.MessageBoxExlamation("Projection was identified. One of the listed projections should be the right one", "Results"); } }
/// <summary> /// Analyzes user input. Closes the dialog if needed. /// </summary> private void btnOk_Click(object sender, EventArgs e) { string MSG_INVALID_PROJECTION = "Invalid projection"; string text = this.textBox1.Text.Trim(); MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (!proj.ImportFromProj4(text)) { if (!proj.ImportFromWKT(text)) { MessageBox.Show("No valid proj4 or WKT string was entered.", MSG_INVALID_PROJECTION, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } MapWinGIS.GeoProjection projBase = new MapWinGIS.GeoProjection(); if (!projBase.ImportFromEPSG(m_coordinateSystem.Code)) { MessageBox.Show("Failed to initialize the base projection.", MSG_INVALID_PROJECTION, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (projBase.ExportToProj4() == text || projBase.ExportToWKT() == text) { MessageBox.Show("The dialect string is the same as base string", MSG_INVALID_PROJECTION, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // do we have this string already? if (m_existingList.Contains(text)) { MessageBox.Show("The entered string is already present in the list.", MSG_INVALID_PROJECTION, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // do we have this string as a base one? IEnumerable <CoordinateSystem> list = m_database.CoordinateSystems.Where(s => s.proj4 == text); if (list.Count() > 0) { // no sense try to save it, base strings are processed first on loading all the same MessageBox.Show("Current string is aready bound to another EPSG code as the base one: " + list.First().Name + "(" + list.First().Code.ToString() + ")", MSG_INVALID_PROJECTION, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } // is this really a dialect; user will be allowed to save as dialect CS with different parameters, // as sometimes they differ insignificantly because of the rounding if (!proj.get_IsSameExt(projBase, m_coordinateSystem.Extents, 5)) { if (MessageBox.Show("The base projection and its dialect have different transformation parameters." + "This can lead to incorrect disaply of data." + Environment.NewLine + "Do you want to save the dialect all the same?", "Projection mismatch", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } // TODO: check whether this dialect is used for some other EPSG code this.DialogResult = DialogResult.OK; }
/// <summary> /// Does the reprojection work /// </summary> private void DoReprojection(IEnumerable <string> filenames, MapWinGIS.GeoProjection projection, bool inPlace) { frmTesterReport report = new frmTesterReport(); report.InitProgress(projection); List <string> files = new List <string>(); int count = 0; // number of successfully reprojected shapefiles foreach (string filename in filenames) { LayerSource layer = new LayerSource(filename); LayerSource layerNew = null; if (projection.get_IsSame(layer.Projection)) { report.AddFile(layer.Filename, projection.Name, ProjectionOperaion.SameProjection, ""); files.Add(layer.Filename); } else { TestingResult result = CoordinateTransformation.ReprojectLayer(layer, out layerNew, projection, report); if (result == TestingResult.Ok || result == TestingResult.Substituted) { ProjectionOperaion oper = result == TestingResult.Ok ? ProjectionOperaion.Reprojected : ProjectionOperaion.Substituted; string newName = layerNew == null ? "" : layerNew.Filename; report.AddFile(layer.Filename, layer.Projection.Name, oper, newName); files.Add(newName == "" ? layer.Filename : newName); count++; } else { ProjectionOperaion operation = result == TestingResult.Error ? ProjectionOperaion.FailedToReproject : ProjectionOperaion.Skipped; report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, ""); } } layer.Close(); if (layerNew != null) { layerNew.Close(); } } report.ShowReport(projection, "Reprojection results:", ReportType.Loading); IEnumerable <string> names = m_mapWin.Layers.Select(l => l.FileName); names = files.Except(names); if (count > 0) { if (projection.get_IsSame(m_mapWin.Project.GeoProjection)) { if (names.Count() > 0) { if (MessageBox.Show("Do you want to add layers to the project?", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { m_mapWin.Layers.StartAddingSession(); foreach (string filename in names) { m_mapWin.Layers.Add(filename); } m_mapWin.Layers.StopAddingSession(); } } else { MessageBox.Show("No files to add to the map.", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("Chosen projection is different from the project one. The layers can't be added to map.", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("No files to add to the map.", m_mapWin.ApplicationInfo.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> /// 显示报告 /// </summary> /// <param name="proj">投影的目标工程</param> public void ShowReport(MapWinGIS.GeoProjection proj) { m_report.ShowReport(proj, "", ReportType.Loading); }
/// <summary> /// 测试单层投影 /// </summary> /// <param name="layer">图层源(Shapefile或grid对象)</param> /// <param name="newLayer">输出新图层</param> /// <returns>返回测试结果</returns> public TestingResult TestLayer(LayerSource layer, out LayerSource newLayer) { if (layer == null) { throw new ArgumentException("空图层引用被通过"); } newLayer = null; MapWinGIS.GeoProjection projectProj = m_mapWin.Project.GeoProjection; MapWinGIS.GeoProjection layerProj = layer.Projection; bool isSame = projectProj.get_IsSameExt(layerProj, layer.Extents, 10); // 让我们看看我们是否有项目的投影的一种方言 if (!isSame && !projectProj.IsEmpty) { ProjectionDatabase db = (ProjectionDatabase)m_mapWin.ProjectionDatabase;//投影数据源 if (db != null) { CoordinateSystem cs = db.GetCoordinateSystem(projectProj, ProjectionSearchType.Enhanced);//坐标系统 if (cs != null) { db.ReadDialects(cs); foreach (string dialect in cs.Dialects) { MapWinGIS.GeoProjection projTemp = new MapWinGIS.GeoProjection(); if (!projTemp.ImportFromAutoDetect(dialect)) { continue; } if (layerProj.get_IsSame(projTemp)) { isSame = true; break; } } } } } // 投影中可以包含的文件的后缀名,让我们试着用正确的后缀搜索文件 if (!isSame) { if (CoordinateTransformation.SeekSubstituteFile(layer, projectProj, out newLayer)) { m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Substituted, newLayer.Filename); return(TestingResult.Substituted); } } if (!layer.Projection.IsEmpty) { if (projectProj.IsEmpty) { // 层具有投影,项目没有;分配到投影,不提示用户 // 让我们找个众所周知的投影与EPSG编码 ProjectionDatabase db = m_mapWin.ProjectionDatabase as ProjectionDatabase; if (db != null) { CoordinateSystem cs = db.GetCoordinateSystem(layerProj, ProjectionSearchType.UseDialects); if (cs != null) { MapWinGIS.GeoProjection proj = new MapWinGIS.GeoProjection(); if (proj.ImportFromEPSG(cs.Code)) { layerProj = proj; } } } m_mapWin.Project.GeoProjection = layerProj; return(TestingResult.Ok); } else if (isSame) { // 相同投影 return(TestingResult.Ok); } else { // 用户必须被提示 if (!m_usePreviousAnswerMismatch && !m_mapWin.ApplicationInfo.NeverShowProjectionDialog) { bool dontShow = false; bool useForOthers = false; ArrayList list = new ArrayList(); list.Add("Ignore mismatch"); list.Add("Reproject file"); //list.Add("Skip file"); // PM 2013-05-03: list.Add("Don't load the layer"); frmProjectionMismatch form = new frmProjectionMismatch((ProjectionDatabase)m_mapWin.ProjectionDatabase); int choice = form.ShowProjectionMismatch(list, (int)m_mapWin.ApplicationInfo.ProjectionMismatchBehavior, projectProj, layer.Projection, out useForOthers, out dontShow); form.Dispose(); if (choice == -1) { return(TestingResult.CancelOperation); } m_usePreviousAnswerMismatch = useForOthers; m_mapWin.ApplicationInfo.ProjectionMismatchBehavior = (ProjectionMismatchBehavior)choice; m_mapWin.ApplicationInfo.NeverShowProjectionDialog = dontShow; } MapWinGIS.Interfaces.ProjectionMismatchBehavior behavior = m_mapWin.ApplicationInfo.ProjectionMismatchBehavior; switch (behavior) { case ProjectionMismatchBehavior.Reproject: TestingResult result = CoordinateTransformation.ReprojectLayer(layer, out newLayer, projectProj, m_report); if (result == TestingResult.Ok || result == TestingResult.Substituted) { ProjectionOperaion oper = result == TestingResult.Ok ? ProjectionOperaion.Reprojected : ProjectionOperaion.Substituted; string newName = newLayer == null ? "" : newLayer.Filename; m_report.AddFile(layer.Filename, layer.Projection.Name, oper, newName); return(newName == layer.Filename ? TestingResult.Ok : TestingResult.Substituted); } else { m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.FailedToReproject, ""); return(TestingResult.Error); } case ProjectionMismatchBehavior.IgnoreMismatch: m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.MismatchIgnored, ""); return(TestingResult.Ok); case ProjectionMismatchBehavior.SkipFile: m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, ""); return(TestingResult.SkipFile); } } } else if (!projectProj.IsEmpty) // 图层投影是空的 { bool projectProjectionExists = !projectProj.IsEmpty; // 用户必须被提示 if (!m_usePreviousAnswerAbsence && !m_mapWin.ApplicationInfo.NeverShowProjectionDialog) { bool dontShow = false; bool useForOthers = false; ArrayList list = new ArrayList(); // 当在投影第一变体应排除 int val = projectProjectionExists ? 0 : 1; if (projectProjectionExists) { // PM 2013-05-03: //list.Add("Assign projection from project"); list.Add("Use the project's projection"); } // list.Add("Ignore the absence"); // list.Add("Skip the file"); list.Add("Ignore the missing of projection file"); list.Add("Don't load the layer"); frmProjectionMismatch form = new frmProjectionMismatch((ProjectionDatabase)m_mapWin.ProjectionDatabase); int choice = form.ShowProjectionAbsence(list, (int)m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior - val, projectProj, out useForOthers, out dontShow); form.Dispose(); if (choice == -1) { return(TestingResult.CancelOperation); } choice += val; m_usePreviousAnswerAbsence = useForOthers; m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior = (ProjectionAbsenceBehavior)choice; m_mapWin.ApplicationInfo.NeverShowProjectionDialog = dontShow; } // 当在项目没有投影,它不能分配层 ProjectionAbsenceBehavior behavior = m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior; if (!projectProjectionExists && m_mapWin.ApplicationInfo.ProjectionAbsenceBehavior == ProjectionAbsenceBehavior.AssignFromProject) { behavior = ProjectionAbsenceBehavior.IgnoreAbsence; } switch (behavior) { case ProjectionAbsenceBehavior.AssignFromProject: m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Assigned, ""); layer.Projection = projectProj; return(TestingResult.Ok); case ProjectionAbsenceBehavior.IgnoreAbsence: m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.AbsenceIgnored, ""); return(TestingResult.Ok); case ProjectionAbsenceBehavior.SkipFile: m_report.AddFile(layer.Filename, layer.Projection.Name, ProjectionOperaion.Skipped, ""); return(TestingResult.SkipFile); } } else { // 层没有投影,项目也没有,不在这里 } System.Diagnostics.Debug.Print("Invalid result in projection tester"); return(TestingResult.Ok); }