private static void AssertEqual(Dictionary <string, object> expected, AttributesTable actual) { // test everything after a serialize + deserialize round-trip ExpectedAndActual.RoundTrip(ref expected, ref actual); Assert.That(actual.Count, Is.EqualTo(expected.Count)); Assert.That(actual, Is.EquivalentTo(expected)); Assert.That(actual.GetNames().Count, Is.EqualTo(expected.Keys.Count)); Assert.That(actual.GetNames(), Is.EquivalentTo(expected.Keys)); Assert.That(actual.GetValues().Count, Is.EqualTo(expected.Values.Count)); Assert.That(actual.GetValues(), Is.EquivalentTo(expected.Values)); foreach ((string key, object value) in expected) { Assert.That(actual.Exists(key)); Assert.That(actual[key], Is.EqualTo(value)); object retrieved = actual.GetOptionalValue(key); Assert.That(retrieved, Is.EqualTo(value)); Assert.That(actual.GetType(key), Is.EqualTo(value?.GetType() ?? typeof(object))); } }
public static void DumpAttributes(string title, AttributesTable attributes) { string str = "[" + title + "] "; var names = attributes.GetNames(); foreach (var name in names) { str += name + "=" + attributes[name] + " ; "; } Console.WriteLine(str); }
public void ReadJsonWithInnerObjectTest() { var atS = new AttributesTable(); var inner = new AttributesTable(); atS.Add("test1", "value1"); atS.Add("test2", inner); inner.Add("innerTest1", "innerValue1"); inner.Add("innerTest2", "innerValue2"); var options = DefaultOptions; IAttributesTable atD = null; using (var ms = new MemoryStream()) { Serialize(ms, atS, options); Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray())); atD = Deserialize(ms, options); } Assert.That(atD, Is.Not.Null); // Check properties on attribute table Assert.That(atD, Is.InstanceOf <IAttributesTable>()); Assert.That(atD.Count, Is.EqualTo(atS.Count)); Assert.That(atD.GetNames()[0], Is.EqualTo(atS.GetNames()[0])); Assert.That(atD.GetNames()[1], Is.EqualTo(atS.GetNames()[1])); Assert.That(atD[atD.GetNames()[0]], Is.EqualTo(atS[atS.GetNames()[0]])); Assert.That(atD[atD.GetNames()[1]], Is.InstanceOf <IAttributesTable>()); var atdInner = (IAttributesTable)atD[atD.GetNames()[1]]; object atdInnerTest1 = atdInner["innerTest1"]; Assert.That(atdInnerTest1, Is.InstanceOf <string>()); Assert.That((string)atdInnerTest1, Is.EqualTo((string)inner["innerTest1"])); object atdInnerTest2 = atdInner["innerTest2"]; Assert.That(atdInnerTest2, Is.InstanceOf <string>()); Assert.That((string)atdInnerTest2, Is.EqualTo((string)inner["innerTest2"])); }
internal static IEnumerable <Feature> Load(string v) { var shapeFileDataReader = Shapefile.CreateDataReader(v, new GeometryFactory()); ShapefileHeader shpHeader = shapeFileDataReader.ShapeHeader; DbaseFileHeader header = shapeFileDataReader.DbaseHeader; shapeFileDataReader.Reset(); //Read through all records of the shapefile (geometry and attributes) into a feature collection List <Feature> features = new List <Feature>(); int j = 1; while (shapeFileDataReader.Read()) { Feature feature = new Feature(); AttributesTable attributesTable = new AttributesTable(); string[] keys = new string[header.NumFields]; var pm = new PrecisionModel(10.0); var pop = new NetTopologySuite.Precision.GeometryPrecisionReducer(pm); Geometry geometry = NetTopologySuite.Simplify.DouglasPeuckerSimplifier.Simplify(pop.Reduce((Geometry)shapeFileDataReader.Geometry), 0.5); // geometry = NetTopologySuite.Operation.BoundaryOp.GetBoundary(geometry); // var pol = new NetTopologySuite.Operation.Polygonize.Polygonizer(); // pol.Add() if (geometry.IsEmpty) { continue; } for (int i = 0; i < header.NumFields; i++) { DbaseFieldDescriptor fldDescriptor = header.Fields[i]; keys[i] = fldDescriptor.Name; attributesTable.Add(fldDescriptor.Name, shapeFileDataReader.GetValue(i + 1)); } if (!attributesTable.GetNames().Contains("NAME", StringComparer.InvariantCulture)) { attributesTable.Add("NAME", j); } feature.Geometry = geometry; feature.Attributes = attributesTable; features.Add(feature); j++; } //Close and free up any resources shapeFileDataReader.Close(); shapeFileDataReader.Dispose(); return(features); }
public void WriteJsonTest() { var sb = new StringBuilder(); var atS = new AttributesTable(); IAttributesTable atD = null; atS.Add("test1", "value1"); atS.Add("test2", "value2"); var options = DefaultOptions; using (var ms = new MemoryStream()) { Serialize(ms, atS, options); Assert.AreEqual("{\"test1\":\"value1\",\"test2\":\"value2\"}", Encoding.UTF8.GetString(ms.ToArray())); atD = Deserialize(ms, options); } Assert.That(atD, Is.Not.Null); Assert.That(atD.Count, Is.EqualTo(atS.Count)); Assert.That(atD.GetNames()[0], Is.EqualTo(atS.GetNames()[0])); Assert.That(atD.GetNames()[1], Is.EqualTo(atS.GetNames()[1])); Assert.That(atD[atD.GetNames()[0]], Is.EqualTo(atS[atS.GetNames()[0]])); Assert.That(atD[atD.GetNames()[1]], Is.EqualTo(atS[atS.GetNames()[1]])); }
private void ReadFromShapeFile() { ArrayList featureCollection = new ArrayList(); string filename = @"country"; if (!File.Exists(filename + ".dbf")) { throw new FileNotFoundException(filename + " not found at " + Environment.CurrentDirectory); } ShapefileDataReader dataReader = new ShapefileDataReader(filename, new GeometryFactory()); while (dataReader.Read()) { Feature feature = new Feature(); feature.Geometry = dataReader.Geometry; int length = dataReader.DbaseHeader.NumFields; string[] keys = new string[length]; for (int i = 0; i < length; i++) { keys[i] = dataReader.DbaseHeader.Fields[i].Name; } feature.Attributes = new AttributesTable(); for (int i = 0; i < length; i++) { object val = dataReader.GetValue(i); feature.Attributes.AddAttribute(keys[i], val); } featureCollection.Add(feature); } int index = 0; Console.WriteLine("Elements = " + featureCollection.Count); foreach (Feature feature in featureCollection) { Console.WriteLine("Feature " + index++); AttributesTable table = feature.Attributes as AttributesTable; foreach (string name in table.GetNames()) { Console.WriteLine(name + ": " + table[name]); } } // Test write with stub header string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/testWriteStubHeader"); if (File.Exists(file + ".shp")) { File.Delete(file + ".shp"); } if (File.Exists(file + ".shx")) { File.Delete(file + ".shx"); } if (File.Exists(file + ".dbf")) { File.Delete(file + ".dbf"); } ShapefileDataWriter dataWriter = new ShapefileDataWriter(file); dataWriter.Header = ShapefileDataWriter.GetHeader(featureCollection[0] as Feature, featureCollection.Count); dataWriter.Write(featureCollection); // Test write with header from a existing shapefile file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/testWriteShapefileHeader"); if (File.Exists(file + ".shp")) { File.Delete(file + ".shp"); } if (File.Exists(file + ".shx")) { File.Delete(file + ".shx"); } if (File.Exists(file + ".dbf")) { File.Delete(file + ".dbf"); } dataWriter = new ShapefileDataWriter(file); dataWriter.Header = ShapefileDataWriter.GetHeader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../NetTopologySuite.Samples.Shapefiles/country.dbf")); dataWriter.Write(featureCollection); }