Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Sep 19, 2023
1 parent 4334cee commit a833183
Show file tree
Hide file tree
Showing 25 changed files with 253 additions and 159 deletions.
2 changes: 1 addition & 1 deletion pyflowline/algorithms/auxiliary/find_index_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def find_vertex_on_edge(aVertex_in, pEdge_in):
aIndex.append(k)
npoint = npoint + 1
else:
if diff < 1.0:
if diff < 1.0:
iFlag_overlap = pEdge_in.check_vertex_on_edge(pVertex)

pass
Expand Down
23 changes: 12 additions & 11 deletions pyflowline/algorithms/auxiliary/find_vertex_in_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ def find_vertex_in_list(aVertex_in, pVertex_in, dThreshold_in=1.0E-6):
lID = i
x=aVertex_in[i].dLongitude_degree
y=aVertex_in[i].dLatitude_degree
left= x - 1E-5
right= x + 1E-5
bottom= y-1E-5
top= y+1E-5
pBound= (left, bottom, right, top)
left = x - 1E-5
right = x + 1E-5
bottom = y - 1E-5
top = y + 1E-5
pBound = (left, bottom, right, top)
index_vertex.insert(lID, pBound) #
pass

#now the new vertex
x=pVertex_in.dLongitude_degree
Expand All @@ -52,9 +53,9 @@ def find_vertex_in_list(aVertex_in, pVertex_in, dThreshold_in=1.0E-6):

for k in aIntersect:
pVertex = aVertex_in[k]
dDistance = pVertex.calculate_distance(pVertex_in)
#if pVertex == pVertex_in:
if dDistance < dThreshold_in:
#dDistance = pVertex.calculate_distance(pVertex_in)
if pVertex == pVertex_in: #if dDistance < dThreshold_in:

iFlag_exist = 1
lIndex = k
break
Expand All @@ -66,9 +67,9 @@ def find_vertex_in_list(aVertex_in, pVertex_in, dThreshold_in=1.0E-6):
if nVertex > 0 :
for i in np.arange( nVertex):
pVertex = aVertex_in[i]
dDistance = pVertex.calculate_distance(pVertex_in)
#if pVertex == pVertex_in:
if dDistance < dThreshold_in:
#dDistance = pVertex.calculate_distance(pVertex_in)
if pVertex == pVertex_in: #if dDistance < dThreshold_in:

iFlag_exist = 1
lIndex = i
break
Expand Down
16 changes: 8 additions & 8 deletions pyflowline/algorithms/direction/correct_flowline_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sys.setrecursionlimit(10000)
import numpy as np
from pyflowline.algorithms.auxiliary.check_head_water import check_head_water
lID=0
lFlowlineIndex=0
def correct_flowline_direction(aFlowline_in, pVertex_outlet_in):
"""_summary_ This function should expect the flowline may not be ordered, so the stream order info is not available.
Expand All @@ -17,7 +17,7 @@ def correct_flowline_direction(aFlowline_in, pVertex_outlet_in):
aFlowline_out= list()
#we have to go reversely
aFlag_process=None
global lID
global lFlowlineIndex
nFlowline = len(aFlowline_in)
aFlag_process=np.full(nFlowline, 0, dtype =int)
iFlag_first = 1
Expand All @@ -42,14 +42,14 @@ def correct_flowline_direction(aFlowline_in, pVertex_outlet_in):

pass

lID = 0
lFlowlineIndex = 0
pFlowline = aFlowline_in[lIndex_outlet]
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lFlowlineIndex
pFlowline.iFlag_dam = 1 # this one is not dam, but it should be preserved
aFlowline_out.append(pFlowline)
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
lID = lID + 1
lFlowlineIndex = lFlowlineIndex + 1
#we might find more than 1 upstream
def find_upstream_flowline(pVertex_start_in, pVertex_end_in):
nUpstream = 0
Expand Down Expand Up @@ -82,7 +82,7 @@ def find_upstream_flowline(pVertex_start_in, pVertex_end_in):

def tag_upstream(pVertex_start_in, pVertex_end_in):

global lID
global lFlowlineIndex
if(check_head_water(aFlowline_in, pVertex_start_in)==1):
pass
else:
Expand All @@ -96,9 +96,9 @@ def tag_upstream(pVertex_start_in, pVertex_end_in):
pass
else:
pass
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lFlowlineIndex
aFlowline_out.append(pFlowline)
lID = lID + 1
lFlowlineIndex = lFlowlineIndex + 1
tag_upstream( pFlowline.pVertex_start, pFlowline.pVertex_end )

pass
Expand Down
74 changes: 37 additions & 37 deletions pyflowline/algorithms/intersect/intersect_flowline_with_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
pLayerDefn = pLayerOut.GetLayerDefn()
pFeatureOut = ogr.Feature(pLayerDefn)

lID_flowline = 0
lFlowlineID = 0
aFlowline_intersect_all=list()
if iFlag_use_rtree ==1: #use the rtree to speed up
#index_flowline = rtree.index.Index()
Expand Down Expand Up @@ -114,7 +114,7 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
iStream_order = pFeature_flowline.GetField("stream_order")
if pGeometrytype_intersect == 'LINESTRING':
pFeatureOut.SetGeometry(pGeometry_intersect)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("lineid", lFlowlineID)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)
Expand All @@ -125,22 +125,22 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
aCoords.append( [ pt[0], pt[1]])

dummy1= np.array(aCoords)
pLine = convert_gcs_coordinates_to_flowline(dummy1)
pLine.calculate_length()
pLine.lIndex = lID_flowline
pLine.iStream_segment = iStream_segment
pLine.iStream_order = iStream_order
aFlowline_intersect.append(pLine)
aFlowline_intersect_all.append(pLine)
lID_flowline = lID_flowline + 1
pFlowline = convert_gcs_coordinates_to_flowline(dummy1)
pFlowline.calculate_length()
pFlowline.lFlowlineIndex = lFlowlineID
pFlowline.iStream_segment = iStream_segment
pFlowline.iStream_order = iStream_order
aFlowline_intersect.append(pFlowline)
aFlowline_intersect_all.append(pFlowline)
lFlowlineID = lFlowlineID + 1

else:
if(pGeometrytype_intersect == 'MULTILINESTRING'):
nLine = pGeometry_intersect.GetGeometryCount()
for i in range(nLine):
Line = pGeometry_intersect.GetGeometryRef(i)
pFeatureOut.SetGeometry(Line)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("lineid", lFlowlineID)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)
Expand All @@ -150,14 +150,14 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
aCoords.append( [ pt[0], pt[1]])

dummy1= np.array(aCoords)
pLine = convert_gcs_coordinates_to_flowline(dummy1)
pLine.calculate_length()
pLine.lIndex = lID_flowline
pLine.iStream_segment = iStream_segment
pLine.iStream_order = iStream_order
aFlowline_intersect.append(pLine)
aFlowline_intersect_all.append(pLine)
lID_flowline = lID_flowline + 1
pFlowline = convert_gcs_coordinates_to_flowline(dummy1)
pFlowline.calculate_length()
pFlowline.lFlowlineIndex = lFlowlineID
pFlowline.iStream_segment = iStream_segment
pFlowline.iStream_order = iStream_order
aFlowline_intersect.append(pFlowline)
aFlowline_intersect_all.append(pFlowline)
lFlowlineID = lFlowlineID + 1
pass
else:
pass
Expand Down Expand Up @@ -230,7 +230,7 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
pGeometrytype_intersect = pGeometry_intersect.GetGeometryName()
if pGeometrytype_intersect == 'LINESTRING':
pFeatureOut.SetGeometry(pGeometry_intersect)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("lineid", lFlowlineID)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)
Expand All @@ -241,22 +241,22 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
aCoords.append( [ pt[0], pt[1]])

dummy1= np.array(aCoords)
pLine = convert_gcs_coordinates_to_flowline(dummy1)
pLine.calculate_length()
pLine.lIndex = lID_flowline
pLine.iStream_segment = iStream_segment
pLine.iStream_order = iStream_order
aFlowline_intersect.append(pLine)
aFlowline_intersect_all.append(pLine)
lID_flowline = lID_flowline + 1
pFlowline = convert_gcs_coordinates_to_flowline(dummy1)
pFlowline.calculate_length()
pFlowline.lFlowlineIndex = lFlowlineID
pFlowline.iStream_segment = iStream_segment
pFlowline.iStream_order = iStream_order
aFlowline_intersect.append(pFlowline)
aFlowline_intersect_all.append(pFlowline)
lFlowlineID = lFlowlineID + 1

else:
if(pGeometrytype_intersect == 'MULTILINESTRING'):
nLine = pGeometry_intersect.GetGeometryCount()
for i in range(nLine):
Line = pGeometry_intersect.GetGeometryRef(i)
pFeatureOut.SetGeometry(Line)
pFeatureOut.SetField("lineid", lID_flowline)
pFeatureOut.SetField("lineid", lFlowlineID)
pFeatureOut.SetField("stream_segment", iStream_segment)
pFeatureOut.SetField("stream_order", iStream_order)
pLayerOut.CreateFeature(pFeatureOut)
Expand All @@ -266,14 +266,14 @@ def intersect_flowline_with_mesh(iMesh_type_in, sFilename_mesh_in, sFilename_flo
aCoords.append( [ pt[0], pt[1]])

dummy1= np.array(aCoords)
pLine = convert_gcs_coordinates_to_flowline(dummy1)
pLine.calculate_length()
pLine.lIndex = lID_flowline
pLine.iStream_segment = iStream_segment
pLine.iStream_order = iStream_order
aFlowline_intersect.append(pLine)
aFlowline_intersect_all.append(pLine)
lID_flowline = lID_flowline + 1
pFlowline = convert_gcs_coordinates_to_flowline(dummy1)
pFlowline.calculate_length()
pFlowline.lFlowlineIndex = lFlowlineID
pFlowline.iStream_segment = iStream_segment
pFlowline.iStream_order = iStream_order
aFlowline_intersect.append(pFlowline)
aFlowline_intersect_all.append(pFlowline)
lFlowlineID = lFlowlineID + 1
pass
else:
pass
Expand Down
4 changes: 2 additions & 2 deletions pyflowline/algorithms/loop/remove_flowline_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def find_paralle_stream( pVertex_start_in):
ndownstream , aDownstream, aStream_order = find_paralle_stream( pVertex_start)
if ndownstream == 1:
if aFlag[i] !=1:
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID + 1
aFlag[i]=1
Expand All @@ -48,7 +48,7 @@ def find_paralle_stream( pVertex_start_in):
lIndex = aDownstream[ sort_index[0] ]
pFlowline_down = aFlowline_in[lIndex]
if aFlag[ lIndex ] !=1:
pFlowline_down.lIndex = lID
pFlowline_down.lFlowlineIndex = lID
aFlowline_out.append(pFlowline_down)
lID = lID + 1
aFlag[lIndex]=1
Expand Down
4 changes: 2 additions & 2 deletions pyflowline/algorithms/merge/merge_flowline.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def merge_flowline_reach(lIndex_in, pVertex_start_in, pVertex_end_in):
if find_vertex_in_list(aVertex_headwater.tolist(), pVertex_current)[0] ==1:
pFlowline.iStream_segment = iSegment
pFlowline.iStream_order = 1
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID + 1
return
else:
pFlowline.iStream_segment = iSegment
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID + 1
#confluence
Expand Down
6 changes: 3 additions & 3 deletions pyflowline/algorithms/simplification/remove_small_river.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ def remove_small_river(aFlowline_in, dThreshold_in):
pVertex_end = pFlowline.pVertex_end
dLength = pFlowline.calculate_length()
if iFlag_dam ==1:
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID +1
else:
if pFlowline.iStream_order == 1:
if dLength > dThreshold_in :
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID + 1
pass
else:
pass
pass
else: #this one might be not used
pFlowline.lIndex = lID
pFlowline.lFlowlineIndex = lID
aFlowline_out.append(pFlowline)
lID = lID +1
pass
Expand Down
18 changes: 11 additions & 7 deletions pyflowline/algorithms/split/find_flowline_confluence.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import importlib
iFlag_cython = importlib.util.find_spec("cython")

from pyflowline.algorithms.split.find_flowline_vertex import find_flowline_vertex
if iFlag_cython is not None:
from pyflowline.algorithms.cython.kernel import add_unique_vertex
from pyflowline.algorithms.cython.kernel import find_vertex_in_list
Expand Down Expand Up @@ -33,13 +35,15 @@ def find_flowline_confluence(aFlowline_in, pVertex_outlet_in):

#add all the flowline start and end vertex into a host list
#because the flowlines are order from outlet to headwater, there is no order in vertex list
for i in range(0, nFlowline):
pFlowline = aFlowline_in[i]
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
aVertex, dummy = add_unique_vertex(aVertex, pVertex_start)
aVertex, dummy = add_unique_vertex(aVertex, pVertex_end)
pass
#for i in range(0, nFlowline):
# pFlowline = aFlowline_in[i]
# pVertex_start = pFlowline.pVertex_start
# pVertex_end = pFlowline.pVertex_end
# aVertex, dummy = add_unique_vertex(aVertex, pVertex_start)
# aVertex, dummy = add_unique_vertex(aVertex, pVertex_end)
# pass

aVertex = find_flowline_vertex(aFlowline_in)

nVertex=len(aVertex)
aConnectivity = np.full( nVertex , 0, dtype=int )
Expand Down
43 changes: 41 additions & 2 deletions pyflowline/algorithms/split/find_flowline_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,51 @@
from pyflowline.algorithms.auxiliary.find_index_in_list import add_unique_vertex

def find_flowline_vertex(aFlowline_in, dThreshold_in=1.0E-6):
"""use the set to add unique vertex into a list
Args:
aFlowline_in (_type_): _description_
dThreshold_in (_type_, optional): _description_. Defaults to 1.0E-6.
Returns:
_type_: _description_
"""
aVertex = set()
nFlowline = len(aFlowline_in)
aVertex=list()
#set up index and id first
lVertexID = 1

for i in range(0, nFlowline):
pFlowline = aFlowline_in[i]
#update the start and end vertex
pFlowline.pVertex_start.lVertexID = lVertexID
lVertexID = lVertexID + 1
pFlowline.pVertex_end.lVertexID = lVertexID
lVertexID = lVertexID + 1
aVertex.add(pFlowline.pVertex_start)
aVertex.add(pFlowline.pVertex_end)
pass

#conver the set back to list
aVertex=list(aVertex)
#for i in range(0, nFlowline):
# pFlowline = aFlowline_in[i]
# pVertex_start = pFlowline.pVertex_start
# pVertex_end = pFlowline.pVertex_end
# aVertex, dummy = add_unique_vertex(aVertex, pVertex_start,dThreshold_in)
# aVertex, dummy = add_unique_vertex(aVertex, pVertex_end, dThreshold_in)
# pass

return aVertex

def find_flowline_vertex_old(aFlowline_in, dThreshold_in=1.0E-6):
aVertex = list()
nFlowline = len(aFlowline_in)
aVertex=list(aVertex)
for i in range(0, nFlowline):
pFlowline = aFlowline_in[i]
pVertex_start = pFlowline.pVertex_start
pVertex_end = pFlowline.pVertex_end
pVertex_end = pFlowline.pVertex_end
aVertex, dummy = add_unique_vertex(aVertex, pVertex_start,dThreshold_in)
aVertex, dummy = add_unique_vertex(aVertex, pVertex_end, dThreshold_in)
pass
Expand Down
Loading

0 comments on commit a833183

Please sign in to comment.