Hello, when I try to show the Attributevalues from the attached shapefile, i get an error. The function ‘GetDbfColumns’ returns incorrect Columnnames. If I convert the shapefile using QGIS, there are no errors. I used different tools to open the dbf-file. All tools haven’t any problems. The attached shapefile is an example. My customers got hundred shapefiles from an authorithy. To convert all these files using QGIS is not an option. Please fix this issue.
Regards Torsten
Anlagen.zip (35.2 KB)
Error open Attributefile from Shapefile
Hi Torsten,
Seems this issue is caused by column name of
your data contains string end flag("\0"). For example, the name of
first column is "IGZ\0ÌÕ¤ ºj", in C#, characters after “\0” in a
string will be ignored when display it, so if you try to Quick Watch on the
column name, it will display “IGZ”.
The way to resolve this issue
is remove characters from “\0” to end. I think we can create a tool to pre-process
these shapefiles with below code:
ShapeFileFeatureSource featureSource =
new
ShapeFileFeatureSource(originalShapeFileName);
featureSource.Open();
//Remove '\0' in column name;
var columns = featureSource.GetDbfColumns().Select(col =>
new
DbfColumn(col.ColumnName.Contains(
'\0'
) ? col.ColumnName.Remove(col.ColumnName.IndexOf(
'\0'
)) : col.ColumnName, col.ColumnType, col.Length, col.DecimalLength));
var features = featureSource.GetAllFeatures(ReturningColumnsType.AllColumns);
ShapeFileFeatureSource.CreateShapeFile(featureSource.GetShapeFileType(),
"./processedShapefile.shp"
, columns);
ShapeFileFeatureSource newFeatureSource =
new
ShapeFileFeatureSource(
"./processedShapefile.shp"
, ShapeFileReadWriteMode.ReadWrite);
newFeatureSource.Open();
newFeatureSource.BeginTransaction();
foreach
(var feature
in
features)
{
//Remove '\0' in column values
Dictionary<
string
,
string
> columnValues = feature.ColumnValues.ToDictionary(col => col.Key.Contains(
'\0'
) ? col.Key.Remove(col.Key.IndexOf(
'\0'
)) : col.Key, col => col.Value);
newFeatureSource.AddFeature(feature.GetShape(), columnValues);
}
newFeatureSource.CommitTransaction();
newFeatureSource.Close();
Hope this would be helpful and any question please feel free to let us know.
Thanks,
Kevin