I have a Shapefile that contains several thousand polygons.
I need to read from this file in C# and output a list of WKT formatted strings.
I looked at DotSpatial and the "CatFood" ESRI Shapefile Reader. I can get either to load the shapefile just fine, but I cannot figure out how to then export as WKT.
In DotSpatial, the only examples I could find use a WktWriter
which takes a Geometry
. I couldn't figure out how to get a Geometry
from a Shape
.
Is there a library that's more appropriate for this?
Update
Thanks to mdm20's answer, I was able to write the following:
using (var fs = FeatureSet.Open(path))
{
var writer = new WktWriter();
var numRows = fs.NumRows();
for (int i = 0; i < numRows; i++)
{
var shape = fs.GetShape(i, true);
var geometry = shape.ToGeometry();
var wkt = writer.Write((Geometry) geometry);
Debug.WriteLine(wkt);
}
}
The reason I missed it originally is because I was following this sample, which uses fs.ShapeIndices
instead of fs.GetShape()
. That returns not a Shape
, but a ShapeRange
, which I couldn't convert to a geometry.
New Questions
- Should I be setting
fs.IndexMode = true
? Why or why not? It doesn't seem to have any performance or results impact. fs.GetShape()
takes a boolean calledgetAttributes
. I do have attributes on my shapes, and they seem to come through whether this is set true or false. Again, there is no noticeable performance impact either way. Is that expected?- By getting them in this way, does the WKT represent the actual values stored in the shapefile? Or are they transformed in any way? Is it taking any default settings from dotSpatial into account, and should I be concerned about changing them?
- The shapefile I am importing is the world timezone map. It does contain a .prj file. Does dotSpatial take this into account, and if not - do I need to do anything extra?
Many Thanks!