0

我正在运行此代码,但出现缩进错误。似乎是什么问题?我跑了这么多次。当我通过 IDLE 运行代码时,我得到这个框,上面写着:

语法错误:您的程序中存在错误:需要一个缩进块

代码:

import arcpy
from arcpy import env
import math
folder_path = r"J:\sanda"
# Define workspace as your folder path 
env.workspace = folder_path 
# Allow overwriting output files
arcpy.env.overwriteOutput = True

#parameters as text
input_lake = arcpy.GetParameterAsText(0)
input_cities = arcpy.GetParameterAsText(1)
output_lake = arcpy.GetParameterAsText(2)

city= "CITY_NAME"
cntry= "CITY_CNTRY"
admin= "ADMIN_NAME"
pop_city= "Population"
dist_km= "Distance"
x_coord= "X_CORD"
y_coord= "Y_CORD"


#copy lakes shapefile
arcpy.CopyFeatures_management(input_lake, output_lake)

#Add fields (city_name, x coord, y coord, etc)
arcpy.AddField_management(output_lake, city, "TEXT")
arcpy.AddField_management(output_lake, cntry , "TEXT")
arcpy.AddField_management(output_lake, admin, "TEXT")
arcpy.AddField_management(output_lake, pop_city, "DOUBLE")
arcpy.AddField_management(output_lake, dist_km, "DOUBLE")
arcpy.AddField_management(output_lake, x_coord, "DOUBLE")
arcpy.AddField_management(output_lake, y_coord, "DOUBLE")


#create empty lists
citylist_city_name = []
citylist_X = []
citylist_Y = []
city_name = []
city_cntry = []
admin_name = []
dist_km= []
pop= []

#populate these lists with values
city_cursor= arcpy.SearchCursor(input_cities)
for city in city_cursor:
    geom = city.Shape
    citylist_X.append(geom.firstPoint.X)
    citylist_Y.append(geom.firstPoint.Y)
    citylist_city_name.append(city.CITY_NAME)
    city_cntry.append(city.CNTRY_NAME)
    admin_name.append(city.ADMIN_NAME)
    pop.append(city.Population)

#get the number of cities
city_length = len(citylist_X)

#read lake geometries
lake_cursor = arcpy.UpdateCursor(output_lake)

#loop through each lake
for lake in lake_cursor:
    lake_geom = lake.Shape

#initiate lake distances
city_dist_list = []

#loop through each city
for cityID in range(0, city_length - 1):

#get x and y for the current city
cityX=citylist_X[cityID]
cityY=citylist_Y[cityID]

#get x and y for the current lake
lakeX = lake_geom.centroid.X
lakeY = lake_geom.centroid.Y

       #calculate the distance
dist = math.sqrt((cityX-lakeX)**2 +(cityY-lakeY)**2
city_dist_list.append(dist)

    closest = min(city_dist_list)
    closestID = city_dist_list.index(closest)

    #set values into the new lake feature
    lake.CITY_NAME = citylist_city_name[closestID]
    lake.X_CORD = citylist_X [closestID]
    lake.Y_CORD = citylist_Y [closestID]
    lake.Distance = closest*(0.001)
    lake.ADMIN_NAME = admin_name [closestID]
    lake.Population = pop [closestID]
    lake.city_cntry = city_cntry [closestID]
    lake_cursor.updateRow(lake)

#kill the cursors
del city_cursor, lake_cursor, lake, city, cityID, geom, lake_geom

print "Done"
4

1 回答 1

3

代码中的 for 循环没有正确缩进,它必须是这样的:

for cityID in range(0, city_length - 1):
    #get x and y for the current city
    cityX=citylist_X[cityID]
    cityY=citylist_Y[cityID]

    #get x and y for the current lake
    lakeX = lake_geom.centroid.X
    lakeY = lake_geom.centroid.Y

       #calculate the distance
    dist = math.sqrt((cityX-lakeX)**2 +(cityY-lakeY)**2)
    city_dist_list.append(dist)

    closest = min(city_dist_list)
    closestID = city_dist_list.index(closest)

    #set values into the new lake feature
    lake.CITY_NAME = citylist_city_name[closestID]
    lake.X_CORD = citylist_X [closestID]
    lake.Y_CORD = citylist_Y [closestID]
    lake.Distance = closest*(0.001)
    lake.ADMIN_NAME = admin_name [closestID]
    lake.Population = pop [closestID]
    lake.city_cntry = city_cntry [closestID]
    lake_cursor.updateRow(lake)

其次,您错过了)这一行的结束:

dist = math.sqrt((cityX-lakeX)**2 +(cityY-lakeY)**2)
于 2013-05-06T06:21:08.067 回答