Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
geopat
geopat
Commits
b8cdcfde
Commit
b8cdcfde
authored
Apr 04, 2019
by
Dumoulin Nicolas
Browse files
optimization of function: speedup 2.86
parent
399a6a45
Changes
1
Hide whitespace changes
Inline
Side-by-side
scenariosAleatoires/resilience_list.py
View file @
b8cdcfde
...
...
@@ -11,6 +11,13 @@ class Resilience:
:param grille: Grid divising the PAT in squares
'''
self
.
grid
=
gpd
.
GeoDataFrame
.
from_file
(
grille
)
intersection
=
gpd
.
sjoin
(
self
.
grid
[[
'id'
,
'geometry'
]],
patches
[[
'geometry'
,
'SURF_PARC'
]],
how
=
'inner'
,
op
=
'intersects'
)
self
.
grouped_by_cell
=
intersection
.
groupby
(
'id'
)
self
.
size
=
len
(
self
.
grouped_by_cell
)
self
.
surfaces
=
intersection
[[
'id'
,
'SURF_PARC'
]].
groupby
(
'id'
).
sum
()
# The parameters are : the grid layer, a list corresponding to a scenario and a boolean value True or False signifying if we want to see the printing lines or not
def
compute_indicator
(
self
,
patches
,
affichage
=
False
):
...
...
@@ -28,21 +35,21 @@ class Resilience:
:param patches: The scenario to analyse as a list
:param affichage: True if we want the display in the console, False is the other case
'''
intersection
=
gpd
.
sjoin
(
self
.
grid
[[
'id'
,
'geometry'
]],
patches
[[
'geometry'
,
'cultgeopat'
,
'SURF_PARC'
]],
how
=
'inner'
,
op
=
'intersects'
)
resilience
=
0
group
ed
_by_cell
=
intersection
.
groupby
(
'id'
)
for
cell
,
group_by_cell
in
grouped_by_cell
:
surf_tot
=
group_by_cell
[
'SURF_PARC'
].
sum
()
prop_cult
=
group_by_cell
.
groupby
(
'cultgeopat'
)[
'SURF_PARC'
].
sum
()
/
surf_tot
for
cell
,
group_by_cell
in
self
.
grouped_by_cell
:
surf_tot
=
self
.
surfaces
.
loc
[
cell
].
values
[
0
]
intersection
=
patches
[[
'ID_PARCEL'
,
'cultgeopat'
,
'SURF_PARC'
]].
loc
[
group_by_cell
[
'index_right'
]]
prop_cult
=
intersection
.
groupby
(
'cultgeopat'
)[
'SURF_PARC'
].
sum
()
/
surf_tot
res_cell
=
-
(
prop_cult
*
prop_cult
.
apply
(
math
.
log2
)).
sum
()
resilience
+=
res_cell
return
resilience
/
len
(
grouped_by_cell
)
return
resilience
/
self
.
size
if
__name__
==
'__main__'
:
import
geopandas
as
gpd
patches
=
gpd
.
GeoDataFrame
.
from_file
(
"../output/PAT_patches/PAT_patches.shp"
,
encoding
=
'utf-8'
)
import
time
res
=
Resilience
(
"Parcelle_PAT/Grille_resilience.shp"
)
start
=
time
.
time
()
print
(
res
.
compute_indicator
(
patches
))
elapsed
=
time
.
time
()
-
start
print
(
elapsed
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment