//--------------------------------------------------------- bool SG_Get_Crossing(TSG_Point &Crossing, const TSG_Point &a1, const TSG_Point &a2, const TSG_Point &b1, const TSG_Point &b2, bool bExactMatch) { //----------------------------------------------------- if( bExactMatch && ( (M_GET_MAX(a1.x, a2.x) < M_GET_MIN(b1.x, b2.x)) || (M_GET_MIN(a1.x, a2.x) > M_GET_MAX(b1.x, b2.x)) || (M_GET_MAX(a1.y, a2.y) < M_GET_MIN(b1.y, b2.y)) || (M_GET_MIN(a1.y, a2.y) > M_GET_MAX(b1.y, b2.y)) ) ) { return( false ); } //----------------------------------------------------- if( (a1.x == b1.x && a1.y == b1.y) || (a1.x == b2.x && a1.y == b2.y) ) { Crossing = a1; return( true ); } if( (a2.x == b1.x && a2.y == b1.y) || (a2.x == b2.x && a2.y == b2.y) ) { Crossing = a2; return( true ); } //----------------------------------------------------- double lambda, div, a_dx, a_dy, b_dx, b_dy; a_dx = a2.x - a1.x; a_dy = a2.y - a1.y; b_dx = b2.x - b1.x; b_dy = b2.y - b1.y; if( (div = a_dx * b_dy - b_dx * a_dy) != 0.0 ) { lambda = ((b1.x - a1.x) * b_dy - b_dx * (b1.y - a1.y)) / div; Crossing.x = a1.x + lambda * a_dx; Crossing.y = a1.y + lambda * a_dy; if( !bExactMatch ) { return( true ); } else if( 0.0 <= lambda && lambda <= 1.0 ) { lambda = ((b1.x - a1.x) * a_dy - a_dx * (b1.y - a1.y)) / div; if( 0.0 <= lambda && lambda <= 1.0 ) { return( true ); } } } return( false ); }
//--------------------------------------------------------- inline bool CFlow_by_Slope::Get_Decay(int x, int y, double &Decay) { double d; if( !m_pDEM->Get_Gradient(x, y, d, Decay) ) { return( false ); } if( d < m_Slope_Min ) { d = m_Slope_Min; } Decay = Get_Fuzzy(d, 0.0, m_Slope_Max); if( m_Flow_Min > 0.0 ) { d = Get_Fuzzy(m_pFlow->asDouble(x, y), m_Flow_Min, m_Flow_Max); switch( 1 ) { default: Decay = M_GET_MAX(Decay, d); break; case 1: Decay = Decay + d - Decay * d; break; } } return( Decay > 0.0 ); }
//--------------------------------------------------------- bool CPointCloud_From_Text_File::On_Execute(void) { CSG_String fileName; int iField, iType; TSG_Data_Type Type; CSG_String Name, Types, s; CSG_PointCloud *pPoints; CSG_Parameters P; CSG_Parameter *pNode; int xField, yField, zField, nAttribs, max_iField; bool bSkipHeader; char fieldSep; std::vector<int> vCol; std::ifstream tabStream; std::string tabLine; double lines; long cntPt, cntInvalid; double x, y, z, value; //----------------------------------------------------- fileName = Parameters("FILE") ->asString(); xField = Parameters("XFIELD") ->asInt() - 1; yField = Parameters("YFIELD") ->asInt() - 1; zField = Parameters("ZFIELD") ->asInt() - 1; nAttribs = Parameters("ATTRIBS") ->asInt(); bSkipHeader = Parameters("SKIP_HEADER") ->asBool(); switch (Parameters("FIELDSEP")->asInt()) { default: case 0: fieldSep = '\t'; break; case 1: fieldSep = ' '; break; case 2: fieldSep = ','; break; } Types.Printf(SG_T("%s|%s|%s|%s|%s|"), _TL("1 byte integer"), _TL("2 byte integer"), _TL("4 byte integer"), _TL("4 byte floating point"), _TL("8 byte floating point") ); P.Set_Name(_TL("Attribute Field Properties")); for(iField=1; iField<=nAttribs; iField++) { s.Printf(SG_T("NODE_%03d") , iField); pNode = P.Add_Node(NULL, s, CSG_String::Format(SG_T("%d. %s"), iField, _TL("Field")), _TL("")); s.Printf(SG_T("FIELD_%03d"), iField); P.Add_String(pNode, s, _TL("Name"), _TL(""), s); s.Printf(SG_T("COLUMN_%03d"), iField); P.Add_Value(pNode, s, _TL("Attribute is Column ..."), _TL(""), PARAMETER_TYPE_Int, iField+3, 1, true); s.Printf(SG_T("TYPE_%03d") , iField); P.Add_Choice(pNode, s, _TL("Type"), _TL(""), Types, 3); } //----------------------------------------------------- if( nAttribs > 0 ) { if( Dlg_Parameters(&P, _TL("Field Properties")) ) { pPoints = SG_Create_PointCloud(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); for(iField=0; iField<nAttribs; iField++) { Name = P(CSG_String::Format(SG_T("FIELD_%03d" ), iField + 1).c_str())->asString(); iType = P(CSG_String::Format(SG_T("TYPE_%03d" ), iField + 1).c_str())->asInt(); vCol.push_back(P(CSG_String::Format(SG_T("COLUMN_%03d"), iField + 1).c_str())->asInt() - 1); switch( iType ) { default: case 0: Type = SG_DATATYPE_Char; break; case 1: Type = SG_DATATYPE_Short; break; case 2: Type = SG_DATATYPE_Int; break; case 3: Type = SG_DATATYPE_Float; break; case 4: Type = SG_DATATYPE_Double; break; } pPoints->Add_Field(Name, Type); } } else return( false ); } else { pPoints = SG_Create_PointCloud(); pPoints->Create(); pPoints->Set_Name(SG_File_Get_Name(fileName, false)); Parameters("POINTS")->Set_Value(pPoints); } max_iField = M_GET_MAX(xField, yField); max_iField = M_GET_MAX(max_iField, zField); for( unsigned int i=0; i<vCol.size(); i++ ) { if( max_iField < vCol.at(i) ) max_iField = vCol.at(i); } // open input stream //--------------------------------------------------------- tabStream.open(fileName.b_str(), std::ifstream::in); if( !tabStream ) { SG_UI_Msg_Add_Error(CSG_String::Format(_TL("Unable to open input file!"))); return (false); } tabStream.seekg(0, std::ios::end); // get length of file lines = (double)tabStream.tellg(); tabStream.seekg(0, std::ios::beg); std::getline(tabStream, tabLine); // as a workaround we assume the number of lines from the length of the first line lines = lines / (double)tabStream.tellg(); if( !bSkipHeader ) { tabStream.clear(); // let's forget we may have reached the EOF tabStream.seekg(0, std::ios::beg); // and rewind to the beginning } // import //--------------------------------------------------------- cntPt = cntInvalid = 0; SG_UI_Process_Set_Text(CSG_String::Format(_TL("Importing data ..."))); while( std::getline(tabStream, tabLine) ) { std::istringstream stream(tabLine); std::vector<std::string> tabCols; std::string tabEntry; if( cntPt%10000 == 0 ) SG_UI_Process_Set_Progress((double)cntPt, lines); cntPt++; while( std::getline(stream, tabEntry, fieldSep) ) // read every column in this line and fill vector { if (tabEntry.length() == 0) continue; tabCols.push_back(tabEntry); } if ((int)tabCols.size() < max_iField - 1 ) { SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipping misformatted line (%.0f)!"), cntPt), true); cntInvalid++; continue; } x = strtod(tabCols[xField].c_str(), NULL); y = strtod(tabCols[yField].c_str(), NULL); z = strtod(tabCols[zField].c_str(), NULL); pPoints->Add_Point(x, y, z); for( int i=0; i<nAttribs; i++ ) { value = strtod(tabCols.at(vCol.at(i)).c_str(), NULL); pPoints->Set_Attribute(i, value); } } // finalize //--------------------------------------------------------- tabStream.close(); CSG_Parameters sParms; DataObject_Get_Parameters(pPoints, sParms); if (sParms("COLORS_ATTRIB") && sParms("COLORS_TYPE") && sParms("METRIC_COLORS") && sParms("METRIC_ZRANGE") && sParms("COLORS_AGGREGATE")) { sParms("COLORS_AGGREGATE")->Set_Value(3); // highest z sParms("COLORS_TYPE")->Set_Value(2); // graduated color sParms("METRIC_COLORS")->asColors()->Set_Count(255); // number of colors sParms("COLORS_ATTRIB")->Set_Value(2); // z attrib sParms("METRIC_ZRANGE")->asRange()->Set_Range(pPoints->Get_Minimum(2),pPoints->Get_Maximum(2)); DataObject_Set_Parameters(pPoints, sParms); DataObject_Update(pPoints); } if (cntInvalid > 0) SG_UI_Msg_Add(CSG_String::Format(_TL("WARNING: Skipped %d invalid points!"), cntInvalid), true); SG_UI_Msg_Add(CSG_String::Format(_TL("%d points sucessfully imported."), (cntPt-cntInvalid)), true); return( true ); }
bool CGrid_Proximity_Buffer::On_Execute(void){ CSG_Grid *pSource, *pDistance, *pAlloc, *pBuffer; double dBufDist, dDist, cellSize; int x, y, i, j, imin, imax, jmin, jmax, iBufDist, alloc, ival; pSource = Parameters("SOURCE")->asGrid(); pDistance = Parameters("DISTANCE")->asGrid(); pAlloc = Parameters("ALLOC")->asGrid(); pBuffer = Parameters("BUFFER")->asGrid(); ival = Parameters("IVAL")->asInt(); cellSize = pSource->Get_Cellsize(); dBufDist = Parameters("DIST")->asDouble() / cellSize; iBufDist = (int) (dBufDist + 2.0); dBufDist = pow(dBufDist, 2); pDistance->Assign_NoData(); pAlloc->Assign_NoData(); pBuffer->Assign_NoData(); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pSource->is_NoData(x, y) ) { alloc = pSource->asInt(x, y); pAlloc->Set_Value(x, y, alloc); pDistance->Set_Value(x, y, 0.0); imin = M_GET_MAX(0, x-iBufDist); imax = M_GET_MIN(x+iBufDist, Get_NX()); jmin = M_GET_MAX(0, y-iBufDist); jmax = M_GET_MIN(y+iBufDist, Get_NY()); for(i=imin; i<imax; i++) { for(j=jmin; j<jmax; j++) { if( pSource->is_NoData(i, j) ) { dDist= (x-i)*(x-i)+(y-j)*(y-j); if( dDist <= dBufDist && (pDistance->is_NoData(i, j) || pDistance->asDouble(i, j) > dDist) ) { pDistance->Set_Value(i, j, dDist); pAlloc->Set_Value(i, j, alloc); } } }//for }//for }//if }//for }//for for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pDistance->is_NoData(x, y) ) { dDist = sqrt(pDistance->asDouble(x, y)) * cellSize; pDistance->Set_Value(x, y, dDist); i = 0; while( i< dDist ) i += ival; pBuffer->Set_Value(x, y, i); } } } return( true ); }