diff --git a/pyflowline/algorithms/merge/merge_flowline.py b/pyflowline/algorithms/merge/merge_flowline.py index 4099596..0827800 100644 --- a/pyflowline/algorithms/merge/merge_flowline.py +++ b/pyflowline/algorithms/merge/merge_flowline.py @@ -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) diff --git a/pyflowline/classes/basin.py b/pyflowline/classes/basin.py index 194bd56..40281ff 100644 --- a/pyflowline/classes/basin.py +++ b/pyflowline/classes/basin.py @@ -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): @@ -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): @@ -552,10 +558,10 @@ 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 @@ -563,10 +569,10 @@ def flowline_simplification(self): 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: @@ -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) @@ -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 @@ -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): @@ -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 diff --git a/pyflowline/classes/pycase.py b/pyflowline/classes/pycase.py index 12abb22..9f1d0a8 100644 --- a/pyflowline/classes/pycase.py +++ b/pyflowline/classes/pycase.py @@ -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 @@ -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 @@ -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 @@ -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() @@ -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: diff --git a/pyflowline/external/pyearth/gis/kml/convert_geojson_to_kml.py b/pyflowline/external/pyearth/gis/kml/convert_geojson_to_kml.py index 19b5c10..1a16b62 100644 --- a/pyflowline/external/pyearth/gis/kml/convert_geojson_to_kml.py +++ b/pyflowline/external/pyearth/gis/kml/convert_geojson_to_kml.py @@ -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() diff --git a/pyflowline/external/pyearth/visual/map/map_multiple_vector_data.py b/pyflowline/external/pyearth/visual/map/map_multiple_vector_data.py index 9101aae..bbc8208 100644 --- a/pyflowline/external/pyearth/visual/map/map_multiple_vector_data.py +++ b/pyflowline/external/pyearth/visual/map/map_multiple_vector_data.py @@ -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 @@ -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() diff --git a/pyflowline/external/pyearth/visual/map/map_vector_polygon_data.py b/pyflowline/external/pyearth/visual/map/map_vector_polygon_data.py index e0d5ba7..ebd7c30 100644 --- a/pyflowline/external/pyearth/visual/map/map_vector_polygon_data.py +++ b/pyflowline/external/pyearth/visual/map/map_vector_polygon_data.py @@ -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() diff --git a/pyflowline/external/pyearth/visual/map/map_vector_polyline_data.py b/pyflowline/external/pyearth/visual/map/map_vector_polyline_data.py index d626d90..596eb21 100644 --- a/pyflowline/external/pyearth/visual/map/map_vector_polyline_data.py +++ b/pyflowline/external/pyearth/visual/map/map_vector_polyline_data.py @@ -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 ) @@ -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() diff --git a/pyflowline/formats/export_flowline.py b/pyflowline/formats/export_flowline.py index 0f53416..c60eea0 100644 --- a/pyflowline/formats/export_flowline.py +++ b/pyflowline/formats/export_flowline.py @@ -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: @@ -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]