Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

将下述代码转换为类、函数形式:

  1. User

    将下述代码转换为类、函数形式:

  2. Assistant

    class Calculator: def init(self, x, y): self.x = x self.y = y

    def add(self):
        return self.x + self.y
    
    def subtract(self):
        return self.x - self.y
    
    def multiply(self):
        return self.x * self.y
    
    def divide(self):
        if self.y != 0:
            return self.x / self.y
        else:
            raise ValueError("除数不能为零")
    
  3. User

    import arcpy import os import glob import re

    from arcpy.sa import *

    arcpy.CheckOutExtension('Spatial') arcpy.env.workspace = (r"F:\GEE\liangzhongchang\mask\ndvi") folder =arcpy.env.workspace[-4:] folder_name = "reclassify" + folder folder_path = "F:\GEE\liangzhongchang\mask" output_path = os.path.join(folder_path,folder_name) if not os.path.exists(output_path): os.mkdir(output_path)

    print (output_path)

    rasterlist = arcpy.ListRasters("*","tif")

    rasters = glob.glob(os.path.join(arcpy.env.workspace, "*.tif"))

    for raster in rasterlist: inRaster = arcpy.Raster(raster) print(inRaster) time = inRaster.name[:8] print(time) out = os.path.join(output_path, time + ".tif") print(out)

    # NDVI
    outCon = Con(inRaster<=0.19,1,
    Con((inRaster>0.19) & (inRaster<=0.39),2,
    Con((inRaster>0.39) & (inRaster<=0.55),3,
    Con((inRaster>0.55) & (inRaster<=0.75),4,
    Con((inRaster>0.75),5)))))
    
    # # GNDVI
    # outCon = Con(inRaster<=0.2,1,
    # Con((inRaster>0.2) & (inRaster<=0.4),2,
    # Con((inRaster>0.4) & (inRaster<=0.6),3,
    # Con((inRaster>0.6) & (inRaster<=0.8),4,
    # Con((inRaster>0.8),5)))))
    
    # # vci
    # outCon = Con(inRaster<=0.15,1,
    # Con((inRaster>0.15) & (inRaster<=0.3),2,
    # Con((inRaster>0.3) & (inRaster<=0.45),3,
    # Con((inRaster>0.45) & (inRaster<=0.6),4,
    # Con((inRaster>0.6),5)))))
    
    # # chlor
    # outCon = Con(inRaster<=12,1,
    # Con((inRaster>12) & (inRaster<=21),2,
    # Con((inRaster>21) & (inRaster<=32),3,
    # Con((inRaster>32) & (inRaster<=43),4,
    # Con((inRaster>43),5)))))
    
    outCon.save(out)
    
    print(str(raster) + "--down")
    

    print(" ---down--- ") 将上述代码转化为类、函数形式

  4. Assistant

    import arcpy import os

    class RasterProcessing: def init(self): self.workspace = "" self.folder_path = "" self.output_path = ""

    def set_workspace(self, workspace):
        self.workspace = workspace
        arcpy.CheckOutExtension('Spatial')
        arcpy.env.workspace = self.workspace
    
    def set_folder_path(self, folder_path):
        self.folder_path = folder_path
    
    def create_output_folder(self):
        folder = self.workspace[-4:]
        folder_name = "reclassify" + folder
        self.output_path = os.path.join(self.folder_path, folder_name)
        if not os.path.exists(self.output_path):
            os.mkdir(self.output_path)
    
    def process_rasters(self):
        raster_list = arcpy.ListRasters("*", "tif")
        for raster in raster_list:
            in_raster = arcpy.Raster(raster)
            time = in_raster.name[:8]
            out = os.path.join(self.output_path, time + ".tif")
    
            # NDVI
            out_con = arcpy.sa.Con(in_raster <= 0.19, 1,
                                   arcpy.sa.Con((in_raster > 0.19) & (in_raster <= 0.39), 2,
                                                arcpy.sa.Con((in_raster > 0.39) & (in_raster <= 0.55), 3,
                                                             arcpy.sa.Con((in_raster > 0.55) & (in_raster <= 0.75), 4,
                                                                          arcpy.sa.Con((in_raster > 0.75), 5)))))
    
    
            out_con.save(out)
    
            print(str(raster) + "--processed")
        print("---processing complete---")
    

    def main(): rp = RasterProcessing() rp.set_workspace(r"F:\GEE\liangzhongchang\mask\ndvi") rp.set_folder_path(r"F:\GEE\liangzhongchang\mask") rp.create_output_folder() rp.process_rasters()

    if name == "main": main()

  5. User

    将上述代码中NDVI、chor、vci转化成可选择的方式

  6. Assistant

    import arcpy import os

    class RasterProcessing: def init(self): self.workspace = "" self.folder_path = "" self.output_path = ""

    def set_workspace(self, workspace):
        self.workspace = workspace
        arcpy.CheckOutExtension('Spatial')
        arcpy.env.workspace = self.workspace
    
    def set_folder_path(self, folder_path):
        self.folder_path = folder_path
    
    def create_output_folder(self):
        folder = self.workspace[-4:]
        folder_name = "reclassify" + folder
        self.output_path = os.path.join(self.folder_path, folder_name)
        if not os.path.exists(self.output_path):
            os.mkdir(self.output_path)
    
    def process_rasters(self, index):
        raster_list = arcpy.ListRasters("*","tif")
        for raster in raster_list:
            in_raster = arcpy.Raster(raster)
            time = in_raster.name[:8]
            out = os.path.join(self.output_path, time + ".tif")
    
            if index == 1:
                # NDVI
                out_con = arcpy.sa.Con(in_raster <= 0.19, 1,
                                       arcpy.sa.Con((in_raster > 0.19) & (in_raster <= 0.39), 2,
                                                    arcpy.sa.Con((in_raster > 0.39) & (in_raster <= 0.55), 3,
                                                                 arcpy.sa.Con((in_raster > 0.55) & (in_raster <= 0.75), 4,
                                                                              arcpy.sa.Con((in_raster > 0.75), 5))))))
            elif index == 2:
                # VCI
                out_con = arcpy.sa.Con(in_raster <= 0.15,1,
                                       arcpy.sa.Con((in_raster > 0.15) & (in_raster <= 0.3), 2,
                                                    arcpy.sa.Con((in_raster > 0.3) & (in_raster <= 0.45), 3,
                                                                 arcpy.sa.Con((in_raster > 0.45) & (in_raster <= 0.6), 4,
                                                                              arcpy.sa.Con((in_raster > 0.6), 5))))))
    
            elif index == 3:
                # Chlor
                out_con = arcpy.sa.Con(in_raster <= 12, 1,
                                       arcpy.sa.Con((in_raster > 12) & (in_raster <= 21), 2,
                                                    arcpy.sa.Con((in_raster > 21) & (in_raster <= 32), 3,
                                                                 arcpy.sa.Con((in_raster > 32) & (in_raster <= 43), 4,
                                                                              arcpy.sa.Con((in_raster > 43), 5))))))
    
            out_con.save(out)
    
            print(str(raster) + "--processed")
        print("---processing complete---")
    

    def main(): rp = RasterProcessing() rp.set_workspace(r"F:\GEE\liangzhongchang\mask\ndvi") rp.set_folder_path(r"F:\GEE\liangzhongchang\mask") rp.create_output_folder()

    while True:
        print("Select an index:")
        print("1. NDVI")
        print("2. VCI")
        print("3. Chlor")
        index = int(input("Enter the index: "))
    
        if index in [1, 2, 3]:
            break
        else:
            print("Invalid index. Please try again.")
    
    rp.process_rasters(index)
    

    if name == "main": main()

  7. User

    将上述代码和下述代码合并在一块

  8. Assistant

    import arcpy import os

    class RasterProcessing: def init(self): self.workspace = "" self.folder_path = "" self.output_path = ""

    def set_workspace(self, workspace):
        self.workspace = workspace
        arcpy.CheckOutExtension('Spatial')
        arcpy.env.workspace = self.workspace
    
    def set_folder_path(self, folder_path):
        self.folder_path = folder_path
    
    def create_output_folder(self):
        folder = self.workspace[-4:]
        folder_name = "reclassify" + folder
        self.output_path = os.path.join(self.folder_path, folder_name)
        if not os.path.exists(self.output_path):
            os.mkdir(self.output_path)
    
    def process_rasters(self, index):
        raster_list = arcpy.ListRasters("*","tif")
        for raster in raster_list:
            in_raster = arcpy.Raster(raster)
            time = in_raster.name[:8]
            out = os.path.join(self.output_path, time + ".tif")
    
            if index == 1:
                # NDVI
                out_con = arcpy.sa.Con(in_raster <= 0.19, 1,
                                       arcpy.sa.Con((in_raster > 0.19) & (in_raster <= 0.39), 2,
                                                    arcpy.sa.Con((in_raster > 0.39) & (in_raster <= 0.55), 3,
                                                                 arcpy.sa.Con((in_raster > 0.55) & (in_raster <= 0.75), 4,
                                                                              arcpy.sa.Con((in_raster > 0.75), 5))))))
    
            elif index == 2:
                # VCI
                out_con = arcpy.sa.Con(in_raster <= 0.15,1,
                                       arcpy.sa.Con((in_raster > 0.15) & (in_raster <= 0.3), 2,
                                                    arcpy.sa.Con((in_raster > 0.3) & (in_raster <= 0.45), 3,
                                                                 arcpy.sa.Con((in_raster > 0.45) & (in_raster <= 0.6), 4,
                                                                              arcpy.sa.Con((in_raster > 0.6), 5))))))
    
            elif index == 3:
                # Chlor
                out_con = arcpy.sa.Con(in_raster <= 12, 1,
                                       arcpy.sa.Con((in_raster > 12) & (in_raster <= 21), 2,
                                                    arcpy.sa.Con((in_raster > 21) & (in_raster <= 32), 3,
                                                                 arcpy.sa.Con((in_raster > 32) & (in_raster <= 43), 4,
                                                                              arcpy.sa.Con((in_raster > 43), 5))))))
    
            out_con.save(out)
    
            print(str(raster) + "--processed")
        print("---processing complete---")
    

    def main(): rp = RasterProcessing() rp.set_workspace(r"F:\GEE\liangzhongchang\mask\ndvi") rp.set_folder_path(r"F:\GEE\liangzhongchang\mask") rp.create_output_folder()

    while True:
        print("Select an index:")
        print("1. NDVI")
        print("2. VCI")
        print("3. Chlor")
        index = int(input("Enter the index: "))
    
        if index in [1, 2, 3]:
            break
        else:
            print("Invalid index. Please try again.")
    
    rp.process_rasters(index)
    

    if name == "main": main()

    import arcpy import os import glob import re

    from arcpy.sa import *

    class RasterProcessing: def init(self): self.workspace = "" self.folder_path = "" self.output_path = ""

    def set_workspace(self, workspace):
        self.workspace = workspace
        arcpy.CheckOutExtension('Spatial')
        arcpy.env.workspace = self.workspace
    
    def set_folder_path(self, folder_path):
        self.folder_path = folder_path
    
    def create_output_folder(self):
        folder = self.workspace[-4:]
        folder_name = "reclassify" + folder
        self.output_path = os.path.join(self.folder_path, folder_name)
        if not os.path.exists(self.output_path):
            os.mkdir(self.output_path)
    
    def process_rasters(self):
        raster_list = arcpy.ListRasters("*","tif")
        for raster in raster_list:
            in_raster = arcpy.Raster(raster)
            time = in_raster.name[:8]
            out = os.path.join(self.output_path, time + ".tif")
    
            # NDVI
            out_con = arcpy.sa.Con(in_raster<=0.19,1,
                                   arcpy.sa.Con((in_raster>0.19) & (in_raster<=0.39),2,
                                                arcpy.sa.Con((in_raster>0.39) & (in_raster<=0.55),3,
                                                             arcpy.sa.Con((in_raster>0.55) & (in_raster<=0.75),4,
                                                                          arcpy.sa.Con((in_raster>0.75),5)))))
    
    
            out_con.save(out)
    
            print(str(raster) + "--processed")
        print("---processing complete---")
    

    def main(): rp = RasterProcessing() rp.set_workspace(r"F:\GEE\liangzhongchang\mask\ndvi") rp.set_folder_path(r"F:\GEE\liangzhongchang\mask") rp.create_output_folder() rp.process_rasters()

    if name == "main": main()