Ejemplo n.º 1
0
        internal void HandleTypeLoadException(TypeLoadException ex)
        {
            if (ex == null)
            {
                return;
            }

            string message = ex.Message;

            if (!existedExceptions.ContainsKey(this))
            {
                existedExceptions[this] = new Collection <string>();
            }

            if (!existedExceptions[this].Contains(message))
            {
                existedExceptions[this].Add(message);

                //string tempMessage1 = string.Format(CultureInfo.InvariantCulture, "A TypeLoadException is caught. This exception might cause by a breaking change of MapSuite APIs. It will break functionality of {0}. Please ", ex.Source);
                //string tempMessage2 = string.Format(CultureInfo.InvariantCulture, " to update corresponding assemblies. ");

                GisEditorMessageBox messageBox = new GisEditorMessageBox(MessageBoxButton.OK);
                messageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                messageBox.Owner   = Application.Current.MainWindow;
                messageBox.Title   = "Info";
                messageBox.Message = string.Format(CultureInfo.InvariantCulture, "A TypeLoadException is caught. This exception might because \"{0}\" plugin's version is not match this GIS Editor or some plugin(s) don’t implement the required APIs. Please update the plugins to match this version of GIS Editor. Remove those plugins would also remove this exception.", ex.Source);

                //Run run = new Run("contact ThinkGeo");
                //Hyperlink hyperlink = new Hyperlink(run);
                //hyperlink.Click += (s, e) =>
                //{
                //    messageBox.Close();
                //    GisEditor.LoggerManager.Log(LoggerLevel.Error, message, ex);
                //};
                //messageBox.Inlines.Add(tempMessage1);
                //messageBox.Inlines.Add(hyperlink);
                //messageBox.Inlines.Add(tempMessage2);

                StringBuilder sb = new StringBuilder();
                sb.Append(String.Format(CultureInfo.InvariantCulture, "Source: \t{0}\r\n", ex.Source));
                sb.Append(String.Format(CultureInfo.InvariantCulture, "Message: \t{0}\r\n", ex.Message));
                sb.Append(String.Format(CultureInfo.InvariantCulture, "Stack: \t{0}\r\n", ex.StackTrace));
                messageBox.ErrorMessage = sb.ToString();

                messageBox.ToggleButtonClickAction = new Action(() =>
                {
                    messageBox.Close();
                    GisEditor.LoggerManager.Log(LoggerLevel.Error, message, ex);
                });

                messageBox.ShowDialog();
            }

            GisEditor.LoggerManager.Log(LoggerLevel.Debug, message, ex);
        }
        private void SaveChangesClick(object sender, RoutedEventArgs e)
        {
            if (viewModel.EditDataChanges.Count > 0)
            {
                string[] ids = viewModel.EditDataChanges.Select(edc => edc.FeatureID).Distinct().ToArray();
                try
                {
                    TransactionResult result = null;
                    lock (viewModel.SelectedLayerAdapter)
                    {
                        var overlays = DataViewerViewModel.FindLayerOverlayContaining((Tag as GisEditorWpfMap), viewModel.SelectedLayer);
                        foreach (var overlay in overlays)
                        {
                            overlay.Close();
                        }
                        viewModel.ChangeCurrentLayerReadWriteMode(GeoFileReadWriteMode.ReadWrite);
                        viewModel.OpenFeatureLayer();
                        Collection <Feature> features = viewModel.SelectedLayer.QueryTools.GetFeaturesByIds(ids, viewModel.SelectedLayer.GetDistinctColumnNames());
                        var columns = viewModel.SelectedLayer.QueryTools.GetColumns();
                        foreach (var group in viewModel.EditDataChanges.GroupBy(edc => edc.FeatureID))
                        {
                            Feature resultFeature = features.FirstOrDefault(f => f.Id.Equals(group.Key));
                            if (resultFeature != null)
                            {
                                foreach (var item in group)
                                {
                                    resultFeature.ColumnValues[item.ColumnName] = item.NewValue;
                                }
                                var isColumnValueValid = CheckColumnValuesAreValid(resultFeature, columns);
                                if (!isColumnValueValid)
                                {
                                    var failureReasons = new Dictionary <string, string>();
                                    failureReasons.Add("InvalidValue", "Invalid value, please input a valid value.");
                                    result = new TransactionResult(0, 1, failureReasons, TransactionResultStatus.Failure);
                                    features.Remove(resultFeature);
                                    foreach (var item in group)
                                    {
                                        item.Undo();
                                    }
                                }
                            }
                        }

                        if (features.Count > 0)
                        {
                            viewModel.SelectedLayer.EditTools.BeginTransaction();
                            foreach (var feature in features)
                            {
                                viewModel.SelectedLayer.EditTools.Update(feature);
                            }
                            result = viewModel.SelectedLayer.EditTools.CommitTransaction();
                        }
                    }

                    if (result != null && result.TotalFailureCount != 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (var keyValue in result.FailureReasons)
                        {
                            sb.AppendLine(keyValue.Value);
                            foreach (var item in viewModel.EditDataChanges.Where(edc => edc.FeatureID.Equals(keyValue.Key)))
                            {
                                item.Undo();
                            }
                        }

                        if (!isClosing)
                        {
                            if (result.TotalFailureCount > 1)
                            {
                                string message = "Many errors occur.";
                                GisEditorMessageBox messageBox = new GisEditorMessageBox(MessageBoxButton.OK);
                                messageBox.Message      = message;
                                messageBox.ErrorMessage = sb.ToString();
                                messageBox.Title        = GisEditor.LanguageManager.GetStringResource("FeatureAttibuteWindowUpdateFailedCaption");
                                messageBox.ShowDialog();
                            }
                            else
                            {
                                string message = sb.ToString();
                                if (message.Contains("The Value you input is too long"))
                                {
                                    message = "Value entered exceeds field length.";
                                }
                                GisEditorMessageBox messageBox = new GisEditorMessageBox(MessageBoxButton.OK);
                                messageBox.Message      = message;
                                messageBox.ErrorMessage = sb.ToString();
                                messageBox.Title        = GisEditor.LanguageManager.GetStringResource("FeatureAttibuteWindowUpdateFailedCaption");
                                messageBox.ShowDialog();
                            }
                        }
                    }
                    else
                    {
                        if (!viewModel.ChangedLayers.Contains(viewModel.SelectedLayer))
                        {
                            viewModel.ChangedLayers.Add(viewModel.SelectedLayer);
                        }
                    }
                }
                catch (UnauthorizedAccessException accessEx)
                {
                    System.Windows.Forms.MessageBox.Show(accessEx.Message, "Access Denied");
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, accessEx.Message, new ExceptionInfo(accessEx));
                }
                finally
                {
                    viewModel.CloseFeatureLayer();
                    viewModel.ChangeCurrentLayerReadWriteMode(GeoFileReadWriteMode.Read);
                }
                viewModel.EditDataChanges.Clear();
            }
        }
        private void ExportToShapeFile(Collection <Feature> resultFeatures, Collection <FeatureSourceColumn> columns, FeatureLayerPlugin sourceLayerPlugin, WellKnownType type)
        {
            int count = resultFeatures.Count;

            if (count > 0)
            {
                FeatureLayerPlugin targetLayerPlugin = (FeatureLayerPlugin)GisEditor.LayerManager.GetLayerPlugins(typeof(ShapeFileFeatureLayer)).FirstOrDefault();
                FeatureLayer       resultLayer       = null;

                if (targetLayerPlugin != null)
                {
                    GetLayersParameters             getLayerParameters = new GetLayersParameters();
                    ConfigureFeatureLayerParameters parameters         = targetLayerPlugin.GetCreateFeatureLayerParameters(columns);
                    if (parameters != null && sourceLayerPlugin != null)
                    {
                        bool needColumns = false;
                        Collection <string> tempColumns = new Collection <string>();
                        if (parameters.CustomData.ContainsKey("Columns"))
                        {
                            tempColumns = parameters.CustomData["Columns"] as Collection <string>;
                        }
                        else
                        {
                            needColumns = true;
                        }

                        var featureColumns = columns.Where(c => needColumns || tempColumns.Contains(c.ColumnName));
                        if (targetLayerPlugin.CanCreateFeatureLayerWithSourceColumns(sourceLayerPlugin))
                        {
                            foreach (var item in featureColumns)
                            {
                                FeatureSourceColumn column = new FeatureSourceColumn(item.ColumnName, item.TypeName, item.MaxLength);
                                if (column.TypeName.Equals("c", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    column.TypeName = "Character";
                                }
                                parameters.AddedColumns.Add(column);
                            }
                        }
                        else
                        {
                            var geoColumns = sourceLayerPlugin.GetIntermediateColumns(featureColumns);
                            foreach (var item in geoColumns)
                            {
                                if (item.TypeName.Equals("c", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    item.TypeName = "Character";
                                }
                                parameters.AddedColumns.Add(item);
                            }
                        }


                        parameters.WellKnownType = type;
                        //parameters.CustomData["SourceLayer"] = featureLayer;

                        getLayerParameters.LayerUris.Add(parameters.LayerUri);
                        foreach (var item in parameters.CustomData)
                        {
                            getLayerParameters.CustomData[item.Key] = item.Value;
                        }

                        Proj4Projection proj4 = new Proj4Projection();
                        proj4.InternalProjectionParametersString = parameters.Proj4ProjectionParametersString;
                        proj4.ExternalProjectionParametersString = GisEditor.ActiveMap.DisplayProjectionParameters;
                        proj4.SyncProjectionParametersString();
                        proj4.Open();

                        foreach (var item in resultFeatures)
                        {
                            Feature feature = proj4.ConvertToInternalProjection(item);
                            parameters.AddedFeatures.Add(feature);
                        }

                        if (parameters.MemoColumnConvertMode == MemoColumnConvertMode.ToCharacter)
                        {
                            foreach (var item in parameters.AddedColumns.Where(c => c.TypeName.Equals("Memo", StringComparison.InvariantCultureIgnoreCase)).ToList())
                            {
                                item.TypeName  = "Character";
                                item.MaxLength = 254;
                                DbfColumn tmpDbfColumn = item as DbfColumn;
                                if (tmpDbfColumn != null)
                                {
                                    tmpDbfColumn.ColumnType = DbfColumnType.Character;
                                    tmpDbfColumn.Length     = 254;
                                }
                            }
                        }

                        resultLayer = targetLayerPlugin.CreateFeatureLayer(parameters);
                        resultLayer.FeatureSource.Projection = proj4;
                        resultLayer = targetLayerPlugin.GetLayers(getLayerParameters).FirstOrDefault() as FeatureLayer;
                    }
                }

                if (resultLayer != null)
                {
                    GisEditorMessageBox messageBox = new GisEditorMessageBox(MessageBoxButton.YesNo);
                    messageBox.Owner = Application.Current.MainWindow;
                    messageBox.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                    messageBox.Title        = GisEditor.LanguageManager.GetStringResource("NavigatePluginAddToMap");
                    messageBox.Message      = GisEditor.LanguageManager.GetStringResource("DoYouWantToAddToMap");
                    messageBox.ErrorMessage = string.Empty;
                    if (messageBox.ShowDialog().Value)
                    {
                        GisEditor.ActiveMap.AddLayerToActiveOverlay(resultLayer);
                        GisEditor.ActiveMap.RefreshActiveOverlay();
                        RefreshArgs refreshArgs = new RefreshArgs(this, "LoadToMapCore");
                        InvokeRefreshPlugins(GisEditor.UIManager, refreshArgs);
                        GisEditor.ActiveMap.Refresh();
                    }
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("There is no features to export.", "Export");
            }
        }