Skip to content

Commit

Permalink
rectify main.py (lauch by tile or folder), get_pointcloud_origin.py -…
Browse files Browse the repository at this point in the history
…> io.py
  • Loading branch information
mdupaysign authored and leavauchier committed Mar 14, 2024
1 parent 8f74170 commit 3d96548
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 49 deletions.
11 changes: 6 additions & 5 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ channels:
- conda-forge
dependencies:
- python==3.11.*
- black
- flake8
- isort
- pre-commit
- pip
- pytest
- gdal
- laspy
Expand All @@ -24,3 +19,9 @@ dependencies:
# --------- hydra configs --------- #
- hydra-core
- hydra-colorlog
# ----------- linting --------------- #
- black
- flake8
- isort
- pre-commit
- pip
8 changes: 8 additions & 0 deletions example_lidro_by_tile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# For lauching Mask Hydro
python -m lidro.main \
io.input_filename=LHD_FXX_0706_6627_PTS_C_LAMB93_IGN69_TEST.las \
io.input_dir=./data/pointcloud/ \
io.output_dir=./tmp/ \



56 changes: 28 additions & 28 deletions lidro/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
""" Main script for calculate Mask HYDRO 1
"""
import os
import logging
from pathlib import Path
from pyproj import CRS
import os

import hydra
from omegaconf import DictConfig
from pyproj import CRS

from lidro.vectors.convert_to_vector import create_hydro_vector_mask

Expand All @@ -20,50 +19,51 @@ def main(config: DictConfig):
Raises:
RuntimeError: Check have a las and an input directory
"""
"""
logging.basicConfig(level=logging.INFO)

# Check input/output files and folders
input_dir = config.io.input_dir
output_dir = config.io.output_dir

# Check pointcloud tile
initial_las_filename = config.io.input_filename

# Parameters for creating Mask Hydro
pixel_size = config.io.pixel_size
tile_size = config.io.tile_size
crs= CRS.from_user_input(config.io.srid)
crs = CRS.from_user_input(config.io.srid)
classe = config.filter.keep_classes

if input_dir is None:
raise RuntimeError(
"""In input you have to give a las, an input directory .
For more info run the same command by adding --help"""
)

# Create output folders
os.makedirs(output_dir, exist_ok=True)


# Lauch creating mask hydro by tiles
if os.path.isdir(input_dir):
for file in os.listdir(input_dir):
tilename, _ = os.path.splitext(file)
input_file = os.path.join(input_dir, f"{tilename}{_}")
output_file = os.path.join(output_dir, f"MaskHydro_{tilename}.GeoJSON")
if os.path.exists(input_file):
# Lauch the create Hydro's Mask
if initial_las_filename is None and input_dir is not None:
# Lauch creating mask hydro by tiles
if os.path.isdir(input_dir):
for file in os.listdir(input_dir):
tilename, _ = os.path.splitext(file)
input_file = os.path.join(input_dir, f"{tilename}{_}")
output_file = os.path.join(output_dir, f"MaskHydro_{tilename}.GeoJSON")
logging.info(f"\nCreate Mask Hydro 1 for tile : {tilename}")
create_hydro_vector_mask(input_file, output_file, pixel_size, tile_size, classe, crs)
else:
raise RuntimeError(
"""An input file(s) doesn't/don't exist.
else:
raise RuntimeError(
"""An input directory doesn't exist.
For more info run the same command by adding --help"""
)
)
elif initial_las_filename is not None and input_dir is not None:
tilename = os.path.splitext(initial_las_filename)[0]
# Lauch creating mask by one tile:
input_file = os.path.join(input_dir, initial_las_filename)
output_file = os.path.join(output_dir, f"MaskHydro_{tilename}.GeoJSON")
logging.info(f"\nCreate Mask Hydro 1 for tile : {tilename}")
create_hydro_vector_mask(input_file, output_file, pixel_size, tile_size, classe, crs)
else:
raise RuntimeError(
"""An input directory doesn't exist.
"""In input you haven't to give a las, an input directory.
For more info run the same command by adding --help"""
)


if __name__ == "__main__":
main()
main()
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
""" extract coordinates the original tiles without buffer """
import numpy as np


def get_pointcloud_origin(points: np.array, tile_size: int = 1000, buffer_size: float = 0):
# Extract coordinates xmin, xmax, ymin and ymax of the original tile without buffer
x_min, y_min = np.min(points[:, :2], axis=0) + buffer_size
Expand All @@ -16,4 +17,4 @@ def get_pointcloud_origin(points: np.array, tile_size: int = 1000, buffer_size:
origin_y = np.ceil(y_max / tile_size) * tile_size # round top
return origin_x, origin_y
else:
raise ValueError(f"Extents (diff_x={diff_x} and diff_y={diff_y}) is bigger than tile_size ({tile_size}).")
raise ValueError(f"Extents (diff_x={diff_x} and diff_y={diff_y}) is bigger than tile_size ({tile_size}).")
30 changes: 15 additions & 15 deletions lidro/rasters/create_mask_raster.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# -*- coding: utf-8 -*-
""" Create mask non hydro from pointcloud filtering
"""
from typing import Tuple, List
from typing import List, Tuple

import numpy as np
import scipy.ndimage

from lidro.pointcloud.read_las import read_pointcloud
from lidro.pointcloud.filter_las import filter_pointcloud
from lidro.utils.get_pointcloud_origin import get_pointcloud_origin

from lidro.pointcloud.io import get_pointcloud_origin
from lidro.pointcloud.read_las import read_pointcloud


def create_occupancy_map(points: np.array, tile_size: int, pixel_size: float, origin: Tuple[int, int]):
"""Convert a numpy.array to a binary array
"""Create a binary image to extract water surfaces
Args:
points (np.array): array from pointcloud
tile_size (int): size of the raster grid (in meters)
pixel_size (float): distance between each node of the raster grid (in meters)
origin (Tuple[int, int]): spatial coordinate of the upper-left corner of the raster
(center of the upper-left pixel)
origin (Tuple[int, int]):
Returns:
bins (np.array): bins
Expand All @@ -30,11 +29,12 @@ def create_occupancy_map(points: np.array, tile_size: int, pixel_size: float, or
_bins, _, _ = np.histogram2d(points[:, 1], points[:, 0], bins=[bins_y, bins_x])
bins = np.flipud(_bins)
bins = np.where(bins > 0, 0, 1)

return bins


def detect_hydro_by_tile(filename: str, tile_size: int, pixel_size: float, classes: List[int]):
""""Detect hydrographic surfaces by LIDAR tile
""" "Detect hydrographic surfaces by tile origin extracted from the point cloud
Args:
filename (str): input pointcloud
Expand All @@ -46,20 +46,20 @@ def detect_hydro_by_tile(filename: str, tile_size: int, pixel_size: float, class
Returns:
bins (np.array): array from pointcloud
pcd_origin (list): extract origin from pointcloud
"""
"""
# Read pointcloud, and extract coordinates (X, Y, Z, and classification) of all points
array, crs = read_pointcloud(filename)

# Extracts parameters for binarisation
pcd_origin = get_pointcloud_origin(array, tile_size)
pcd_origin = get_pointcloud_origin(array, tile_size)

# Filter pointcloud by classes
array_filter = filter_pointcloud(array, classes)

# create occupancy map (2D)
_bins = create_occupancy_map(array_filter, tile_size, pixel_size, pcd_origin)

# Apply a mathematical morphology operations: clonsing
closing_bins = scipy.ndimage.binary_closing(_bins, structure=np.ones((5,5))).astype(np.uint8)
# Apply a mathematical morphology operations: clonsing
closing_bins = scipy.ndimage.binary_closing(_bins, structure=np.ones((5, 5))).astype(np.uint8)

return closing_bins, pcd_origin
return closing_bins, pcd_origin

0 comments on commit 3d96548

Please sign in to comment.