Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
a906c973
Commit
a906c973
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
update: second split method
parent
93a57316
No related branches found
No related tags found
1 merge request
!6
Clhp map
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/map/__pycache__/get_relation.cpython-311.pyc
+0
-0
0 additions, 0 deletions
server/map/__pycache__/get_relation.cpython-311.pyc
server/map/get_relation.py
+11
-57
11 additions, 57 deletions
server/map/get_relation.py
with
11 additions
and
57 deletions
server/map/__pycache__/get_relation.cpython-311.pyc
+
0
−
0
View file @
a906c973
No preview for this file type
This diff is collapsed.
Click to expand it.
server/map/get_relation.py
+
11
−
57
View file @
a906c973
import
geopandas
as
gpd
from
shapely.geometry
import
Polygon
,
Point
,
LineString
,
MultiLineString
,
MultiPolygon
from
shapely.geometry
import
Polygon
,
Point
,
LineString
import
matplotlib.pyplot
as
plt
from
shapely.ops
import
linemerge
,
unary_union
,
polygonize
import
matplotlib.ticker
as
ticker
import
numpy
as
np
import
pandas
as
pd
def
get_relation
(
self
,
body_of_water
:
str
):
...
...
@@ -22,32 +21,22 @@ def get_relation(self, body_of_water: str):
print
(
"
Failed to convert to polygons
"
)
return
########################################################################################
print
(
"
Performing div call
"
)
# NB: any cell size under 0.02 will require patience
# For example, 0.007 requires up to 3 minutes to compute
# tiles = divide_relation(polygons, 0.02)
test_line
=
LineString
([(
10.4600
,
60.7451
),
(
11.2000
,
60.7451
)])
line_pol
=
test_line
.
buffer
(
1e-3
)
new_polygon
=
polygons
[
0
].
difference
(
line_pol
)
new_polygons
=
[]
for
polygon
in
polygons
:
new_polygon
=
cut_polygon_by_line
(
polygon
,
test_line
)
new_polygons
.
extend
(
new_polygon
)
tiles
=
gpd
.
GeoDataFrame
(
geometry
=
[
new_polygon
]
)
tiles
=
gpd
.
GeoDataFrame
(
geometry
=
new_polygon
s
)
fig
,
ax
=
plt
.
subplots
()
ax
.
set_aspect
(
1.5
)
ax
.
xaxis
.
set_major_locator
(
ticker
.
MultipleLocator
(
0.2
))
tiles
.
plot
(
ax
=
ax
,
color
=
'
blue
'
,
edgecolor
=
'
blue
'
,
alpha
=
0.5
,
label
=
'
Original Polygon
'
)
plt
.
show
()
if
test_line
.
intersects
(
polygons
[
0
].
boundary
):
print
(
"
Intersects!
"
)
else
:
print
(
"
Test line does not intersect the polygon
"
)
########################################################################################
# Convert GeoDataFrame to GeoJSON
tiles_json
=
tiles
.
to_json
()
...
...
@@ -60,47 +49,12 @@ def get_relation(self, body_of_water: str):
self
.
wfile
.
write
(
tiles_json
.
encode
(
'
utf-8
'
))
######################### Approach 1 #################################
def
divide_relation
(
polygons
,
cell_size
):
# Calculate the combined bounding box of all polygons
combined_bounds
=
polygons
[
0
].
bounds
for
polygon
in
polygons
[
1
:]:
combined_bounds
=
(
min
(
combined_bounds
[
0
],
polygon
.
bounds
[
0
]),
min
(
combined_bounds
[
1
],
polygon
.
bounds
[
1
]),
max
(
combined_bounds
[
2
],
polygon
.
bounds
[
2
]),
max
(
combined_bounds
[
3
],
polygon
.
bounds
[
3
]))
# Create an empty list to store polygons representing tiles
tiles
=
[]
# Iterate over each polygon
for
polygon
in
polygons
:
# Iterate over each cell
for
i
in
np
.
arange
(
combined_bounds
[
1
],
combined_bounds
[
3
],
cell_size
):
for
j
in
np
.
arange
(
combined_bounds
[
0
],
combined_bounds
[
2
],
cell_size
):
# Define the bounds of the cell
cell_bounds
=
(
j
,
i
,
j
+
cell_size
,
i
+
cell_size
)
# Create a polygon representing the cell
cell_polygon
=
Polygon
([(
cell_bounds
[
0
],
cell_bounds
[
1
]),
(
cell_bounds
[
2
],
cell_bounds
[
1
]),
(
cell_bounds
[
2
],
cell_bounds
[
3
]),
(
cell_bounds
[
0
],
cell_bounds
[
3
])])
# Check if the cell polygon intersects with the current polygon
if
polygon
.
intersects
(
cell_polygon
):
tiles
.
append
(
cell_polygon
)
# Create a GeoDataFrame from the list of tiles
tiles_gdf
=
gpd
.
GeoDataFrame
(
geometry
=
tiles
)
return
tiles_gdf
######################### Approach 2 #################################
# From https://stackoverflow.com/questions/39338550/cut-a-polygon-with-two-lines-in-shapely
def
cut_polygon_by_line
(
polygon
,
line
):
merged
=
linemerge
([
polygon
.
boundary
,
line
])
if
polygon
.
geom_type
==
'
Polygon
'
:
polygon
=
[
polygon
]
merged
=
linemerge
([
poly
.
boundary
for
poly
in
polygon
]
+
[
line
])
borders
=
unary_union
(
merged
)
polygons
=
polygonize
(
borders
)
return
list
(
polygons
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment