Closed cjahermanns closed 3 years ago
can you share a sample data that reproduce the error and the code you use?
Hi, I prepared some sample code:
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
// prepare two features and the collection
NetTopologySuite.Geometries.LinearRing shell1 = new NetTopologySuite.Geometries.LinearRing(
new NetTopologySuite.Geometries.Coordinate[]
{
new NetTopologySuite.Geometries.Coordinate(),
new NetTopologySuite.Geometries.Coordinate(10, 0),
new NetTopologySuite.Geometries.Coordinate(10, 10),
new NetTopologySuite.Geometries.Coordinate(0, 10),
new NetTopologySuite.Geometries.Coordinate()
});
NetTopologySuite.Geometries.Polygon geom1 = new NetTopologySuite.Geometries.Polygon(shell1);
NetTopologySuite.Features.AttributesTable attrTable1 = new NetTopologySuite.Features.AttributesTable()
{
{ "Test1", 5.0 },
{ "Test2", "string2" }
};
NetTopologySuite.Features.Feature feature1 = new NetTopologySuite.Features.Feature(geom1, attrTable1);
NetTopologySuite.Geometries.LinearRing shell2 = new NetTopologySuite.Geometries.LinearRing(
new NetTopologySuite.Geometries.Coordinate[]
{
new NetTopologySuite.Geometries.Coordinate(5,5),
new NetTopologySuite.Geometries.Coordinate(25, 5),
new NetTopologySuite.Geometries.Coordinate(25, 25),
new NetTopologySuite.Geometries.Coordinate(5, 25),
new NetTopologySuite.Geometries.Coordinate(5,5)
});
NetTopologySuite.Geometries.Polygon geom2 = new NetTopologySuite.Geometries.Polygon(shell2);
NetTopologySuite.Features.AttributesTable attrTable2 = new NetTopologySuite.Features.AttributesTable()
{
{ "Test1", 5.0 },
{ "Test2", "string2" },
{ "Test3", 3 }
};
NetTopologySuite.Features.Feature feature2 = new NetTopologySuite.Features.Feature(geom2, attrTable2);
NetTopologySuite.Features.FeatureCollection features = new NetTopologySuite.Features.FeatureCollection();
features.Add(feature1);
features.Add(feature2);
// write the shapefile after creating the feature collection
WriteShapes(features); // works as expected
// serialize and deserialize the collection
await SerializeResultsAsync(features);
NetTopologySuite.Features.FeatureCollection features2 = await DeSerializeResultsAsync();
// try writing the shapefile from the deserialized feature collection
WriteShapes(features2); // throws "Type Decimal not supported"
void WriteShapes(NetTopologySuite.Features.FeatureCollection features)
{
string path = @"D:\tmp\example";
NetTopologySuite.IO.ShapefileDataWriter writer = new NetTopologySuite.IO.ShapefileDataWriter(path);
writer.Header = NetTopologySuite.IO.ShapefileDataWriter.GetHeader(features.First(), features.Count);
writer.Header.LastUpdateDate = DateTime.Now;
writer.Write(features);
}
async Task SerializeResultsAsync(NetTopologySuite.Features.FeatureCollection features)
{
string path = @"D:\tmp\example";
System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions();
options.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
using FileStream fs = File.Create(path + ".json");
await System.Text.Json.JsonSerializer.SerializeAsync(fs, features, options).ConfigureAwait(false);
}
async Task<NetTopologySuite.Features.FeatureCollection> DeSerializeResultsAsync()
{
string path = @"D:\tmp\example";
System.Text.Json.JsonSerializerOptions options = new System.Text.Json.JsonSerializerOptions();
options.Converters.Add(new NetTopologySuite.IO.Converters.GeoJsonConverterFactory());
using StreamReader sr = new StreamReader(path + ".json");
string jsonString = await sr.ReadToEndAsync();
return System.Text.Json.JsonSerializer.Deserialize<NetTopologySuite.Features.FeatureCollection>(jsonString, options);
}
Actually I'm using a Result
class which holds 2 FeatureCollection
props with about 5 to 10 Feature
s and I want to (de)serialize an ObservableCollection<Result>
.
Edit: I wanted to show a difference between my two FeatureCollection
s hence the two different AttributeTable
. I don't mix the two kind of Feature
s.
Since I had some more problems with the deserialized StjFeature
s I now create new Feature
s and FeatureCollection
s for every deserialized object. The Shapefile
write method works as expected with this workaround. Nevertheless do deserialized Feature
s not work properly in terms of functionality compared to the non deserialized versions but that's probably something to discuss on the https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON project page.
Hi guys, I'm working on a project that has to be able to serialize and deserialize many
FeatureCollection
. I'm using theSystem.Test.Json.JsonSerializer
with theGeoJsonConverterFactory
to do so, but I can't write anyShapeFile
from the deserialized collections. It has something to do with some attibutes getting deserialized asdecimal
(they weredouble
). Or could the deserializedStjFeature
cause the problem? The error occurs while getting the header viaShapeFileDataWriter.GetHeader(features.First(), features.Count)
. The regularFeature
shows different types compared to theStjFeature
(the pictures show differentFeatureCollection
).