EasyGIS.NET icon indicating copy to clipboard operation
EasyGIS.NET copied to clipboard

Zoom to selection fails

Open katterfelto opened this issue 5 months ago • 1 comments

I have a shapefile in EPSG:27700 (GB NGR). When a tiled base layer is loaded (e.g. Open Street Maps) SFMap.ZoomToSelection() fails to zoom the the correct coordinates.

My initial solution:

The extent that is compiled from the selected objects in each shapefile needs to be transformed to EPSG:3857 before the call to SFMap.FitToExtent(RectangleD extent) is made.

The following diff shows my change, is there a better way?

diff --git a/EGIS.Controls/SFMap.cs b/EGIS.Controls/SFMap.cs
index b09fe3e..a0edef1 100644
--- a/EGIS.Controls/SFMap.cs
+++ b/EGIS.Controls/SFMap.cs
@@ -1047,20 +1047,35 @@ namespace EGIS.Controls
                 System.Collections.ObjectModel.ReadOnlyCollection<int> selectedIndicies = layer.SelectedRecordIndices;
                 if (selectedIndicies.Count > 0)
                 {
+                    RectangleD selExtent = RectangleD.Empty;
+                    bool selExtentSet = false;
                     for (int n = 0; n < selectedIndicies.Count; ++n)
                     {
-                        if (extentSet)
+                        if (selExtentSet)
                         {
-                            extent = RectangleD.Union(extent, layer.GetShapeBoundsD(selectedIndicies[n]));
+                            selExtent = RectangleD.Union(selExtent, layer.GetShapeBoundsD(selectedIndicies[n]));
                         }
                         else
                         {
-                            extent = layer.GetShapeBoundsD(selectedIndicies[n]);
-                            extentSet = true;
+                            selExtent = layer.GetShapeBoundsD(selectedIndicies[n]);
+                            selExtentSet = true;
                         }
                     }
+
+                    if (extentSet)
+                    {
+                        extent = RectangleD.Union(extent,
+                            selExtent.Transform(layer.CoordinateReferenceSystem, this.MapCoordinateReferenceSystem));
+                    }
+                    else
+                    {
+                        extent = selExtent.Transform(layer.CoordinateReferenceSystem,
+                            this.MapCoordinateReferenceSystem);
+                        extentSet = true;
+                    }
                 }
             }
             if (extentSet) FitToExtent(extent);
         }

katterfelto avatar Sep 25 '24 15:09 katterfelto