2014-02-14 34 views
1

我在嘗試使用Python提取shapefile表中每年不爲零的最小值時遇到問題。我的表格有「工作站ID」,「經度」,「緯度」和年份字段,其中F是第一個字母。例如:F1970,F1971,F1972 ...我打算循環遍歷每年的字段,然後將每年的所有值從最小值排序到最大值,然後得到第一條記錄不等於零,因爲我只想要值大於零。到目前爲止,我只能得到第一個記錄,即零。任何人都可以告訴我如何修改代碼,並獲得第一個最小值不爲零?非常感謝![我想獲得站點ID,經度和緯度與每年的最小值對應]Python - 如何獲得shapefile表中每年字段中不爲零的最小值?

# Obtain the list of all fields in the attribute table of the shapefile "Station" 
fields=arcpy.ListFields(Station) 
# Construct a for loop to iterate through all the year attribute in the input feature class,in order to find the record with the minimum value in each year. 
for field in fields: 
    year=str(field.name) 
    # find the year field 
    if ("F" in year): 
     where=year+" ASCENDING" 
     # Process: Sort 
     arcpy.Sort_management(Station, outputFC, where, "UR") 
     rows = arcpy.SearchCursor(outputFC) 
     row=rows.next 

     for row in rows: 
      # only got me the first record in each year, which is zero. 
      value=row.getValue(year) 
      stationID= row.getValue("Station_ID") 
      obsLon= row.getValue("Longitude") 
      obsLat=row.getValue("Latitude") 

      row=rows.next() 

      break 
     del row,rows 

回答

0

您可以使用SearchCursor來篩選行並對它們進行排序。

# Obtain the list of all fields in the attribute table of the shapefile "Station" 
fields=arcpy.ListFields(Station) 

# Construct a for loop to iterate through all the year attribute in the input feature class,in order to find the record with the minimum value in each year. 
for field in fields: 
    # find the year field 
    if ("F" in field.name): 
     year=str(field.name) 

     where_clause = year + " > 0" 
     sort_clause = year + " ASCENDING" 

     # Use a SearchCursor to filter and sort the rows 
     # Docs: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/SearchCursor/000v00000039000000/ 
     rows = arcpy.SearchCursor(Station, where_clause, "", "", "", sort_clause) 

     # only got the first record in each year, which is > zero. 
     row = rows.next() 
     value=row.getValue(year) 
     stationID= row.getValue("Station_ID") 
     obsLon= row.getValue("Longitude") 
     obsLat=row.getValue("Latitude") 

     print "Record:", year, value, stationID, obsLon, obsLat 

     del row,rows 
相關問題