-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from IGNF/regression_model_from_skeleton
Regression model from skeleton
- Loading branch information
Showing
36 changed files
with
1,781 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule data
updated
from 19f969 to 36a33e
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions
46
lidro/create_virtual_point/pointcloud/convert_list_points_to_las.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
""" this function convert GeoDataframe "virtual points" to LIDAR points | ||
""" | ||
import logging | ||
import os | ||
from typing import List | ||
|
||
import laspy | ||
import numpy as np | ||
import pandas as pd | ||
from shapely.geometry import Point | ||
|
||
|
||
def list_points_to_las(virtual_points: List[Point], output_dir: str, crs: str, virtual_points_classes: int): | ||
"""This function convert virtual points (List of virtuals points) to LIDAR points | ||
with classification for virtual points | ||
Args: | ||
virtual_points (List[Point]): A list containing virtuals points by hydrological entity | ||
output_dir (str): folder to output LAS | ||
crs (str): a pyproj CRS object used to create the output GeoJSON file | ||
virtual_points_classes (int): The classification value to assign to those virtual points | ||
""" | ||
# Combine all virtual points into a single GeoDataFrame | ||
grid_with_z = pd.concat(virtual_points, ignore_index=True) | ||
|
||
# Create a LAS file with laspy | ||
header = laspy.LasHeader(point_format=6, version="1.4") | ||
|
||
# Create a LAS file with laspy and add the VLR for CRS | ||
las = laspy.LasData(header) | ||
las.x = grid_with_z.geometry.x | ||
las.y = grid_with_z.geometry.y | ||
las.z = grid_with_z.geometry.z | ||
las.classification = virtual_points_classes * np.ones(len(grid_with_z.index)) | ||
|
||
# Add CRS information to the VLR | ||
vlr = laspy.vlrs.known.WktCoordinateSystemVlr(crs.to_wkt()) | ||
las.vlrs.append(vlr) | ||
|
||
# Save the LAS file | ||
output_laz = os.path.join(output_dir, "virtual_points.laz") | ||
with laspy.open(output_laz, mode="w", header=las.header, do_compress=True) as writer: | ||
writer.write_points(las.points) | ||
|
||
logging.info(f"Virtual points LAS file saved to {output_laz}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Apply Z from grid | ||
""" | ||
import geopandas as gpd | ||
from shapely import line_locate_point | ||
|
||
|
||
def calculate_grid_z_with_model(points: gpd.GeoDataFrame, line: gpd.GeoDataFrame, model) -> gpd.GeoDataFrame: | ||
"""Calculate Z with regression model. | ||
If points are not on the line, these points will be projected on this line | ||
Args: | ||
points (gpd.GeoDataFrame): A GeoDataFrame containing the grid points | ||
line (gpd.GeoDataFrame): A GeoDataFrame containing each line from Hydro's Skeleton | ||
model (dict): A dictionary representing the regression model | ||
Returns: | ||
gpd.GeoDataFrame: A GeoDataFrame of initial points, but with a Z. | ||
""" | ||
# Calculate curvilinear abscises for all points of the grid | ||
curvilinear_abs = line_locate_point(line.loc[0, "geometry"], points["geometry"].array, normalized=False) | ||
|
||
# Prediction of Z values using the regression model | ||
# Its possible to use non-linear models for prediction | ||
predicted_z = model(curvilinear_abs) | ||
|
||
# Generate a new geodataframe, with 3D points | ||
grid_with_z = calculate_grid_z(points, predicted_z) | ||
|
||
return grid_with_z | ||
|
||
|
||
def calculate_grid_z(points: gpd.GeoDataFrame, predicted_z: float) -> gpd.GeoDataFrame: | ||
"""Calculate Z grid | ||
Args: | ||
points (gpd.GeoDataFrame): A GeoDataFrame containing the grid points | ||
predicted_z (float): predicted Z for river | ||
Returns: | ||
gpd.GeoDataFrame: A GeoDataFrame of initial points, but with a Z. | ||
""" | ||
# Generate a new geodataframe, with 3D points | ||
grid_with_z = gpd.GeoDataFrame( | ||
geometry=gpd.GeoSeries().from_xy(points.geometry.x, points.geometry.y, predicted_z), crs=points.crs | ||
) | ||
|
||
return grid_with_z |
46 changes: 46 additions & 0 deletions
46
lidro/create_virtual_point/vectors/create_grid_2D_inside_maskhydro.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Create a regula 2D grid | ||
""" | ||
import geopandas as gpd | ||
import numpy as np | ||
import shapely.vectorized | ||
|
||
|
||
def generate_grid_from_geojson(mask_hydro: gpd.GeoDataFrame, spacing: float = 0.5, margin: float = 0): | ||
""" | ||
Generates a regular 2D grid of evenly spaced points within a polygon defined | ||
in a GeoJSON file. | ||
Args: | ||
mask_hydro (gpd.GeoDataFrame): A GeoDataFrame containing each mask hydro from Hydro's Skeleton | ||
spacing (float, optional): Spacing between the grid points in meters. The default value is 0.5 meter. | ||
margin (int, optional): Margin around mask for grid creation. The default value is 0 meter. | ||
Returns: | ||
geopandas.GeoDataFrame: A GeoDataFrame containing the grid points within the polygon. | ||
""" | ||
# Extract the polygon | ||
polygon = mask_hydro.geometry.unary_union # Combine all polygons into a single shape if there are multiple | ||
|
||
if margin: | ||
polygon = polygon.buffer(margin) | ||
|
||
# Get the bounds of the polygon | ||
minx, miny, maxx, maxy = polygon.bounds | ||
|
||
# Generate the grid points | ||
x_points = np.arange(minx, maxx, spacing) | ||
y_points = np.arange(miny, maxy, spacing) | ||
grid_points = np.meshgrid(x_points, y_points) | ||
|
||
# Filter points that are within the polygon | ||
grid_points_in_polygon = shapely.vectorized.contains(polygon, *grid_points) | ||
|
||
filtred_x = grid_points[0][grid_points_in_polygon] | ||
filtred_y = grid_points[1][grid_points_in_polygon] | ||
|
||
# Convert to GeoDataFrame | ||
points_gs = gpd.points_from_xy(filtred_x, filtred_y) | ||
grid_gdf = gpd.GeoDataFrame(geometry=points_gs, crs=mask_hydro.crs) | ||
|
||
return grid_gdf |
Oops, something went wrong.