Exemplo n.º 1
0
        /// <summary>
        /// Click event handler of the addMSSQLSpatialLayerToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source object of this event.</param>
        /// <param name="e">The event parameters.</param>
        private void addMSSQLSpatialLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode source = CurrentTree.SelectedNode;
            if (source != null && source.Tag != null)
                RaiseItemSelect((MapObjectHolder)source.Tag);

            int numlayers = map.numlayers;

            SqlConnectionDialog form = new SqlConnectionDialog();
            if (form.ShowDialog(this) == DialogResult.OK)
            {
                layerObj layer = null;
                try
                {
                    // trying to open the layer
                    MapUtils.TestMSSQLConnection(form.GetConnectionString(), form.GetDataString());

                    layer = CreateNewLayer();
                    layer.connectiontype = MS_CONNECTION_TYPE.MS_PLUGIN;
                    layer.plugin_library = layer.plugin_library_original = "msplugin_mssql2008.dll";

                    layer.connection = form.GetConnectionString();
                    layer.data = form.GetDataString();

                    switch (form.GeometryType)
                    {
                        case "Point":
                        case "MultiPoint":
                            layer.type = MS_LAYER_TYPE.MS_LAYER_POINT;
                            break;
                        case "LineString":
                        case "MultiLineString":
                            layer.type = MS_LAYER_TYPE.MS_LAYER_LINE;
                            break;
                        case "Polygon":
                        case "MultiPolygon":
                            layer.type = MS_LAYER_TYPE.MS_LAYER_POLYGON;
                            break;
                        default:
                            layer.type = MS_LAYER_TYPE.MS_LAYER_LINE;
                            break;
                    }

                    MapUtils.CreateRandomClass(layer);
                    MapUtils.InitializeDefaultLabel(layer);

                    layer.name = MapUtils.GetUniqueLayerName(map, form.TableName, 0);
                    string projName;
                    string proj4text;
                    int epsg;

                    if (form.GetProj4Text() != null)
                    {
                        projName = MapUtils.FindProjection(form.GetProj4Text(), out proj4text, out epsg);
                        layer.setProjection(form.GetProj4Text());
                        layer.setMetaData("coordsys_name", projName);
                    }
                    else if (form.GetSRText() != null && form.GetSRText() != "")
                    {
                        SpatialReference srs = new SpatialReference(form.GetSRText());
                        string proj4;
                        srs.ExportToProj4(out proj4);
                        if (proj4 != null || proj4 != "")
                        {
                            projName = MapUtils.FindProjection(proj4, out proj4text, out epsg);
                            //Steph: looks like there was a copy and paste error, I have replaced form.GetProj4Text() with proj4text
                            layer.setProjection(proj4text);
                            layer.setMetaData("coordsys_name", projName);
                        }
                    }
                    else
                        layer.setProjection("AUTO");

                    layer.status = mapscript.MS_ON;

                    // setting up the selected layer
                    selected = new MapObjectHolder(layer, target.GetParent());
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Unable to open layer, " + ex.Message, "MapManager", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (layer != null)
                    {
                        if (layer.map != null)
                            layer.map.removeLayer(layer.index);
                        layer = null;
                    }
                    return;
                }

                // if this is the first layer or the map projection hasn't been set zoom to this extent
                if (map.numlayers == 1 || map.getProjection().Trim().Length == 0)
                {
                    ZoomToLayerExtent(layer);
                    if (InitialExtentSet != null)
                        InitialExtentSet(this, null);

                    // setting the projection of the map according to the layer projection
                    string proj = layer.getProjection().Trim();
                    if (proj.Length > 0 && proj != "+AUTO")
                    {
                        map.setProjection(proj);
                        // setting up the default unit
                        if (proj.Contains("+proj=longlat"))
                            map.units = MS_UNITS.MS_DD;
                        else
                            map.units = MS_UNITS.MS_METERS;
                    }
                }

                if (target != null)
                    target.RaisePropertyChanged(this);
                RefreshView();

                TreeNode src = CurrentTree.SelectedNode;
                if (src != null && src.Tag != null)
                    RaiseItemSelect((MapObjectHolder)src.Tag);
                UpdateToolbar();
            }
        }