Skip to content

Commit

Permalink
assign_regions added
Browse files Browse the repository at this point in the history
  • Loading branch information
PennyHow committed May 29, 2024
1 parent bf6c211 commit da85dae
Show file tree
Hide file tree
Showing 16 changed files with 383 additions and 809 deletions.
406 changes: 0 additions & 406 deletions src/griml/examples/getInventory_funcs.py

This file was deleted.

182 changes: 0 additions & 182 deletions src/griml/examples/getInventory_obj.py

This file was deleted.

1 change: 1 addition & 0 deletions src/griml/metadata/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from griml.metadata.assign_certainty import *
from griml.metadata.assign_id import *
from griml.metadata.assign_names import *
from griml.metadata.assign_regions import *
from griml.metadata.assign_sources import *
from griml.metadata.add_metadata import *
52 changes: 21 additions & 31 deletions src/griml/metadata/add_metadata.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from griml.metadata import assign_id, assign_sources, assign_certainty, \
assign_names
from griml.load import load
from griml.metadata import assign_id, assign_sources, assign_certainty, \
assign_names, assign_regions
import geopandas as gpd

def add_metadata(iml_file, names_file, outfile=None):
'''Add metadata to collection of features
Parameters
----------
iml_file : str, geopandas.dataframe.DataFrame
Filepath or geopandas.dataframe.DataFrame object to assign metadata to
Returns
-------
iml: geopandas.dataframe.GeoDataFrame
Geodataframe with metadata
'''

# Load files
iml = load(iml_file)
names = load(names_file)
def add_metadata(iml, names, regions, outfile=None):

iml = load(iml)
names = load(names)
regions = load(regions)

# Perform metadata steps
print('Assigning ID...')
iml = assign_id(iml)

Expand All @@ -35,24 +22,27 @@ def add_metadata(iml_file, names_file, outfile=None):
n = ['S1','S2','ARCTICDEM']
scores = [0.298, 0.398, 0.304]
iml = assign_certainty(iml, n, scores)

print('Assigning regions...')
iml = assign_regions(iml, regions)

print('Assigning placenames...')
iml = assign_names(iml, names)

# Save to file if given
if outfile is not None:
if outfile:
print('Saving file...')
iml.to_file(outfile)
print('Saved to '+str(outfile))

return iml
print('Saved to '+str(outfile)+"_metadata.shp")


if __name__ == "__main__":
infile1 = '../test/test_merge_2.shp'
infile2 = '../test/test_placenames.shp'
add_metadata(infile1, infile2)
# indir = "/home/pho/python_workspace/GrIML/other/iml_2017/merged_vectors/griml_2017_inventory.shp"
indir = "/home/pho/python_workspace/GrIML/other/iml_2017/inspected_vectors/lakes_all-0000000000-0000037888_filtered.shp"


infile_names = '/home/pho/python_workspace/GrIML/other/datasets/placenames/oqaasileriffik_placenames.shp'
infile_regions = '/home/pho/python_workspace/GrIML/other/datasets/drainage_basins/greenland_basins_polarstereo.shp'


iml = gpd.read_file(infile1)
names = gpd.read_file(infile2)
add_metadata(iml, names)
outfile = "/home/pho/python_workspace/GrIML/other/iml_2017/metadata_vectors/griml_2017_inventory_final.shp"
add_metadata(indir, infile_names, infile_regions, outfile)
107 changes: 54 additions & 53 deletions src/griml/metadata/assign_names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def assign_names(gdf, gdf_names, distance=500.0):
import itertools
from operator import itemgetter

import geopandas as gpd
import numpy as np
import pandas as pd

from scipy.spatial import cKDTree
from shapely.geometry import Point, LineString, Polygon
from griml.load import load

def assign_names(gdf, gdf_names):
'''Assign placenames to geodataframe geometries based on names in another
geodataframe point geometries
Expand All @@ -19,62 +30,41 @@ def assign_names(gdf, gdf_names, distance=500.0):
gdf : pandas.GeoDataFrame
Vectors with assigned IDs
'''
placenames = _compile_names(gdf_names)

# Load geodataframes
gdf1 = load(gdf)
gdf2 = load(gdf_names)

# Compile placenames into new dataframe
names = _compile_names(gdf2)
placenames = gpd.GeoDataFrame({"geometry": list(gdf2['geometry']),
"placename": names})

lakename=[]
for i,v in gdf.iterrows():

geom = v['geometry']

polynames=[]
for pt in range(len(placenames)):
if geom.contains(gdf_names['geometry'][pt]) == True:
polynames.append(placenames[pt])

if len(polynames)==0:
for pt in range(len(placenames)):
if gdf_names['geometry'][pt].distance(geom) < distance:
polynames.append(placenames[pt])
lakename.append(polynames)

elif len(polynames)==1:
lakename.append(polynames)

else:
out=[]
for p in polynames:
out.append(p)
lakename.append(out)

lakeid = gdf['unique_id'].tolist()
lakename2 = []
# Assign names based on proximity
a = _get_nearest_point(gdf1, placenames)
return a


def _get_nearest_point(gdA, gdB, distance=500.0):
'''Return properties of nearest point in Y to geometry in X'''
nA = np.array(list(gdA.geometry.centroid.apply(lambda x: (x.x, x.y))))
nB = np.array(list(gdB.geometry.apply(lambda x: (x.x, x.y))))

btree = cKDTree(nB)
dist, idx = btree.query(nA, k=1)
gdB_nearest = gdB.iloc[idx].drop(columns="geometry").reset_index(drop=True)
gdf = pd.concat(
[
gdA.reset_index(drop=True),
gdB_nearest,
pd.Series(dist, name='dist')
],
axis=1)

for x in gdf.index:
indx = _get_indices(lakeid, x)
findname=[]
for l in indx:
if len(lakename[l])!=0:
findname.append(lakename[l])

for i in range(len(indx)):
if len(findname)==0:
lakename2.append('')

else:
unique = set(findname[0])
unique = list(unique)

if len(unique)==1:
lakename2.append(findname[0][0])

else:
out2 = ', '
out2 = out2.join(unique)
lakename2.append(out2)
gdf['placename'] = lakename2
gdf.loc[gdf['dist']>=distance, 'placename'] = 'Unknown'
gdf = gdf.drop(columns=['dist'])
return gdf


def _get_indices(mylist, value):
'''Get indices for value in list'''
return[i for i, x in enumerate(mylist) if x==value]
Expand All @@ -95,3 +85,14 @@ def _compile_names(gdf):
else:
placenames.append(None)
return placenames


if __name__ == "__main__":

# Define file attributes
infile = '/home/pho/Desktop/python_workspace/GrIML/src/griml/test/test_merge_1.shp'
names = '/home/pho/Desktop/python_workspace/GrIML/other/datasets/placenames/oqaasileriffik_placenames_polarstereo.shp'


# Perform conversion
a = assign_names(infile, names)
Loading

0 comments on commit da85dae

Please sign in to comment.