-
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 #8 from JonasCinquini/main
Merging main into dev
- Loading branch information
Showing
9 changed files
with
174 additions
and
363 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,37 @@ | ||
# - Python Dependencies | ||
from __future__ import print_function | ||
import os | ||
import argparse | ||
from datetime import datetime | ||
import numpy as np | ||
import geopandas as gpd | ||
from rm_z_coord import rm_z_coord | ||
|
||
|
||
def main() -> None: | ||
# - Path to data | ||
dat_path = os.path.join(os.path.expanduser("~"), 'Desktop', 'MapItaly', | ||
'MAPITALY_CSG1_2_CSK1_2.shp') | ||
|
||
# - Read data | ||
gdf = gpd.read_file(dat_path) | ||
print(f"# - Data loaded: {dat_path}") | ||
print(f"# - Data shape: {gdf.shape}") | ||
print(f"# - Data columns: {gdf.columns}") | ||
|
||
# - Remove Z-Coordinate from geometry | ||
gdf = rm_z_coord(gdf) | ||
|
||
# - Loop through the GeoDataFrame lines and extract a reference grid | ||
# - for each sub-track. | ||
for _, row in gdf.iterrows(): | ||
# - Check Polygon Validity | ||
print(row['geometry'].is_valid) | ||
|
||
|
||
# - run main program | ||
if __name__ == '__main__': | ||
start_time = datetime.now() | ||
main() | ||
end_time = datetime.now() | ||
print(f"# - Computation Time: {end_time - start_time}") |
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,34 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Written by Enrico Ciraci' | ||
January 2024 | ||
Reproject a Shapely geometry. | ||
""" | ||
from shapely.geometry import Polygon, Point | ||
from pyproj import CRS, Transformer | ||
from shapely.ops import transform | ||
|
||
|
||
def reproject_geometry(geometry: Polygon | Point, | ||
source_epsg: int, target_epsg: int) -> Polygon | Point: | ||
""" | ||
Reproject a Shapely geometry. | ||
Args: | ||
geometry: shapely.geometry.Polygon | shapely.geometry.Point | ||
source_epsg: source EPSG code | ||
target_epsg: target EPSG code | ||
Returns: shapely.geometry.Polygon | shapely.geometry.Point | ||
""" | ||
# Set up coordinate reference systems | ||
target_crs = CRS.from_epsg(target_epsg) | ||
# Create a transformer for the coordinate transformation | ||
transformer = Transformer.from_crs(source_epsg, target_crs, always_xy=True) | ||
try: | ||
# - Polygon | ||
return geometry.apply(lambda geom: | ||
transform(transformer.transform, geom)) | ||
except AttributeError: | ||
# - Point | ||
return transform(transformer.transform, geometry) |
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,30 @@ | ||
#!/usr/bin/env python | ||
import geopandas as gpd | ||
from shapely.geometry import Polygon | ||
""" | ||
Written by Enrico Ciraci' | ||
January 2024 | ||
Remove z coordinate from a GeoDataFrame. | ||
Convert a GeoDataFrame with z coordinate to a GeoDataFrame without z coordinate. | ||
""" | ||
|
||
|
||
def rm_z_coord(gdf) -> gpd.GeoDataFrame: | ||
""" | ||
Remove z coordinate from a GeoDataFrame | ||
Args: | ||
gdf: GeoDataFrame | ||
Returns: | ||
""" | ||
# - remove z coordinate | ||
no_z_coord = [] | ||
for _, row in gdf.iterrows(): | ||
# - remove z coordinate | ||
z_coords = row['geometry'].exterior.coords[:-1] | ||
no_z_coord.append(Polygon([(crd[0], crd[1]) for crd in z_coords])) | ||
gdf['geometry'] = no_z_coord | ||
|
||
return gdf |
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,22 @@ | ||
import geopandas as gpd | ||
from shapely.geometry import Polygon | ||
from generate_grid import generate_grid | ||
|
||
|
||
def test_generate_grid(): | ||
# Create a sample GeoDataFrame for testing | ||
d = {'index': 1, | ||
'geometry': Polygon([(12, 36), (11.5, 38), (14, 38.5), | ||
(14, 36.5), (12, 36)])} | ||
gdf_t = gpd.GeoDataFrame(d, crs='EPSG:4326', index=[0]) | ||
|
||
n_c = 3 | ||
az_res = 5000 | ||
buffer_dist = 1000 | ||
|
||
# Ensure the function runs without errors | ||
result_gdf = generate_grid(gdf_t, n_c, az_res, buffer_dist) | ||
|
||
# Perform assertions based on your expectations for the result | ||
assert isinstance(result_gdf, gpd.GeoDataFrame) | ||
assert len(result_gdf) > 0 |
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,20 @@ | ||
import geopandas as gpd | ||
from shapely.geometry import Polygon | ||
from iride_csk_frame_grid_utils import rotate_polygon_to_north_up | ||
from math import isclose | ||
|
||
|
||
def test_rotate_polygon_to_north_up(): | ||
""" | ||
Test the rotate_polygon_to_north_up function. | ||
""" | ||
# Create a sample GeoDataFrame for testing | ||
polygon = Polygon([(12, 36), (11.5, 38), (14, 38.5), | ||
(14, 36.5), (12, 36)]) | ||
|
||
# Ensure the function runs without errors | ||
rotated_polygon, angle = rotate_polygon_to_north_up(polygon) | ||
|
||
# Perform assertions based on your expectations for the result | ||
assert isinstance(rotated_polygon, Polygon) | ||
assert isinstance(angle, float) |