[mapguide-commits] r5824 - in trunk/Tools/Maestro: Maestro.Base/Editor Maestro.Base/UI Maestro.Editors/MapDefinition OSGeo.MapGuide.MaestroAPI OSGeo.MapGuide.MaestroAPI/ObjectModels

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Fri May 20 08:29:34 EDT 2011


Author: jng
Date: 2011-05-20 05:29:34 -0700 (Fri, 20 May 2011)
New Revision: 5824

Modified:
   trunk/Tools/Maestro/Maestro.Base/Editor/ResourcePreviewEngine.cs
   trunk/Tools/Maestro/Maestro.Base/UI/ResourcePropertiesDialog.cs
   trunk/Tools/Maestro/Maestro.Editors/MapDefinition/MapSettingsSectionCtrl.cs
   trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerInterfaceExtensions.cs
   trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
   trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Utility.cs
Log:
#1698: Remove the old GetSpatialExtent extension method for one that includes an out parameter where the CS wkt will be set. This is used to identify the coordinates that these extents are in.


Modified: trunk/Tools/Maestro/Maestro.Base/Editor/ResourcePreviewEngine.cs
===================================================================
--- trunk/Tools/Maestro/Maestro.Base/Editor/ResourcePreviewEngine.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/Maestro.Base/Editor/ResourcePreviewEngine.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -69,10 +69,11 @@
 
             //Create temp map definition to house our current layer
             var mdfId = "Session:" + sessionId + "//" + Guid.NewGuid() + ".MapDefinition";
-            var extent = ldf.GetSpatialExtent(true);
+            string csWkt;
+            var extent = ldf.GetSpatialExtent(true, out csWkt);
 
             //TODO: Based on the visible scales in this layer, size this extents accordingly
-            var mdf = ObjectFactory.CreateMapDefinition(conn, Properties.Resources.PreviewMap, ldf.GetCoordinateSystemWkt(), extent);
+            var mdf = ObjectFactory.CreateMapDefinition(conn, Properties.Resources.PreviewMap, csWkt, extent);
 
             var layer = mdf.AddLayer(null, ResourceIdentifier.GetName(_edSvc.ResourceID), ldf.ResourceID);
 

Modified: trunk/Tools/Maestro/Maestro.Base/UI/ResourcePropertiesDialog.cs
===================================================================
--- trunk/Tools/Maestro/Maestro.Base/UI/ResourcePropertiesDialog.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/Maestro.Base/UI/ResourcePropertiesDialog.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -795,8 +795,9 @@
 
                 System.Globalization.CultureInfo ic = System.Globalization.CultureInfo.InvariantCulture;
                 ILayerDefinition ldef = (ILayerDefinition)m_connection.ResourceService.GetResource(m_resourceId);
-                var env = ldef.GetSpatialExtent(true);
-
+                string csWkt;
+                var env = ldef.GetSpatialExtent(true, out csWkt);
+                //TODO: Convert wkt to EPSG code and use that?
                 //TODO: Convert to lon/lat
 
                 bounds = "<Bounds west=\"" + env.MinX.ToString(ic) + "\" east=\"" + env.MaxX.ToString(ic) + "\" south=\"" + env.MinY.ToString(ic) + "\" north=\"" + env.MaxY.ToString(ic) + "\" ";

Modified: trunk/Tools/Maestro/Maestro.Editors/MapDefinition/MapSettingsSectionCtrl.cs
===================================================================
--- trunk/Tools/Maestro/Maestro.Editors/MapDefinition/MapSettingsSectionCtrl.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/Maestro.Editors/MapDefinition/MapSettingsSectionCtrl.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -240,12 +240,14 @@
                         }
                     }
                 }
-                
-                var env = Util.GetCombinedExtents(_map, layers);
+
+                bool hasFailures = false;
+                var env = Util.GetCombinedExtents(_map, layers, out hasFailures);
                 if (env != null)
                 {
                     _map.SetExtents(env.MinX, env.MinY, env.MaxX, env.MaxY);
-                    MessageBox.Show(Properties.Resources.WarningMapExtentCalculation, Properties.Resources.TitleWarning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                    if (hasFailures)
+                        MessageBox.Show(Properties.Resources.WarningMapExtentCalculation, Properties.Resources.TitleWarning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                 }
                 else
                 {
@@ -256,17 +258,20 @@
 
         internal class Util
         {
-            public static IEnvelope GetCombinedExtents(IMapDefinition mapDef, IEnumerable<ILayerDefinition> layers)
+            public static IEnvelope GetCombinedExtents(IMapDefinition mapDef, IEnumerable<ILayerDefinition> layers, out bool hasFailures)
             {
+                hasFailures = false;
                 Check.NotNull(layers, "layers");
                 IEnvelope env = null;
                 foreach (var layer in layers)
                 {
-                    var e1 = layer.GetSpatialExtent(true);
-                    var wkt = layer.GetCoordinateSystemWkt();
+                    string wkt;
+                    var e1 = layer.GetSpatialExtent(true, out wkt);
                     if (wkt != mapDef.CoordinateSystem)
                     {
-                        e1 = TransformEnvelope(e1, wkt, mapDef.CoordinateSystem);
+                        //Transform if not the same, otherwise assume either arbitrary or same as the map
+                        if (!string.IsNullOrEmpty(wkt))
+                            e1 = Utility.TransformEnvelope(e1, wkt, mapDef.CoordinateSystem);
                     }
 
                     if (e1 != null)
@@ -289,35 +294,14 @@
                     }
                     else
                     {
+                        hasFailures = true;
                         System.Diagnostics.Trace.TraceWarning("Could not transform extent of layer " + layer.ResourceID + " to the map definition's coordinate system. Extents ignored");
                     }
                 }
                 return env;
             }
 
-            private static IEnvelope TransformEnvelope(IEnvelope env, string srcCsWkt, string dstCsWkt)
-            {
-                try
-                {
-                    var trans = new DefaultSimpleTransform(srcCsWkt, dstCsWkt);
-
-                    var oldExt = env;
-
-                    double llx;
-                    double lly;
-                    double urx;
-                    double ury;
-
-                    trans.Transform(oldExt.MinX, oldExt.MinY, out llx, out lly);
-                    trans.Transform(oldExt.MaxX, oldExt.MaxY, out urx, out ury);
-
-                    return ObjectFactory.CreateEnvelope(llx, lly, urx, ury);
-                }
-                catch
-                {
-                    return null;
-                }
-            }
+            
         }
     }
 }

Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerInterfaceExtensions.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerInterfaceExtensions.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/LayerInterfaceExtensions.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -219,6 +219,17 @@
             return string.Empty;
         }
 
+        /// <summary>
+        /// Returns the spatial extent of the data.
+        /// This is calculated by asking the underlying featuresource for the minimum rectangle that
+        /// contains all the features in the specified table. If the <paramref name="allowFallbackToContextInformation"/>
+        /// is set to true, and the query fails, the code will attempt to read this information
+        /// from the spatial context information instead.
+        /// </summary>
+        /// <param name="layer">The layer definition</param>
+        /// <param name="allowFallbackToContextInformation">If true, will default to the extents of the active spatial context.</param>
+        /// <param name="csWkt">The coordinate system WKT that this extent corresponds to</param>
+        /// <returns></returns>
         public static IEnvelope GetSpatialExtent(this ILayerDefinition layer, bool allowFallbackToContextInformation, out string csWkt)
         {
             csWkt = null;
@@ -232,137 +243,56 @@
                 case LayerType.Vector:
                     {
                         IEnvelope env = null;
+                        IFdoSpatialContext activeSc = null;
                         try
                         {
-                            env = conn.FeatureService.GetSpatialExtent(layer.SubLayer.ResourceId, ((IVectorLayerDefinition)layer.SubLayer).FeatureName, ((IVectorLayerDefinition)layer.SubLayer).Geometry);
-                            return env;
-                        }
-                        catch //Default to extents of active spatial context
-                        {
                             var scList = conn.FeatureService.GetSpatialContextInfo(layer.SubLayer.ResourceId, true);
                             if (scList.SpatialContext.Count > 0)
                             {
-                                var sc = scList.SpatialContext[0];
-                                return ObjectFactory.CreateEnvelope(
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.Y),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.Y));
+                                activeSc = scList.SpatialContext[0];
                             }
-                            return null;
-                        }
-                    }
-                case LayerType.Raster:
-                    {
-                        IEnvelope env = null;
-                        try
-                        {
-                            env = conn.FeatureService.GetSpatialExtent(layer.SubLayer.ResourceId, ((IRasterLayerDefinition)layer.SubLayer).FeatureName, ((IRasterLayerDefinition)layer.SubLayer).Geometry);
+                            //TODO: Check if ones like SQL Server will return the WKT, otherwise we'll need to bring in the
+                            //CS catalog to do CS code to WKT conversion.
+                            csWkt = activeSc.CoordinateSystemWkt;
+
+                            //This can fail if SpatialExtents() aggregate function is not supported
+                            env = conn.FeatureService.GetSpatialExtent(layer.SubLayer.ResourceId, ((IVectorLayerDefinition)layer.SubLayer).FeatureName, ((IVectorLayerDefinition)layer.SubLayer).Geometry);
                             return env;
                         }
-                        catch //Default to extents of active spatial context
+                        catch 
                         {
-                            var scList = conn.FeatureService.GetSpatialContextInfo(layer.SubLayer.ResourceId, true);
-                            if (scList.SpatialContext.Count > 0)
-                            {
-                                var sc = scList.SpatialContext[0];
-                                return ObjectFactory.CreateEnvelope(
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.Y),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.Y));
-                            }
-                            return null;
+                            //Which in that case, default to extents of active spatial context
+                            if (activeSc != null && activeSc.Extent != null)
+                                return activeSc.Extent.Clone();
+                            else
+                                return null;
                         }
                     }
-                default:
+                case LayerType.Raster:
                     {
-                        int[] services = conn.Capabilities.SupportedServices;
-                        if (Array.IndexOf(services, (int)ServiceType.Drawing) >= 0)
-                        {
-                            var sheet = ((IDrawingLayerDefinition)layer.SubLayer).Sheet;
-                            var dws = (IDrawingSource)conn.ResourceService.GetResource(((IDrawingLayerDefinition)layer.SubLayer).ResourceId);
-
-                            if (dws.Sheet != null)
-                            {
-                                //find matching sheet
-                                foreach (var sht in dws.Sheet)
-                                {
-                                    if (sheet.Equals(sht.Name))
-                                    {
-                                        return ObjectFactory.CreateEnvelope(sht.Extent.MinX, sht.Extent.MinY, sht.Extent.MaxX, sht.Extent.MaxY);
-                                    }
-                                }
-                            }
-                        }
-                        return null;
-                    }
-            }
-        }
-
-        /// <summary>
-        /// Returns the spatial extent of the data.
-        /// This is calculated by asking the underlying featuresource for the minimum rectangle that
-        /// contains all the features in the specified table. If the <paramref name="allowFallbackToContextInformation"/>
-        /// is set to true, and the query fails, the code will attempt to read this information
-        /// from the spatial context information instead.
-        /// </summary>
-        /// <param name="layer">The layer.</param>
-        /// <param name="allowFallbackToContextInformation">True to allow reading spatial extents from the spatial context information, if the spatial query fails.</param>
-        /// <returns>The envelope for the data in the table</returns>
-        public static IEnvelope GetSpatialExtent(this ILayerDefinition layer, bool allowFallbackToContextInformation)
-        {
-            Check.NotNull(layer, "layer");
-            if (layer.CurrentConnection == null)
-                throw new System.Exception("No server set for object");
-
-            var conn = layer.CurrentConnection;
-            switch (layer.SubLayer.LayerType)
-            {
-                case LayerType.Vector:
-                    {
                         IEnvelope env = null;
+                        IFdoSpatialContext activeSc = null;
                         try
                         {
-                            env = conn.FeatureService.GetSpatialExtent(layer.SubLayer.ResourceId, ((IVectorLayerDefinition)layer.SubLayer).FeatureName, ((IVectorLayerDefinition)layer.SubLayer).Geometry);
-                            return env;
-                        }
-                        catch //Default to extents of active spatial context
-                        {
                             var scList = conn.FeatureService.GetSpatialContextInfo(layer.SubLayer.ResourceId, true);
                             if (scList.SpatialContext.Count > 0)
                             {
-                                var sc = scList.SpatialContext[0];
-                                return ObjectFactory.CreateEnvelope(
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.Y),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.Y));
+                                activeSc = scList.SpatialContext[0];
                             }
-                            return null;
-                        }
-                    }
-                case LayerType.Raster:
-                    {
-                        IEnvelope env = null;
-                        try
-                        {
+
+                            //TODO: Would any raster provider *not* return a WKT?
+                            csWkt = activeSc.CoordinateSystemWkt;
+
+                            //Can fail if SpatialExtents() aggregate function is not supported
                             env = conn.FeatureService.GetSpatialExtent(layer.SubLayer.ResourceId, ((IRasterLayerDefinition)layer.SubLayer).FeatureName, ((IRasterLayerDefinition)layer.SubLayer).Geometry);
                             return env;
                         }
                         catch //Default to extents of active spatial context
                         {
-                            var scList = conn.FeatureService.GetSpatialContextInfo(layer.SubLayer.ResourceId, true);
-                            if (scList.SpatialContext.Count > 0)
-                            {
-                                var sc = scList.SpatialContext[0];
-                                return ObjectFactory.CreateEnvelope(
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.LowerLeftCoordinate.Y),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.X),
-                                    Convert.ToDouble(sc.Extent.UpperRightCoordinate.Y));
-                            }
-                            return null;
+                            if (activeSc != null && activeSc.Extent != null)
+                                return activeSc.Extent.Clone();
+                            else
+                                return null;
                         }
                     }
                 default:
@@ -380,6 +310,7 @@
                                 {
                                     if (sheet.Equals(sht.Name))
                                     {
+                                        csWkt = dws.CoordinateSpace;
                                         return ObjectFactory.CreateEnvelope(sht.Extent.MinX, sht.Extent.MinY, sht.Extent.MaxX, sht.Extent.MaxY);
                                     }
                                 }

Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/ObjectModels/MapDefinition.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -219,19 +219,42 @@
 
             if (this.MapLayer.Count == 1) //First one
             {
-                var ldf = (ILayerDefinition)this.CurrentConnection.ResourceService.GetResource(layer.ResourceId);
-                if (string.IsNullOrEmpty(this.CoordinateSystem))
+                OnFirstLayerAdded(layer);
+            }
+
+            return layer;
+        }
+
+        private void OnFirstLayerAdded(MapLayerType layer)
+        {
+            string csWkt;
+            var ldf = (ILayerDefinition)this.CurrentConnection.ResourceService.GetResource(layer.ResourceId);
+            var env = ldf.GetSpatialExtent(true, out csWkt);
+
+            if (!string.IsNullOrEmpty(csWkt) && string.IsNullOrEmpty(this.CoordinateSystem))
+            {
+                this.CoordinateSystem = csWkt;
+            }
+            if (IsEmpty(this.Extents))
+            {
+                if (string.IsNullOrEmpty(csWkt) || csWkt.Equals(this.CoordinateSystem))
                 {
-                    this.CoordinateSystem = ldf.GetCoordinateSystemWkt();
+                    ((IMapDefinition)this).SetExtents(env.MinX, env.MinY, env.MaxX, env.MaxY);
                 }
-                if (IsEmpty(this.Extents))
+                else
                 {
-                    var env = ldf.GetSpatialExtent(true);
-                    ((IMapDefinition)this).SetExtents(env.MinX, env.MinY, env.MaxX, env.MaxY);
+                    if (string.IsNullOrEmpty(csWkt)) //Assume arbitrary or same as map
+                    {
+                        ((IMapDefinition)this).SetExtents(env.MinX, env.MinY, env.MaxX, env.MaxY);
+                    }
+                    else
+                    {
+                        var env2 = Utility.TransformEnvelope(env, csWkt, this.CoordinateSystem);
+                        if (env2 != null)
+                            ((IMapDefinition)this).SetExtents(env2.MinX, env2.MinY, env2.MaxX, env2.MaxY);
+                    }
                 }
             }
-
-            return layer;
         }
 
 
@@ -279,16 +302,7 @@
 
             if (this.MapLayer.Count == 1) //First one
             {
-                var ldf = (ILayerDefinition)this.CurrentConnection.ResourceService.GetResource(layer.ResourceId);
-                if (string.IsNullOrEmpty(this.CoordinateSystem))
-                {
-                    this.CoordinateSystem = ldf.GetCoordinateSystemWkt();
-                }
-                if (IsEmpty(this.Extents))
-                {
-                    var env = ldf.GetSpatialExtent(true);
-                    ((IMapDefinition)this).SetExtents(env.MinX, env.MinY, env.MaxX, env.MaxY);
-                }
+                OnFirstLayerAdded(layer);
             }
 
             return layer;

Modified: trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Utility.cs
===================================================================
--- trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Utility.cs	2011-05-20 11:31:31 UTC (rev 5823)
+++ trunk/Tools/Maestro/OSGeo.MapGuide.MaestroAPI/Utility.cs	2011-05-20 12:29:34 UTC (rev 5824)
@@ -24,6 +24,8 @@
 using OSGeo.MapGuide.MaestroAPI.IO;
 using OSGeo.MapGuide.MaestroAPI.Schema;
 using System.Collections.Specialized;
+using OSGeo.MapGuide.MaestroAPI.CoordinateSystem;
+using OSGeo.MapGuide.ObjectModels;
 
 namespace OSGeo.MapGuide.MaestroAPI
 {
@@ -695,5 +697,29 @@
                 }
             }
         }
+
+        public static OSGeo.MapGuide.ObjectModels.Common.IEnvelope TransformEnvelope(OSGeo.MapGuide.ObjectModels.Common.IEnvelope env, string srcCsWkt, string dstCsWkt)
+        {
+            try
+            {
+                var trans = new DefaultSimpleTransform(srcCsWkt, dstCsWkt);
+
+                var oldExt = env;
+
+                double llx;
+                double lly;
+                double urx;
+                double ury;
+
+                trans.Transform(oldExt.MinX, oldExt.MinY, out llx, out lly);
+                trans.Transform(oldExt.MaxX, oldExt.MaxY, out urx, out ury);
+
+                return ObjectFactory.CreateEnvelope(llx, lly, urx, ury);
+            }
+            catch
+            {
+                return null;
+            }
+        }
     }
 }



More information about the mapguide-commits mailing list