-
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.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import json | ||
import random | ||
|
||
import contextily as cx | ||
import geojson | ||
import matplotlib.pyplot as plt | ||
import shapely | ||
import trajgenpy.Logging | ||
from trajgenpy import Utils | ||
from trajgenpy.Geometries import ( | ||
GeoMultiPolygon, | ||
GeoMultiTrajectory, | ||
GeoPolygon, | ||
decompose_polygon, | ||
generate_sweep_pattern, | ||
get_sweep_offset, | ||
) | ||
from trajgenpy.Query import query_features | ||
|
||
log = trajgenpy.Logging.get_logger() | ||
|
||
|
||
def get_line_segments(polygon: GeoPolygon): | ||
polygon_list = decompose_polygon(polygon.get_geometry()) | ||
|
||
log.info(f"Number of polygons: {len(polygon_list)}") | ||
|
||
offset = get_sweep_offset(0.1, 15, 90) | ||
result = [] | ||
for decomposed_poly in polygon_list: | ||
sweeps_connected = generate_sweep_pattern(decomposed_poly, offset, connect_sweeps=False) | ||
result.extend(sweeps_connected) | ||
return result | ||
|
||
|
||
def export_to_geojson(tasks: GeoMultiTrajectory, polygon: GeoPolygon): | ||
log.info(f"Number of sweeps: {len(tasks.get_geometry().geoms)}") | ||
geojson_collection = geojson.FeatureCollection( | ||
[ | ||
polygon.to_geojson(id="boundary"), | ||
multi_traj.to_geojson(id="tasks"), | ||
] | ||
) | ||
with open("environment.geojson", "w") as f: | ||
geojson.dump(geojson_collection, f) | ||
|
||
|
||
import osmnx as ox | ||
Check failure on line 48 in DemaScenarios/task_generator.py
|
||
|
||
if __name__ == "__main__": | ||
# polygon_file = "examples/DemaScenarios/FlatTerrainNature.geojson" | ||
# polygon_file = "examples/DemaScenarios/HillyTerrainNature.geojson" | ||
# polygon_file = "examples/DemaScenarios/Urban.geojson" | ||
polygon_file = "DemaScenarios/Water.geojson" | ||
|
||
# Load the GeoJSON data | ||
with open(polygon_file, "r") as f: | ||
data = json.load(f) | ||
|
||
# Extract the polygon coordinates | ||
coordinates = data["features"][0]["geometry"]["coordinates"][0] | ||
|
||
# Create the shapely Polygon object | ||
polygon = GeoPolygon(shapely.Polygon(coordinates)).set_crs("EPSG:2197") | ||
polygon.plot(facecolor="none", edgecolor="black", linewidth=2) | ||
|
||
query_polygon = GeoPolygon(shapely.Polygon(coordinates)).set_crs("WGS84") | ||
features = query_features( | ||
query_polygon, | ||
{ | ||
"natural": ["coastline"], | ||
}, | ||
) | ||
GeoMultiTrajectory(features["natural"]).set_crs("EPSG:2197").plot(color="blue", linewidth=2) | ||
|
||
tasks = get_line_segments(polygon) | ||
|
||
multi_traj = GeoMultiTrajectory(tasks, "EPSG:2197") | ||
multi_traj.plot(color="red") | ||
|
||
Utils.plot_basemap(provider=cx.providers.OpenStreetMap.Mapnik, crs="EPSG:2197") | ||
export_to_geojson(multi_traj, polygon) | ||
|
||
# No axis on the plot | ||
plt.axis("equal") | ||
plt.axis("off") | ||
plt.show() |