Commit 3670c789 authored by Roelofsen, Hans's avatar Roelofsen, Hans
Browse files

nearest neighbor search method

parent 3956932f
......@@ -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)
......
......@@ -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, raster.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
......@@ -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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment