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
05b1ca38
Commit
05b1ca38
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
update: minor optimalisation
parent
4546ff39
No related branches found
Branches containing commit
No related tags found
1 merge request
!6
Clhp map
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/map/get_relation.py
+35
-25
35 additions, 25 deletions
server/map/get_relation.py
with
35 additions
and
25 deletions
server/map/get_relation.py
+
35
−
25
View file @
05b1ca38
...
...
@@ -3,12 +3,14 @@ from shapely.geometry import Polygon, Point, LineString, MultiPolygon
import
matplotlib.pyplot
as
plt
import
matplotlib.ticker
as
ticker
import
random
import
math
import
numpy
as
np
polygon_min_x
=
None
# The left most point of the entire polygon
def
get_relation
(
self
,
body_of_water
:
str
):
# Read a json file with relation data and send to response object
def
get_relation
(
self
,
body_of_water
:
str
):
# NB: implement body_of_water
# Load GeoJSON data using geopandas
geo_data
=
gpd
.
read_file
(
"
server/map/mjosa.geojson
"
)
...
...
@@ -112,29 +114,8 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
else
:
remaining_shape
.
append
(
point
)
#################################################################
# Prepend new points onto the newly created edge to facilitate vertical splitting
if
len
(
split_shape
)
>
2
and
polygon_min_x
is
not
None
:
# Define starting point with an x-value that will be common for all polygons
starting_point
=
polygon_min_x
# Get corners of the newly created shape which make up the new edge
right_corner
=
split_shape
[
-
1
]
left_corner
=
split_shape
[
0
]
print
(
"
right_corner:
"
,
right_corner
,
"
left_corner:
"
,
left_corner
,
"
cell_size:
"
,
cell_size
)
while
starting_point
<
left_corner
.
x
:
# Increment starting point until it is withing the polygons bounds
starting_point
+=
cell_size
# Insert new points with cell_size spacing while starting_point is within bounds
while
starting_point
<
right_corner
.
x
:
split_shape
.
insert
(
0
,
(
starting_point
,
divisor
))
# NB may have to add/subtract small offset of 0.00001
remaining_shape
.
insert
(
0
,
(
starting_point
,
divisor
))
# Prepend new point to shape
starting_point
+=
cell_size
#################################################################
# Add points to the newly created edges of split_shape and remaining_shape
split_shape
,
remaining_shape
=
populate_new_edge
(
split_shape
,
remaining_shape
,
cell_size
,
divisor
)
else
:
# Vertical split
for
point
in
exterior_coords
:
...
...
@@ -162,6 +143,35 @@ def cut_polygon_by_points(polygon: Polygon, divisor: float, cell_size: float):
return
Polygon
(
split_shape
),
Polygon
(
remaining_shape
)
# Adds equally spaced points along an edge that is created after a polygon is cut.
def
populate_new_edge
(
split_shape
,
remaining_shape
,
cell_size
:
float
,
divisor
:
float
):
# Prepend new points onto the newly created edge to facilitate vertical splitting
if
len
(
split_shape
)
>
2
and
polygon_min_x
is
not
None
:
# Define starting point with an x-value that will be common for all polygons
starting_point
=
polygon_min_x
# Get corners of the newly created shape which make up the new edge
right_corner
=
split_shape
[
-
1
]
left_corner
=
split_shape
[
0
]
print
(
"
right_corner:
"
,
right_corner
,
"
left_corner:
"
,
left_corner
,
"
cell_size:
"
,
cell_size
)
while
starting_point
<
left_corner
.
x
:
# Increment starting point until it is withing the polygons bounds
starting_point
+=
cell_size
# if starting_point < left_corner.x: # NB: optimised substitute for previous while loop, requires testing
# starting_point += cell_size * math.floor(starting_point - left_corner.x)
# Insert new points with cell_size spacing while starting_point is within bounds
while
starting_point
<
right_corner
.
x
:
split_shape
.
insert
(
0
,
(
starting_point
,
divisor
))
# NB may have to add/subtract small offset of 0.00001
remaining_shape
.
insert
(
0
,
(
starting_point
,
divisor
))
# Prepend new point to shape
starting_point
+=
cell_size
return
split_shape
,
remaining_shape
# Generate grid of equally spaced x and y coordinates where the grid size is determined by cell_size
def
create_grid_coords
(
polygon
:
Polygon
,
cell_size
:
float
):
# Define boundaries of grid
...
...
@@ -174,7 +184,7 @@ def create_grid_coords(polygon: Polygon, cell_size: float):
x_coords
=
np
.
arange
(
min_x
,
max_x
,
cell_size
)
y_coords
=
np
.
arange
(
min_y
,
max_y
,
cell_size
)
# Round
the
coordinates to 4 decimals
# Round coordinates to 4 decimals
x_coords_rounded
=
np
.
around
(
x_coords
,
decimals
=
4
)
y_coords_rounded
=
np
.
around
(
y_coords
,
decimals
=
4
)
...
...
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