//--------------------------------------------------------- bool CPolygon_Shared_Edges::Get_Shared_Edges(CSG_Shape_Part *pA, CSG_Shape_Part *pB, double Epsilon) { int Edge_1st = m_pEdges->Get_Count(); CSG_Shape *pEdge = NULL; for(int iPoint=0, jPoint; iPoint<pA->Get_Count(); iPoint++) { CSG_Point Point = pA->Get_Point(iPoint); if( !pEdge ) { for(jPoint=0; jPoint<pB->Get_Count(); jPoint++) { if( Point.is_Equal(pB->Get_Point(jPoint), Epsilon) ) { pEdge = m_pEdges->Add_Shape(); pEdge ->Add_Point(Point); if( m_Field < 0 ) { pEdge->Set_Value(0, pA->Get_Owner()->Get_Index()); pEdge->Set_Value(1, pB->Get_Owner()->Get_Index()); } else { pEdge->Set_Value(0, pA->Get_Owner()->asString(m_Field)); pEdge->Set_Value(1, pB->Get_Owner()->asString(m_Field)); } break; } } } else { int j = jPoint; if( Point.is_Equal(pB->Get_Point(jPoint = Get_Next_Vertex(pB, j, false)), Epsilon) || Point.is_Equal(pB->Get_Point(jPoint = Get_Next_Vertex(pB, j, true )), Epsilon) ) { pEdge->Add_Point(Point); } else { pEdge = NULL; } } } //----------------------------------------------------- if( pEdge ) { CSG_Shape *pEdge_1st = m_pEdges->Get_Shape(Edge_1st); if( pEdge != pEdge_1st && SG_Is_Equal(pA->Get_Point(0), pEdge_1st->Get_Point(0)) ) { for(int i=0; i<pEdge_1st->Get_Point_Count(0); i++) { pEdge->Add_Point(pEdge_1st->Get_Point(i)); } m_pEdges->Del_Shape(Edge_1st); } } //----------------------------------------------------- for(int iEdge=m_pEdges->Get_Count()-1; iEdge>=Edge_1st; iEdge--) { if( m_pEdges->Get_Shape(iEdge)->Get_Point_Count() <= 1 ) // touches at point { m_pEdges->Del_Shape(iEdge); } } //----------------------------------------------------- return( true ); }
bool Cconnectivity_analysis::On_Execute(void) { CSG_Grid *pinpgrid, *bingrid, *symb_grid, *hgrid; CSG_Shapes *pOutlines; bool filter, corners_centers, remove_marginal_regions; unsigned short numrows; unsigned short numcols; unsigned char center; double xmin; double ymin; short interm_grid_created; simple_REGIONC_list *reg_first; simple_REGIONC_list *reg_last; simple_REGIONC_list *reg_curr; pinpgrid = Parameters ("INPUT_GRID")->asGrid(); bingrid = Parameters ("FILTERED_MASK")->asGrid(); filter = Parameters ("FILTER")->asBool(); corners_centers = Parameters ("BORDER_PIXEL_CENTERS")->asBool(); remove_marginal_regions = Parameters ("REMOVE_MARGINAL_REGIONS")->asBool(); pOutlines = Parameters("OUTLINES")->asShapes(); symb_grid = Parameters ("SYMBOLIC_IMAGE")->asGrid(); CSG_String sName = pOutlines->Get_Name(); pOutlines->Destroy(); pOutlines->Set_Name(sName); pOutlines->Add_Field(SG_T("ID"), SG_DATATYPE_Int); numrows=pinpgrid->Get_NY()+2; numcols=pinpgrid->Get_NX()+2; // xmin=pinpgrid->Get_XMin()-2*pinpgrid->Get_Cellsize(); // ymin=pinpgrid->Get_YMin()-2*pinpgrid->Get_Cellsize(); xmin=pinpgrid->Get_XMin(); ymin=pinpgrid->Get_YMin(); unsigned char **bin_image; long **symb_image; if (corners_centers) center = 1; else center = 0; bin_image = (unsigned char **) matrix_all_alloc (numrows, numcols, 'U', 0); symb_image = (long **) matrix_all_alloc (numrows, numcols, 'L', 0); interm_grid_created = 0; //SG_Free(bin_image); //CSG_Grid *pTmp = new CSG_Grid(); //delete pTmp; if (filter) { if (bingrid == NULL) { SG_UI_Msg_Add(_TL("Filtered mask will be created automatically ..."), true); bingrid = SG_Create_Grid(SG_DATATYPE_Char, pinpgrid->Get_NX(), pinpgrid->Get_NY(), pinpgrid->Get_Cellsize(), pinpgrid->Get_XMin(), pinpgrid->Get_YMin()); if (bingrid == NULL) { SG_UI_Msg_Add_Error(_TL("Unable to create filtered mask grid!")); matrix_all_free ((void **) bin_image); matrix_all_free ((void **) symb_image); return (false); } Parameters("FILTERED_MASK")->Set_Value(bingrid); interm_grid_created = 1; } //----------------------------------------------------- RUN_MODULE("grid_filter" , 13, SET_PARAMETER("INPUT_GRID" , pinpgrid) && SET_PARAMETER("OUTPUT_GRID" , bingrid) && SET_PARAMETER("RADIUS" , Parameters ("SIZE")->asInt()) ) hgrid = bingrid; } else { hgrid = pinpgrid; } for (int y = 0; y < hgrid->Get_NY () && Set_Progress(y, hgrid->Get_NY()); y++) { for (int x = 0; x < hgrid->Get_NX(); x++) { if (hgrid->is_NoData(x,y)) bin_image[y+1][x+1] = 0; else bin_image[y+1][x+1] = hgrid->asChar(x,y); } } // Here the regions are removed which have contact with the image margins; // this is achieved by a region growing if (remove_marginal_regions) { for (int y = 1; y < numrows - 1; y++) { if (bin_image [y][1] != 0) background_region_growing (bin_image, numrows, numcols, y, 1); if (bin_image [y][numcols - 2] != 0) background_region_growing (bin_image, numrows, numcols, y, numcols-2); } for (int x = 1; x < numcols - 1; x++) { if (bin_image [1][x] != 0) background_region_growing (bin_image, numrows, numcols, 1, x); if (bin_image [numrows-2][x] != 0) background_region_growing (bin_image, numrows, numcols, numrows-2, x); } if (filter) { for (int y = 0; y < bingrid->Get_NY (); y++) { #pragma omp parallel for for (int x = 0; x < bingrid->Get_NX(); x++) { bingrid->Set_Value(x, y, bin_image[y+1][x+1]); } } } } if (interm_grid_created) bingrid->Destroy(); // The function which does the proper work: // computation of the symbolic image, construction of the border chains (shapes) comb_contour_region_marking (numrows, numcols, bin_image, symb_image, ®_first, ®_last, center); for (int y = 0; y < symb_grid->Get_NY () && Set_Progress(y, symb_grid->Get_NY()); y++) { #pragma omp parallel for for (int x = 0; x < symb_grid->Get_NX(); x++) { symb_grid->Set_Value(x, y, symb_image[y+1][x+1]); } } // Here the shapes are generated int iPolygon; for (iPolygon = 0, reg_curr = reg_first; reg_curr != NULL; reg_curr = reg_curr -> next, iPolygon++) { CSG_Shape *pShape = pOutlines->Add_Shape(); pShape->Set_Value(0, iPolygon); // set ID field (= first field in table) to polygon ID for (simple_PIXELC_list *pix_curr = reg_curr->first_pix; pix_curr != NULL; pix_curr = pix_curr->next) { TSG_Point point = symb_grid->Get_System().Get_Grid_to_World(pix_curr->col - 1, pix_curr->row - 1); pShape->Add_Point(point, 0); } int iHoles; simple_INNER_REGION_list *inner_curr; for (iHoles=0, inner_curr = reg_curr->inner_first; iHoles < reg_curr->num_holes; iHoles++, inner_curr = inner_curr->next) { for (simple_PIXELC_list *pix_curr = inner_curr->first_pix; pix_curr != NULL; pix_curr = pix_curr->next) { TSG_Point point = symb_grid->Get_System().Get_Grid_to_World(pix_curr->col - 1, pix_curr->row - 1); pShape->Add_Point(point, iHoles+1); } } if (!corners_centers) shift_shape (pShape, -Get_Cellsize()/2.0, -Get_Cellsize()/2.0); } matrix_all_free ((void **) bin_image); matrix_all_free ((void **) symb_image); free_regions (®_first, ®_last); return( true ); }
//--------------------------------------------------------- bool CPolygon_Shared_Edges::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); m_Field = Parameters("ATTRIBUTE")->asInt(); m_pEdges = Parameters("EDGES")->asShapes(); m_pEdges->Create(SHAPE_TYPE_Line, CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("Edges"))); m_pEdges->Add_Field("ID_A", m_Field < 0 ? SG_DATATYPE_Int : pPolygons->Get_Field_Type(m_Field)); m_pEdges->Add_Field("ID_B", m_Field < 0 ? SG_DATATYPE_Int : pPolygons->Get_Field_Type(m_Field)); // m_pNodes = Parameters("NODES")->asShapes(); // m_pNodes->Create(SHAPE_TYPE_Point, CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("Nodes"))); // m_pNodes->Add_Field("ID", SG_DATATYPE_Int); bool bVertices = Parameters("VERTICES")->asBool (); double Epsilon = Parameters("EPSILON" )->asDouble(); int iPolygon, nAdded = 0, nRemoved = 0; //----------------------------------------------------- if( bVertices ) { for(iPolygon=0; iPolygon<pPolygons->Get_Count() && Set_Progress(iPolygon, pPolygons->Get_Count()); iPolygon++) { CSG_Shape_Polygon *pPolygon = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); for(int iPart=0; iPart<pPolygon->Get_Part_Count() && Process_Get_Okay(); iPart++) { CSG_Shape_Part *pPart = pPolygon->Get_Part(iPart); CSG_Point A = pPart->Get_Point(pPart->Get_Count() - 1); if( A != pPart->Get_Point(0) ) { pPart->Add_Point(A); } for(int iPoint=pPart->Get_Count()-2; iPoint>=0; iPoint--) { CSG_Point B = A; A = pPart->Get_Point(iPoint); if( A == B ) { pPart->Del_Point(iPoint + 1); nRemoved--; } } } } } //----------------------------------------------------- for(iPolygon=0; iPolygon<pPolygons->Get_Count()-1 && Set_Progress(iPolygon, pPolygons->Get_Count()-1); iPolygon++) { CSG_Shape_Polygon *pA = (CSG_Shape_Polygon *)pPolygons->Get_Shape(iPolygon); for(int jPolygon=iPolygon+1; jPolygon<pPolygons->Get_Count() && Process_Get_Okay(); jPolygon++) { CSG_Shape_Polygon *pB = (CSG_Shape_Polygon *)pPolygons->Get_Shape(jPolygon); for(int iPart=0; iPart<pA->Get_Part_Count() && Process_Get_Okay(); iPart++) { for(int jPart=0; jPart<pB->Get_Part_Count() && Process_Get_Okay(); jPart++) { if( pA->Get_Part(iPart)->Get_Extent().Intersects(pB->Get_Part(jPart)->Get_Extent()) ) { if( bVertices ) { nAdded += Check_Vertices(pA->Get_Part(iPart), pB->Get_Part(jPart), Epsilon); nAdded += Check_Vertices(pB->Get_Part(jPart), pA->Get_Part(iPart), Epsilon); } Get_Shared_Edges(pA->Get_Part(iPart), pB->Get_Part(jPart), Epsilon); } } } } } //----------------------------------------------------- if( Parameters("DOUBLE")->asBool() ) { for(int iEdge=0, nEdges=m_pEdges->Get_Count(); iEdge<nEdges && Set_Progress(iEdge, nEdges); iEdge++) { CSG_Shape *pA = m_pEdges->Get_Shape(iEdge); CSG_Shape *pB = m_pEdges->Add_Shape(pA); *(pB->Get_Value(0)) = *(pA->Get_Value(1)); *(pB->Get_Value(1)) = *(pA->Get_Value(0)); } } //----------------------------------------------------- if( nAdded > 0 || nRemoved > 0 ) { Message_Add(CSG_String::Format(SG_T("\n%s: %d %s, %d %s\n"), _TL("Vertices"), nAdded , _TL("added" ), nRemoved, _TL("removed") ), false); DataObject_Update(pPolygons); } return( true ); }
//--------------------------------------------------------- bool CDirect_Georeferencing::On_Execute(void) { //----------------------------------------------------- if( !m_Georeferencer.Set_Transformation(Parameters, Get_NX(), Get_NY()) ) { return( false ); } //----------------------------------------------------- CSG_Grid *pDEM = Parameters("DEM" )->asGrid(); double zRef = Parameters("ZREF" )->asDouble(); bool bFlip = Parameters("ROW_ORDER")->asInt() == 1; //----------------------------------------------------- TSG_Grid_Resampling Resampling; switch( Parameters("RESAMPLING")->asInt() ) { default: Resampling = GRID_RESAMPLING_NearestNeighbour; break; case 1: Resampling = GRID_RESAMPLING_Bilinear; break; case 2: Resampling = GRID_RESAMPLING_BicubicSpline; break; case 3: Resampling = GRID_RESAMPLING_BSpline; break; } //----------------------------------------------------- TSG_Point p[4]; p[0] = m_Georeferencer.Image_to_World( 0, 0, zRef); p[1] = m_Georeferencer.Image_to_World(Get_NX(), 0, zRef); p[2] = m_Georeferencer.Image_to_World(Get_NX(), Get_NY(), zRef); p[3] = m_Georeferencer.Image_to_World( 0, Get_NY(), zRef); CSG_Rect r(p[0], p[1]); r.Union(p[2]); r.Union(p[3]); //----------------------------------------------------- CSG_Shapes *pShapes = Parameters("EXTENT")->asShapes(); if( pShapes ) { pShapes->Create(SHAPE_TYPE_Polygon, _TL("Extent")); pShapes->Add_Field(_TL("OID"), SG_DATATYPE_Int); CSG_Shape *pExtent = pShapes->Add_Shape(); pExtent->Add_Point(p[0]); pExtent->Add_Point(p[1]); pExtent->Add_Point(p[2]); pExtent->Add_Point(p[3]); } //----------------------------------------------------- double Cellsize = SG_Get_Distance(p[0], p[1]) / Get_NX(); CSG_Grid_System System(Cellsize, r); m_Grid_Target.Set_User_Defined(Get_Parameters("TARGET"), System); if( !Dlg_Parameters("TARGET") ) { return( false ); } System = m_Grid_Target.Get_System(); if( !System.is_Valid() ) { return( false ); } //----------------------------------------------------- CSG_Parameter_Grid_List *pInput = Parameters("INPUT" )->asGridList(); CSG_Parameter_Grid_List *pOutput = Parameters("OUTPUT")->asGridList(); pOutput->Del_Items(); if( pInput->Get_Grid_Count() <= 0 ) { return( false ); } else { TSG_Data_Type Type; switch( Parameters("DATA_TYPE")->asInt() ) { case 0: Type = SG_DATATYPE_Byte; break; case 1: Type = SG_DATATYPE_Char; break; case 2: Type = SG_DATATYPE_Word; break; case 3: Type = SG_DATATYPE_Short; break; case 4: Type = SG_DATATYPE_DWord; break; case 5: Type = SG_DATATYPE_Int; break; case 6: Type = SG_DATATYPE_Float; break; case 7: Type = SG_DATATYPE_Double; break; default: Type = SG_DATATYPE_Undefined; break; } for(int i=0; i<pInput->Get_Grid_Count(); i++) { CSG_Grid *pGrid = SG_Create_Grid(System, Type != SG_DATATYPE_Undefined ? Type : pInput->Get_Grid(i)->Get_Type()); if( !pGrid || !pGrid->is_Valid() ) { if( pGrid ) { delete(pGrid); } return( false ); } pOutput->Add_Item(pGrid); pGrid->Set_Name(pInput->Get_Grid(i)->Get_Name()); } } //----------------------------------------------------- for(int y=0; y<System.Get_NY() && Set_Progress(y, System.Get_NY()); y++) { double py = System.Get_YMin() + y * System.Get_Cellsize(); #pragma omp parallel for for(int x=0; x<System.Get_NX(); x++) { double pz, px = System.Get_XMin() + x * System.Get_Cellsize(); if( !pDEM || !pDEM->Get_Value(px, py, pz) ) { pz = zRef; } TSG_Point p = m_Georeferencer.World_to_Image(px, py, pz); if( bFlip ) { p.y = (Get_NY() - 1) - p.y; } for(int i=0; i<pInput->Get_Grid_Count(); i++) { if( pInput->Get_Grid(i)->Get_Value(p.x, p.y, pz, Resampling) ) { pOutput->Get_Grid(i)->Set_Value(x, y, pz); } else { pOutput->Get_Grid(i)->Set_NoData(x, y); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CWatersheds_ext::Get_Basin(CSG_Grid *pBasins, CSG_Shapes *pPolygons, int xMouth, int yMouth, int Main_ID) { int x, y, Basin_ID = 1 + pPolygons->Get_Count(); CSG_Shape *pPolygon; CSG_Grid_Stack Stack; CSG_Simple_Statistics s_Height, s_Distance; //----------------------------------------------------- Stack.Push(x = xMouth, y = yMouth); pBasins ->Set_Value(x, y, Basin_ID); m_Distance .Set_Value(x, y, 0.0); s_Height += m_pDEM->asDouble(x, y); s_Distance += 0.0; //----------------------------------------------------- while( Stack.Get_Size() > 0 && Process_Get_Okay() ) { Stack.Pop(x, y); double d = m_Distance.asDouble(x, y); //------------------------------------------------- for(int i=0; i<8; i++) { int ix = Get_xFrom(i, x); int iy = Get_yFrom(i, y); if( is_InGrid(ix, iy) && pBasins->is_NoData(ix, iy) && i == m_Direction.asInt(ix, iy) ) { Stack.Push(ix, iy); pBasins ->Set_Value(ix, iy, Basin_ID); m_Distance .Set_Value(ix, iy, d + Get_Length(i)); s_Height += m_pDEM->asDouble(ix, iy); s_Distance += d + Get_Length(i); } } } //----------------------------------------------------- if( s_Height.Get_Count() > 1 && (pPolygon = Get_Basin(pBasins, pPolygons)) != NULL ) { double d, Area, Perimeter, Side_A, Side_B; CSG_String Gravelius; // Area = s_Height.Get_Count() * Get_System()->Get_Cellarea(); Area = ((CSG_Shape_Polygon*)pPolygon)->Get_Area(); Perimeter = ((CSG_Shape_Polygon*)pPolygon)->Get_Perimeter(); d = 0.28 * Perimeter / sqrt(Area); Gravelius = d > 1.75 ? _TL("rectangular") : d > 1.5 ? _TL("ovalooblonga-rectangularoblonga") : d > 1.25 ? _TL("ovaloredonda-ovalooblonga") : _TL("redonda-ovaloredonda"); d = pow(Perimeter, 2.0) - 8.0 * Area; Side_A = d > 0.0 ? (Perimeter + sqrt(d)) / 4.0 : -1.0; Side_B = d > 0.0 ? (Perimeter - 2.0 * Side_A) / 2.0 : -1.0; pPolygon->Set_Value(FIELD_ID , Basin_ID); pPolygon->Set_Value(FIELD_ID_MAIN , Main_ID); pPolygon->Set_Value(FIELD_MOUTH_X , Get_System()->Get_xGrid_to_World(xMouth)); pPolygon->Set_Value(FIELD_MOUTH_Y , Get_System()->Get_yGrid_to_World(yMouth)); pPolygon->Set_Value(FIELD_PERIMETER , Perimeter); pPolygon->Set_Value(FIELD_AREA , Area); pPolygon->Set_Value(FIELD_CENTROID_X , ((CSG_Shape_Polygon*)pPolygon)->Get_Centroid().x); pPolygon->Set_Value(FIELD_CENTROID_Y , ((CSG_Shape_Polygon*)pPolygon)->Get_Centroid().y); pPolygon->Set_Value(FIELD_Z_MEAN , s_Height .Get_Mean()); pPolygon->Set_Value(FIELD_Z_RANGE , s_Height .Get_Range()); pPolygon->Set_Value(FIELD_DIST_MEAN , s_Distance.Get_Mean()); pPolygon->Set_Value(FIELD_DIST_MAX , s_Distance.Get_Maximum()); pPolygon->Set_Value(FIELD_CONCTIME , s_Height.Get_Range() <= 0.0 ? -1.0 : pow(0.87 * pow(s_Distance.Get_Maximum() / 1000.0, 3.0) / s_Height.Get_Range(), 0.385) ); pPolygon->Set_Value(FIELD_BASIN_TYPE , Gravelius); pPolygon->Set_Value(FIELD_EQVRECT_A , Side_A); pPolygon->Set_Value(FIELD_EQVRECT_B , Side_B); pPolygon->Set_Value(FIELD_OROG_IDX , SG_Get_Square(s_Height.Get_Mean()) / (0.0001 * Area)); // Orographic index, defined as the mean catchment altitude times the ratio of the mean catchment altitude to the orthogonal projection of drainage area (Alcázar, Palau (2010): Establishing environmental flow regimes in a Mediterranean watershed based on a regional classification. Journal of Hydrology, V. 388 pPolygon->Set_Value(FIELD_MASS_IDX , Perimeter / (0.0001 * Area)); // Perimeter / (0.0001 * Area) ??!! pPolygon->Set_Value(FIELD_BASINS_UP , 0.0); // Upslope Basins pPolygon->Set_Value(FIELD_BASINS_DOWN , 0.0); // Downslope Basins return( true ); } return( false ); }
//--------------------------------------------------------- bool CXYZ_Export::On_Execute(void) { bool bHeader; int Field, off_Field, Separate; CSG_File Stream; CSG_Shapes *pPoints; //----------------------------------------------------- pPoints = Parameters("POINTS" )->asShapes(); bHeader = Parameters("HEADER" )->asBool(); Field = Parameters("FIELD" )->asInt(); Separate = pPoints->Get_Type() == SHAPE_TYPE_Point ? 0 : Parameters("SEPARATE")->asInt(); off_Field = pPoints->Get_ObjectType() == DATAOBJECT_TYPE_PointCloud ? 2 : 0; //----------------------------------------------------- if( pPoints->Get_Field_Count() == 0 ) { Error_Set(_TL("data set has no attributes")); return( false ); } //----------------------------------------------------- if( !Stream.Open(Parameters("FILENAME")->asString(), SG_FILE_W, false) ) { Error_Set(_TL("could not open file")); return( false ); } //----------------------------------------------------- if( bHeader ) { Stream.Printf(SG_T("X\tY")); if( Field < 0 ) { for(int iField=off_Field; iField<pPoints->Get_Field_Count(); iField++) { Stream.Printf(SG_T("\t%s"), pPoints->Get_Field_Name(iField)); } } else { Stream.Printf(SG_T("\tZ")); } Stream.Printf(SG_T("\n")); } //------------------------------------------------- for(int iShape=0; iShape<pPoints->Get_Count() && Set_Progress(iShape, pPoints->Get_Count()); iShape++) { CSG_Shape *pShape = pPoints->Get_Shape(iShape); for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { switch( Separate ) { case 1: // * Stream.Printf(SG_T("*\n")); break; case 2: // number of points Stream.Printf(SG_T("%d\n"), pShape->Get_Point_Count(iPart)); break; } for(int iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { TSG_Point Point = pShape->Get_Point(iPoint, iPart); Stream.Printf(SG_T("%f\t%f"), Point.x, Point.y); if( Field < 0 ) { for(int iField=off_Field; iField<pPoints->Get_Field_Count(); iField++) { switch( pPoints->Get_Field_Type(iField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: Stream.Printf(SG_T("\t\"%s\""), pShape->asString(iField)); break; default: Stream.Printf(SG_T("\t%f") , pShape->asDouble(iField)); break; } } } else switch( pPoints->Get_Field_Type(Field) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: Stream.Printf(SG_T("\t\"%s\""), pShape->asString(Field)); break; default: Stream.Printf(SG_T("\t%f") , pShape->asDouble(Field)); break; } Stream.Printf(SG_T("\n")); } } } //------------------------------------------------- return( true ); }
//--------------------------------------------------------- void CD8_Flow_Analysis::Get_Segment(int x, int y) { int i = m_pDir->asInt(x, y); if( i >= 0 ) { CSG_Shape *pSegment = m_pSegments->Add_Shape(); pSegment->Set_Value(0, m_pSegments->Get_Count()); // SEGMENT_ID pSegment->Set_Value(1, m_Nodes.asInt(x, y)); // NODE_A pSegment->Set_Value(3, m_pBasins->asInt(x, y)); // BASIN pSegment->Set_Value(4, m_pOrder->asInt(x, y) + 1 - m_Threshold); // ORDER pSegment->Set_Value(5, m_pOrder->asInt(x, y)); // ORDER_CELL pSegment->Add_Point(Get_System()->Get_Grid_to_World(x, y)); pSegment->Set_Z(m_pDEM->asDouble(x, y), pSegment->Get_Point_Count() - 1); do { x += Get_xTo(i); y += Get_yTo(i); pSegment->Add_Point(Get_System()->Get_Grid_to_World(x, y)); pSegment->Set_Z(m_pDEM->asDouble(x, y), pSegment->Get_Point_Count() - 1); if( m_Nodes.asInt(x, y) ) { pSegment->Set_Value(2, m_Nodes.asInt(x, y)); // NODE_B pSegment->Set_Value(6, ((CSG_Shape_Line *)pSegment)->Get_Length()); // LENGTH return; } } while( (i = m_pDir->asInt(x, y)) >= 0 ); } }
//--------------------------------------------------------- bool CPoint_Trend_Surface::Get_Regression(CSG_Shapes *pPoints, int iAttribute) { //----------------------------------------------------- int i, j, Field; m_Names.Clear(); m_Names += pPoints->Get_Name(); for(i=1; i<=m_xOrder; i++) { m_Names += Get_Power(SG_T("x"), i); } for(i=1; i<=m_yOrder; i++) { m_Names += Get_Power(SG_T("y"), i); for(j=1; j<=m_xOrder && i<m_tOrder && j<m_tOrder; j++) { m_Names += Get_Power(SG_T("x"), j) + Get_Power(SG_T("y"), i); } } //----------------------------------------------------- CSG_Vector Y, xPow, yPow; CSG_Matrix X; Y.Create(pPoints->Get_Count()); X.Create(m_Names.Get_Count(), pPoints->Get_Count()); xPow.Create(m_xOrder + 1); yPow.Create(m_yOrder + 1); xPow[0] = 1.0; yPow[0] = 1.0; //----------------------------------------------------- for(int iShape=0; iShape<pPoints->Get_Count() && Set_Progress(iShape, pPoints->Get_Count()); iShape++) { CSG_Shape *pShape = pPoints->Get_Shape(iShape); if( !pShape->is_NoData(iAttribute) ) { double zShape = pShape->asDouble(iAttribute); TSG_Point Point = pShape->Get_Point(0); Y[iShape] = zShape; X[iShape][0] = 1.0; for(i=1, Field=1; i<=m_xOrder; i++) { X[iShape][Field++] = xPow[i] = xPow[i - 1] * Point.x; } for(i=1; i<=m_yOrder; i++) { X[iShape][Field++] = yPow[i] = yPow[i - 1] * Point.y; for(j=1; j<=m_xOrder && i<m_tOrder && j<m_tOrder; j++) { X[iShape][Field++] = xPow[j] * yPow[i]; } } } } //----------------------------------------------------- CSG_Matrix Xt, XtXinv; Xt = X; Xt .Set_Transpose(); XtXinv = Xt * X; XtXinv .Set_Inverse(); m_Coefficients = XtXinv * Xt * Y; return( true ); }
bool CWaterRetentionCapacity::On_Execute(void){ int i,j; int x,y; int iField; int iShape; int iRows; float fValue = 0; float **pData; int iX, iY; float fC; double dSlope,dAspect; CSG_Shape* pShape; CSG_Shapes* pShapes = Parameters("SHAPES")->asShapes(); CSG_Grid* pDEM = Parameters("DEM")->asGrid(); m_pRetention = Parameters("RETENTION")->asGrid(); m_pSlope = SG_Create_Grid(pDEM); m_pOutput = Parameters("OUTPUT")->asShapes(); m_pOutput->Assign(pShapes); m_pOutput->Add_Field("CCC", SG_DATATYPE_Double); m_pOutput->Add_Field("CIL", SG_DATATYPE_Double); m_pOutput->Add_Field(_TL("Permeability"), SG_DATATYPE_Double); m_pOutput->Add_Field(_TL("Equivalent Moisture"), SG_DATATYPE_Double); m_pOutput->Add_Field(_TL("Water Retention Capacity"), SG_DATATYPE_Double); for(y=0; y<Get_NY() && Set_Progress(y); y++){ for(x=0; x<Get_NX(); x++){ if( pDEM->Get_Gradient(x, y, dSlope, dAspect) ){ m_pSlope->Set_Value(x, y, dSlope); } else{ m_pSlope->Set_NoData(x, y); } } } iRows = pShapes->Get_Field_Count() / 5; pData = new float*[iRows]; for (iShape = 0; iShape < pShapes->Get_Count(); iShape++){ pShape = pShapes->Get_Shape(iShape); for (i = 0; i< iRows; i++){ pData[i] = new float[5]; for (j = 0; j < 5; j++){ pData[i][j] = 0; try{ pData[i][j] = pShape->asFloat(j+i*5); }//try catch(...){} }//for }//for iX = (int)((pShape->Get_Point(0).x - pDEM->Get_XMin())/pDEM->Get_Cellsize()); iY = (int)((pShape->Get_Point(0).y - pDEM->Get_YMin())/pDEM->Get_Cellsize()); fC = (float)(1. - tan(m_pSlope->asFloat(iX,iY,false))); pShape = m_pOutput->Get_Shape(iShape); CalculateWaterRetention(pData, iRows, fC, pShape); }//for iField = m_pOutput->Get_Field_Count()-1; CIDW IDW; IDW.setParameters(m_pRetention, m_pOutput, iField); IDW.Interpolate(); CorrectWithSlope(); return true; }//method
//--------------------------------------------------------- void CDXF_Import::addText(const DL_TextData &data) { if( !Check_Layer(attributes.getLayer().c_str()) ) return; CSG_Shape *pText = m_pText->Add_Shape(); pText->Add_Point(m_Offset.x + data.ipx, m_Offset.y + data.ipy); pText->Set_Value(TBL_TEXT_LAYER , CSG_String(attributes.getLayer().c_str())); pText->Set_Value(TBL_TEXT_Z , m_Offset.z + data.ipz); pText->Set_Value(TBL_TEXT_TEXT , CSG_String(data.text.c_str())); pText->Set_Value(TBL_TEXT_HEIGHT, data.height); pText->Set_Value(TBL_TEXT_ANGLE , data.angle * M_RAD_TO_DEG); pText->Set_Value(TBL_TEXT_APX , m_Offset.z + data.apx); pText->Set_Value(TBL_TEXT_APY , m_Offset.z + data.apy); pText->Set_Value(TBL_TEXT_APZ , m_Offset.z + data.apz); pText->Set_Value(TBL_TEXT_SCALE , data.xScaleFactor); pText->Set_Value(TBL_TEXT_HJUST , data.hJustification); pText->Set_Value(TBL_TEXT_VJUST , data.vJustification); pText->Set_Value(TBL_TEXT_STYLE , CSG_String(data.style.c_str())); pText->Set_Value(TBL_TEXT_SCALE , data.textGenerationFlags); }
//--------------------------------------------------------- bool CSurfer_BLN_Import::On_Execute(void) { bool bOk; int ID, Flag, iPoint, nPoints; double x, y; FILE *Stream; TSG_Shape_Type Type; CSG_String FileName, sLine, sName, sDesc, sTemp; CSG_Table_Record *pRecord; CSG_Table *pTable; CSG_Shape *pShape; CSG_Shapes *pShapes; //----------------------------------------------------- pShapes = Parameters("SHAPES") ->asShapes(); pTable = Parameters("TABLE") ->asTable(); FileName = Parameters("FILE") ->asString(); switch( Parameters("TYPE")->asInt() ) { case 0: Type = SHAPE_TYPE_Point; break; case 1: default: Type = SHAPE_TYPE_Line; break; case 2: Type = SHAPE_TYPE_Polygon; break; } //----------------------------------------------------- if( (Stream = fopen(FileName.b_str(), "r")) != NULL ) { bOk = true; ID = 0; if( pShapes->Get_Type() != SHAPE_TYPE_Undefined && pShapes->Get_Type() != Type ) { pShapes = SG_Create_Shapes(Type, SG_File_Get_Name(FileName, false)); Parameters("SHAPES")->Set_Value(pShapes); DataObject_Add(pShapes); } else { pShapes->Create(Type, SG_File_Get_Name(FileName, false)); } if( Type == SHAPE_TYPE_Point ) { if( pTable == NULL ) { pTable = SG_Create_Table(); Parameters("TABLE")->Set_Value(pTable); } else { pTable->Destroy(); } pTable ->Add_Field("ID" , SG_DATATYPE_Int); pTable ->Add_Field("FLAG" , SG_DATATYPE_Int); pTable ->Add_Field("NAME" , SG_DATATYPE_String); pTable ->Add_Field("DESC" , SG_DATATYPE_String); pShapes->Add_Field("ID" , SG_DATATYPE_Int); pShapes->Add_Field("ID_LUT" , SG_DATATYPE_Int); pShapes->Add_Field("Z" , SG_DATATYPE_Double); } else { pShapes->Add_Field("ID" , SG_DATATYPE_Int); pShapes->Add_Field("FLAG" , SG_DATATYPE_Int); pShapes->Add_Field("NAME" , SG_DATATYPE_String); pShapes->Add_Field("DESC" , SG_DATATYPE_String); } //------------------------------------------------- while( bOk && SG_Read_Line(Stream, sLine) && sLine.BeforeFirst(',').asInt(nPoints) && nPoints > 0 && Process_Get_Okay(true) ) { Process_Set_Text(CSG_String::Format(SG_T("%d. %s"), ++ID, _TL("shape in process"))); sTemp = sLine.AfterFirst (','); sLine = sTemp; Flag = sLine.BeforeFirst(',').asInt(); sTemp = sLine.AfterFirst (','); sLine = sTemp; sTemp = sLine.BeforeFirst(','); sName = sTemp.AfterFirst('\"').BeforeLast('\"'); sTemp = sLine.AfterFirst (','); sLine = sTemp; sTemp = sLine.BeforeFirst(','); sDesc = sTemp.AfterFirst('\"').BeforeLast('\"'); if( Type == SHAPE_TYPE_Point ) { pRecord = pTable->Add_Record(); pRecord->Set_Value(0, ID); pRecord->Set_Value(1, Flag); pRecord->Set_Value(2, sName); pRecord->Set_Value(3, sDesc); for(iPoint=0; iPoint<nPoints && bOk; iPoint++) { if( (bOk = SG_Read_Line(Stream, sLine)) == true ) { pShape = pShapes->Add_Shape(); pShape->Set_Value(0, iPoint + 1); pShape->Set_Value(1, ID); pShape->Set_Value(2, sLine.AfterLast (',').asDouble()); x = sLine.BeforeFirst(',').asDouble(); y = sLine.AfterFirst (',').asDouble(); pShape->Add_Point(x, y); } } } else { pShape = pShapes->Add_Shape(); pShape->Set_Value(0, ID); pShape->Set_Value(1, Flag); pShape->Set_Value(2, sName); pShape->Set_Value(3, sDesc); for(iPoint=0; iPoint<nPoints && bOk; iPoint++) { if( (bOk = SG_Read_Line(Stream, sLine)) == true ) { x = sLine.BeforeFirst(',').asDouble(); y = sLine.AfterFirst (',').asDouble(); pShape->Add_Point(x, y); } } } } fclose(Stream); } //----------------------------------------------------- if( pShapes->is_Valid() && pShapes->Get_Count() > 0 ) { return( true ); } return( false ); }
//--------------------------------------------------------- bool CShapes_Extents::On_Execute(void) { CSG_Shapes *pShapes = Parameters("SHAPES" )->asShapes(); CSG_Shapes *pExtents = Parameters("EXTENTS")->asShapes(); //----------------------------------------------------- if( !pShapes->is_Valid() ) { Message_Add(_TL("invalid input")); return( false ); } //----------------------------------------------------- int iOutput = Parameters("OUTPUT")->asInt(); if( iOutput == 0 ) // all shapes { pExtents->Create(SHAPE_TYPE_Polygon, CSG_String::Format(SG_T("%s [%s]"), pShapes->Get_Name(), _TL("Extent"))); pExtents->Add_Field(SG_T("OID"), SG_DATATYPE_Int); TSG_Rect r = pShapes->Get_Extent(); CSG_Shape *pExtent = pExtents->Add_Shape(); pExtent->Set_Value(0, 1); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); return( true ); } //----------------------------------------------------- if( pShapes->Get_Type() == SHAPE_TYPE_Point ) { Message_Add(_TL("no 'get extents' support for single point layers")); return( false ); } //----------------------------------------------------- pExtents->Create(SHAPE_TYPE_Polygon, pShapes->Get_Name(), pShapes); for(int iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( iOutput == 1 ) // each shape { TSG_Rect r = pShape->Get_Extent(); CSG_Shape *pExtent = pExtents->Add_Shape(pShape, SHAPE_COPY_ATTR); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); } else // if( iOutput == 2 ) // each shape's part { for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { TSG_Rect r = pShape->Get_Extent(iPart); CSG_Shape *pExtent = pExtents->Add_Shape(pShape, SHAPE_COPY_ATTR); pExtent->Add_Point(r.xMin, r.yMin); pExtent->Add_Point(r.xMin, r.yMax); pExtent->Add_Point(r.xMax, r.yMax); pExtent->Add_Point(r.xMax, r.yMin); } } } //----------------------------------------------------- return( pExtents->is_Valid() ); }
//--------------------------------------------------------- bool CPolygon_Split_Parts::On_Execute(void) { bool bIgnoreLakes; CSG_Shapes *pPolygons, *pParts; pPolygons = Parameters("POLYGONS") ->asShapes(); pParts = Parameters("PARTS") ->asShapes(); bIgnoreLakes = Parameters("LAKES") ->asBool(); pParts->Create(SHAPE_TYPE_Polygon, CSG_String::Format(SG_T("%s [%s]"), pPolygons->Get_Name(), _TL("Parts")), pPolygons); //----------------------------------------------------- for(int iShape=0; iShape<pPolygons->Get_Count() && Set_Progress(iShape, pPolygons->Get_Count()); iShape++) { CSG_Shape *pPolygon = pPolygons->Get_Shape(iShape); for(int iPart=0; iPart<pPolygon->Get_Part_Count() && Process_Get_Okay(); iPart++) { if( bIgnoreLakes || !((CSG_Shape_Polygon *)pPolygon)->is_Lake(iPart) ) { CSG_Shape *pPart = pParts->Add_Shape(pPolygon, SHAPE_COPY_ATTR); for(int iPoint=0; iPoint<pPolygon->Get_Point_Count(iPart); iPoint++) { pPart->Add_Point(pPolygon->Get_Point(iPoint, iPart)); } if( !bIgnoreLakes ) { for(int jPart=0; jPart<pPolygon->Get_Part_Count(); jPart++) { if( ((CSG_Shape_Polygon *)pPolygon)->is_Lake(jPart) && ((CSG_Shape_Polygon *)pPart)->Contains(pPolygon->Get_Point(0, jPart)) ) { for(int jPoint=0, nPart=pPart->Get_Part_Count(); jPoint<pPolygon->Get_Point_Count(jPart); jPoint++) { pPart->Add_Point(pPolygon->Get_Point(jPoint, jPart), nPart); } } } } } } } return( true ); }
//--------------------------------------------------------- CSG_Shape * CWatersheds_ext::Get_Basin(CSG_Grid *pBasins, CSG_Shapes *pPolygons) { int x, y, nEdges, Basin_ID; CSG_Grid Edge; CSG_Shape *pPolygon = NULL; Basin_ID = 1 + pPolygons->Get_Count(); //----------------------------------------------------- Edge.Create(SG_DATATYPE_Char, 2 * Get_NX() + 1, 2 * Get_NY() + 1, 0.5 * Get_Cellsize(), Get_XMin() - 0.5 * Get_Cellsize(), Get_YMin() - 0.5 * Get_Cellsize()); Edge.Set_NoData_Value(0); for(y=0, nEdges=0; y<Get_NY() && Process_Get_Okay(); y++) { for(x=0; x<Get_NX(); x++) { if( pBasins->asInt(x, y) == Basin_ID ) { for(int i=0; i<8; i+=2) { int ix = Get_xTo(i, x); int iy = Get_yTo(i, y); if( !is_InGrid(ix, iy) || pBasins->asInt(ix, iy) != Basin_ID ) { ix = 1 + 2 * x; iy = 1 + 2 * y; Edge.Set_Value( ix, iy , 1); Edge.Set_Value(Get_xTo(i , ix), Get_yTo(i , iy), -1); Edge.Set_Value(Get_xTo(i - 1, ix), Get_yTo(i - 1, iy), -1); nEdges++; } } } } } //----------------------------------------------------- if( nEdges > 0 ) { for(int yEdge=0; yEdge<Edge.Get_NY(); yEdge++) for(int xEdge=0; xEdge<Edge.Get_NX(); xEdge++) { int i = 4; if( Edge.asInt(xEdge, yEdge) == 1 && Edge.asInt(Get_xTo(i, xEdge), Get_yTo(i, yEdge)) == -1 ) { if( pPolygon == NULL ) { pPolygon = pPolygons->Add_Shape(); } int iPart = pPolygon->Get_Part_Count(); int xFirst = x = Get_xTo(i, xEdge); int yFirst = y = Get_yTo(i, yEdge); i = i + 2; pPolygon ->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart); do { int ix = Get_xTo(i + 2, x); int iy = Get_yTo(i + 2, y); if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 ) // go right ? { pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart); i = (i + 2) % 8; } else { if( Edge.asInt(ix, iy) == 1 ) { Edge.Set_NoData(ix, iy); // erase class id in right cells } ix = Get_xTo(i, x); iy = Get_yTo(i, y); if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 ) // go ahead ? { // nop } else { ix = Get_xTo(i + 6, x); iy = Get_yTo(i + 6, y); if( Edge.is_InGrid(ix, iy) && Edge.asInt(ix, iy) == -1 ) // go left ? { pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart); i = (i + 6) % 8; } else { return( false ); } } } x = ix; y = iy; } while( x != xFirst || y != yFirst ); pPolygon->Add_Point(Edge.Get_System().Get_Grid_to_World(x, y), iPart); } } } //----------------------------------------------------- return( pPolygon ); }
//--------------------------------------------------------- bool CWatershed_Segmentation::Get_Seeds(void) { Process_Set_Text(_TL("Seeds")); bool bEdge, bEdge_Seeds; int x, y, i, ix, iy, iMax; double z, dz, dzMax; //----------------------------------------------------- bEdge_Seeds = Parameters("EDGE") ->asBool(); //----------------------------------------------------- for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !m_pGrid->is_InGrid(x, y) ) { m_Dir.Set_Value(x, y, -1); m_pSegments->Set_NoData(x, y); } else { for(i=0, iMax=-1, dzMax=0.0, z=m_pGrid->asDouble(x, y), bEdge=false; i<8; i++) { if( !m_pGrid->is_InGrid(ix = Get_xTo(i, x), iy = Get_yTo(i, y)) ) { bEdge = true; } else if( dzMax < (dz = (m_bDown ? m_pGrid->asDouble(ix, iy) - z : z - m_pGrid->asDouble(ix, iy)) / Get_Length(i)) ) { dzMax = dz; iMax = i; } } //--------------------------------------------- m_Dir.Set_Value(x, y, iMax); if( iMax < 0 && (bEdge_Seeds || !bEdge) ) { int ID = m_pSeeds->Get_Count(); CSG_Shape *pSeed = m_pSeeds->Add_Shape(); pSeed->Set_Point(Get_System()->Get_Grid_to_World(x, y), 0); pSeed->Set_Value(SEED_X , x); pSeed->Set_Value(SEED_Y , y); pSeed->Set_Value(SEED_Z , z); pSeed->Set_Value(SEED_ID , ID); pSeed->Set_Value(SEED_JOIN , -1); m_pSegments->Set_Value(x, y, ID); } else { m_pSegments->Set_Value(x, y, -1); } } } } return( m_pSeeds->Get_Count() > 1 ); }
//--------------------------------------------------------- bool CAtlas_BNA_Export::On_Execute(void) { int iShape, iPart, iPoint, iName1, iName2; FILE *Stream; TSG_Point p; CSG_Points Pts; CSG_Shape *pShape; CSG_Shapes *pShapes; CSG_String fName; //----------------------------------------------------- pShapes = Parameters("SHAPES") ->asShapes(); fName = Parameters("FILE") ->asString(); iName1 = Parameters("PNAME") ->asInt(); iName2 = Parameters("SNAME") ->asInt(); //----------------------------------------------------- if( (Stream = fopen(fName.b_str(), "w")) != NULL ) { for(iShape=0; iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { pShape = pShapes->Get_Shape(iShape); switch( pShapes->Get_Type() ) { default: break; //--------------------------------------------- case SHAPE_TYPE_Point: fprintf(Stream, "\"%s\",\"%s\",%d\n", pShape->asString(iName1), pShape->asString(iName2), 1 ); p = pShape->Get_Point(0); fprintf(Stream, "%f,%f\n", p.x, p.y); break; //--------------------------------------------- case SHAPE_TYPE_Line: for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { fprintf(Stream, "\"%s\",\"%s\",%d\n", pShape->asString(iName1), pShape->asString(iName2), -pShape->Get_Point_Count(iPart) ); for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { p = pShape->Get_Point(iPoint, iPart); fprintf(Stream, "%f,%f\n", p.x, p.y); } } break; //--------------------------------------------- case SHAPE_TYPE_Polygon: if( pShape->Get_Part_Count() > 0 && pShape->Get_Point_Count(0) > 2 ) { Pts.Clear(); for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++) { Pts.Add(pShape->Get_Point(iPoint, iPart)); } if( iPart > 0 ) { Pts.Add(pShape->Get_Point(0, 0)); } } if( Pts.Get_Count() > 2 ) { fprintf(Stream, "\"%s\",\"%s\",%d\n", pShape->asString(iName1), pShape->asString(iName2), Pts.Get_Count() ); for(iPoint=0; iPoint<Pts.Get_Count(); iPoint++) { fprintf(Stream, "%f,%f\n", Pts[iPoint].x, Pts[iPoint].y); } } } break; } } fclose(Stream); return( true ); } //----------------------------------------------------- return( false ); }
//--------------------------------------------------------- bool CSG_Shapes::On_Update(void) { if( Get_Count() > 0 ) { CSG_Shape *pShape = Get_Shape(0); m_Extent = pShape->Get_Extent(); m_ZMin = pShape->Get_ZMin(); m_ZMax = pShape->Get_ZMax(); m_MMin = pShape->Get_MMin(); m_MMax = pShape->Get_MMax(); for(int i=1; i<Get_Count(); i++) { pShape = Get_Shape(i); m_Extent.Union(pShape->Get_Extent()); switch( m_Vertex_Type ) { case SG_VERTEX_TYPE_XYZM: if( m_MMin > pShape->Get_MMin() ) m_MMin = pShape->Get_MMin(); if( m_MMax < pShape->Get_MMax() ) m_MMax = pShape->Get_MMax(); case SG_VERTEX_TYPE_XYZ: if( m_ZMin > pShape->Get_ZMin() ) m_ZMin = pShape->Get_ZMin(); if( m_ZMax < pShape->Get_ZMax() ) m_ZMax = pShape->Get_ZMax(); break; default: break; } } } else { m_Extent.Assign(0.0, 0.0, 0.0, 0.0); } return( CSG_Table::On_Update() ); }
//--------------------------------------------------------- bool CAtlas_BNA_Import::On_Execute(void) { bool bOk; int iPoint, nPoints; double x, y; FILE *Stream; CSG_String FileName, sLine, sName1, sName2; CSG_Shape *pShape; CSG_Shapes *pPoints, *pLines, *pPolygons; //----------------------------------------------------- FileName = Parameters("FILE") ->asString(); //----------------------------------------------------- if( (Stream = fopen(FileName.b_str(), "r")) != NULL ) { pPoints = SG_Create_Shapes(SHAPE_TYPE_Point , SG_File_Get_Name(FileName, false)); pPoints ->Add_Field("NAME1" , SG_DATATYPE_String); pPoints ->Add_Field("NAME2" , SG_DATATYPE_String); pLines = SG_Create_Shapes(SHAPE_TYPE_Line , SG_File_Get_Name(FileName, false)); pLines ->Add_Field("NAME1" , SG_DATATYPE_String); pLines ->Add_Field("NAME2" , SG_DATATYPE_String); pPolygons = SG_Create_Shapes(SHAPE_TYPE_Polygon, SG_File_Get_Name(FileName, false)); pPolygons ->Add_Field("NAME1" , SG_DATATYPE_String); pPolygons ->Add_Field("NAME2" , SG_DATATYPE_String); //------------------------------------------------- bOk = true; while( bOk && SG_Read_Line(Stream, sLine) && Process_Get_Okay(true) ) { sName1 = sLine.AfterFirst('\"').BeforeFirst('\"'); sName2 = sLine.BeforeLast('\"').AfterLast('\"'); sLine = sLine.AfterLast('\"'); if( sLine.Find(',', true) >= 0 ) sLine = sLine.AfterLast(','); nPoints = sLine.asInt(); if( nPoints == 1 ) { pShape = pPoints ->Add_Shape(); } else if( nPoints < 0 ) { pShape = pLines ->Add_Shape(); nPoints = -nPoints; } else if( nPoints > 2 ) { pShape = pPolygons ->Add_Shape(); } else { bOk = false; } if( bOk ) { pShape->Set_Value(0, sName1); pShape->Set_Value(1, sName2); for(iPoint=0; iPoint<nPoints && bOk; iPoint++) { if( (bOk = SG_Read_Line(Stream, sLine)) == true ) { SG_SSCANF(sLine, SG_T("%lf %lf"), &x, &y); pShape->Add_Point(x, y); } } } } fclose(Stream); //------------------------------------------------- bOk = false; if( pPoints->is_Valid() && pPoints->Get_Count() > 0 ) { bOk = true; DataObject_Add(pPoints); } else { delete(pPoints); } if( pLines->is_Valid() && pLines->Get_Count() > 0 ) { bOk = true; DataObject_Add(pLines); } else { delete(pLines); } if( pPolygons->is_Valid() && pPolygons->Get_Count() > 0 ) { bOk = true; DataObject_Add(pPolygons); } else { delete(pPolygons); } return( bOk ); } //----------------------------------------------------- return( false ); }
//--------------------------------------------------------- bool CAdd_Polygon_Attributes::On_Execute(void) { //----------------------------------------------------- CSG_Shapes *pInput = Parameters("INPUT")->asShapes(); if( !pInput->is_Valid() ) { Error_Set(_TL("Invalid points layer.")); return( false ); } //----------------------------------------------------- CSG_Shapes *pPolygons = Parameters("POLYGONS")->asShapes(); if( !pPolygons->is_Valid() ) { Error_Set(_TL("Invalid polygon layer.")); return( false ); } //----------------------------------------------------- CSG_Parameter_Table_Fields *pFields = Parameters("FIELDS")->asTableFields(); if( pFields->Get_Count() == 0 ) { CSG_String sFields; for(int iField=0; iField<pPolygons->Get_Field_Count(); iField++) { sFields += CSG_String::Format(SG_T("%d,"), iField); } pFields->Set_Value(sFields); } //----------------------------------------------------- CSG_Shapes *pOutput = Parameters("OUTPUT")->asShapes(); if( pOutput && pOutput != pInput ) { pOutput->Create(*pInput); } else { Parameters("OUTPUT")->Set_Value(pOutput = pInput); } pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), pPolygons->Get_Name())); //----------------------------------------------------- int outField = pOutput->Get_Field_Count(); { for(int iField=0; iField<pFields->Get_Count(); iField++) { int jField = pFields->Get_Index(iField); pOutput->Add_Field(pPolygons->Get_Field_Name(jField), pPolygons->Get_Field_Type(jField)); } } //----------------------------------------------------- for(int iPoint=0; iPoint<pOutput->Get_Count() && Set_Progress(iPoint, pOutput->Get_Count()); iPoint++) { CSG_Shape *pPoint = pOutput ->Get_Shape(iPoint); CSG_Shape *pPolygon = pPolygons ->Get_Shape(pPoint->Get_Point(0)); if( pPolygon ) { for(int iField=0; iField<pFields->Get_Count(); iField++) { int jField = pFields->Get_Index(iField); switch( pPolygons->Get_Field_Type(jField) ) { case SG_DATATYPE_String: case SG_DATATYPE_Date: pPoint->Set_Value(outField + iField, pPolygon->asString(jField)); break; default: pPoint->Set_Value(outField + iField, pPolygon->asDouble(jField)); break; } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- CSG_Shape * CSG_PointCloud::_Set_Shape(int iPoint) { SG_UI_Progress_Lock(true); CSG_Shape *pShape = m_Shapes.Get_Shape(0); if( pShape->is_Modified() && m_Shapes_Index >= 0 && m_Shapes_Index < Get_Count() ) { m_Cursor = m_Points[m_Shapes_Index]; for(int i=0; i<Get_Field_Count(); i++) { switch( Get_Field_Type(i) ) { default: Set_Value(i, pShape->asDouble(i)); break; case SG_DATATYPE_Date: case SG_DATATYPE_String: Set_Value(i, pShape->asString(i)); break; } } Set_Value(0, pShape->Get_Point(0).x); Set_Value(1, pShape->Get_Point(0).y); Set_Value(2, pShape->Get_Z (0) ); } if( iPoint >= 0 && iPoint < Get_Count() ) { if( iPoint != m_Shapes_Index ) { m_Cursor = m_Points[iPoint]; pShape->Set_Point(Get_X(), Get_Y(), 0, 0); pShape->Set_Z (Get_Z() , 0, 0); for(int i=0; i<Get_Field_Count(); i++) { switch( Get_Field_Type(i) ) { default: pShape->Set_Value(i, Get_Value(i)); break; case SG_DATATYPE_Date: case SG_DATATYPE_String: { CSG_String s; Get_Value(i, s); pShape->Set_Value(i, s); } break; } } m_Shapes_Index = iPoint; } m_Shapes.Set_Modified(false); SG_UI_Progress_Lock(false); return( pShape ); } m_Shapes_Index = -1; SG_UI_Progress_Lock(false); return( NULL ); }
//--------------------------------------------------------- bool CShapes_Save::On_Execute(void) { if( !Get_Connection()->has_PostGIS() ) { Error_Set(_TL("not a valid PostGIS database!")); return( false ); } //----------------------------------------------------- CSG_Shapes *pShapes; CSG_String SQL, Name, Type, Field, SavePoint; pShapes = Parameters("SHAPES")->asShapes(); Name = Parameters("NAME" )->asString(); if( Name.Length() == 0 ) Name = pShapes->Get_Name(); Field = "geometry"; int SRID = Get_SRID(); //----------------------------------------------------- if( !CSG_Shapes_OGIS_Converter::from_ShapeType(Type, pShapes->Get_Type(), pShapes->Get_Vertex_Type()) ) { Error_Set(_TL("invalid or unsupported shape or vertex type")); return( false ); } //----------------------------------------------------- Get_Connection()->Begin(SavePoint = Get_Connection()->is_Transaction() ? "SHAPES_SAVE" : ""); //----------------------------------------------------- if( Get_Connection()->Table_Exists(Name) ) { Message_Add(_TL("table already exists") + CSG_String(": ") + Name); switch( Parameters("EXISTS")->asInt() ) { case 0: // abort export return( false ); case 1: // replace existing table Message_Add(_TL("trying to drop table") + CSG_String(": ") + Name); if( !Get_Connection()->Table_Drop(Name, false) ) { Message_Add(CSG_String(" ...") + _TL("failed") + "!"); return( false ); } break; case 2: // append records, if table structure allows break; } } //----------------------------------------------------- if( !Get_Connection()->Table_Exists(Name) ) { if( !Get_Connection()->Table_Create(Name, *pShapes, Get_Constraints(&Parameters, "SHAPES"), false) ) { Error_Set(_TL("could not create table")); Get_Connection()->Rollback(SavePoint); return( false ); } //------------------------------------------------- // SELECT AddGeometryColumn(<table_name>, <column_name>, <srid>, <type>, <dimension>) SQL.Printf(SG_T("SELECT AddGeometryColumn('%s', '%s', %d, '%s', %d)"), Name.c_str(), Field.c_str(), SRID, Type.c_str(), pShapes->Get_Vertex_Type() == SG_VERTEX_TYPE_XY ? 2 : pShapes->Get_Vertex_Type() == SG_VERTEX_TYPE_XYZ ? 3 : 4 ); if( !Get_Connection()->Execute(SQL) ) { Error_Set(_TL("could not create geometry field")); Get_Connection()->Rollback(SavePoint); return( false ); } } //----------------------------------------------------- bool bBinary = Get_Connection()->has_Version(9); int iShape, iField, nAdded; CSG_String Insert = "INSERT INTO \"" + Name + "\" (" + Field; for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { Insert += CSG_String(", ") + pShapes->Get_Field_Name(iField); } Insert += ") VALUES ("; //----------------------------------------------------- for(iShape=0, nAdded=0; iShape==nAdded && iShape<pShapes->Get_Count() && Set_Progress(iShape, pShapes->Get_Count()); iShape++) { CSG_Shape *pShape = pShapes->Get_Shape(iShape); if( pShape->is_Valid() ) { SQL = Insert; if( bBinary ) { CSG_Bytes WKB; CSG_Shapes_OGIS_Converter::to_WKBinary(pShape, WKB); SQL += "ST_GeomFromWKB(E'\\\\x" + WKB.toHexString() + CSG_String::Format("', %d)", SRID); } else { CSG_String WKT; CSG_Shapes_OGIS_Converter::to_WKText(pShape, WKT); SQL += "ST_GeomFromText('" + WKT + CSG_String::Format("', %d)", SRID); } for(iField=0; iField<pShapes->Get_Field_Count(); iField++) { CSG_String s = pShape->asString(iField); if( pShapes->Get_Field_Type(iField) == SG_DATATYPE_String ) { if( 1 ) { char *_s = NULL; if( s.to_ASCII(&_s) ) s = _s; SG_FREE_SAFE(_s); } s.Replace("'", "\""); s = "'" + s + "'"; } SQL += ", " + s; } SQL += ")"; if( Get_Connection()->Execute(SQL) ) { nAdded++; } else { Message_Add(CSG_String::Format("%s [%d/%d]", _TL("could not save shape"), 1 + iShape, pShapes->Get_Count())); } } } //----------------------------------------------------- if( nAdded < pShapes->Get_Count() ) { Message_Add(SQL); Get_Connection()->Rollback(SavePoint); return( false ); } Get_Connection()->Commit(SavePoint); Get_Connection()->GUI_Update(); Get_Connection()->Add_MetaData(*pShapes, Name); pShapes->Set_Modified(false); return( true ); }
//--------------------------------------------------------- bool CSearchInTable::On_Execute(void) { bool *WasSelected; int i, iMethod; CSG_String sExpression; CSG_Shapes *pShapes; pShapes = Parameters("SHAPES") ->asShapes(); sExpression = Parameters("EXPRESSION") ->asString(); iMethod = Parameters("METHOD") ->asInt(); //----------------------------------------------------- if( iMethod == METHOD_SELECT_FROM_SEL ) { WasSelected = new bool[pShapes->Get_Count()]; for(i=0; i<pShapes->Get_Count() && Set_Progress(i, pShapes->Get_Count()); i++) { WasSelected[i] = pShapes->Get_Record(i)->is_Selected(); } } if( iMethod != METHOD_ADD_TO_SEL ) { pShapes->Select(); } //----------------------------------------------------- std::vector <int> m_Selection; for(i=0; i<pShapes->Get_Count() && Set_Progress(i, pShapes->Get_Count()); i++) { CSG_Shape *pShape = pShapes->Get_Shape(i); for(int j=0; j<pShapes->Get_Field_Count(); j++) { CSG_String sValue = pShape->asString(j); if( sValue.Find(sExpression) != -1 ) { m_Selection.push_back(i); break; } } } //----------------------------------------------------- for(i=0; i<m_Selection.size() && Set_Progress(i, m_Selection.size()); i++) { int iSelection = m_Selection[i]; if( !pShapes->Get_Record(iSelection)->is_Selected() ) { if( iMethod != METHOD_SELECT_FROM_SEL || WasSelected[iSelection] ) { ((CSG_Table *)pShapes)->Select(iSelection, true); } } } //----------------------------------------------------- if( iMethod == METHOD_SELECT_FROM_SEL ) { delete(WasSelected); } Message_Add(CSG_String::Format(SG_T("%s: %d"), _TL("selected shapes"), m_Selection.size())); DataObject_Update(pShapes); return( true ); }
//--------------------------------------------------------- bool CDirect_Georeferencing_WorldFile::On_Execute(void) { //----------------------------------------------------- int nx = Parameters("NX")->asInt(); int ny = Parameters("NY")->asInt(); if( !m_Georeferencer.Set_Transformation(Parameters, nx, ny) ) { return( false ); } //----------------------------------------------------- CSG_String File = Parameters("FILE")->asString(); if( File.is_Empty() ) { return( false ); } //----------------------------------------------------- CSG_File Stream; if( !Stream.Open(File, SG_FILE_W, false) ) { return( false ); } //----------------------------------------------------- CSG_Matrix R(m_Georeferencer.Get_Transformation()); R *= 0.001 * Parameters("Z")->asDouble() / Parameters("CFL")->asDouble() * Parameters("PXSIZE")->asDouble(); TSG_Point p = m_Georeferencer.Image_to_World(0, ny); Stream.Printf(SG_T("%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n"), R[0][0], // A: pixel size in the x-direction in map units/pixel R[1][0], // D: rotation about y-axis -R[0][1], // B: rotation about x-axis -R[1][1], // E: pixel size in the y-direction in map units, almost always negative p.x, // X: top left pixel center p.y // Y: top left pixel center ); //----------------------------------------------------- CSG_Shapes *pExtents = Parameters("EXTENT")->asShapes(); if( pExtents ) { pExtents->Create(SHAPE_TYPE_Polygon, SG_File_Get_Name(File, false)); pExtents->Add_Field(_TL("NAME"), SG_DATATYPE_String); CSG_Shape *pExtent = pExtents->Add_Shape(); p = m_Georeferencer.Image_to_World( 0, 0); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World( 0, ny); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World(nx, ny); pExtent->Add_Point(p.x, p.y); p = m_Georeferencer.Image_to_World(nx, 0); pExtent->Add_Point(p.x, p.y); pExtent->Set_Value(0, SG_File_Get_Name(File, false)); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CWatersheds_ext::On_Execute(void) { int x, y; CSG_Grid *pBasins, *pSubBasins, Inflows; CSG_Shapes *pHeads, *pMouths, *pVBasins, *pVSubBasins; m_pDEM = Parameters("DEM") ->asGrid(); m_pChannels = Parameters("CHANNELS") ->asGrid(); pBasins = Parameters("BASINS") ->asGrid(); pSubBasins = Parameters("SUBBASINS") ->asGrid(); pVBasins = Parameters("V_BASINS") ->asShapes(); pVSubBasins = Parameters("V_SUBBASINS") ->asShapes(); pHeads = Parameters("HEADS") ->asShapes(); pMouths = Parameters("MOUTHS") ->asShapes(); //----------------------------------------------------- Inflows .Create(*Get_System(), SG_DATATYPE_Char); m_Direction .Create(*Get_System(), SG_DATATYPE_Char); m_Direction .Set_NoData_Value(-1); m_Distance .Create(*Get_System(), SG_DATATYPE_Float); m_Distance .Set_NoData_Value(-1); m_Distance .Assign_NoData(); pBasins ->Assign(0.0); pBasins ->Set_NoData_Value(0.0); pSubBasins ->Assign(0.0); pSubBasins ->Set_NoData_Value(0.0); pHeads ->Create(SHAPE_TYPE_Point , _TL("Heads")); pHeads ->Add_Field("ID" , SG_DATATYPE_Int); pHeads ->Add_Field("MAIN_ID" , SG_DATATYPE_Int); pHeads ->Add_Field("ELEVATION" , SG_DATATYPE_Double); pHeads ->Add_Field("DISTANCE" , SG_DATATYPE_Double); pMouths ->Create(SHAPE_TYPE_Point , _TL("Mouths")); pMouths ->Add_Field("ID" , SG_DATATYPE_Int); pMouths ->Add_Field("MAIN_ID" , SG_DATATYPE_Int); pMouths ->Add_Field("ELEVATION" , SG_DATATYPE_Double); pVBasins ->Create(SHAPE_TYPE_Polygon , _TL("Basins")); BASIN_ADD_FIELDS(pVBasins); pVSubBasins ->Create(SHAPE_TYPE_Polygon , _TL("Subbasins")); BASIN_ADD_FIELDS(pVSubBasins); //----------------------------------------------------- Process_Set_Text(_TL("flow directions...")); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { int Direction = -1; if( m_pDEM->is_InGrid(x, y) ) { double dMax = 0.0; for(int i=0; i<8; i++) { int ix = Get_xTo(i, x); int iy = Get_yTo(i, y); if( m_pDEM->is_InGrid(ix, iy) && !m_pChannels->is_NoData(ix, iy) ) { double dz = (m_pDEM->asDouble(x, y) - m_pDEM->asDouble(ix, iy)) / Get_Length(i); if( dMax < dz ) { dMax = dz; Direction = i; } } } if( !m_pChannels->is_NoData(x, y) ) { if( Direction >= 0 ) { int ix = Get_xTo(Direction, x); int iy = Get_yTo(Direction, y); if( m_pDEM->is_InGrid(ix, iy) ) { Inflows.Add_Value(ix, iy, 1); } } } else if( Direction < 0 ) { Direction = m_pDEM->Get_Gradient_NeighborDir(x, y); } } m_Direction.Set_Value(x, y, Direction); } } //----------------------------------------------------- Process_Set_Text(_TL("main basins...")); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( m_pChannels->is_InGrid(x, y) && is_Outlet(x, y) ) { Get_Basin(pBasins, pVBasins, x, y, -1); CSG_Shape *pMouth = pMouths->Add_Shape(); pMouth->Add_Point(Get_System()->Get_Grid_to_World(x, y)); pMouth->Set_Value(0, pVBasins->Get_Count()); // ID pMouth->Set_Value(1, pVBasins->Get_Count()); // MAIN_ID pMouth->Set_Value(2, m_pDEM->asDouble(x, y)); // ELEVATION } } } if( Parameters("DISTANCE")->asBool() ) { m_Distance.Set_Name(_TL("Flow Distance")); DataObject_Add(SG_Create_Grid(m_Distance)); } //----------------------------------------------------- Process_Set_Text(_TL("heads and mouths...")); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( m_pChannels->is_InGrid(x, y) ) { //----------------------------------------- if( Inflows.asInt(x, y) > 1 ) // mouth, linking channels of subcatchments { CSG_Shape *pMouth = pMouths->Add_Shape(); pMouth->Add_Point(Get_System()->Get_Grid_to_World(x, y)); pMouth->Set_Value(0, pMouths->Get_Count()); // ID pMouth->Set_Value(1, pBasins->asDouble(x, y)); // MAIN_ID pMouth->Set_Value(2, m_pDEM->asDouble(x, y)); // ELEVATION } //----------------------------------------- else if( Inflows.asInt(x, y) == 0 ) // head { CSG_Shape *pHead = pHeads->Add_Shape(); pHead->Add_Point(Get_System()->Get_Grid_to_World(x, y)); pHead->Set_Value(0, pHeads->Get_Count() + 1); pHead->Set_Value(1, m_Distance.asDouble(x, y)); } } } } //----------------------------------------------------- Process_Set_Text(_TL("subbasins...")); pMouths->Set_Index(1, TABLE_INDEX_Ascending, 2, TABLE_INDEX_Descending); for(int iMouth=0; iMouth<pMouths->Get_Count() && Set_Progress(iMouth, pMouths->Get_Count()); iMouth++) { CSG_Shape *pMouth = pMouths->Get_Shape_byIndex(iMouth); if( Get_System()->Get_World_to_Grid(x, y, pMouth->Get_Point(0)) ) { if( pMouth->asInt(0) == pMouth->asInt(1) ) { Get_Basin(pSubBasins, pVSubBasins, x, y, pMouth->asInt(1)); } else { for(int i=0; i<8; i++) { int ix = Get_xFrom(i, x); int iy = Get_yFrom(i, y); if( m_pChannels->is_InGrid(ix, iy) && m_Direction.asInt(ix, iy) == i ) { Get_Basin(pSubBasins, pVSubBasins, ix, iy, pMouth->asInt(1)); } } } } } if( Parameters("DISTANCE")->asBool() ) { m_Distance.Set_Name(_TL("Subbasin Flow Distance")); DataObject_Add(SG_Create_Grid(m_Distance)); } //----------------------------------------------------- m_Distance .Destroy(); m_Direction .Destroy(); return( true ); }