Exemple #1
0
        protected virtual string GetInternalProj4ProjectionParametersCore(FeatureLayer featureLayer)
        {
            Uri    uri          = GetUri(featureLayer);
            string result       = string.Empty;
            string pathFileName = string.Empty;

            if (uri != null && uri.Scheme.Equals("file") && File.Exists(uri.LocalPath))
            {
                pathFileName = uri.LocalPath;
            }

            if (File.Exists(pathFileName))
            {
                string prjPathFileName = Path.ChangeExtension(pathFileName, ".prj");
                if (File.Exists(prjPathFileName))
                {
                    string prjWkt = File.ReadAllText(prjPathFileName);
                    try
                    {
                        result = Proj4Projection.ConvertPrjToProj4(prjWkt);
                    }
                    catch (Exception e)
                    {
                        GisEditor.LoggerManager.Log(LoggerLevel.Debug, e.Message, new ExceptionInfo(e));
                    }
                }
            }

            return(result);
        }
        public static string GetProj4ProjectionParameter(string prjPathFileName)
        {
            string proj4Parameter = string.Empty;

            try
            {
                if (!File.Exists(prjPathFileName))
                {
                    var projectionWindow = new ProjectionWindow();
                    if (projectionWindow.ShowDialog().GetValueOrDefault())
                    {
                        proj4Parameter = projectionWindow.Proj4ProjectionParameters;
                        File.WriteAllText(prjPathFileName, Proj4Projection.ConvertProj4ToPrj(projectionWindow.Proj4ProjectionParameters));
                    }
                }
                else
                {
                    string wkt = File.ReadAllText(prjPathFileName);
                    proj4Parameter = Proj4Projection.ConvertPrjToProj4(wkt);
                }
            }
            catch (Exception ex)
            {
                GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
            }

            return(proj4Parameter);
        }
        private void SetInternalProjection()
        {
            string prjPath      = Path.ChangeExtension(shpLayer.ShapePathFilename, ".prj");
            var    requireIndex = shpLayer.RequireIndex;

            shpLayer.RequireIndex = false;
            bool isDecimalDegree = LayerPluginHelper.IsDecimalDegree(shpLayer);

            shpLayer.RequireIndex             = requireIndex;
            shpLayer.FeatureSource.Projection = new Proj4Projection();
            if (File.Exists(prjPath))
            {
                string wkt = File.ReadAllText(prjPath);
                try
                {
                    string          proj4      = Proj4Projection.ConvertPrjToProj4(wkt);
                    Proj4Projection projection = (Proj4Projection)ShpLayer.FeatureSource.Projection;
                    projection.InternalProjectionParametersString = proj4;
                    projection.SyncProjectionParametersString();
                    IsInternalProjectionDetermined = true;
                }
                catch (Exception ex)
                {
                    GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                    IsInternalProjectionDetermined = false;
                }
            }
            else if (isDecimalDegree)
            {
                Proj4Projection projection = (Proj4Projection)ShpLayer.FeatureSource.Projection;
                projection.InternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326);
                projection.SyncProjectionParametersString();
                IsInternalProjectionDetermined = true;
            }
            else
            {
                IsInternalProjectionDetermined = false;
            }
        }
Exemple #4
0
        private void LoadFromFileClick(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Multiselect = false;
            openFileDialog.Filter      = "Projection File(*.prj)|*.prj";
            if (openFileDialog.ShowDialog().GetValueOrDefault())
            {
                viewModel.SelectedProj4Model = new Proj4Model();
                string wktString = File.ReadAllText(openFileDialog.FileName);
                if (!string.IsNullOrEmpty(wktString))
                {
                    try
                    {
                        viewModel.SelectedProj4Model.Proj4Parameter = Proj4Projection.ConvertPrjToProj4(wktString);
                    }
                    catch (Exception ex)
                    {
                        GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                        System.Windows.Forms.MessageBox.Show(ex.Message, GisEditor.LanguageManager.GetStringResource("MessageBoxWarningTitle"), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                    if (string.IsNullOrEmpty(viewModel.SelectedProj4Model.Proj4Parameter))
                    {
                        System.Windows.Forms.MessageBox.Show(GisEditor.LanguageManager.GetStringResource("OtherProjectionNotSupportedText"), GisEditor.LanguageManager.GetStringResource("MessageBoxWarningTitle"), System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    }
                    var hostWindow = Window.GetWindow(this);
                    if (hostWindow != null)
                    {
                        var projectionSelectionViewModel = hostWindow.DataContext as ProjectionSelectionViewModel;
                        if (projectionSelectionViewModel != null)
                        {
                            projectionSelectionViewModel.SelectedViewModel = viewModel;
                        }
                    }
                }
            }
        }
        protected override void RunCore()
        {
            //if (FeatureLayer != null && FeatureLayer.FeatureSource.LinkSources.Count > 0)
            //{
            //    hasLinkSource = true;
            //}

            if (FeaturesForExporting.Count == 0 && FeatureIdsForExporting.Count > 0 && FeatureLayer != null)
            {
                FeatureLayer.CloseAll();
                FeatureLayer.SafeProcess(() =>
                {
                    //if (FeatureLayer.FeatureSource.LinkSources.Count > 0)
                    //{
                    //    featuresForExporting = FeatureLayer.FeatureSource.GetFeaturesByIds(FeatureIdsForExporting, FeatureLayer.GetDistinctColumnNames());
                    //    hasLinkSource = true;
                    //}
                    //else
                    //{
                    //    featuresForExporting = FeatureLayer.QueryTools.GetFeaturesByIds(FeatureIdsForExporting, FeatureLayer.GetDistinctColumnNames());
                    //    hasLinkSource = false;
                    //}

                    featuresForExporting = FeatureLayer.QueryTools.GetFeaturesByIds(FeatureIdsForExporting, FeatureLayer.GetDistinctColumnNames());
                    hasLinkSource        = false;
                });
            }

            Proj4Projection proj4 = new Proj4Projection();

            proj4.InternalProjectionParametersString = InternalPrj4Projection;
            proj4.ExternalProjectionParametersString = Proj4Projection.ConvertPrjToProj4(ProjectionWkt);
            proj4.SyncProjectionParametersString();
            proj4.Open();

            Collection <Feature> exportFeatures = FeaturesForExporting;

            if (proj4.CanProject())
            {
                exportFeatures = new Collection <Feature>();
                foreach (var item in FeaturesForExporting)
                {
                    Feature newFeature = item;
                    newFeature = proj4.ConvertToExternalProjection(item);
                    exportFeatures.Add(newFeature);
                }
            }
            proj4.Close();

            #region This is a trick to fix that fox pro columns cannot write to dbf, convert all fox pro columns to character column type.

            if (hasLinkSource)
            {
                Collection <string> dateTimeColumns = new Collection <string>();
                Collection <string> dateColumns     = new Collection <string>();

                foreach (var item in FeatureSourceColumns)
                {
                    string doubleInBinary  = DbfColumnType.DoubleInBinary.ToString();
                    string integerInBinary = DbfColumnType.IntegerInBinary.ToString();
                    string dateTime        = DbfColumnType.DateTime.ToString();
                    string date            = DbfColumnType.Date.ToString();
                    string logical         = DbfColumnType.Logical.ToString();

                    if (item.TypeName.Equals(doubleInBinary, StringComparison.OrdinalIgnoreCase) ||
                        item.TypeName.Equals(integerInBinary, StringComparison.OrdinalIgnoreCase) ||
                        item.TypeName.Equals(dateTime, StringComparison.OrdinalIgnoreCase) ||
                        item.TypeName.Equals(date, StringComparison.OrdinalIgnoreCase) ||
                        item.TypeName.Equals(logical, StringComparison.OrdinalIgnoreCase))
                    {
                        item.TypeName = DbfColumnType.Character.ToString();
                    }
                }

                Dictionary <string, int> longLengthColumnNames = new Dictionary <string, int>();

                //foreach (var feature in exportFeatures)
                //{
                //    // Fill link column values into column values.
                //    foreach (var item in feature.LinkColumnValues)
                //    {
                //        string key = item.Key;
                //        string value = string.Join("\r\n", item.Value.Select(v => v.Value));
                //        feature.ColumnValues[key] = value;
                //        longLengthColumnNames[key] = value.Length;
                //    }
                //}

                // fix too long column value
                foreach (var key in longLengthColumnNames.Keys)
                {
                    FeatureSourceColumn column = FeatureSourceColumns.FirstOrDefault(f => f.ColumnName.Equals(key, StringComparison.OrdinalIgnoreCase));
                    if (column != null)
                    {
                        int length = longLengthColumnNames[key];
                        if (length > column.MaxLength)
                        {
                            column.MaxLength = length;
                        }
                    }
                }
            }

            #endregion This is a trick to fix that fox pro columns cannot write to dbf.

            if (NeedConvertMemoToCharacter)
            {
                foreach (var feature in exportFeatures)
                {
                    foreach (var item in FeatureSourceColumns)
                    {
                        if (item.TypeName.Equals(DbfColumnType.Memo.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            item.TypeName  = DbfColumnType.Character.ToString();
                            item.MaxLength = 254;
                        }
                        else if (item.TypeName.Equals(DbfColumnType.Character.ToString(), StringComparison.OrdinalIgnoreCase) &&
                                 item.MaxLength > 254)
                        {
                            item.MaxLength = 254;
                        }
                        //if (feature.ColumnValues.ContainsKey(item.ColumnName) && feature.ColumnValues[item.ColumnName].Length > 254)
                        //{
                        //    feature.ColumnValues[item.ColumnName] = feature.ColumnValues[item.ColumnName].Substring(0, 254);
                        //}
                    }
                }
            }

            var info = new FileExportInfo(exportFeatures, FeatureSourceColumns, OutputPathFileName, ProjectionWkt);
            foreach (var item in CostomizedColumnNames)
            {
                info.CostomizedColumnNames.Add(item.Key, item.Value);
            }
            Export(info);
        }