NetTopologySuite / NetTopologySuite.IO.ShapeFile

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

NetTopologySuite.IO.ShapeFile with Xamarin.Mac - ArgumentNullException #34

Open TomQv opened 4 years ago

TomQv commented 4 years ago

Im trying to read shp-files in a Xamarin.Mac project with NetTopologySuite.IO.Shapefile 2.0.0. Project is targeting Xamarin.Mac Full NET 4.8.

Calling _reader = new ShapefileDataReader(file, _geometryFactory, System.Text.Encoding.Default); results in ArgumentNullException, see stacktrace below.

With a plain .NET Core 3.0 project, everything is fine.

Any ideas?

System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument argument) in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/external/corefx/src/System.Memory/src/System/ThrowHelper.cs:27
System.Collections.Generic.Dictionary<System.Text.Encoding,byte>.FindEntry(System.Text.Encoding key) in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:380
System.Collections.Generic.Dictionary<System.Text.Encoding,byte>.ContainsKey(System.Text.Encoding key) in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:286
NetTopologySuite.IO.DbaseEncodingUtility.AddLdidEncodingPair(byte ldid, System.Text.Encoding encodingToAdd) in 
NetTopologySuite.IO.DbaseEncodingUtility.RegisterEncodings(object[][] ldidCodePagePairs) in 
NetTopologySuite.IO.DbaseEncodingUtility..cctor() in 
NetTopologySuite.IO.DbaseFileHeader.GetEncoding(NetTopologySuite.IO.Streams.ByteStreamProvider provider) in 
NetTopologySuite.IO.DbaseFileHeader.DetectEncoding(byte ldid, NetTopologySuite.IO.Streams.ByteStreamProvider cpgFile) in 
NetTopologySuite.IO.DbaseFileHeader.ReadHeader(System.IO.BinaryReader reader, NetTopologySuite.IO.Streams.ByteStreamProvider cpgStreamProvider) in 
NetTopologySuite.IO.DbaseFileReader.GetHeader() in 
NetTopologySuite.IO.ShapefileDataReader..ctor(string filename, NetTopologySuite.Geometries.GeometryFactory geometryFactory, System.Text.ASCIIEncoding encoding) in 
QvShape.Reader..ctor(string file) in /Users/tomflemming/QuoVadis/Qvx1/CodeFromExternal/NetTopologySuite/QvShape/Reader.cs:26
QvShape.Reader.Test() in /Users/tomflemming/QuoVadis/Qvx1/CodeFromExternal/NetTopologySuite/QvShape/Reader.cs:83
QvxAppShared.QvxApp..ctor(Eto.Mac.Platform platform, string[] args) in /Users/tomflemming/QuoVadis/Qvx1/QvxShared/App/QvxApp.cs:49
QvxAppShared.Mac.Program.Main(string[] args) in /Users/tomflemming/QuoVadis/Qvx1/QvxMac/Program.cs:16
cmarqueztecnosylva commented 4 years ago

Hi TomQv, i have a similar problem, but when i write a shapefile. Do you found any solution for your problem? I also use NetTopologySuite.Features 2.0 NetTopologySuite.IO.Shapefile 2.0 on xamarin.

Thanks in advance

TomQv commented 4 years ago

Hi, no, unfortunate not yet. I mean, I downloaded the source, commented out DetectEncoding() because I don't have text in that specific project so its working with me. But of course this is just a workaround if you don't have text in db.

cmarqueztecnosylva commented 4 years ago

Perfect, i haven text also, so, do you download the project from here https://github.com/NetTopologySuite/NetTopologySuite???

because i go to do the same.

A lot of thanks for your pretty fast response TomQv.

TomQv commented 4 years ago

I only downloaded https://github.com/NetTopologySuite/NetTopologySuite.IO.ShapeFile Because I only need Shp. You must add a reference to NetTopologySuite.Features.

These are my changes only in file DbaseEncodingUtility:

internal static readonly Encoding Latin1 = Encoding.UTF8;                                 // TOM  GetEncodingForCodePageName("iso-8859-1");
public static Encoding DefaultEncoding { get; set; } = Encoding.UTF8;                     //  TOM   GetEncodingForCodePageIdentifier(1252);
public static Encoding GetEncodingForCodePageIdentifier(int codePage) => DefaultEncoding; // TOM  CodePagesEncodingProvider.Instance.GetEncoding(codePage);
public static Encoding GetEncodingForCodePageName(string name) => DefaultEncoding;        // TOM   CodePagesEncodingProvider.Instance.GetEncoding(name) ?? Encoding.GetEncoding(name);
cmarqueztecnosylva commented 4 years ago

Thanks a lot Tom,

i done the same, but my bug was another, in the method GetLdidFromEncoding; i added a try catch and is done:

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

Thanks.

TomQv commented 4 years ago

👍

KubaSzostak commented 3 months ago

Hi @TomQv, do you need more support on this issue? If so, provide further details, please.