Skip to content

Commit

Permalink
fix grid decimation (code and test) + add gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
alavenant committed Dec 18, 2023
1 parent 7afe71f commit ad89bca
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
./xcode
./install
__pycache__
./test/__pycache__
28 changes: 28 additions & 0 deletions ci/launch_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/sh

FILE=~/anaconda3/etc/profile.d/conda.sh
if [ -e ~/anaconda3/etc/profile.d/conda.sh ]
then
source ~/anaconda3/etc/profile.d/conda.sh
elif [ -e ~/miniconda3/etc/profile.d/conda.sh ]
then
source ~/miniconda3/etc/profile.d/conda.sh
elif [ -e ~/miniforge3/etc/profile.d/conda.sh ]
then
source ~/miniforge3/etc/profile.d/conda.sh
elif [[ -z "${CONDASH}" ]]; then
echo ERROR: Failed to load conda.sh : ~/anaconda3/etc/profile.d/conda.sh or ~/miniforge3/etc/profile.d/conda.sh or env CONDASH
exit 1 # terminate and indicate error
else
echo DEBUG "$FILE does not exist, using env CONDASH."
source $CONDASH
fi

conda activate pdal_ign_plugin

export PDAL_DRIVER_PATH=./install/lib
echo $PDAL_DRIVER_PATH

python -m pytest

conda deactivate
5 changes: 4 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ dependencies:
- pdal
- python-pdal
- pytest
- pytest-dependency
- black
- isort
- shapely
- pip:
- ign-pdal-tools


27 changes: 21 additions & 6 deletions src/filter_grid_decimation/grid_decimationFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include <pdal/PointView.hpp>
#include <pdal/StageFactory.hpp>

#include <pdal/private/gdal/GDALUtils.hpp>

#include <sstream>
#include <cstdarg>

Expand All @@ -25,7 +23,7 @@ static StaticPluginInfo const s_info
"",
};

CREATE_STATIC_STAGE(GridDecimationFilter, s_info)
CREATE_SHARED_STAGE(GridDecimationFilter, s_info)

std::string GridDecimationFilter::getName() const { return s_info.name; }

Expand All @@ -42,6 +40,8 @@ void GridDecimationFilter::addArgs(ProgramArgs& args)
args.add("resolution", "Cell edge size, in units of X/Y",m_args->m_edgeLength, 1.);
args.add("output_type", "Point keept into the cells ('min', 'max')", m_args->m_methodKeep, "max" );
args.add("output_name_attribut", "Name of the add attribut", m_args->m_nameAddAttribut, "grid" );
args.add("output_wkt", "Export the grid as wkt", m_args->m_nameWktgrid, "" );

}

void GridDecimationFilter::initialize()
Expand Down Expand Up @@ -116,17 +116,32 @@ void GridDecimationFilter::createGrid(BOX2D bounds)
int width = static_cast<int>(d_width);
int height = static_cast<int>(d_height);

for (size_t l(0); l<d_height; l++)
for (size_t c(0); c<d_width; c++)
std::vector<Polygon> vgrid;

for (size_t l(0); l<height; l++)
for (size_t c(0); c<width; c++)
{
BOX2D bounds_dalle ( bounds.minx + c*m_args->m_edgeLength, bounds.miny + l*m_args->m_edgeLength,
bounds.minx + (c+1)*m_args->m_edgeLength, bounds.miny + (l+1)*m_args->m_edgeLength );
vgrid.push_back(Polygon(bounds_dalle));
this->grid.insert( std::make_pair( std::make_pair(c,l), -1) );
}

if (!m_args->m_nameWktgrid.empty())
{
std::ofstream oss (m_args->m_nameWktgrid);
for (auto pol : vgrid)
oss << pol.wkt() << std::endl;
}

}

PointViewSet GridDecimationFilter::run(PointViewPtr view)
{
BOX2D bounds;
view->calculateBounds(bounds);
createGrid(bounds);

for (PointId i = 0; i < view->size(); ++i)
{
PointRef point = view->point(i);
Expand Down
1 change: 1 addition & 0 deletions src/filter_grid_decimation/grid_decimationFilter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class PDAL_DLL GridDecimationFilter : public Filter
std::string m_methodKeep; // type of output (min, max)
double m_edgeLength; // lenght of grid
std::string m_nameAddAttribut; // name of the new attribut
std::string m_nameWktgrid; // export wkt grid
Dimension::Id m_dim;
};

Expand Down
66 changes: 45 additions & 21 deletions test/test_grid_decimation.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
import pytest
import csv
import json
import pdal
import math
import os
import tempfile

from test import utils

import pdal
import pdaltools.las_info as li
import pytest
import shapely


def test_grid_decimation():
ini_las = "test/data/4_6.las"
resolution = 10

tmp_out_las = tempfile.NamedTemporaryFile(suffix='.las').name
tmp_out_las = tempfile.NamedTemporaryFile(suffix=".las").name
tmp_out_wkt = tempfile.NamedTemporaryFile(suffix=".wkt").name

filter = 'filters.grid_decimation'
filter = "filters.grid_decimation"
utils.pdal_has_plugin(filter)

bounds = li.las_get_xy_bounds(ini_las)

d_width = math.floor((bounds[0][1] - bounds[0][0]) / resolution) + 1
d_height = math.floor((bounds[1][1] - bounds[1][0]) / resolution) + 1
nb_dalle = d_width * d_height
print("size of the grid", nb_dalle)

PIPELINE = [
{"type": "readers.las", "filename": ini_las},
{
"type":"readers.las",
"filename":"test/data/4_6.las"
"type": filter,
"resolution": resolution,
"output_type": "max",
"output_name_attribut": "grid",
"output_wkt": tmp_out_wkt,
},
{
"type":filter,
"resolution":1,
"output_type":"max",
"output_name_attribut":"grid"
"type": "writers.las",
"extra_dims": "all",
"filename": tmp_out_las,
"where": "grid==1",
},
{
"type":"writers.las",
"extra_dims":"all",
"filename":tmp_out_las
}
]

pipeline = pdal.Pipeline(json.dumps(PIPELINE))

# execute the pipeline
pipeline.execute()
arrays = pipeline.arrays
array = arrays[0]

nb_pts_grid = 0
for pt in array:
if pt["grid"] > 0:
nb_pts_grid += 1

assert nb_pts_grid == 3196
assert nb_pts_grid <= nb_dalle

NbPtsGrid = 0
for pt in arrays:
if pt[grid] > 0:
NbPtsGrid += 1
data = []
with open(tmp_out_wkt, "r") as f:
reader = csv.reader(f, delimiter="\t")
for i, line in enumerate(reader):
data.append(line[0])

assert nbThreadPts == 65067
assert len(data) == nb_dalle
9 changes: 5 additions & 4 deletions test/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import pdal
import os
import subprocess

import pdal


def pdal_has_plugin(name_filter):
os.environ["PDAL_DRIVER_PATH"] = os.path.abspath('./install/lib')
print("init pdal driver : ", os.environ["PDAL_DRIVER_PATH"])
result = subprocess.run(['pdal', '--drivers'], stdout=subprocess.PIPE)
if name_filter not in result.stdout.decode('utf-8'):
result = subprocess.run(["pdal", "--drivers"], stdout=subprocess.PIPE)
if name_filter not in result.stdout.decode("utf-8"):
raise ValueError("le script " + name_filter + " n'est pas visible")
2 changes: 1 addition & 1 deletion test/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


if __name__ == "__main__":
print(__version__)
print(__version__)

0 comments on commit ad89bca

Please sign in to comment.