Skip to content

Commit

Permalink
update output field names
Browse files Browse the repository at this point in the history
  • Loading branch information
changliao1025 committed Aug 17, 2023
1 parent b0ece5d commit fa18496
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
8 changes: 4 additions & 4 deletions pyflowline/algorithms/merge/merge_flowline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

lID = 0

def merge_flowline(aFlowline_in, aVertex_in, \
pVertex_outlet_in, \
aIndex_headwater_in,\
aIndex_middle_in, \
def merge_flowline(aFlowline_in, aVertex_in,
pVertex_outlet_in,
aIndex_headwater_in,
aIndex_middle_in,
aIndex_confluence_in ):

nVertex=len(aVertex_in)
Expand Down
35 changes: 25 additions & 10 deletions pyflowline/classes/basin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
else:
from pyflowline.algorithms.auxiliary.find_vertex_in_list import find_vertex_in_list


iFlag_kml = importlib.util.find_spec("simplekml")
if iFlag_kml is not None:
from pyflowline.external.pyearth.gis.kml.convert_geojson_to_kml import convert_geojson_to_kml
else:
pass
sys.setrecursionlimit(10000)

class BasinClassEncoder(JSONEncoder):
Expand Down Expand Up @@ -310,6 +314,8 @@ def __init__(self, aParameter):
self.sFilename_variable_polygon = os.path.join(str(self.sWorkspace_output_basin ), "variable_polygon.geojson" )
self.sFilename_variable_polyline = os.path.join(str(self.sWorkspace_output_basin ), "variable_polyline.geojson" )

#kml
self.sFilename_flowline_conceptual_kml = os.path.join(str(self.sWorkspace_output_basin ),'flowline_conceptual.kml')
return

def flowline_simplification(self):
Expand Down Expand Up @@ -552,21 +558,21 @@ def flowline_simplification(self):
ptimer.stop()
if self.iFlag_debug ==1:
sFilename_out = self.sFilename_flowline_segment_index_before_intersect
export_flowline_to_geojson( aFlowline_basin_simplified,
export_flowline_to_geojson(aFlowline_basin_simplified,
sFilename_out,
aAttribute_data=[aStream_segment],
aAttribute_field=['segment'],
aAttribute_field=['iStream_segment'],
aAttribute_dtype=['int'])

#build stream order
ptimer.start()
aFlowline_basin_simplified, aStream_order = define_stream_order(aFlowline_basin_simplified)
ptimer.stop()
sFilename_out = self.sFilename_flowline_simplified
export_flowline_to_geojson( aFlowline_basin_simplified,
export_flowline_to_geojson(aFlowline_basin_simplified,
sFilename_out,
aAttribute_data=[aStream_segment, aStream_order],
aAttribute_field=['segment','order'],
aAttribute_field=['iStream_segment','iStream_order'],
aAttribute_dtype=['int','int'])

if self.iFlag_break_by_distance==1:
Expand Down Expand Up @@ -648,7 +654,7 @@ def reconstruct_topological_relationship(self, iMesh_type, sFilename_mesh):
sFilename_out = 'flowline_edge_correct_flowline_direction.geojson'
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
export_flowline_to_geojson( aFlowline_basin_conceptual, sFilename_out)
aFlowline_basin_conceptual = remove_flowline_loop( aFlowline_basin_conceptual )
aFlowline_basin_conceptual = remove_flowline_loop(aFlowline_basin_conceptual )
if self.iFlag_debug ==1:
sFilename_out = 'flowline_edge_remove_flowline_loop.geojson'
sFilename_out = os.path.join(sWorkspace_output_basin, sFilename_out)
Expand Down Expand Up @@ -688,7 +694,7 @@ def reconstruct_topological_relationship(self, iMesh_type, sFilename_mesh):
export_flowline_to_geojson( aFlowline_basin_conceptual,
sFilename_out,
aAttribute_data=[aStream_segment, aStream_order],
aAttribute_field=['segment','order'],
aAttribute_field=['iStream_segment','iStream_order'],
aAttribute_dtype=['int','int'])

self.aFlowline_basin_conceptual = aFlowline_basin_conceptual
Expand Down Expand Up @@ -783,7 +789,16 @@ def export(self):
"""
self.export_basin_info_to_json()
self.export_flowline_info_to_json()
self.export_confluence_info_to_json()
self.export_confluence_info_to_json()

if iFlag_kml is not None:
#only convert final conceptual flowline to kml
sFilename_conceptual = self.sFilename_flowline_conceptual
sFilename_conceptual_kml = self.sFilename_flowline_conceptual_kml

convert_geojson_to_kml(sFilename_conceptual, sFilename_conceptual_kml)


return

def export_flowline(self, aFlowline_in, sFilename_json_in,iFlag_projected_in = None, pSpatial_reference_in = None):
Expand All @@ -796,8 +811,8 @@ def export_flowline(self, aFlowline_in, sFilename_json_in,iFlag_projected_in = N
iFlag_projected_in (int, optional): Flag if re-projection is needed. Defaults to None.
pSpatial_reference_in (object, optional): The spatial reference if re-projection is needed. Defaults to None.
"""
export_flowline_to_geojson(aFlowline_in, sFilename_json_in,\
iFlag_projected_in= iFlag_projected_in, \
export_flowline_to_geojson(aFlowline_in, sFilename_json_in,
iFlag_projected_in= iFlag_projected_in,
pSpatial_reference_in = pSpatial_reference_in)

return
Expand Down
39 changes: 25 additions & 14 deletions pyflowline/classes/pycase.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,19 @@ def mesh_generation(self, iFlag_antarctic_in=None):
dLatitude_bot = self.dLatitude_bot
dLongitude_left = self.dLongitude_left
dLongitude_right = self.dLongitude_right
sWorkspace_output = self.sWorkspace_output + slash + 'dggrid'


if self.iFlag_standalone == 1:
sWorkspace_output = self.sWorkspace_output + slash + 'dggrid'
pass
else:
sWorkspace_output = self.sWorkspace_output + slash + '..'+ slash + 'dggrid'

sWorkspace_output = os.path.abspath(sWorkspace_output)
pass

if not os.path.exists(sWorkspace_output):
os.makedirs(sWorkspace_output)

if iFlag_mesh_boundary ==1:
#create a polygon based on
Expand Down Expand Up @@ -800,12 +812,7 @@ def mesh_generation(self, iFlag_antarctic_in=None):
aCell_out = read_mesh_json_w_topology(iMesh_type, self.sFilename_mesh)
pass

#convert the mesh into the kml format so it can be visualized in google earth and google map


if iFlag_kml is not None:
convert_geojson_to_kml(self.sFilename_mesh, self.sFilename_mesh_kml)

print('Finish mesh generation.')
return aCell_out

Expand Down Expand Up @@ -936,14 +943,13 @@ def setup(self):

if self.iFlag_dggrid == 1:
#create dggrid output folder
sWorkspace_output = self.sWorkspace_output + slash + 'dggrid'

#if (os.path.exists(sWorkspace_output)):
# sCommand = 'rm -rf ' + sWorkspace_output
# print(sCommand)
# p = subprocess.Popen(sCommand, shell= True)
# p.wait()
if self.iFlag_standalone == 1:
sWorkspace_output = self.sWorkspace_output + slash + 'dggrid'
else:
sWorkspace_output = self.sWorkspace_output + slash + '..'+ slash + 'dggrid'
sWorkspace_output = os.path.abspath(sWorkspace_output)


Path(sWorkspace_output).mkdir(parents=True, exist_ok=True)
#then copy the binary file to the folder
#copy execulate
Expand Down Expand Up @@ -989,6 +995,11 @@ def export(self):
Export the model outputs
"""
self.export_mesh_info_to_json()
#convert the mesh into the kml format so it can be visualized in google earth and google map
#shoule move this to the export function
if iFlag_kml is not None:
convert_geojson_to_kml(self.sFilename_mesh, self.sFilename_mesh_kml)

if self.iFlag_flowline ==1:
for pBasin in self.aBasin:
pBasin.export()
Expand Down Expand Up @@ -1117,7 +1128,7 @@ def export_basin_config_to_json(self, sFilename_output_in= None):
else:
#use current output path
sName = 'configuration_basin.json'
sFilename_output = os.path.join( self.sWorkspace_output , sName)
sFilename_output = os.path.join( self.sWorkspace_output, sName)

#all basins
with open(sFilename_output, 'w', encoding='utf-8') as f:
Expand Down
15 changes: 10 additions & 5 deletions pyflowline/external/pyearth/gis/kml/convert_geojson_to_kml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ def convert_geojson_to_kml(sFilename_geojson, sFilename_kml):
while feature:
# Get the geometry of the feature
geometry = feature.GetGeometryRef()

sGeometry_type = geometry.GetGeometryName()
# Convert the geometry to WKT
wkt = geometry.ExportToWkt()

# Create a KML placemark and add it to the KML object
polygon = kml.newpolygon()
polygon.geometry = simplekml.Geometry(wkt=wkt)
if(sGeometry_type == 'POLYGON'):
# Create a KML placemark and add it to the KML object
polygon = kml.newpolygon()
polygon.geometry = simplekml.Geometry(wkt=wkt)
else:
if (sGeometry_type == 'LINESTRING'):
# Create a KML placemark and add it to the KML object
linestring = kml.newlinestring()
linestring.geometry = simplekml.Geometry(wkt=wkt)

# Move to the next feature
feature = layer.GetNextFeature()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def map_multiple_vector_data(aFiletype_in,
if iDPI_in is not None:
iDPI = iDPI_in
else:
iDPI = 600
iDPI = 300

if dMissing_value_in is not None:
dMissing_value = dMissing_value_in
Expand Down Expand Up @@ -452,5 +452,7 @@ def map_multiple_vector_data(aFiletype_in,
plt.savefig(sFilename_out, bbox_inches='tight')
else:
plt.savefig(sFilename_out, bbox_inches='tight', format ='ps')
plt.close('all')
plt.clf()

#clean cache
plt.close('all')
plt.clf()
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,7 @@ def map_vector_polygon_data(sFilename_in,
plt.savefig(sFilename_out, bbox_inches='tight')
else:
plt.savefig(sFilename_out, bbox_inches='tight', format ='ps')
plt.close('all')
plt.clf()

#clean cache
plt.close('all')
plt.clf()
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def map_vector_polyline_data(sFilename_in,
globe=None)


fig = plt.figure( dpi=300)
fig = plt.figure( dpi = iDPI )
iSize_x= 8
iSize_y= 8
fig.set_figwidth( iSize_x )
Expand Down Expand Up @@ -312,5 +312,6 @@ def map_vector_polyline_data(sFilename_in,
plt.savefig(sFilename_out, bbox_inches='tight')
else:
plt.savefig(sFilename_out, bbox_inches='tight', format ='ps')
plt.close('all')
plt.clf()
#clean
plt.close('all')
plt.clf()
6 changes: 3 additions & 3 deletions pyflowline/formats/export_flowline.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def export_flowline_to_geojson( aFlowline_in,

pLayer_json = pDataset_json.CreateLayer('flowline', pSpatial_reference_in, ogr.wkbLineString)
# Add one attribute
pLayer_json.CreateField(ogr.FieldDefn('id', ogr.OFTInteger64)) #long type for high resolution
pLayer_json.CreateField(ogr.FieldDefn('flowlineid', ogr.OFTInteger64)) #long type for high resolution

#add the other fields
if iFlag_attribute ==1:
Expand Down Expand Up @@ -94,8 +94,8 @@ def export_flowline_to_geojson( aFlowline_in,
pGeometry_out = ogr.CreateGeometryFromWkb(dummy1.wkb)
pFeature_out.SetGeometry(pGeometry_out)

pFeature_out.SetField("id", lID)
if iFlag_attribute ==1:
pFeature_out.SetField("flowlineid", lID)
if iFlag_attribute == 1:
for k in range(nAttribute1):
sField = aAttribute_field[k]
dtype = aAttribute_dtype[k]
Expand Down

0 comments on commit fa18496

Please sign in to comment.