import arcpy import os import glob import re from arcpy.sa import *…
- 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--- ") 将上述代码转化成类函数形式、
- Assistant
import arcpy import os import glob import re
from arcpy.sa import *
arcpy.CheckOutExtension('Spatial')
class RasterReclassify: def init(self, workspace): # Set workspace arcpy.env.workspace = workspace
- User
将上述NDVI,GNDVI,chor转换成可选择的形式
- Assistant
- User
将上述NDVI,GNDVI,chor转换成可选择的形式并将相应的结果分别输出
- Assistant
- User
vscode python 代码自动对齐
- Assistant
在 Visual Studio Code 中,可以使用快捷键
Shift + Alt + F来自动对齐代码。这个快捷键可以在打开的 Python 文件中应用于整个文件或者你所选中的代码块。如果你想要自定义对齐规则,可以在 Visual Studio Code 的设置中找到
Editor: Format On Save选项,并将其设置为 true。然后每次在保存文件时,代码将会自动对齐。另外,你也可以安装一些格式化插件,如
autopep8或black,它们可以根据预定义的格式化规则对代码进行自动对齐。安装插件后,可以使用Ctrl + Shift + P快捷键打开命令面板,然后输入相应的命令来对代码进行格式化。 - User
coding:utf-8
import arcpy import os import re from arcpy import env
栅格文件所在文件夹
input_raster_path = r"F:\GEE\liangzhongchang\mask\reclassifyndvi" # type: str
输出 shp 文件所在文件夹
folder =input_raster_path[-4:] folder_name = "polygon" + folder folder_path = "F:\GEE\liangzhongchang\mask" output_shp_path = os.path.join(folder_path,folder_name) if not os.path.exists(output_shp_path): os.mkdir(output_shp_path)
读取 input_raster_path 中的 raster 文件
arcpy.env.workspace = input_raster_path rasters = arcpy.ListFiles("*.tif") for raster in rasters: # 原 raster 文件名格式形如 ntwd2010.tif # 提取文件名,作为输出文件的文件名 # print raster catchment_name = raster[:8] print catchment_name
shp_out_name = os.path.join(output_shp_path,catchment_name) print shp_out_name arcpy.RasterToPolygon_conversion(raster, shp_out_name, "true", "Value") print "finished"转换成类、函数形式
- Assistant
coding:utf-8
import arcpy import os import re from arcpy import env
class RasterToPolygonConverter: def init(self, input_raster_path, output_shp_path): self.input_raster_path = input_raster_path self.output_shp_path = output_shp_path
def convert_raster_to_polygon(self): # Set workspace arcpy.env.workspace = self.input_raster_path # Create output folder if it doesn't exist if not os.path.exists(self.output_shp_path): os.mkdir(self.output_shp_path) # List raster files rasters = arcpy.ListFiles("*.tif") for raster in rasters: # Extract catchment name from raster filename catchment_name = raster[:8] print(catchment_name) # Set output shapefile path shp_out_name = os.path.join(self.output_shp_path, catchment_name) print(shp_out_name) # Convert raster to polygon arcpy.RasterToPolygon_conversion(raster, shp_out_name, "true", "Value") print("finished")Example usage
- User
python vscode 自动对齐
- Assistant
在 Visual Studio Code 中,可以通过安装合适的 Python 代码格式化插件来实现自动对齐。以下是几个常用的插件:
-
Pylance: Microsoft 开发的插件,提供强大的语法检查和代码提示功能。可以自动对齐代码。
-
Python: Visual Studio Code 内置的 Python 插件,提供基本的语法检查和代码提示功能,也支持自动对齐代码。
-
autopep8: 一个流行的 Python 代码格式化工具,可以根据 PEP 8 标准自动对齐代码。可以通过
pip install autopep8安装。 -
black: 另一个流行的 Python 代码格式化工具,可以生成一致且美观的 Python 代码。可以通过
pip install black安装。
你可以根据自己的需求选择安装合适的插件,并根据插件的设置进行相应的配置。如果你已经安装了相应的插件,可以使用
Ctrl + Shift + P快捷键打开命令面板,然后输入插件名称来查找插件提供的自动对齐功能。 -