public void Test() { var features = new List<Features.IFeature>(); var seq = DotSpatialAffineCoordinateSequenceFactory.Instance.Create(1, Ordinates.XY); seq.SetOrdinate(0, Ordinate.X, -91.0454); seq.SetOrdinate(0, Ordinate.Y, 32.5907); var pt = new GeometryFactory(DotSpatialAffineCoordinateSequenceFactory.Instance).CreatePoint(seq); var attr = new Features.AttributesTable(); attr.AddAttribute("FirstName", "John"); attr.AddAttribute("LastName", "Doe"); features.Add(new Features.Feature(pt, attr)); var fileName = Path.GetTempFileName(); fileName = fileName.Substring(0, fileName.Length - 4); var shpWriter = new ShapefileDataWriter(fileName, features[0].Geometry.Factory) { Header = ShapefileDataWriter.GetHeader(features[0], features.Count) }; shpWriter.Write(features); bool isTrue; using (var reader = new ShapefileDataReader(fileName, pt.Factory)) @isTrue = reader.ShapeHeader.ShapeType.ToString() == "Point"; foreach (var file in Directory.GetFiles(Path.GetTempPath(), Path.GetFileName(fileName) + ".*" )) { File.Delete(file); } Assert.IsTrue(@isTrue); }
private void TestWriteZMValuesShapeFile(bool testM) { var points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); var csFactory = NetTopologySuite.Geometries.Implementation.DotSpatialAffineCoordinateSequenceFactory.Instance; var sequence = csFactory.Create(3, Ordinates.XYZM); for (var i = 0; i < 3; i ++) { sequence.SetOrdinate(i, Ordinate.X, points[i].X); sequence.SetOrdinate(i, Ordinate.Y, points[i].Y); sequence.SetOrdinate(i, Ordinate.Z, 1 + i); if (testM) sequence.SetOrdinate(i, Ordinate.M, 11 + i); } var lineString = Factory.CreateLineString(sequence); var attributes = new AttributesTable(); attributes.AddAttribute("FOO", "Trond"); var feature = new Feature(Factory.CreateMultiLineString(new[] { lineString }), attributes); var features = new Feature[1]; features[0] = feature; var shpWriter = new ShapefileDataWriter("ZMtest", Factory) { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shpWriter.Write(features); // Now let's read the file and verify that we got Z and M back var factory = new GeometryFactory(NetTopologySuite.Geometries.Implementation.DotSpatialAffineCoordinateSequenceFactory.Instance); using (var reader = new ShapefileDataReader("ZMtest", factory)) { reader.Read(); var geom = reader.Geometry; for (var i = 0; i < 3; i++) { var c = geom.Coordinates[i]; Assert.AreEqual(i + 1, c.Z); } if (testM) { sequence = ((ILineString) geom).CoordinateSequence; for (var i = 0; i < 3; i++) { Assert.AreEqual(sequence.GetOrdinate(i, Ordinate.M), 11 + i); } } // Run a simple attribute test too var v = reader.GetString(0); Assert.AreEqual(v, "Trond"); } }
public void ok_when_writing_shapefile_with_no_features() { DbaseFileHeader header = new DbaseFileHeader(); header.AddColumn("X", 'C', 10, 0); ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36") { Header = header }; IList<IFeature> features = new List<IFeature>(); writer.Write(features); }
public void ok_when_writing_shapefile_with_no_features() { DbaseFileHeader header = new DbaseFileHeader(); header.AddColumn("X", 'C', 10, 0); ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36") { Header = header }; IList<IFeature> features = new List<IFeature>(); Assert.DoesNotThrow(() => writer.Write(features)); _numPassed++; }
public void shapefile_with_empty_attributes_table_should_not_thrown_errors() { IFeature feature = new Feature(new Point(0, 0), new AttributesTable()); IList<IFeature> features = new List<IFeature> { feature }; string path = CreateShapefilePath(); DbaseFileHeader header = ShapefileDataWriter.GetHeader(feature, features.Count); ShapefileDataWriter writer = new ShapefileDataWriter(path) { Header = header }; writer.Write(features); Assert.That(File.Exists(Path.ChangeExtension(path, ".shp")), Is.True); }
public void ok_when_writing_shapefile_with_features() { DbaseFileHeader header = new DbaseFileHeader(); header.AddColumn("X", 'C', 10, 0); ShapefileDataWriter writer = new ShapefileDataWriter(@"issue36") { Header = header }; IAttributesTable attributesTable = new AttributesTable(); attributesTable.AddAttribute("X", "y"); IFeature feature = new Feature(new Point(1, 2), attributesTable); IList<IFeature> features = new List<IFeature>(); features.Add(feature); writer.Write(features); }
public void BuildStradeFixed() { var path = "strade" + shp; Assert.IsTrue(File.Exists(path)); var reader = new ShapefileDataReader(path, factory); var features = new List<IFeature>(reader.RecordCount); while (reader.Read()) { var feature = new Feature(reader.Geometry, new AttributesTable()); var values = new object[reader.FieldCount - 1]; reader.GetValues(values); for (var i = 0; i < values.Length; i++) { var name = reader.GetName(i + 1); var value = values[i]; feature.Attributes.AddAttribute(name, value); } features.Add(feature); } Assert.AreEqual(703, features.Count); var shapepath = "strade_fixed"; if (File.Exists(shapepath + shp)) File.Delete(shapepath + shp); Assert.IsFalse(File.Exists(shapepath + shp)); if (File.Exists(shapepath + shx)) File.Delete(shapepath + shx); Assert.IsFalse(File.Exists(shapepath + shx)); if (File.Exists(shapepath + dbf)) File.Delete(shapepath + dbf); Assert.IsFalse(File.Exists(shapepath + dbf)); var header = reader.DbaseHeader; var writer = new ShapefileDataWriter(shapepath, factory) {Header = header}; writer.Write(features); Assert.IsTrue(File.Exists(shapepath + shp)); Assert.IsTrue(File.Exists(shapepath + shx)); Assert.IsTrue(File.Exists(shapepath + dbf)); }
public void Setup() { ShapefileDataWriter sfdr = new ShapefileDataWriter("encoding_sample"); DbaseFileHeader h = new DbaseFileHeader(); h.AddColumn("id", 'n', 8, 0); h.AddColumn("Test", 'C', 15, 0); h.AddColumn("Ålder", 'N', 8, 0); h.AddColumn("Ödestext", 'C', 255, 0); h.NumRecords = 1; sfdr.Header = h; List<IFeature> feats = new List<IFeature>(); AttributesTable at = new AttributesTable(); at.AddAttribute("id", "0"); at.AddAttribute("Test", "Testar"); at.AddAttribute("Ålder", 10); at.AddAttribute("Ödestext", "Lång text med åäö etc"); feats.Add(new Feature(new Point(0, 0), at)); sfdr.Write(feats); }
public void TestCreateEmptyShapefile() { const string filename = "__empty"; const string emptyShp = filename + ".shp"; const string emptyShx = filename + ".shx"; const string emptyDbf = filename + ".dbf"; if (File.Exists(emptyShp)) File.Delete(emptyShp); if (File.Exists(emptyShx)) File.Delete(emptyShx); if (File.Exists(emptyDbf)) File.Delete(emptyDbf); ShapefileDataWriter writer = new ShapefileDataWriter(filename, Factory); writer.Header = new DbaseFileHeader(); writer.Write(new IFeature[0]); Assert.That(File.Exists(emptyShp), Is.True); Assert.That(File.Exists(emptyShx), Is.True); Assert.That(File.Exists(emptyDbf), Is.True); }
private void TestShapeCreation() { var points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); var line_string = new LineString(points); var attributes = new AttributesTable(); attributes.AddAttribute("FOO", "FOO"); var feature = new Feature(Factory.CreateMultiLineString(new ILineString[] { line_string }), attributes); var features = new Feature[1]; features[0] = feature; var shp_writer = new ShapefileDataWriter("line_string") { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shp_writer.Write(features); }
private void ReadFromShapeFile() { var featureCollection = new ArrayList(); const string filename = @"country"; if (!File.Exists(filename + ".dbf")) throw new FileNotFoundException(filename + " not found at " + Environment.CurrentDirectory); var dataReader = new ShapefileDataReader(filename, new GeometryFactory()); while (dataReader.Read()) { var feature = new Feature {Geometry = dataReader.Geometry}; var length = dataReader.DbaseHeader.NumFields; var keys = new string[length]; for (var i = 0; i < length; i++) keys[i] = dataReader.DbaseHeader.Fields[i].Name; feature.Attributes = new AttributesTable(); for (var i = 0; i < length; i++) { var val = dataReader.GetValue(i); feature.Attributes.AddAttribute(keys[i], val); } featureCollection.Add(feature); } var index = 0; Console.WriteLine("Elements = " + featureCollection.Count); foreach (IFeature feature in featureCollection) { Console.WriteLine("Feature " + index++); var table = feature.Attributes as AttributesTable; foreach (var name in table.GetNames()) Console.WriteLine(name + ": " + table[name]); } //Directory var dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format(@"..{0}..{0}..{0}NetTopologySuite.Samples.Shapefiles{0}", Path.DirectorySeparatorChar)); // Test write with stub header var file = dir + "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"); var dataWriter = new ShapefileDataWriter(file); dataWriter.Header = ShapefileDataWriter.GetHeader(featureCollection[0] as IFeature, featureCollection.Count); dataWriter.Write(featureCollection); // Test write with header from a existing shapefile file = dir + "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) { Header = ShapefileDataWriter.GetHeader(dir + "country.dbf") }; dataWriter.Write(featureCollection); }
// see https://code.google.com/p/nettopologysuite/issues/detail?id=146 public void Issue146_ShapeCreationWithInvalidAttributeName() { Coordinate[] points = new Coordinate[3]; points[0] = new Coordinate(0, 0); points[1] = new Coordinate(1, 0); points[2] = new Coordinate(1, 1); LineString ls = new LineString(points); IMultiLineString mls = GeometryFactory.Default.CreateMultiLineString(new ILineString[] { ls }); AttributesTable attrs = new AttributesTable(); attrs.AddAttribute("Simulation name", "FOO"); Feature[] features = new[] { new Feature(mls, attrs) }; ShapefileDataWriter shp_writer = new ShapefileDataWriter("invalid_line_string") { Header = ShapefileDataWriter.GetHeader(features[0], features.Length) }; shp_writer.Write(features); }
private void WriteWayPoints(string file, List<WayPoint> points, TtMetaData md) { #if !(PocketPC || WindowsCE || Mobile) string FileName = file + "_WayPoints"; GeometryFactory geoFac = new GeometryFactory(); ShapefileDataWriter sdw = new ShapefileDataWriter(FileName, geoFac); ArrayList features = new ArrayList(); AttributesTable attTable; Feature feat = new Feature(); DbaseFileHeader dbh; foreach (WayPoint point in points) { feat = new Feature(); attTable = new AttributesTable(); attTable.AddAttribute("PID", point.PID); attTable.AddAttribute("Comment", point.Comment ?? String.Empty); attTable.AddAttribute("CN", point.CN); attTable.AddAttribute("Group", point.GroupName ?? String.Empty); attTable.AddAttribute("RMSEr", point.RMSEr ?? -1); attTable.AddAttribute("UnAdjX", point.UnAdjX); attTable.AddAttribute("UnAdjY", point.UnAdjY); attTable.AddAttribute("UnAdjZ", point.UnAdjZ); feat.Geometry = new NetTopologySuite.Geometries.Point(point.UnAdjX, point.UnAdjY, point.UnAdjZ); feat.Attributes = attTable; features.Add(feat); } dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); #endif }
public void WriteShapeFiles(DataAccessLayer DAL) { #if !(PocketPC || WindowsCE || Mobile) List<TtPolygon> polys = DAL.GetPolygons(); Dictionary<string, TtMetaData> metas = DAL.GetMetaData().ToDictionary(m => m.CN, m => m); string folder = SelectedPath + "\\GIS"; Directory.CreateDirectory(folder); foreach (TtPolygon poly in polys) { string _CurrDir = String.Format("{0}\\{1}\\", folder, poly.Name.ScrubFileName()); string _File = String.Format("{0}\\{1}\\{1}", folder, poly.Name.ScrubFileName()); Directory.CreateDirectory(_CurrDir); string CurrFileName = System.IO.Path.Combine(folder, poly.Name); List<TtPoint> points = DAL.GetPointsInPolygon(poly.CN).ToList(); List<WayPoint> wayPoints = points.FilterOnly(OpType.WayPoint).Cast<WayPoint>().ToList(); points = points.FilterOut(OpType.WayPoint).ToList(); TtMetaData md; if (points.Count > 0 || wayPoints.Count > 0) { if (points.Count > 0) md = DAL.GetMetaDataByCN(points[0].MetaDefCN); else md = DAL.GetMetaDataByCN(wayPoints[0].MetaDefCN); if (md == null) continue; } else continue; if (wayPoints.Count > 0) WriteWayPoints(_File, wayPoints, md); if (points.Count < 1) continue; CoordinateList BAdjCoords = new CoordinateList(); CoordinateList BUnAdjCoords = new CoordinateList(); CoordinateList NavAdjCoords = new CoordinateList(); CoordinateList NavUnAdjCoords = new CoordinateList(); bool hasWayPoints = false; TtPoint firstBndPoint = null, lastBntPoint = null; foreach (TtPoint point in points) { if (point.IsNavPoint()) { NavAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ)); NavUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ)); } if (point.IsBndPoint()) { BAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ)); BUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ)); if (firstBndPoint == null) firstBndPoint = point; lastBntPoint = point; } if (point.op == OpType.WayPoint) { hasWayPoints = true; } } double polyLinePerim = -1; if (firstBndPoint != null) polyLinePerim = poly.Perimeter - TtUtils.Distance(firstBndPoint, lastBntPoint, true); #region Navigation #region Adj string FileName = _File + "_NavAdj"; GeometryFactory geoFac = new GeometryFactory(); ShapefileDataWriter sdw; Polygonizer polyizer = new Polygonizer(); ArrayList features = new ArrayList(); AttributesTable attTable = new AttributesTable(); attTable.AddAttribute("Poly_Name", poly.Name); attTable.AddAttribute("Desc", poly.Description); attTable.AddAttribute("Poly", "Navigation Adjusted"); attTable.AddAttribute("CN", poly.CN); attTable.AddAttribute("Perim_M", poly.Perimeter); attTable.AddAttribute("PerimLine_M", polyLinePerim); Feature feat = new Feature(); DbaseFileHeader dbh; if (NavAdjCoords.Count > 1) { sdw = new ShapefileDataWriter(FileName, geoFac); feat.Geometry = new NetTopologySuite.Geometries.LineString(NavAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_NavAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation Adjusted Points"; features = GetPointFeatures(points.Where(p => p.IsNavPoint()), true, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region UnAdj if (NavUnAdjCoords.Count > 1) { FileName = _File + "_NavUnAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation UnAdjusted"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(NavUnAdjCoords.ToArray()); //feat.Geometry = new NetTopologySuite.Geometries.LinearRing(NavUnAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_NavUnAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Navigation UnAdjusted Points"; features = GetPointFeatures(points.Where(p => p.IsNavPoint()), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #endregion #region Boundary #region Adj Line if (BAdjCoords.Count > 1) { FileName = _File + "_BndAdjLine"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary Adjusted Line"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(BAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } #endregion #region UnAdj Line if (BUnAdjCoords.Count > 1) { FileName = _File + "_BndUnAdjLine"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted Line"; feat = new Feature(); feat.Geometry = new NetTopologySuite.Geometries.LineString(BUnAdjCoords.ToArray()); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } #endregion #region Adj if (BAdjCoords.Count > 3) { FileName = _File + "_BndAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable.AddAttribute("Area_MtSq", poly.Area); attTable["Poly"] = "Boundary Adjusted"; feat = new Feature(); if (BAdjCoords[0] != BAdjCoords[BAdjCoords.Count - 1]) BAdjCoords.Add(BAdjCoords[0]); feat.Geometry = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(BAdjCoords.ToArray())); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_BndAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary Adjusted Points"; features = GetPointFeatures(points.Where(p => p.IsBndPoint()), true, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region UnAdj if (BUnAdjCoords.Count > 3) { FileName = _File + "_BndUnAdj"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted"; feat = new Feature(); if (BUnAdjCoords[0] != BUnAdjCoords[BUnAdjCoords.Count - 1]) BUnAdjCoords.Add(BUnAdjCoords[0]); feat.Geometry = new NetTopologySuite.Geometries.Polygon(new LinearRing(BUnAdjCoords.ToArray())); feat.Attributes = attTable; features.Add(feat); dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); //points FileName = _File + "_BndUnAdj_Points"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "Boundary UnAdjusted Points"; features = GetPointFeatures(points.Where(p => p.IsBndPoint()), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #region WayPoints if (hasWayPoints) { //points FileName = _File + "_WayPoints"; geoFac = new GeometryFactory(); sdw = new ShapefileDataWriter(FileName, geoFac); features = new ArrayList(); attTable["Poly"] = "WayPoints"; features = GetPointFeatures(points.Where(p => p.op == OpType.WayPoint), false, DAL, metas); if (features.Count > 0) { dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count); sdw.Header = dbh; sdw.Write(features); WriteProjection(FileName, md.Zone); } } #endregion #endregion } #endif }
private void SaveGraphResult(IGeometry path) { if (path == null) throw new ArgumentNullException("path"); const string shapepath = "graphresult"; if (File.Exists(shapepath + shp)) File.Delete(shapepath + shp); Assert.IsFalse(File.Exists(shapepath + shp)); if (File.Exists(shapepath + shx)) File.Delete(shapepath + shx); Assert.IsFalse(File.Exists(shapepath + shx)); if (File.Exists(shapepath + dbf)) File.Delete(shapepath + dbf); Assert.IsFalse(File.Exists(shapepath + dbf)); const string field1 = "OBJECTID"; var feature = new Feature(path, new AttributesTable()); feature.Attributes.AddAttribute(field1, 0); var header = new DbaseFileHeader {NumRecords = 1, NumFields = 1}; header.AddColumn(field1, 'N', 5, 0); var writer = new ShapefileDataWriter(shapepath, factory) {Header = header}; writer.Write(new List<IFeature>(new[] {feature,})); Assert.IsTrue(File.Exists(shapepath + shp)); Assert.IsTrue(File.Exists(shapepath + shx)); Assert.IsTrue(File.Exists(shapepath + dbf)); }