public static Shapefile OpenShapefile(string fileLocation, bool checkInvalidShapes = true) { if (!File.Exists(fileLocation)) { throw new Exception($"Input file [{fileLocation}] does not exists"); } DebugMsg("Opening " + fileLocation); var stopWatch = new Stopwatch(); stopWatch.Start(); var sf = new Shapefile(); if (!sf.Open(fileLocation)) { throw new Exception("Can't open " + fileLocation + " Error: " + sf.ErrorMsg[sf.LastErrorCode]); } stopWatch.Stop(); DebugMsg("Time it took to open shapefile: " + stopWatch.Elapsed); if (!checkInvalidShapes) { return(sf); } if (sf.HasInvalidShapes()) { DebugMsg("Input has invalid shapes"); } return(sf); }
public void FixUpShapes() { // MWGIS-90 // Open shapefile: var sfInvalid = new Shapefile { GlobalCallback = this }; Shapefile sfFixed = null; try { var result = sfInvalid.Open(@"sf\invalid.shp"); Assert.IsTrue(result, "Could not open shapefile"); Assert.IsTrue(sfInvalid.HasInvalidShapes(), "Shapefile has no invalid shapes"); Helper.PrintExtents(sfInvalid.Extents); result = sfInvalid.FixUpShapes(out sfFixed); Assert.IsTrue(result, "Could not fix shapefile"); Assert.IsFalse(sfFixed.HasInvalidShapes(), "Returning shapefile has invalid shapes"); Assert.AreEqual(sfInvalid.NumShapes, sfFixed.NumShapes, "Number of shapes are not equal"); Helper.PrintExtents(sfFixed.Extents); } finally { sfInvalid.Close(); sfFixed?.Close(); } }
public void FixUpShapes() { var utils = new Utils { GlobalCallback = this }; // Open shapefile: var sfInvalid = new Shapefile(); var sfFixed = new Shapefile(); try { var result = sfInvalid.Open(@"sf\invalid.shp"); Assert.IsTrue(result, "Could not open shapefile"); result = sfInvalid.HasInvalidShapes(); Assert.IsTrue(result, "Shapefile has no invalid shapes"); Helper.PrintExtents(sfInvalid.Extents); for (var i = 0; i < sfInvalid.NumShapes; i++) { var shp = sfInvalid.Shape[i]; Assert.IsFalse(shp.IsValid, "Shape should be invalid"); var reason = shp.IsValidReason; Console.WriteLine(reason); Assert.IsFalse(string.IsNullOrEmpty(reason), "Cannot get validation reason"); } var newFilename = Path.Combine(Path.GetTempPath(), "FixUpShapes.shp"); result = utils.FixUpShapes(sfInvalid, false, newFilename, true); Assert.IsTrue(result, "Could not fix shapefile"); Assert.IsTrue(File.Exists(newFilename), newFilename + " doesn't exists"); result = sfFixed.Open(newFilename); Assert.IsTrue(result, "Could not open fixed shapefile"); Assert.AreEqual(sfInvalid.NumShapes, sfFixed.NumShapes, "Number of shapes are not equal"); Helper.PrintExtents(sfFixed.Extents); } finally { sfInvalid.Close(); sfFixed.Close(); } }
public static Shapefile CreateSfFromWkt(string wkt, int epsgCode) { var sf = new Shapefile(); if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON)) { throw new Exception("Can't create shapefile. Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var shp = new Shape(); if (!shp.ImportFromWKT(wkt)) { throw new Exception("Could not import wkt" + shp.ErrorMsg[shp.LastErrorCode]); } if (sf.EditAddShape(shp) == -1) { throw new Exception("Can't EditAddShape. Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var geoProjection = new GeoProjection(); if (!geoProjection.ImportFromEPSG(epsgCode)) { throw new Exception("Can't ImportFromEPSG Error: " + geoProjection.ErrorMsg[geoProjection.LastErrorCode]); } sf.GeoProjection = geoProjection; if (sf.HasInvalidShapes()) { throw new Exception("Shapefile has invalid shapes"); } return(sf); }
public bool HasInvalidShapes() { return(_shapefile.HasInvalidShapes()); }
public static bool GetInfoShapefile(ref Shapefile sf) { if (sf == null) { throw new ArgumentNullException(nameof(sf)); } bool retVal; try { Console.WriteLine("Working with " + sf.Filename); Console.WriteLine("ShapefileType: " + sf.ShapefileType); Console.WriteLine("ShapefileType2D: " + sf.ShapefileType2D); Console.WriteLine("Num fields: " + sf.NumFields); Console.WriteLine("Num shapes: " + sf.NumShapes); Console.WriteLine("HasSpatialIndex: " + sf.HasSpatialIndex); Console.WriteLine("Projection: " + sf.Projection); if (sf.NumShapes < 100) { Console.WriteLine("HasInvalidShapes: " + sf.HasInvalidShapes()); } else { Console.WriteLine("Warning. Large number of shapes. Skipping HasInvalidShapes check."); } if (!sf.GeoProjection.IsEmpty) { int epsgCode; sf.GeoProjection.TryAutoDetectEpsg(out epsgCode); Console.WriteLine("epsgCode: " + epsgCode); } if (sf.NumShapes <= 0) { throw new Exception("Shapefile has no shapes"); } var shp = sf.Shape[0]; if (shp == null) { throw new NullReferenceException("Cannot get shape: " + sf.ErrorMsg[sf.LastErrorCode]); } Console.WriteLine("numPoints: " + shp.numPoints); Console.WriteLine("NumParts: " + shp.NumParts); Console.WriteLine("ShapeType: " + shp.ShapeType); Console.WriteLine("ShapeType2D: " + shp.ShapeType2D); var isValid = shp.IsValid; Console.WriteLine("IsValid: " + isValid); if (!isValid) { Console.WriteLine("IsValidReason: " + shp.IsValidReason); } if (sf.ShapefileType != shp.ShapeType) { Console.WriteLine("Warning shape(file) type mismatch"); } double x = 0d, y = 0d; double z, m; if (!shp.XY[0, ref x, ref y])
private static Shapefile CreateBorder(double multiplier = 1d) { var sf = new Shapefile(); if (!sf.CreateNewWithShapeID("", ShpfileType.SHP_POLYGON)) { Assert.Fail("Can't create shapefile Error: " + sf.ErrorMsg[sf.LastErrorCode]); } var geoProjection = new GeoProjection(); // WGS 84 / UTM zone 31N if (!geoProjection.ImportFromEPSG(32631)) { Assert.Fail("Can't ImportFromEPSG Error: " + geoProjection.ErrorMsg[geoProjection.LastErrorCode]); } sf.GeoProjection = geoProjection; const double startX = 693502.4; const double startY = 5841019.6; var shp = new Shape(); if (!shp.Create(ShpfileType.SHP_POLYGON)) { Assert.Fail("Can't create shape Error: " + shp.ErrorMsg[shp.LastErrorCode]); } var numPoints = 0; if (!shp.InsertPoint(new Point { x = startX, y = startY }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX - 107 * multiplier, y = startY - 12 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX - 9 * multiplier, y = startY - 99 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX + 11 * multiplier, y = startY - 83 * multiplier }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.InsertPoint(new Point { x = startX, y = startY }, ref numPoints)) { Assert.Fail($"Can't insert point with id: {numPoints} Error: {shp.ErrorMsg[shp.LastErrorCode]}"); } if (!shp.IsValid) { Assert.Fail("Shape is invalid: " + shp.IsValidReason); } var numShapes = 0; if (!sf.EditInsertShape(shp, ref numShapes)) { Assert.Fail("Can't insert shape Error: " + sf.ErrorMsg[sf.LastErrorCode]); } Assert.IsFalse(sf.HasInvalidShapes(), "Created border file has invalid shapes"); return(sf); }