コード例 #1
0
//---------------------------------------------------------
bool CPointCloud_Create_Tileshape_From_SPCVF::On_Execute(void)
{
	CSG_String		sFileName;
	CSG_Shapes		*pShapes;

	CSG_MetaData	SPCVF;
	CSG_String		sPathSPCVF, sFilePath;
	int				iPoints;
	double			dBBoxXMin, dBBoxYMin, dBBoxXMax, dBBoxYMax;


	//-----------------------------------------------------
	sFileName	= Parameters("FILENAME")->asString();
	pShapes		= Parameters("TILE_SHP")->asShapes();
	

	//-----------------------------------------------------
	if( !SPCVF.Create(sFileName) || SPCVF.Get_Name().CmpNoCase(SG_T("SPCVFDataset")) )
	{
		SG_UI_Msg_Add_Error(_TL("Please provide a valid *.scpvf file!"));
		return( false );
	}


	//-----------------------------------------------------
	CSG_String	sMethodPaths;
	SPCVF.Get_Property(SG_T("Paths"), sMethodPaths);

	if( !sMethodPaths.CmpNoCase(SG_T("absolute")) )
	{
		sPathSPCVF = SG_T("");
	}
	else if( !sMethodPaths.CmpNoCase(SG_T("relative")) )
	{
		sPathSPCVF = SG_File_Get_Path(sFileName);
		sPathSPCVF.Replace(SG_T("\\"), SG_T("/"));
	}
	else
	{
		SG_UI_Msg_Add_Error(_TL("Encountered invalid path description in *.spcvf file!"));
		return( false );
	}


	//-----------------------------------------------------
	pShapes->Destroy();

	pShapes->Add_Field(_TL("ID"),		SG_DATATYPE_Int);
	pShapes->Add_Field(_TL("Filepath"),	SG_DATATYPE_String);
	pShapes->Add_Field(_TL("File"),		SG_DATATYPE_String);
	pShapes->Add_Field(_TL("Points"),	SG_DATATYPE_Int);

	pShapes->Set_Name(CSG_String::Format(_TL("Tileshape_%s"), SG_File_Get_Name(sFileName, false).c_str()));


	//-----------------------------------------------------
	CSG_MetaData	*pDatasets = SPCVF.Get_Child(SG_T("Datasets"));

	for(int i=0; i<pDatasets->Get_Children_Count(); i++)
	{
		CSG_MetaData	*pDataset	= pDatasets->Get_Child(i);
		CSG_MetaData	*pBBox		= pDataset->Get_Child(SG_T("BBox"));

		pDataset->Get_Property(SG_T("File"), sFilePath);

		pDataset->Get_Property(SG_T("Points"), iPoints);

		pBBox->Get_Property(SG_T("XMin"), dBBoxXMin);
		pBBox->Get_Property(SG_T("YMin"), dBBoxYMin);
		pBBox->Get_Property(SG_T("XMax"), dBBoxXMax);
		pBBox->Get_Property(SG_T("YMax"), dBBoxYMax);

		//-----------------------------------------------------
		CSG_Shape	*pShape = pShapes->Add_Shape();

		pShape->Add_Point(dBBoxXMin, dBBoxYMin);
		pShape->Add_Point(dBBoxXMin, dBBoxYMax);
		pShape->Add_Point(dBBoxXMax, dBBoxYMax);
		pShape->Add_Point(dBBoxXMax, dBBoxYMin);

		pShape->Set_Value(0, i + 1);

		if( sPathSPCVF.Length() == 0 )	// absolute paths
		{
			pShape->Set_Value(1, sFilePath.BeforeLast('/'));
			pShape->Set_Value(2, sFilePath.AfterLast('/'));
		}
		else							// relative paths
		{
			pShape->Set_Value(1, sPathSPCVF);
			pShape->Set_Value(2, sFilePath);
		}

		pShape->Set_Value(3, iPoints);
	}


	//-----------------------------------------------------
	return( true );
}
コード例 #2
0
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,
                                 &reg_first,
                                 &reg_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 (&reg_first, &reg_last);

	return( true );
}