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
c9003def
Commit
c9003def
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
update: divide polygon into tiles
parent
e2bdb579
No related branches found
Branches containing commit
No related tags found
2 merge requests
!5
Clhp map
,
!4
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
+44
-60
44 additions, 60 deletions
server/map/get_relation.py
with
44 additions
and
60 deletions
server/map/__pycache__/get_relation.cpython-311.pyc
+
0
−
0
View file @
c9003def
No preview for this file type
This diff is collapsed.
Click to expand it.
server/map/get_relation.py
+
44
−
60
View file @
c9003def
import
string
import
json
import
geopandas
as
gpd
from
server.map.get_markers
import
get_all_markers
from
shapely.geometry
import
Polygon
,
LineString
,
Point
from
shapely.geometry
import
Polygon
# get_relation returns the geojson data for a selected body of water
def
get_relation
(
self
,
body_of_water
:
string
):
def
get_relation
(
self
,
body_of_water
:
str
):
# Load GeoJSON data using geopandas
geo_data
=
gpd
.
read_file
(
"
server/map/mjosa.geojson
"
)
# Filter only polygons, exclude points
# Filter only polygons, exclude points
and other feature types
polygon_data
=
geo_data
[
geo_data
[
'
geometry
'
].
geom_type
==
'
Polygon
'
]
# Convert GeoDataFrame to dictionary
geojson_dict
=
json
.
loads
(
polygon_data
.
to_json
())
# Extract coordinates from polygons and create polygon objects
polygons
=
[
Polygon
(
polygon
.
exterior
)
for
polygon
in
polygon_data
[
'
geometry
'
]]
if
len
(
polygons
)
<=
1
:
print
(
"
Failed to convert to polygons
"
)
tiles
=
[]
for
polygon
in
polygons
:
tiles
.
extend
(
divide_relation
(
polygon
))
# Divide each polygon from relation and append to tiles
# Convert polygon coordinates to lists
tiles_json
=
[
list
(
tile
.
exterior
.
coords
)
for
tile
in
tiles
]
# Convert response data to JSON string
response_json
=
json
.
dumps
(
divide_relation
(
self
,
polygon_data
)
)
response_json
=
json
.
dumps
(
tiles_json
)
# Set headers
self
.
send_response
(
200
)
...
...
@@ -28,54 +36,30 @@ def get_relation(self, body_of_water: string):
self
.
wfile
.
write
(
response_json
.
encode
(
'
utf-8
'
))
def
divide_relation
(
self
,
polygon
):
# Fetch measurements from database
measurements
=
get_all_markers
(
self
.
cursor
,
False
,
'
Mjosa
'
)
# Temporary hardcoded point at which relation is split, should be
splitting_points
=
[(
60.6853
,
10.9571
),
(
60.7018
,
11.1058
)]
# Lists to hold new polygons
divided_relation
=
[]
temp_seg_1
=
[]
temp_seg_2
=
[]
# Loop through polygon coordinates and split at defined coordinates
for
i
in
range
(
len
(
polygon
)):
closest_point
=
[]
# Find the closest point on the polygon to the splitting point
for
point
in
splitting_points
:
closest_point
=
polygon
.
exterior
.
interpolate
(
polygon
.
exterior
.
project
(
Point
(
point
)),
normalized
=
True
)
# Check if the closest point is already in the polygon coordinates
if
closest_point
.
coords
[
0
]
not
in
polygon
:
# If not, add it to the polygon coordinates
polygon
.
append
(
closest_point
.
coords
[
0
])
# Add the splitting point (either the original or the closest) to the splitting points list
splitting_points
.
append
(
closest_point
.
coords
[
0
])
current_point
=
polygon
[
i
]
next_point
=
polygon
[(
i
+
1
)
%
len
(
polygon
)]
# Circular index
# Check if the current segment crosses any splitting point
for
splitting_point
in
splitting_points
:
if
(
current_point
!=
splitting_point
and
next_point
!=
splitting_point
and
LineString
([
current_point
,
next_point
]).
intersects
(
Point
(
splitting_point
))):
# Split the segment at the splitting point
temp_seg_1
.
append
(
current_point
)
temp_seg_2
.
append
(
splitting_point
)
temp_seg_1
.
append
(
splitting_point
)
temp_seg_2
.
append
(
next_point
)
break
else
:
# If the segment doesn't cross any splitting point, add it to both lists
temp_seg_1
.
append
(
current_point
)
temp_seg_2
.
append
(
current_point
)
# Return a list containing all the created polygons
divided_relation
=
[
Polygon
(
temp_seg_1
),
Polygon
(
temp_seg_2
)]
return
divided_relation
def
divide_relation
(
polygon
):
# Define tile size
tile_size
=
0.01
x_min
,
y_min
,
x_max
,
y_max
=
polygon
.
bounds
rows
=
int
((
y_max
-
y_min
)
/
tile_size
)
cols
=
int
((
x_max
-
x_min
)
/
tile_size
)
tiles
=
[]
if
rows
==
0
or
cols
==
0
:
# Return if the polygon is too small
return
[]
for
row
in
range
(
rows
):
for
col
in
range
(
cols
):
tile_bbox
=
Polygon
([
(
x_min
+
col
*
tile_size
,
y_min
+
row
*
tile_size
),
(
x_min
+
(
col
+
1
)
*
tile_size
,
y_min
+
row
*
tile_size
),
(
x_min
+
(
col
+
1
)
*
tile_size
,
y_min
+
(
row
+
1
)
*
tile_size
),
(
x_min
+
col
*
tile_size
,
y_min
+
(
row
+
1
)
*
tile_size
)
])
tiles
.
append
(
tile_bbox
)
if
len
(
tiles
)
<=
1
:
print
(
"
Failed to divide polygon into tiles
"
)
return
tiles
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