Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
doren_2019
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
Container registry
Model registry
Operate
Environments
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
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Roelofsen, Hans
doren_2019
Commits
3670c789
Commit
3670c789
authored
5 years ago
by
Roelofsen, Hans
Browse files
Options
Downloads
Patches
Plain Diff
nearest neighbor search method
parent
3956932f
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
create_doren.py
+2
-2
2 additions, 2 deletions
create_doren.py
utils/doren.py
+22
-3
22 additions, 3 deletions
utils/doren.py
utils/doren_classes.py
+18
-0
18 additions, 0 deletions
utils/doren_classes.py
with
42 additions
and
5 deletions
create_doren.py
+
2
−
2
View file @
3670c789
...
...
@@ -18,8 +18,8 @@ cv_temp_dir = r'c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\a_bro
cv_temp_src
=
"
EObs_v200e_tg_5yrmean
"
sp_req_src
=
r
'
c:\Users\roelo008\OneDrive - WageningenUR\a_projects\DOREN\z_scratch\soortenlijst.csv
'
doren
=
dc
.
Doren
(
header_src
=
param_header_src
,
sp_src
=
param_sp_src
)
doren
.
initiate
()
doren
=
dc
.
Doren
(
header_src
=
param_header_src
,
sp_src
=
param_sp_src
)
doren
.
initiate
(
sample
=
True
)
doren
.
rename
(
old_name
=
'
Oxycoccus palustris
'
,
new_name
=
'
Vaccinium oxycoccos
'
)
doren
.
apply_requirements
(
'
req1
'
,
'
req2
'
,
'
req3
'
,
'
req4
'
,
'
req8
'
,
'
req9
'
,
'
req10
'
,
aoi_src
=
param_aoi_src
,
dem_src
=
param_dem_src
)
...
...
This diff is collapsed.
Click to expand it.
utils/doren.py
+
22
−
3
View file @
3670c789
...
...
@@ -6,14 +6,19 @@ Helper functions for the DOREN project
import
os
import
json
import
rasterstats
as
rast
import
rasterio
as
rio
import
geopandas
as
gp
import
numpy
as
np
import
re
import
pyproj
import
shapely
import
pandas
as
pd
from
shapely.geometry
import
Polygon
# set pyproj project library if needed
if
not
os
.
environ
[
'
PROJ_LIB
'
]:
os
.
environ
[
'
PROJ_LIB
'
]
=
'
C:
\\
Users
\\
roelo008
\\
AppData
\\
Local
\\
ESRI
\\
conda
\\
envs
\\
hdr001
\\
Library
\\
share
'
import
pyproj
def
eva_colnames_orig
():
"""
...
...
@@ -305,6 +310,7 @@ def get_raster_vals(coords, rast_src, nominal=False):
if
not
os
.
path
.
isfile
(
rast_src
):
raise
Exception
(
'
{0} does not exists as valid rastersource
'
.
format
(
rast_src
))
raster
=
rio
.
open
(
rast_src
)
interpolation
=
'
nearest
'
if
nominal
else
'
bilinear
'
...
...
@@ -316,10 +322,23 @@ def get_raster_vals(coords, rast_src, nominal=False):
coords
=
coords
.
apply
(
shapely
.
geometry
.
Point
)
# the numeric raster values
rast_vals
=
rast
.
point_query
(
coords
,
rast
_src
,
interpolate
=
interpolation
)
rast_vals
=
rast
.
point_query
(
coords
,
rast
er
.
read
(
1
)
,
interpolate
=
interpolation
,
affine
=
raster
.
affine
)
return
pd
.
DataFrame
(
data
=
{
'
vals
'
:
rast_vals
},
index
=
coords
.
index
)
def
nearest
(
row
,
geom_union
,
df1
,
df2
,
geom1_col
=
'
geometry
'
,
geom2_col
=
'
geometry
'
,
src_column
=
None
):
"""
Find the nearest point and return the corresponding value from specified column.
Src: https://automating-gis-processes.github.io/CSC18/lessons/L4/nearest-neighbour.html
"""
# Find the geometry that is closest
nearest
=
df2
[
geom2_col
]
==
shapely
.
ops
.
nearest_points
(
row
[
geom1_col
],
geom_union
)[
1
]
# Get the corresponding value from df2 (matching is based on the geometry)
value
=
df2
[
nearest
][
src_column
].
get_values
()[
0
]
return
value
'''
pattern = re.compile(r
'
s\.l\.$| subsp\.? | var\.? | aggr\.? |
""
| ssp\.? | s\.? | mod\.? | \(
'
)
sp1 =
'
Rosa spinosissima
'
# begint met s
...
...
@@ -338,6 +357,6 @@ sp12 = 'Rosa spinosissima mod. HANS'
sp12 =
'
Rosa spinosissima mod HANS
'
sps = [sp1,sp2,sp3,sp4,sp5,sp6,sp7,sp8,sp9,sp10,sp11,sp12]
[re.split(pattern, x) for x in sps]
pattern = re.compile(r
'
[A-Z]{1}[a-z]{1,} [a-z]{1,}
'
)
'''
pattern
=
re
.
compile
(
r
'
[A-Z]{1}[a-z]{1,} [a-z]{1,}
'
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
utils/doren_classes.py
+
18
−
0
View file @
3670c789
...
...
@@ -454,6 +454,24 @@ class Doren:
self
.
nearby_plots
=
nearby_plots
self
.
buffer_size
=
buffer_size
def
nearest_positive
(
self
):
"""
Determine distance of each negative plots to nearest positive plot.
:return:
"""
origin
=
self
.
eva
.
loc
[
self
.
eva
.
index
.
difference
(
self
.
positive_plots
)]
target
=
self
.
eva
.
loc
[
self
.
positive_plots
]
unary_union
=
target
.
geometry
.
unary_union
nearest_n
=
origin
.
apply
(
do
.
nearest
,
geom_union
=
unary_union
,
df1
=
origin
,
df2
=
target
,
geom1_col
=
'
plot_coordinates_3035
'
,
geom2_col
=
'
plot_coordinates_3035
'
,
src_column
=
'
plot_id
'
,
axis
=
1
)
# TODO: join nearest_n series to self.eva and put distance 0 for self.eva.loc[self.positive_plots]
def
write_stuff
(
self
,
what
,
covars
=
False
):
"""
Write contents to file. This should prob be organised as: self.generate_report(X) and then self.write_report(x)
...
...
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