/// <summary> /// Check if the resulting shapefile is correct /// </summary> /// <param name="inputSf"> /// The input sf. /// </param> /// <param name="resultingSf"> /// The resulting sf. /// </param> /// <param name="gdalLastErrorMsg"> /// The gdal last error msg. /// </param> /// <param name="theForm"> /// The the form. /// </param> /// <returns> /// True when no errors else false /// </returns> internal static bool CheckShapefile( IShapefile inputSf, IShapefile resultingSf, string gdalLastErrorMsg, ICallback theForm) { if (resultingSf == null) { var msg = "The resulting shapefile was not created: " + inputSf.ErrorMsg[inputSf.LastErrorCode]; if (gdalLastErrorMsg != string.Empty) { msg += Environment.NewLine + "GdalLastErrorMsg: " + gdalLastErrorMsg; } theForm.Error(string.Empty, msg); return(false); } if (resultingSf.NumShapes < -1) { theForm.Error(string.Empty, "Resulting shapefile has no shapes"); return(false); } if (resultingSf.HasInvalidShapes()) { theForm.Error(string.Empty, "Resulting shapefile has invalid shapes"); return(false); } if (resultingSf.NumFields < -1) { theForm.Error(string.Empty, "Resulting shapefile has no fields"); return(false); } if (resultingSf.NumShapes != resultingSf.Table.NumRows) { var msg = string.Format( "The resulting shapefile has {0} shapes and {1} rows. This should be equal!", resultingSf.NumShapes, resultingSf.Table.NumRows); theForm.Error(string.Empty, msg); return(false); } return(true); }
public static void SaveAsShapefile(IShapefile sf, string filename) { DebugMsg("Saving shapefile ..."); DeleteShapefile(filename); var stopWatch = new Stopwatch(); stopWatch.Start(); if (!sf.SaveAs(Path.Combine(filename))) { throw new Exception("Can't save shapefile Error: " + sf.ErrorMsg[sf.LastErrorCode]); } stopWatch.Stop(); DebugMsg("Time it took to save shapefile: " + stopWatch.Elapsed); if (!File.Exists(filename)) { throw new Exception($"Output file [{filename}] does not exists"); } DebugMsg(filename + " is saved"); }
static void Main(string[] args) { TrajectorySet newSet = new TrajectorySet("gnss_point.csv", new NewTrajectorySetCreator(), CoordinateGenerator.CRSParseFromEPSG(4326)); IShapefile roadShapefile = VectorFactory.OpenShapefile("road.shp", VectorOpenMode.Common); IMapMatch mapMatchFunction = new HMMMapMatch(roadShapefile, 0.000173); IMapMatch mapMatchFunction2 = new HMMMapMatch(roadShapefile, 0.000173, roadShapefile.GetBoundaryBox(), "Direction"); TrajectorySet matchedSet = new TrajectorySet(); foreach (Trajectory trajectory in newSet.TrajectoryList) { Trajectory mapMatchedTrajectory = mapMatchFunction.MatchRoute(trajectory, 0.000173 * 2, 10); matchedSet.AddTrajectory(mapMatchedTrajectory); } IShapefile matchedShapefile = matchedSet.ExportToShapefile(CoordinateGenerator.CRSParseFromEPSG(4326)); matchedShapefile.ExportShapefile("matchedTrajectory2.shp"); }
public static void CheckValidity(IShapefile sf) { if (sf.NumShapes == 0) { throw new Exception("Shapefile has no shapes"); } if (!sf.HasInvalidShapes()) { DebugMsg("Shapefile has no invalid shapes"); return; } var stopwatch = new Stopwatch(); stopwatch.Start(); // Partition the entire source array. var rangePartitioner = Partitioner.Create(0, sf.NumShapes); // Loop over the partitions in parallel. Parallel.ForEach(rangePartitioner, (range, loopState) => { // Loop over each range element without a delegate invocation. for (var i = range.Item1; i < range.Item2; i++) { var shp = sf.Shape[i]; if (!shp.IsValid) { DebugMsg($"Invalid shape with id: {i}. Reason: {shp.IsValidReason}"); } } }); stopwatch.Stop(); DebugMsg("Logging invalid shapes using partioner took: " + stopwatch.Elapsed); }
private static void AddToPolygonSf(IShapefile sfPolygons, Helper.Coordinate coordinate1_1, Helper.Coordinate coordinate1_2, Helper.Coordinate coordinate2_1, Helper.Coordinate coordinate2_2) { var shp = new Shape(); if (!shp.Create(ShpfileType.SHP_POLYGON)) { throw new Exception("Error in creating shape. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } if (shp.AddPoint(coordinate1_1.X, coordinate1_1.Y) < 0) { throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } if (shp.AddPoint(coordinate1_2.X, coordinate1_2.Y) < 0) { throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } if (shp.AddPoint(coordinate2_1.X, coordinate2_1.Y) < 0) { throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } if (shp.AddPoint(coordinate2_2.X, coordinate2_2.Y) < 0) { throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } // Closing: if (shp.AddPoint(coordinate1_1.X, coordinate1_1.Y) < 0) { throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]); } if (!shp.PartIsClockWise[0]) { shp.ReversePointsOrder(0); } if (!shp.IsValid) { shp = shp.FixUp2(tkUnitsOfMeasure.umMeters); if (shp == null) { return; } if (!shp.IsValid) { throw new Exception("Error: shape is not valid. " + shp.IsValidReason); } } // Check if this new shape is overlapping other shapes, // if so clip and add that version instead: var numShapes = sfPolygons.NumShapes; for (var i = 0; i < numShapes; i++) { var shpTesting = sfPolygons.Shape[i]; // If within, don't add again: if (shp.Within(shpTesting)) { Debug.WriteLine("Shape is within " + i); return; } // If overlaps, add only new part: if (shp.Overlaps(shpTesting)) { Debug.WriteLine(i + " overlaps. Touches: " + shp.Touches(shpTesting)); // TODO: Returns wrong part: //shp = shpTesting.Clip(shp, tkClipOperation.clDifference); //if (shp == null) return; } } if (shp.ShapeType2D != ShpfileType.SHP_POLYGON) { return; } if (!shp.PartIsClockWise[0]) { shp.ReversePointsOrder(0); } if (!shp.IsValid) { shp = shp.FixUp2(tkUnitsOfMeasure.umMeters); if (shp == null) { return; } if (!shp.IsValid) { throw new Exception("Error: shape is not valid. " + shp.IsValidReason); } } if (shp.ShapeType2D != ShpfileType.SHP_POLYGON) { return; } if (sfPolygons.EditAddShape(shp) < 0) { throw new Exception("Error in adding shape. Error: " + sfPolygons.ErrorMsg[sfPolygons.LastErrorCode]); } }