Closed benabdelkrim closed 4 years ago
You need to use the class ShapeFileDataWriter
var writer = new ShapefileDataWriter(filename, GeometryFactory.Default);
var header = new DbaseFileHeader();
// TODO configure attributes table
writer.Header = header
IFeature[] data = ...
writer.Write(data);
can you give an exemple how to use this ?? IFeature[] data = ...
basically you need to have an array of Feature objects, any feature it's composed of a NTS Geometry (a Point, a LineString or a Polygon) and a collection of attributes. Very easy to do when you try, follow the compiler :-) Bear in mind two things:
i tried this code but it's not working for me ! any help ?
//create a shapeFile
ShapefileDataWriter shapefileDataWriter = new ShapefileDataWriter(@"C:\Users\hp\Desktop\shape\test10", GeometryFactory.Default);
DbaseFileHeader dbaseFileHeader = new DbaseFileHeader();
dbaseFileHeader.AddColumn("type", 'C',100, 100);
shapefileDataWriter.Header = dbaseFileHeader;
//add a point into shapefile
IEnumerable
int counter = result.ToList().Count();
GeometryFactory geometryFactory = new GeometryFactory();
Coordinate coordinate = new Coordinate(2.2, 4.6);
NetTopologySuite.Geometries.Point point = geometryFactory.CreatePoint(coordinate);
Feature feature1 = new Feature();
feature1.Geometry = point;
feature1.BoundingBox = new Envelope();
feature1.Attributes.Add("id", null);
//feature1.Attributes.Add("x_coord", feature1);
result1.ToList().Add(feature1);
shapefileDataWriter.Write(result1);
result1.ToList().Add(feature1);
You're adding feature1
to a different list than result1
.
why not simply
List result1 = new List<IFeature>()
...
result1.Add(feature1);
?
and note that because you're adding in feature1 a field using key "id", you should use "id" (instead of "type") also in the header definition
actually it's more easier that I said earlier, take a look at this sample code from the tests.
You can obtain a valid header using this static method AFTER that you created the features you want to write
ShapefileDataWriter.GetHeader(features[0], features.Length)
so basically
//add a point into shapefile
var result1 = new List<Feature>();
GeometryFactory geometryFactory = GeometryFactory.Default
Coordinate coordinate = new Coordinate(2.2, 4.6);
NetTopologySuite.Geometries.Point point = geometryFactory.CreatePoint(coordinate);
Feature feature1 = new Feature();
feature1.Geometry = point;
// feature1.BoundingBox = new Envelope(); // NOT NEEDED
feature1.Attributes.Add("id", null);
result1.Add(feature1);
ShapefileDataWriter shapefileDataWriter = new ShapefileDataWriter(@"C:\Users\hp\Desktop\shape\test10", GeometryFactory.Default);
shapefileDataWriter.Header = ShapefileDataWriter.GetHeader(result1[0], result1.Length);
shapefileDataWriter.Write(result1);
hope this helps
thank you @DGuidi for your help now it is working for me
how can i create a new shapefile ?