NetTopologySuite / NetTopologySuite.IO.ShapeFile

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

How to read AttributeTable #52

Closed qiangwai closed 3 years ago

qiangwai commented 3 years ago

I want get ShapeFile's AttributeTable ,I did it this way

ShapefileDataReader dataReader = new ShapefileDataReader(shapepath, Factory, Encoding.UTF8);
for (int i = 1; i < fieldNames.Length; i++)
                    {
                        attributesTable.Add(fieldNames[i], dataReader.GetValue(i));
                    }

but Why dataReader's depth is 0,result in an empty reference exception to be thrown.My shp can be read in ArcMap.Is My method wrong? What is the general way to read AttributeTable?

DGuidi commented 3 years ago

see this sample

using (var reader = new ShapefileDataReader("crustal_test_bugged", Factory))
{
 int length = reader.DbaseHeader.NumFields;
 while (reader.Read())
 {
  Debug.WriteLine(reader.GetValue(length - 1));
 }
}

You missed the reader.Read(), that is basically the standard pattern when reading data from any source in .net

qiangwai commented 3 years ago

yes,you are right.@DGuidi But I have another question ,dataReader.GetValue[i]and dataReader.Geometry only support one Geometry and it's value?My Shape is multi Geometry ,I want read multi attribute ,How should I read?

DGuidi commented 3 years ago

It's not the way it works. A multi-geometry it's a single geometry object, so it has a single set of attributes: a multipoint is a single geometry composed of a number of points, and all these points share the same set of attributes, like a single geometry do.

qiangwai commented 3 years ago

Thank you@DGuidi .Now I can read all attributes.But sometimes messy code characters appear in attributes when read contains chinese character shape.How can i solve this problem? like this shape anhui_shp.zip

DGuidi commented 3 years ago

maybe related to #39

DGuidi commented 3 years ago

please check if last commit fix the issue, as expected