PIE SDK use experience -- four TIPS

It has been two years since I contacted the secondary development of PIE SDK last year. Looking back on the past bits and pieces, what impressed me the most was the meticulous question answering and online training of the technical experts of Aerospace Hongtu. Looking forward to the software products of Aerospace Hongtu in the conceivable future. The rise of domestic software must also have its place. Closer to home, let me introduce my experience, experience and skills using PIE SDK for secondary development.

TIP 1: How to save the map document (PmdContents) without a mapping control (PageLayoutControl).

Reference code: PIE SDK thematic mapping save template ( https://www.cnblogs.com/PIESat/p/10175673.html ). This blog is dedicated to the operation of saving templates when drawing. It uses drawing controls as a bridge to save templates. However, sometimes we may not need the mapping control, in this case, we only need to change the mapping control to the map control.

 1  private void buttonItem_SaveProject_Click(object sender, EventArgs e)
 2         {
 3             //Get the current map document
 4             IMapDocument mapDocument = (mapControlMain as Control).Tag as IMapDocument; //PIE DesktopCommand available in m_Application.MapDocument to get
 5             if (mapDocument == null)
 6             {
 7                 mapDocument = new MapDocument();
 8                 mapDocument.ReplaceContents(mapControlMain);
 9             }
10 
11             //save document
12             string pmdFilePath = mapDocument.GetDocumentFilename();
13             if (string.IsNullOrEmpty(pmdFilePath))
14             {
15                 SaveFileDialog saveFileDialog = new SaveFileDialog();
16                 saveFileDialog.Title = "The map document is saved as:";
17                 saveFileDialog.Filter = "PMD|*.pmd";
18                 if (saveFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
19                 pmdFilePath = saveFileDialog.FileName;
20                 if (string.IsNullOrEmpty(pmdFilePath)) return;
21 
22                 if (!pmdFilePath.EndsWith(".pmd"))
23                 {
24                     pmdFilePath = pmdFilePath + ".pmd";
25                 }
26             }
27             mapDocument.SaveAs(pmdFilePath, false, false);
28 
29             MessageBox.Show(DateTime.Now + " : Project saved successfully!\n", m_AppTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
30         }

TIP 2: Pay attention to memory release in the loop, taking vector feature traversal as an example.

In the process of vector feature traversal, pay attention to adding (featureCursor as IDisposable).Dispose() and featureCursor = null to the loop code, otherwise it will cause unexpected bug s. When I did not release the memory, the program would always get stuck on a certain feature, and no subsequent code would be executed. The sample code is as follows.

 1 private void buttonItem_CreateBatchSampleOfShp_Click(object sender, EventArgs e)
 2         {
 3             Test_AutoCreateBatchImageSampleParasOfShape dlgTest_AutoCreateImageSampleParas = new Test_AutoCreateBatchImageSampleParasOfShape();
 4             if (dlgTest_AutoCreateImageSampleParas.ShowDialog() != DialogResult.OK) return;
 5             string strAppDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
 6             string strBaseImageFile = dlgTest_AutoCreateImageSampleParas.textBox_InputBaseImageFile.Text;
 7             string strBaseVectorFile = dlgTest_AutoCreateImageSampleParas.textBox_InputBaseVectorFile.Text;
 8             string strBaseBoundaryFile = dlgTest_AutoCreateImageSampleParas.textBox_InpurtBaseBoundaryFile.Text;
 9             string strOutputDir = dlgTest_AutoCreateImageSampleParas.textBox_OutputDir.Text;            
10             //store a single boundary scope
11             IField field1 = new Field("Class", FieldType.OFTInteger, 8, 1);
12             field1.Name = "Class";
13             field1.AliasName = "Label";
14             IFields fieldsSave = new Fields();
15             fieldsSave.AddField(field1);
16             ILayer layerLabel = PIE.Carto.LayerFactory.CreateDefaultLayer(strBaseVectorFile);
17             IFeatureLayer labelFeatureLayer = (layerLabel as IFeatureLayer);
18             ILayer layerBoundary = PIE.Carto.LayerFactory.CreateDefaultLayer(strBaseBoundaryFile);
19             IFeatureClass boundaryFeatures = (layerBoundary as IFeatureLayer).FeatureClass;
20             IFields fields = boundaryFeatures.GetFields();
21             int fieldIndex = fields.GetFieldIndex("Name");
22             IField field = fields.GetField(fieldIndex);
23             IEnvelope envelop = new Envelope();
24             //IPointCollection polygon = new Polygon();
25             for (int i = 0; i < boundaryFeatures.GetFeatureCount(); i++)
26             {
27                 IFeature feature = boundaryFeatures.GetFeature(i);
28                 string strBoundaryFile = strTempSample + feature.GetValue(field.Name) + "_boundary.shp";
29                 IFeatureDataset boundaryFeatureDataset = DatasetFactory.CreateFeatureDataset(strBoundaryFile, fieldsSave, GeometryType.GeometryEnvelope, layerLabel.SpatialReference, "SHP");
30                 IFeature boundaryFeature = boundaryFeatureDataset.CreateNewFeature();
31                 boundaryFeature.Geometry = feature.Geometry;
32                 boundaryFeature.Geometry.SpatialReference = layerLabel.SpatialReference;
33                 boundaryFeature.FID = 0;
34                 boundaryFeature.SetValue(0, 0);//Class
35                 boundaryFeatureDataset.AddFeature(boundaryFeature);
36                 boundaryFeatureDataset.Save();
37                 (boundaryFeature as IDisposable).Dispose();
38                 boundaryFeature = null;
39                 (boundaryFeatureDataset as IDisposable).Dispose();
40                 boundaryFeatureDataset = null;
41                 IQueryFilter queryFilter = new QueryFilter();
42                 queryFilter.SetSpatialQuery(feature.Geometry);
43                 IFeatureCursor featureCursor = labelFeatureLayer.FeatureClass.Search(queryFilter);
44                 this.progressBarItem.Value++;
45                 (featureCursor as IDisposable).Dispose();
46                 featureCursor = null;
47             }
48         }

TIP 3: The influence of coordinate system on editing operations of vector features.

When editing vector elements, if the coordinate system of the map frame is the latitude and longitude coordinate system, it will cause flying spots in the element editing function or the program will crash directly (PIE SDK version is 5.2, 32-bit), as shown in the following figure. Check with technicians, this problem does exist, and subsequent versions will update and fix it. After a series of thinking and testing, I found that there is a solution: there is only one theme, and when editing elements, ensure that the map frame coordinate system is the projected coordinate system, so that vector elements can be edited normally. The specific method is: on the map frame (Map), right-click, in the pop-up right-click menu, click "Modify Coordinate System", and set the map frame coordinate system to the corresponding projected coordinate system.

 

TIP 4: A bug in the mapControlMain.FocusMap.DeleteLayers() function.

        The problem I encountered is: when the DeleteLayers() function is called, the layer in the map view window (mapControlMain control) is indeed deleted, but the layer list in the TOCControl control is not deleted synchronously (PIE SDK version is 5.2, 32-bit) ). For this question, the first thing that comes to mind is whether mapControlMain and TOCControl controls are related? Sadly, the two controls are indeed already linked. After checking with the technicians, it was found that this problem does exist. The solution is: call the mapControlMain.FocusMap.DeleteLayer() function, traverse the layers in reverse order, and delete them one by one.

 1 private void buttonItem_CloseProject_Click(object sender, EventArgs e)
 2         {
 3             m_ElementEdit = false;
 4             mapControlMain.CurrentTool = null;
 5             labelItem_CoordinateSystem.Text = "";
 6             if (mapControlMain.FocusMap.LayerCount < 3) return;
 7             //Clear the map in main view and eagle eye view
 8             while (mapControlMain.FocusMap.LayerCount > 2)
 9                 if (mapControlMain.FocusMap.GetLayer(mapControlMain.FocusMap.LayerCount - 1).LayerType != LayerType.LayerTypeTiledLayer)
10                     mapControlMain.FocusMap.DeleteLayer(mapControlMain.FocusMap.GetLayer(mapControlMain.FocusMap.LayerCount - 1));
11             mapHawkEyeControl.FocusMap.ClearLayers();
12             //mapHawkEyeControl.FocusMap.DeleteLayer(mapHawkEyeControl.FocusMap.GetLayer(0));
13             //Clear elements in main view and eagle eye view
14             mapHawkEyeControl.ActiveView.GraphicsContainer.DeleteElement(m_DrawPolygonElement);
15             mapControlMain.ActiveView.GraphicsContainer.DeleteAllElements();
16             //refresh view
17             mapControlMain.ActiveView.ZoomTo(mapControlMain.ActiveView.FullExtent as IGeometry);
18             mapControlMain.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll);       
19             mapHawkEyeControl.ActiveView.PartialRefresh(PIE.Carto.ViewDrawPhaseType.ViewAll);
20         }

So far, my sharing will come to an end. If there is something wrong, please criticize and correct me!

 

Posted by w3evolutions on Mon, 09 May 2022 11:06:34 +0300