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); }
private void RebuildDbf(string path) { string dbfPath = Path.ChangeExtension(path, ".dbf"); if (File.Exists(dbfPath)) { File.SetAttributes(dbfPath, FileAttributes.Normal); using (GeoDbf geoDbf = new GeoDbf(dbfPath, GeoFileReadWriteMode.ReadWrite)) { geoDbf.Open(); int columnNumber = -1; for (int i = 1; i <= geoDbf.ColumnCount; i++) { string columnName = geoDbf.GetColumnName(i); if (columnName.Equals("RECID", StringComparison.OrdinalIgnoreCase)) { columnNumber = i; break; } } if (columnNumber > -1) { for (int i = 1; i <= geoDbf.RecordCount; i++) { geoDbf.WriteField(i, columnNumber, i); } } } } }
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)); }
/// <summary> /// Points to Draw Elapsed MS /// 10000 605 /// 20000 998 /// 50000 2658 /// 100000 4980 /// </summary> private static double GetRecommendPointToValueRatio(FeatureLayer layer, string columnName) { int pointsToDraw = 0; // better performance. if (layer is ShapeFileFeatureLayer) { var shpLayer = (ShapeFileFeatureLayer)layer; var shpFName = shpLayer.ShapePathFilename; var dbfFName = Path.ChangeExtension(shpFName, ".dbf"); if (File.Exists(dbfFName)) { using (GeoDbf geoDbf = new GeoDbf(dbfFName, GeoFileReadWriteMode.Read)) { geoDbf.Open(); for (int i = 1; i <= geoDbf.RecordCount; i++) { string fieldValue = geoDbf.ReadFieldAsString(i, columnName); try { pointsToDraw += (int)Convert.ToDouble(fieldValue); } catch (Exception ex) { GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex)); } } geoDbf.Close(); } } } else { IEnumerable <Feature> features = null; layer.SafeProcess(() => { features = layer.QueryTools.GetAllFeatures(new string[] { columnName }); }); pointsToDraw = features.Sum(tmpFeature => { try { return((int)Convert.ToDouble(tmpFeature.ColumnValues[columnName])); } catch (Exception ex) { GisEditor.LoggerManager.Log(LoggerLevel.Debug, ex.Message, new ExceptionInfo(ex)); return(0); } }); } double pointToValueRatio = 1d; if (maxDotsToDraw < pointsToDraw) { pointToValueRatio = (double)maxDotsToDraw / (double)pointsToDraw; } return(pointToValueRatio); }
public override Collection <FeatureSourceColumn> GetColumnToAdd(string filePath, string customParameter) { Collection <FeatureSourceColumn> result = new Collection <FeatureSourceColumn>(); using (GeoDbf geoDbf = new GeoDbf(filePath, GeoFileReadWriteMode.Read)) { geoDbf.Open(); var columns = geoDbf.GetAllColumns(); foreach (var item in columns) { DataJoinFeatureSourceColumn csvColumn = new DataJoinFeatureSourceColumn(item.ColumnName, item.TypeName, item.MaxLength); result.Add(csvColumn); } geoDbf.Close(); } return(result); }
public override DataTable ReadDataToDataGrid(string filePath, string customParameter) { DataTable PreviewDataTable = new DataTable(); if (File.Exists(filePath)) { DataTable dataTable = new DataTable(); using (GeoDbf geoDbf = new GeoDbf(filePath, GeoFileReadWriteMode.Read)) { geoDbf.Open(); dataTable = new DataTable(); var columns = geoDbf.GetAllColumns(); foreach (var item in columns) { dataTable.Columns.Add(item.ColumnName); } for (int i = 1; i <= geoDbf.RecordCount; i++) { DataRow dr = dataTable.NewRow(); var dictionary = geoDbf.ReadRecordAsString(i); for (int j = 1; j <= geoDbf.ColumnCount; j++) { string value; dictionary.TryGetValue(geoDbf.GetColumnName(j), out value); dr[j - 1] = value; } dataTable.Rows.Add(dr); } geoDbf.Close(); } PreviewDataTable = dataTable; } return(PreviewDataTable); }
private void GenerateExportConfig() { Collection <SplitFileModel> exportedModels = new Collection <SplitFileModel>(); SelectedLayerToSplit.SafeProcess(() => { string selectedColumnName = SelectedFeatureSourceColumn.ColumnName; Collection <Tuple <string, int> > distinctColumnValues = new Collection <Tuple <string, int> >(); if (UseSelectedFeaturesOnly) { var tmpColumnValues = HighlightFeatureLayer.InternalFeatures .Where(tmpFeature => tmpFeature.Tag.Equals(SelectedLayerToSplit)) .Select(tmpFeature => tmpFeature.ColumnValues[selectedColumnName]); foreach (var item in tmpColumnValues.GroupBy(tmpColumnValue => tmpColumnValue)) { distinctColumnValues.Add(new Tuple <string, int>(item.Key, item.Count())); } } else { var tmpShapeFileFeatureLayer = SelectedLayerToSplit as ShapeFileFeatureLayer; Collection <string> values = new Collection <string>(); if (tmpShapeFileFeatureLayer != null) { string dbfPath = Path.ChangeExtension(tmpShapeFileFeatureLayer.ShapePathFilename, ".dbf"); GeoDbf geoDbf = new GeoDbf(dbfPath, GeoFileReadWriteMode.Read, tmpShapeFileFeatureLayer.Encoding); geoDbf.Open(); for (int i = 1; i <= geoDbf.RecordCount; i++) { var columnValue = geoDbf.ReadRecordAsString(i)[selectedColumnName]; values.Add(columnValue); } geoDbf.Close(); } else { foreach (var feature in SelectedLayerToSplit.QueryTools.GetAllFeatures(new string[] { selectedColumnName })) { values.Add(feature.ColumnValues[selectedColumnName]); } } var groupedTmpColumnValues = values.GroupBy(tmpColumnValue => tmpColumnValue); foreach (var item in groupedTmpColumnValues) { distinctColumnValues.Add(new Tuple <string, int>(item.Key, item.Count())); } } foreach (var distinctValue in distinctColumnValues) { string columnValue = distinctValue.Item1; if (!String.IsNullOrEmpty(columnValue)) { Regex regex = new Regex(@"\w*"); var matches = regex.Matches(columnValue); StringBuilder fileNameBuilder = new StringBuilder(); foreach (Match match in matches) { fileNameBuilder.Append(match.Value); } columnValue = fileNameBuilder.ToString(); } SplitFileModel model = new SplitFileModel(); model.ColumnValue = String.IsNullOrEmpty(distinctValue.Item1) ? "<Blank>" : distinctValue.Item1; model.FeatureCount = distinctValue.Item2; model.OutputFileName = String.Format(CultureInfo.InvariantCulture , "{0}_{1}" , SelectedLayerToSplit.Name , String.IsNullOrEmpty(columnValue) ? "unknown" : columnValue); if (exportedModels .Any(tmpModel => tmpModel.OutputFileName.Equals(model.OutputFileName, StringComparison.OrdinalIgnoreCase))) { var count = exportedModels.Count(tmpModel => tmpModel.OutputFileName.Equals(model.OutputFileName, StringComparison.OrdinalIgnoreCase)); model.OutputFileName += String.Format(CultureInfo.InvariantCulture, "_{0}", count); //model.OutputFileName = model.OutputFileName.Replace(".shp" //, String.Format(CultureInfo.InvariantCulture, "_{0}.shp", count)); } exportedModels.Add(model); } }); #if GISEditorUnitTest foreach (var model in exportedModels) { ExportConfiguration.Add(model); } #else if (Application.Current != null) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { foreach (var model in exportedModels) { ExportConfiguration.Add(model); } })); } #endif IsBusy = false; BusyContent = String.Empty; CurrentThread = null; }