NetTopologySuite / NetTopologySuite.IO.ShapeFile

The ShapeFile IO module for NTS.
35 stars 25 forks source link

Can't encoding UTF-8 error #76

Closed yfz34 closed 3 years ago

yfz34 commented 3 years ago

Hello! I have a problem with reading shape file, when I used ShapefileDataWriter function to create shape file and read this shape file can't get correct name, but value can corrected encoding.

Create shape file

var attributesTable = new AttributesTable();
attributesTable.Add("備註1", "數值1");
var features = new List<IFeature>
{
    new Feature(geometry, attributesTable)
};

// Create the shapefile
var encoding = Encoding.UTF8;
var outGeomFactory = GeometryFactory.Default;
var writer = new ShapefileDataWriter(shapeFileName, outGeomFactory, encoding);
var outDbaseHeader = ShapefileDataWriter.GetHeader(features[0], features.Count, encoding);
writer.Header = outDbaseHeader;
writer.Write(features);

read shape file

encoding = Encoding.UTF8;
DbaseFileHeader.DefaultEncoding = encoding;
using (var reader = new ShapefileDataReader(filePath, GeometryFactory.Default, encoding))
{
    while (reader.Read())
    {
        // read
    }
}

and print result image

DGuidi commented 3 years ago

can you elaborate more the problem you are facing? I've built this test against the latest develop, but it does not show the problem you experienced

const string TestKey = "備註1";
const string TestVal = "數值1";
var encoding = Encoding.UTF8;

var factory = GeometryFactory.Default;
var geometry = factory.CreatePoint(new Coordinate(0, 0));
var attributes = new AttributesTable { { TestKey, TestVal } };
var feature = new Feature(geometry, attributes);
string outpath = Path.GetFileNameWithoutExtension(Path.GetTempFileName());
var writer = new ShapefileDataWriter(outpath, factory, encoding)
{
    Header = ShapefileDataWriter.GetHeader(feature, 1, encoding)
};
writer.Write(new List<IFeature> { feature });

using var reader = new ShapefileDataReader(outpath, factory, encoding);
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Geometry.EqualsExact(geometry));
Assert.AreEqual(reader.GetOrdinal(TestKey), 1);
Assert.AreEqual(reader.GetName(1), TestKey);
Assert.AreEqual(reader.GetString(1), TestVal);
Assert.IsFalse(reader.Read());
yfz34 commented 3 years ago

I use nuget to add this package and version is 2.0.0. And I try to built the latest develop package. It's successed encoding. Thank you so much!