/// <summary> /// Intersect minor grids with the currently selected land shapefile. Returns true if current land shape contains minor grid /// </summary> private static bool GetMinorGridsWithinLand() { var objSelected = new object(); var ifldInland = MinorGridsShapefile.FieldIndexByName["inland"]; //this operation selects those minor grids within the selected land shape. MinorGridsShapefile.SelectByShapefile(LandShapefile, tkSpatialRelation.srWithin, true, ref objSelected); try { _withinMinorGrids = (int[])objSelected; for (int n = 0; n < _withinMinorGrids.Length; n++) { MinorGridsShapefile.EditCellValue(ifldInland, _withinMinorGrids[n], true); MinorGridsShapefile.ShapeSelected[_withinMinorGrids[n]] = true; } if (StatusUpdate != null) { CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Inland grids created"; e.GridCount = _withinMinorGrids.Length; StatusUpdate(e); } return(true); } catch (Exception ex) { Logger.Log(ex.Message, "CreateInlandPointDatabase.cs", "GetMinorGridsWithinLand"); return(false); } }
/// <summary> /// Intersects all land area shapes with grid25 major grids /// </summary> private static void ListIntersectLandArea() { if (SetUpMInorGridShapefile()) { //enumerate all shapes in the land shapefile for (int n = 0; n < LandShapefile.NumShapes; n++) { //make sure there are no selections LandShapefile.SelectNone(); //select one shape LandShapefile.ShapeSelected[n] = true; //do the intersection, with the intersection result saved in the objSelected array var objSelected = new object(); Grid25Shapefile.SelectByShapefile(LandShapefile, tkSpatialRelation.srIntersects, SelectedOnly: true, ref objSelected); _intersectedMajorGrids = (int[])objSelected; //raise event if (StatusUpdate != null) { CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Major grids intersected"; e.GridCount = _intersectedMajorGrids.Length; StatusUpdate(e); } PopulateMinorGrid(); if (GetMinorGridsWithinLand()) { ProcessInlandGrids(); } } } }
private void OnStatusUpdate(CreateInlandGridEventArgs e) { Invoke((MethodInvoker)(() => { txtStatus.Text = $"{e.Status}: {e.GridCount}\r\n{txtStatus.Text}"; if (e.StatusDescription != null && e.StatusDescription.Length > 0) { txtStatus.Text = $"{e.StatusDescription}\r\n{txtStatus.Text}"; } })); }
/// <summary> /// Start the process of creating a database of inland minor grids /// </summary> public static bool Start() { bool success = false; if (LandShapefile?.ShapefileType == ShpfileType.SHP_POLYGON && Grid25Shapefile?.ShapefileType == ShpfileType.SHP_POLYGON && FileName.Length > 0) { var proceed = true; if (StatusUpdate != null) { CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Point creation started"; e.StatusDescription = DateTime.Now.ToShortTimeString(); StatusUpdate(e); } proceed = CheckandCreateTable(FileName); if (proceed) { if (LandShapefile.NumSelected > 0) { ListIntersectSelectedLandArea(); } else { ListIntersectLandArea(); } UpdateDatabase(); global.MappingForm.MapLayersHandler.AddLayer(_inlandMinorGrids, "Inland grids", true, true); CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Point creation finished"; e.StatusDescription = DateTime.Now.ToShortTimeString(); StatusUpdate(e); success = true; } } return(success); }
/// <summary> /// Intersects selected land shapes with grid25 major grid shapefiles /// </summary> private static void ListIntersectSelectedLandArea() { if (LandShapefile.NumSelected > 0 && SetUpMInorGridShapefile()) { //get the selected shape indexes var selectedShapesIndexes = MapInterActionHandler.SelectedShapeIndexes; //enumerate all selected shapes for (int n = 0; n < selectedShapesIndexes.Length; n++) { //clear all selections LandShapefile.SelectNone(); //select one shape in the selected shapes LandShapefile.ShapeSelected[selectedShapesIndexes[n]] = true; //intersect the selected shape with the major grid var objSelected = new object(); Grid25Shapefile.SelectByShapefile(LandShapefile, tkSpatialRelation.srIntersects, true, ref objSelected); _intersectedMajorGrids = (int[])objSelected; if (StatusUpdate != null) { CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Major grids intersected"; e.GridCount = _intersectedMajorGrids.Length; StatusUpdate(e); } PopulateMinorGrid(); if (GetMinorGridsWithinLand()) { ProcessInlandGrids(); } } } }
/// <summary> /// Creates minor grid cells inside major grids intersected with land shapefiles /// </summary> private static void PopulateMinorGrid() { MinorGridsShapefile.EditClear(); FishingGrid.UTMZone = UTMZone; int ifldGridNo = Grid25Shapefile.FieldIndexByName["grid_no"]; var gridNumber = 0; //enumerate all intersected major grids for (int n = 0; n < _intersectedMajorGrids.Length; n++) { gridNumber = (int)Grid25Shapefile.CellValue[Grid25Shapefile.FieldIndexByName["grid_no"], _intersectedMajorGrids[n]]; //get the origin of the current major grid var origin = FishingGrid.MajorGridOrigin(gridNumber); //build a minor grid, a point at a time. Here we will be creating 5 points, one point for each corner of the grid. //The 5th point is needed to close the polygon. for (int row = 25; row > 0; row--) { for (int col = 0; col < 25; col++) { var shp = new Shape(); if (shp.Create(ShpfileType.SHP_POLYGON)) { for (int pt = 0; pt < 5; pt++) { switch (pt) { case 0: case 4: shp.AddPoint(origin.x + (col * 2000), origin.y + ((25 - row) * 2000)); break; case 1: shp.AddPoint(origin.x + (col * 2000) + 2000, origin.y + ((25 - row) * 2000)); break; case 2: shp.AddPoint(origin.x + (col * 2000) + 2000, origin.y + ((25 - row) * 2000) + 2000); break; case 3: shp.AddPoint(origin.x + (col * 2000), origin.y + ((25 - row) * 2000) + 2000); break; } } //add the new shape to the shapefile. iShp is the index of the newest added shape var iShp = MinorGridsShapefile.EditAddShape(shp); //a new shape will have an index (iShp) of zero or greater if (iShp >= 0) { //name the cell MinorGridsShapefile.EditCellValue(_iFldName, iShp, $"{gridNumber.ToString()}-{(char)('A' + col)}{row}"); //set the inland attribute to false MinorGridsShapefile.EditCellValue(_iFldInland, iShp, false); } } } } } //raise the event that minor grids were created if (StatusUpdate != null) { CreateInlandGridEventArgs e = new CreateInlandGridEventArgs(); e.Status = "Minor grids created"; e.GridCount = MinorGridsShapefile.NumShapes; StatusUpdate(e); } }