NetTopologySuite / NetTopologySuite.IO.ShapeFile

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

Value cannot be null. Parameter name: key #37

Closed cmarqueztecnosylva closed 1 month ago

cmarqueztecnosylva commented 4 years ago

Hi all I'm currently try to export wkt poligons to Shapefile, but when i go to write features, like that: `// Use the dataWriter to write the shape file with geometry and data ShapefileDataWriter dataWriter = new ShapefileDataWriter(shapeFileName, outGeomFactory, encoding);

// Header DbaseFileHeader outDbaseHeader = ShapefileDataWriter.GetHeader(features[0], features.Count, encoding); dataWriter.Header = outDbaseHeader;

// Write feature dataWriter.Write(features); //Exception is thrown here`

I have an exception, always when i do dataWriter.Write(features);

Exception is: {System.TypeInitializationException: The type initializer for 'NetTopologySuite.IO.DbaseEncodingUtility' threw an exception. ---> System.ArgumentNullException: Value cannot be null. Parameter name: key at System.Collections.Generic.Dictionary2[TKey,TValue].FindEntry (TKey key) [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:380 at System.Collections.Generic.Dictionary2[TKey,TValue].ContainsKey (TKey key) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:286 at NetTopologySuite.IO.DbaseEncodingUtility.AddLdidEncodingPair (System.Byte ldid, System.Text.Encoding encodingToAdd) [0x0000c] in <0e9c02ea98264091a344714de10def9b>:0 at NetTopologySuite.IO.DbaseEncodingUtility.RegisterEncodings (System.Object[][] ldidCodePagePairs) [0x00027] in <0e9c02ea98264091a344714de10def9b>:0 at NetTopologySuite.IO.DbaseEncodingUtility..cctor () [0x00799] in <0e9c02ea98264091a344714de10def9b>:0 --- End of inner exception stack trace --- at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_generic_class_init(intptr) at NetTopologySuite.IO.DbaseFileHeader.GetLdidFromEncoding (System.Text.Encoding encoding) [0x00000] in <0e9c02ea98264091a344714de10def9b>:0 at NetTopologySuite.IO.DbaseFileHeader.WriteHeader (System.IO.BinaryWriter writer) [0x0009b] in <0e9c02ea98264091a344714de10def9b>:0 at NetTopologySuite.IO.DbaseFileWriter.Write (NetTopologySuite.IO.DbaseFileHeader header) [0x00066] in <0e9c02ea98264091a344714de10def9b>:0 at NetTopologySuite.IO.ShapefileDataWriter.Write (System.Collections.Generic.IEnumerable1[T] featureCollection) [0x00086] in <0e9c02ea98264091a344714de10def9b>:0 at GeoAraba.Helpers.Wkt2ShapeHelper.Transform (System.String wkt, System.String name) [0x00180] in D:\Workspace\VisualStudio\d-1815_geoaraba\GeoAraba\GeoAraba.Shared\Helpers\Wkt2ShapeHelper.cs:95 }`

I'm really lost, and desperate. Please, someone help me with this issue?

Thanks in advance.

cmarqueztecnosylva commented 4 years ago

Sorry, i use next libs NetTopologySuite.Features 2.0 NetTopologySuite.IO.Shapefile 2.0

Project is targeting Xamarin.Forms

cmarqueztecnosylva commented 4 years ago

Hi I downloaded your code of NetTopologySuite.IO.ShapeFile, and fixed the referred bug on it.

The bug is in the method GetLdidFromEncoding; i added a try catch in order to get the bug and return a default value; and is fixed:

private static byte GetLdidFromEncoding(Encoding encoding)
{
    byte ldid;
    try
    {
        if (!DbaseEncodingUtility.EncodingToLdid.TryGetValue(encoding, out ldid))
            ldid = 0x03;
    }
    catch
    {
        ldid = 0x03;//UTF-8 by default
    }
    return ldid;
}

If you want i can upload this change, else, the way to fix it is this.

Thanks to NetTopology Suite team for this library that help me a lot.

Bye.

KubaSzostak commented 1 month ago

Support for a different encodings has been added in the successor library. There is also a sample code demonstrating how to custom encoding.