Example #1
0
        /// <summary>
        /// Change the extent of the map according to the extent of the layer. 
        /// Reproject the extent if the layer and the map projection differs.
        /// </summary>
        /// <param name="layer">The layer to be used to calculate the extent</param>
        private void ZoomToLayerExtent(layerObj layer)
        {
            try
            {
                if (layer.connectiontype == MS_CONNECTION_TYPE.MS_PLUGIN &&
                    layer.plugin_library == "msplugin_mssql2008.dll")
                    GetMssqlSpatialLayerExtent(layer);

                using (rectObj extent = layer.getExtent())
                {
                    if (extent.minx < extent.maxx && extent.miny < extent.maxy)
                    {
                        if (map.getProjection() != "" && layer.getProjection() != "")
                        {
                            // need to reproject the extent
                            using (projectionObj oldProj = new projectionObj(layer.getProjection()))
                            {
                                using (projectionObj newProj = new projectionObj(map.getProjection()))
                                {
                                    using (rectObj rect = new rectObj(extent.minx, extent.miny, extent.maxx, extent.maxy, 0))
                                    {
                                        rect.project(oldProj, newProj);
                                        if (rect.minx < rect.maxx && rect.miny < rect.maxy)
                                            map.setExtent(rect.minx, rect.miny, rect.maxx, rect.maxy);
                                        if (target != null)
                                        {
                                            target.RaisePropertyChanged(this);
                                            RaiseZoomChanged();
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            // don't reproject
                            map.setExtent(extent.minx, extent.miny, extent.maxx, extent.maxy);
                            if (target != null)
                            {
                                target.RaisePropertyChanged(this);
                                RaiseZoomChanged();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }