Exemplo n.º 1
0
        public static int CalculateSpeceficValuesCount(string requireColumnName, string fieldValue, ShapeFileFeatureLayer featureLayer)
        {
            int count = 0;

            if (!String.IsNullOrEmpty(requireColumnName))
            {
                featureLayer.SafeProcess(() =>
                {
                    string dbfPath = Path.ChangeExtension(featureLayer.ShapePathFilename, ".dbf");
                    if (File.Exists(dbfPath))
                    {
                        using (GeoDbf dbf = new GeoDbf(dbfPath))
                        {
                            dbf.Open();
                            string orignalColumnName = requireColumnName;
                            for (int i = 1; i <= dbf.RecordCount; i++)
                            {
                                var currentFieldValue = dbf.ReadFieldAsString(i, orignalColumnName);
                                if (currentFieldValue.Equals(fieldValue, StringComparison.Ordinal))
                                {
                                    count++;
                                }
                            }
                            dbf.Close();
                        }
                    }
                });
            }
            return(count);
        }
Exemplo n.º 2
0
        protected override Collection <Feature> SearchPlacesCore(string inputAddress, Layer layerToSearch)
        {
            Collection <Feature> features = new Collection <Feature>();

            ShapeFileFeatureLayer layer = layerToSearch as ShapeFileFeatureLayer;

            if (layer != null)
            {
                Collection <Feature> layerFeatures = null;
                layer.SafeProcess(() =>
                {
                    layerFeatures = layer.QueryTools.GetAllFeatures(ReturningColumnsType.AllColumns);
                });

                if (layerFeatures != null)
                {
                    foreach (var feature in layerFeatures)
                    {
                        foreach (var columnValue in feature.ColumnValues)
                        {
                            if (columnValue.Value.ToUpperInvariant().Contains(inputAddress.ToUpperInvariant()))
                            {
                                FillMatchesInGeocoder(feature);
                                feature.ColumnValues["Street"] = columnValue.Value;
                                features.Add(feature);
                                break;
                            }
                        }
                    }
                }
            }

            return(features);
        }
Exemplo n.º 3
0
        public static IEnumerable <IGrouping <string, string> > GroupColumnValues(string requireColumnName
                                                                                  , ShapeFileFeatureLayer featureLayer)
        {
            Collection <string> fieldValues = new Collection <string>();

            if (!String.IsNullOrEmpty(requireColumnName))
            {
                featureLayer.SafeProcess(() =>
                {
                    string dbfPath = Path.ChangeExtension(featureLayer.ShapePathFilename, ".dbf");
                    if (File.Exists(dbfPath))
                    {
                        string orignalColumnName = requireColumnName;
                        using (GeoDbf dbf = new GeoDbf(dbfPath))
                        {
                            dbf.Open();
                            for (int i = 1; i <= dbf.RecordCount; i++)
                            {
                                fieldValues.Add(dbf.ReadFieldAsString(i, orignalColumnName));
                            }
                            dbf.Close();
                        }
                    }
                });
            }

            return(fieldValues.GroupBy(fieldValue => fieldValue));
        }
        private static void AddFeaturesToShapeFileFeatureLayer(ShapeFileFeatureLayer shapeFileFeatureLayer, Collection <Feature> features)
        {
            shapeFileFeatureLayer.ReadWriteMode = GeoFileReadWriteMode.ReadWrite;

            shapeFileFeatureLayer.SafeProcess(() =>
            {
                List <string> columnNames = shapeFileFeatureLayer.GetDistinctColumnNames().ToList();
                shapeFileFeatureLayer.FeatureSource.BeginTransaction();
                foreach (var columnName in features[0].ColumnValues.Keys)
                {
                    if (!columnNames.Contains(columnName))
                    {
                        shapeFileFeatureLayer.FeatureSource.AddColumn(new FeatureSourceColumn(columnName, DbfColumnType.Character.ToString(), 100));
                    }
                }
                shapeFileFeatureLayer.FeatureSource.CommitTransaction();

                foreach (Feature feature in features)
                {
                    shapeFileFeatureLayer.EditTools.BeginTransaction();
                    shapeFileFeatureLayer.EditTools.Add(feature);
                    shapeFileFeatureLayer.EditTools.CommitTransaction();
                }
            });
        }
        private int GetUpperBounds(IEnumerable <KeyValuePair <string, string> > existingShapePathFileNames)
        {
            int upperBounds = existingShapePathFileNames.Sum(tmpPathFileName =>
            {
                int featuresCount = 0;
                ShapeFileFeatureLayer featureLayer = new ShapeFileFeatureLayer(tmpPathFileName.Key);
                featureLayer.SafeProcess(() =>
                {
                    featuresCount = featureLayer.QueryTools.GetCount();
                });

                return(featuresCount);
            });

            return(upperBounds);
        }
        protected override SimpleShapeType GetFeatureSimpleShapeTypeCore(FeatureLayer featureLayer)
        {
            SimpleShapeType       result = SimpleShapeType.Unknown;
            ShapeFileFeatureLayer shapeFileFeatureLayer = featureLayer as ShapeFileFeatureLayer;
            bool isDataSourceAvailable = DataSourceResolveTool.IsDataSourceAvailable(featureLayer);

            if (shapeFileFeatureLayer != null && isDataSourceAvailable)
            {
                ShapeFileType shapeFileType = ShapeFileType.Null;
                shapeFileFeatureLayer.SafeProcess(()
                                                  => shapeFileType = shapeFileFeatureLayer.GetShapeFileType());

                switch (shapeFileType)
                {
                case ShapeFileType.Point:
                case ShapeFileType.PointZ:
                case ShapeFileType.Multipoint:
                case ShapeFileType.PointM:
                case ShapeFileType.MultipointM:
                    result = SimpleShapeType.Point;
                    break;

                case ShapeFileType.Polyline:
                case ShapeFileType.PolylineZ:
                case ShapeFileType.PolylineM:
                case ShapeFileType.Multipatch:
                    result = SimpleShapeType.Line;
                    break;

                case ShapeFileType.Polygon:
                case ShapeFileType.PolygonM:
                case ShapeFileType.PolygonZ:
                    result = SimpleShapeType.Area;
                    break;
                }
            }

            return(result);
        }
        private static void CreateIndexFile(string idxPathFileName, string sourceShapeFilePath)
        {
            ShapeFileFeatureLayer sourceLayer = new ShapeFileFeatureLayer(sourceShapeFilePath);

            sourceLayer.RequireIndex = false;
            ShapeFileType shapeFileType = ShapeFileType.Null;

            sourceLayer.SafeProcess(() =>
            {
                shapeFileType = sourceLayer.GetShapeFileType();
            });
            //sourceLayer.Open();
            //ShapeFileType shapeFileType = sourceLayer.GetShapeFileType();
            //sourceLayer.Close();

            if (shapeFileType == ShapeFileType.Point || shapeFileType == ShapeFileType.PointZ || shapeFileType == ShapeFileType.PointM)
            {
                RtreeSpatialIndex.CreatePointSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
            }
            else
            {
                RtreeSpatialIndex.CreateRectangleSpatialIndex(idxPathFileName, RtreeSpatialIndexPageSize.EightKilobytes, RtreeSpatialIndexDataFormat.Float);
            }
        }
        private void CreateShapeFile(ObservableCollection <FeatureSourceColumn> includedColumnsList
                                     , string OutputPath, Encoding ShapeFileEncoding, string csvFilePath
                                     , List <Feature> features
                                     , bool isIncludeAllFeatures
                                     , IEnumerable <MatchCondition> matchConditions
                                     , Action <UpdatingTaskProgressEventArgs> updateAction
                                     , Dictionary <string, string> invalidColumns)
        {
            Collection <DbfColumn> includeColumns = new Collection <DbfColumn>();

            RemoveUnduplicateColumn(includedColumnsList);

            foreach (var column in includedColumnsList)
            {
                DbfColumnType tmpDbfColumnType = DbfColumnType.Character;
                if (Enum.TryParse(column.TypeName, out tmpDbfColumnType))
                {
                    DbfColumn dbfColumn = new DbfColumn(column.ColumnName, tmpDbfColumnType, column.MaxLength, 0);
                    includeColumns.Add(dbfColumn);
                }
            }

            ShapeFileType shapeFileType = GetShapeFileType(features.FirstOrDefault());

            if (shapeFileType != ShapeFileType.Null)
            {
                ShapeFileFeatureLayer.CreateShapeFile(shapeFileType,
                                                      OutputPath, includeColumns, ShapeFileEncoding, OverwriteMode.Overwrite);

                var dataTable   = DataJoinAdapter.ReadDataToDataGrid(csvFilePath, Delimiter);
                var featureRows = dataTable.Rows;
                var index       = 0;
                var count       = features.Count;

                ShapeFileFeatureLayer newShapeFileFeatureLayer = new ShapeFileFeatureLayer(OutputPath, GeoFileReadWriteMode.ReadWrite);
                newShapeFileFeatureLayer.SafeProcess(() =>
                {
                    newShapeFileFeatureLayer.EditTools.BeginTransaction();

                    foreach (var feature in features)
                    {
                        index++;
                        try
                        {
                            var matchedDataRow = featureRows.Cast <DataRow>().FirstOrDefault(r => matchConditions.All(tmpCondition => feature.ColumnValues[tmpCondition.SelectedLayerColumn.ColumnName]
                                                                                                                      == r[tmpCondition.SelectedDelimitedColumn.ColumnName].ToString()));

                            if (matchedDataRow != null)
                            {
                                SetFeatureColumnValues(feature, matchedDataRow, includedColumnsList, invalidColumns);
                                newShapeFileFeatureLayer.EditTools.Add(feature);
                            }
                            else if (isIncludeAllFeatures)
                            {
                                newShapeFileFeatureLayer.EditTools.Add(feature);
                            }

                            if (UpdateProgress(updateAction, index, count))
                            {
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            var errorEventArgs   = new UpdatingTaskProgressEventArgs(TaskState.Error);
                            errorEventArgs.Error = new ExceptionInfo(string.Format(CultureInfo.InvariantCulture, "Feature id: {0}, {1}"
                                                                                   , feature.Id, ex.Message)
                                                                     , ex.StackTrace
                                                                     , ex.Source);
                            GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex));
                            errorEventArgs.Message = feature.Id;
                            updateAction(errorEventArgs);
                        }
                    }
                    newShapeFileFeatureLayer.EditTools.CommitTransaction();
                });

                SavePrjFile(OutputPath, DisplayProjectionParameters);
            }
        }
        protected override Collection <MenuItem> GetLayerListItemContextMenuItemsCore(GetLayerListItemContextMenuParameters parameters)
        {
            var menuItems = base.GetLayerListItemContextMenuItemsCore(parameters);
            ShapeFileFeatureLayer shpLayer = (ShapeFileFeatureLayer)parameters.LayerListItem.ConcreteObject;
            MenuItem rebuildShpItem        = new MenuItem();

            rebuildShpItem.Header = GisEditor.LanguageManager.GetStringResource("ShapefileRebuildHeader");
            rebuildShpItem.Name   = "Rebuild";
            rebuildShpItem.Icon   = new Image {
                Source = new BitmapImage(new Uri("/GisEditorPluginCore;component/Images/rebuildShp.png", UriKind.RelativeOrAbsolute))
            };
            rebuildShpItem.Click += (s, e) =>
            {
                string directory = Path.Combine(GisEditor.InfrastructureManager.TemporaryPath, "Output");
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                shpLayer.CloseInOverlay();

                //bool isCanceld = false;

                ProgressWindow window = new ProgressWindow();
                window.Title = GisEditor.LanguageManager.GetStringResource("RebuildShapefileWindowTitle");
                window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                window.Owner       = Application.Current.MainWindow;
                window.MainContent = "Rebuilding...";

                window.ProgressAction = () =>
                {
                    shpLayer.SafeProcess(() =>
                    {
                        ShapeFileFeatureSource.Rebuilding += (sender, e1) =>
                        {
                            if (e1.ShapePathFilename.Equals(shpLayer.ShapePathFilename, StringComparison.InvariantCultureIgnoreCase))
                            {
                                e1.Cancel = window.IsCanceled;
                                Application.Current.Dispatcher.BeginInvoke(() =>
                                {
                                    window.Maximum       = e1.RecordCount;
                                    window.ProgressValue = e1.CurrentRecordIndex;
                                });
                            }
                        };
                        ShapeFileFeatureSource.Rebuild(shpLayer.ShapePathFilename);
                    });

                    Application.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (window.DialogResult == null)
                        {
                            window.DialogResult = !window.IsCanceled;
                        }
                    });
                };

                if (window.ShowDialog().GetValueOrDefault())
                {
                    GisEditor.ActiveMap.GetOverlaysContaining(shpLayer).ForEach(o => o.Invalidate());
                    MessageBox.Show(GisEditor.LanguageManager.GetStringResource("RebuildCompletedLabel"));
                }
            };
            var index = menuItems.Count - 3;

            if (index >= 0 && index <= menuItems.Count)
            {
                menuItems.Insert(index, LayerListMenuItemHelper.GetRebuildIndexMenuItem());
                menuItems.Insert(index++, rebuildShpItem);
            }
            return(menuItems);
        }
        private void Refresh()
        {
            Dictionary <string, object> information = new Dictionary <string, object>();

            targetFeatureLayer.SafeProcess(() =>
            {
                string fileName = targetFeatureLayer.ShapePathFilename.Replace('/', '\\');
                information.Add("File Name", fileName);
                information.Add("File Size", string.Format("{0} bytes", new FileInfo(targetFeatureLayer.ShapePathFilename).Length));

                string indexPathFileName = targetFeatureLayer.IndexPathFilename.Replace('/', '\\');
                information.Add("Index File", indexPathFileName);
                information.Add("ShapeFile Type", targetFeatureLayer.GetShapeFileType().ToString());
                information.Add("Layer Name", targetFeatureLayer.Name);
                information.Add("Columns Count", targetFeatureLayer.QueryTools.GetColumns().Count.ToString());
                information.Add("Rows Count", targetFeatureLayer.GetRecordCount().ToString());


                if (targetFeatureLayer.HasBoundingBox)
                {
                    RectangleShape boundingBox = targetFeatureLayer.GetBoundingBox();
                    information.Add("Upper Left X:", boundingBox.UpperLeftPoint.X.ToString("N4"));
                    information.Add("Upper Left Y:", boundingBox.UpperLeftPoint.Y.ToString("N4"));
                    information.Add("Lower Right X:", boundingBox.LowerRightPoint.X.ToString("N4"));
                    information.Add("Lower Right Y:", boundingBox.LowerRightPoint.Y.ToString("N4"));
                }
                else
                {
                    information.Add("Upper Left X:", double.NaN.ToString());
                    information.Add("Upper Left Y:", double.NaN.ToString());
                    information.Add("Lower Right X:", double.NaN.ToString());
                    information.Add("Lower Right Y:", double.NaN.ToString());
                }

                if (!targetFeatureLayer.Name.Equals("TempLayer"))
                {
                    information.Add("Encoding", GetAllEncodings());
                    selectedEncoding = targetFeatureLayer.Encoding;
                    targetFeatureLayer.SafeProcess(() =>
                    {
                        columns.Add(PleaseSelectText, PleaseSelectText);
                        foreach (var item in targetFeatureLayer.FeatureSource.GetColumns())
                        {
                            columns.Add(item.ColumnName, targetFeatureLayer.FeatureSource.GetColumnAlias(item.ColumnName));
                        }

                        information.Add("Feature ID Column", columns);
                        if (string.IsNullOrEmpty(FeatureIDColumn) && columns.Count > 0)
                        {
                            FeatureIDColumnAlias = columns.FirstOrDefault();
                        }

                        if (!string.IsNullOrEmpty(FeatureIDColumn) && columns.Count > 0)
                        {
                            FeatureIDColumnAlias = columns.FirstOrDefault(c => c.Key == FeatureIDColumn);
                        }
                    });
                }
            });

            LayerInformation = information;
        }