def PostProcessTauDEM(dstdir): tauDir = dstdir + os.sep + DIR_NAME_TAUDEM streamNetFile = tauDir + os.sep + streamNet subbasinFile = tauDir + os.sep + subbasinM flowDirFileTau = tauDir + os.sep + flowDirM streamRasterFile = tauDir + os.sep + streamRasterM reachDir = dstdir + os.sep + DIR_NAME_REACH if not os.path.exists(reachDir): os.mkdir(reachDir) outputReachFile = reachDir + os.sep + reachesOut outputSubbasinFile = dstdir + os.sep + subbasinOut outputFlowDirFile = dstdir + os.sep + flowDirOut outputStreamLinkFile = dstdir + os.sep + streamLinkOut subbasinDir = dstdir + os.sep + DIR_NAME_SUBBSN if not os.path.exists(subbasinDir): os.mkdir(subbasinDir) subbasinVectorFile = subbasinDir + os.sep + subbasinVec idMap = SerializeStreamNet(streamNetFile, outputReachFile) SerializeSubbasin(subbasinFile, streamRasterFile, idMap, outputSubbasinFile, outputStreamLinkFile) ## Change TauDEM code to ArcGIS. Now, it is deprecated, By LJ. if(isTauDEM): shutil.copy(flowDirFileTau, outputFlowDirFile) else: ChangeFlowDir(flowDirFileTau, outputFlowDirFile) accFile = dstdir + os.sep + accM chwidthFile = dstdir + os.sep + chwidthName width = chwidth(accFile, chwidthFile) AddWidthToReach(outputReachFile, outputStreamLinkFile, width) print "Generating subbasin vector..." GenerateSubbasinVector(dstdir, outputSubbasinFile, subbasinVectorFile) maskFile = dstdir + os.sep + mask_to_ext basinVector = dstdir + os.sep + basinVec # basinVectorTmp = dstdir + os.sep + "basin_tmp.shp" # RemoveShpFile(basinVectorTmp) RemoveShpFile(basinVector) strCmd = '%s %s/gdal_polygonize.py -f "ESRI Shapefile" %s %s %s %s' % \ (sys.executable, PREPROC_SCRIPT_DIR, maskFile, basinVector, "basin", FLD_BASINID) os.system(strCmd)
def PostProcessTauDEM(dstdir): tauDir = dstdir + os.sep + DIR_NAME_TAUDEM streamNetFile = tauDir + os.sep + streamNet subbasinFile = tauDir + os.sep + subbasinM flowDirFileTau = tauDir + os.sep + flowDirM streamRasterFile = tauDir + os.sep + streamRasterM reachDir = dstdir + os.sep + DIR_NAME_REACH if not os.path.exists(reachDir): os.mkdir(reachDir) outputReachFile = reachDir + os.sep + reachesOut outputSubbasinFile = dstdir + os.sep + subbasinOut outputFlowDirFile = dstdir + os.sep + flowDirOut outputStreamLinkFile = dstdir + os.sep + streamLinkOut subbasinDir = dstdir + os.sep + DIR_NAME_SUBBSN if not os.path.exists(subbasinDir): os.mkdir(subbasinDir) subbasinVectorFile = subbasinDir + os.sep + subbasinVec idMap = SerializeStreamNet(streamNetFile, outputReachFile) SerializeSubbasin(subbasinFile, streamRasterFile, idMap, outputSubbasinFile, outputStreamLinkFile) # Change TauDEM code to ArcGIS. Now, it is deprecated, By LJ. if(isTauDEM): shutil.copy(flowDirFileTau, outputFlowDirFile) else: ChangeFlowDir(flowDirFileTau, outputFlowDirFile) accFile = dstdir + os.sep + accM chwidthFile = dstdir + os.sep + chwidthName width = chwidth(accFile, chwidthFile) AddWidthToReach(outputReachFile, outputStreamLinkFile, width) print "Generating subbasin vector..." GenerateSubbasinVector(outputSubbasinFile, subbasinVectorFile, "subbasin", FLD_SUBBASINID) maskFile = dstdir + os.sep + mask_to_ext basinVector = dstdir + os.sep + basinVec print "Generating basin vector..." GenerateSubbasinVector(maskFile, basinVector, "basin", FLD_BASINID)
def PostProcessTauDEM(dstdir): tauDir = dstdir + os.sep + "taudir" streamNetFile = tauDir + os.sep + streamNet subbasinFile = tauDir + os.sep + subbasinM flowDirFileTau = tauDir + os.sep + flowDirM streamRasterFile = tauDir + os.sep + streamRasterM reachDir = dstdir + os.sep + "reaches" if not os.path.exists(reachDir): os.mkdir(reachDir) outputReachFile = reachDir + os.sep + reachesOut outputSubbasinFile = dstdir + os.sep + subbasinOut outputFlowDirFile = dstdir + os.sep + flowDirOut outputStreamLinkFile = dstdir + os.sep + streamLinkOut subbasinDir = dstdir + os.sep + "subbasins" if not os.path.exists(subbasinDir): os.mkdir(subbasinDir) subbasinVectorFile = subbasinDir + os.sep + subbasinVec idMap = SerializeStreamNet(streamNetFile, outputReachFile) SerializeSubbasin(subbasinFile, streamRasterFile, idMap, \ outputSubbasinFile, outputStreamLinkFile) ChangeFlowDir(flowDirFileTau, outputFlowDirFile) accFile = dstdir + os.sep + accM chwidthFile = dstdir + os.sep + chwidthName width = chwidth(accFile, chwidthFile) AddWidthToReach(outputReachFile, outputStreamLinkFile, width) print "Generating subbasin vector..." GenerateSubbasinVector(dstdir, outputSubbasinFile, subbasinVectorFile) maskFile = dstdir + os.sep + mask_to_ext basinVectorTmp = dstdir + os.sep + "basin_tmp.shp" util.RemoveShpFile(basinVectorTmp) strCmd = 'python %s/gdal_polygonize.py -f "ESRI Shapefile" %s %s' % ( PREPROC_SCRIPT_DIR, maskFile, basinVectorTmp) os.system(strCmd) ds = ogr.Open(basinVectorTmp) layer = ds.GetLayer(0) layerDef = layer.GetLayerDefn() basinVector = dstdir + os.sep + basinVec drv = ogr.GetDriverByName("ESRI Shapefile") util.RemoveShpFile(basinVector) dsNew = drv.CreateDataSource(basinVector) lyrName = "basin" lyr = dsNew.CreateLayer(lyrName, layer.GetSpatialRef(), ogr.wkbPolygon) newField = ogr.FieldDefn("Subbasin", ogr.OFTInteger) lyr.CreateField(newField) layerDefBasin = lyr.GetLayerDefn() newFt = ogr.Feature(layerDefBasin) geom = ogr.Geometry(ogr.wkbPolygon) ft = layer.GetNextFeature() while ft is not None: geom.AddGeometry(ft.GetGeometryRef().GetGeometryRef(0)) ft = layer.GetNextFeature() newFt.SetGeometry(geom) newFt.SetField("Subbasin", 1) lyr.CreateFeature(newFt) lyr.SyncToDisk() dsNew.Destroy() ds.Destroy()
def PostProcessTauDEM(dstdir): tauDir = dstdir + os.sep + "taudir" streamNetFile = tauDir + os.sep + streamNet subbasinFile = tauDir + os.sep + subbasinM flowDirFileTau = tauDir + os.sep + flowDirM streamRasterFile = tauDir + os.sep + streamRasterM reachDir = dstdir + os.sep + "reaches" if not os.path.exists(reachDir): os.mkdir(reachDir) outputReachFile = reachDir + os.sep + reachesOut outputSubbasinFile = dstdir + os.sep + subbasinOut outputFlowDirFile = dstdir + os.sep + flowDirOut outputStreamLinkFile = dstdir + os.sep + streamLinkOut subbasinDir = dstdir + os.sep + "subbasins" if not os.path.exists(subbasinDir): os.mkdir(subbasinDir) subbasinVectorFile = subbasinDir + os.sep + subbasinVec idMap = SerializeStreamNet(streamNetFile, outputReachFile) SerializeSubbasin(subbasinFile, streamRasterFile, idMap, outputSubbasinFile, outputStreamLinkFile) ChangeFlowDir(flowDirFileTau, outputFlowDirFile) accFile = dstdir + os.sep + accM chwidthFile = dstdir + os.sep + chwidthName width = chwidth(accFile, chwidthFile) AddWidthToReach(outputReachFile, outputStreamLinkFile, width) print "Generating subbasin vector..." GenerateSubbasinVector(dstdir, outputSubbasinFile, subbasinVectorFile) maskFile = dstdir + os.sep + mask_to_ext basinVectorTmp = dstdir + os.sep + "basin_tmp.shp" util.RemoveShpFile(basinVectorTmp) strCmd = 'python %s/gdal_polygonize.py -f "ESRI Shapefile" %s %s' % (PREPROC_SCRIPT_DIR, maskFile, basinVectorTmp) os.system(strCmd) ds = ogr.Open(basinVectorTmp) layer = ds.GetLayer(0) layerDef = layer.GetLayerDefn() basinVector = dstdir + os.sep + basinVec drv = ogr.GetDriverByName("ESRI Shapefile") util.RemoveShpFile(basinVector) dsNew = drv.CreateDataSource(basinVector) lyrName = "basin" lyr = dsNew.CreateLayer(lyrName, layer.GetSpatialRef(), ogr.wkbPolygon) newField = ogr.FieldDefn("Subbasin", ogr.OFTInteger) lyr.CreateField(newField) layerDefBasin = lyr.GetLayerDefn() newFt = ogr.Feature(layerDefBasin) geom = ogr.Geometry(ogr.wkbPolygon) ft = layer.GetNextFeature() while ft is not None: geom.AddGeometry(ft.GetGeometryRef().GetGeometryRef(0)) ft = layer.GetNextFeature() newFt.SetGeometry(geom) newFt.SetField("Subbasin", 1) lyr.CreateFeature(newFt) lyr.SyncToDisk() dsNew.Destroy() ds.Destroy()