Prevent geometries being created with z

284 views
Skip to first unread message

Diego Guidi

unread,
Sep 2, 2014, 10:55:20 AM9/2/14
to nettopol...@googlegroups.com
Justin said (https://github.com/NetTopologySuite/NetTopologySuite/issues/4):

Hi

When I create and save a geometry like this:

List coordinates = new List();
coordinates
.Add(new Coordinate(0, 0));
coordinates
.Add(new Coordinate(1, 0));
coordinates
.Add(new Coordinate(1, 1));
coordinates
.Add(new Coordinate(0, 1));
coordinates
.Add(new Coordinate(0, 0));



AttributesTable attributesTable = new AttributesTable();
Polygon polygon = new Polygon(new LinearRing(coordinates.ToArray()));
IList features = new List();
features
.Add(new Feature(polygon, attributesTable));

string shapefilePath = @"C:\test";
ShapefileDataWriter writer = new ShapefileDataWriter(shapefilePath) { Header = ShapefileDataWriter.GetHeader(features[0], features.Count) };
writer
.Write((System.Collections.IList)features);


The resulting shape file has polygons with Z values, although they are null. In ArcMAP the polygons show as "Polygon ZM". This is not very good for my further processing of the data. How can get it to save it as just normal xy based polygons?


try to copy your coordinates to a coordinatesequence, like

int max = coordinates.Count;
CoordinateArraySequenceFactory factory = CoordinateArraySequenceFactory.Instance;
ICoordinateSequence sequence = factory.Create(max, 2);            
for (int i = 0; i < max; i++)
{
   sequence
.SetOrdinate(i, Ordinate.X, coords[i].X);
   sequence
.SetOrdinate(i, Ordinate.Y, coords[i].Y);                
}

IPolygon polygon = GeometryFactory.Default.CreatePolygon(sequence);

 this should work

Diego Guidi

unread,
Sep 3, 2014, 7:31:47 AM9/3/14
to nettopol...@googlegroups.com
checked with arcgis, shapefile looks ok to me

Jonas Koefoed Rømer

unread,
Oct 19, 2016, 4:25:36 PM10/19/16
to NetTopologySuite
Hi, just following up on this old post.

I had the same problem and was unable to get NTS to save shapefiles without ZM values. I'm sharing my workaround here, simply patch the shapefile after saving. In this case I change the shape type from PolyLineZ (0x0d) to PolyLine (0x03):

https://www.esri.com/library/whitepapers/pdfs/shapefile.pdf

No guarantees that the pattern replace works for all shapefiles, precondition is that no other data than shape type specification matches the pattern.

            // write shapefile
            string outFile = @"c:\workingdata\test";
            ShapefileDataWriter shapeWriter = new ShapefileDataWriter(outFile);
            DbaseFileHeader outDbaseHeader = ShapefileDataWriter.GetHeader(radialLines[0], radialLines.Count);
            shapeWriter.Header = outDbaseHeader;
            shapeWriter.Write(radialLines.Features);
            shapeWriter = null;

            // remove ZM values / change to polyline, can't figure out why NTS keeps adding these even if geometries only has XY...
            using (Stream stream = new FileStream(outFile + ".shp", FileMode.OpenOrCreate))
            {
                byte[] buffer = new byte[4];
                stream.Seek(0, 0);
                while (stream.Read(buffer, 0, 4) > 0)
                {
                    if (buffer[0] == 0x0d && buffer[1] == 0x00 && buffer[2] == 0x00 && buffer[3] == 0x00)
                    {
                        stream.Seek(-4, System.IO.SeekOrigin.Current);
                        stream.Write(new byte[] { 0x03, 0, 0, 0 }, 0, 4);
                    }
                }
            }


Jonas
Message has been deleted

dtu...@cazadoresrp.com

unread,
May 3, 2019, 11:54:24 AM5/3/19
to NetTopologySuite
Can NTS write .shp files without ZM values?

I am currently running into a similar issue with the application I'm working on. We are using NTS to write / export shapefiles from our data, but the .shp files always get ZM values added in. This causes issues when users try to import the shapes into other applications they use.

In our case, it is a Polygon geometry being written into a PolygonZM .shp. I wouldn't mind trying a fix like Jonas posted here, but I'm not completely clear on how that works - not enough to modify it to work for our case - in our case it is Polygon instead of Polyline.

Any help anyone could offer would be greatly appreciated. Thank you.
Reply all
Reply to author
Forward
0 new messages