簡體   English   中英

如何在Python中從一個多邊形移動到另一個多邊形?

[英]how to move from one polygon to another in file in python?

shp中有3個多邊形。 文件。

需要找到每個的最小/最大坐標。 接下來我只能做1個:

import arcpy # csv
from arcpy import env

print "Creating and defining variables."
env.workspace = r"C:\Users\Desktop\data"
env.overwriteOutput = 1

theme = 'interestAreas.shp' 
# Look for .next()  in SearchCursor, need a loop
# the same when we read line by line
for i in theme:
    Curs = arcpy.da.SearchCursor(theme, 'SHAPE@').next()
    polygon = Curs[0]
    ext = polygon.extent
del Curs

# Find min X, Y and max X, Y for each polygon and write it to the file:
print 'xmin is: ', ext.XMin
print 'ymin is: ', ext.YMin
print 'xmax is: ', ext.XMax
print 'ymax is: ', ext.YMax
minX = ext.XMin
minY = ext.YMin

如何使用arcpy和loop進行一段時間的循環? 或如何在多邊形的ID(1,2,3)中移動?

謝謝您的幫助。

我不太習慣python,但是每種語言的邏輯基本相同...您可能應該做類似的事情(可能會有一些小的語法錯誤):

polygons = arcpy.SearchCursor(theme, 'SHAPE@')
curPolygon = polygons.next()
while Curs:
    polygon = Curs[0]
    ext = polygon.extent

    # Find min X, Y and max X, Y for each polygon and write it to the file:
    print 'xmin is: ', ext.XMin
    print 'ymin is: ', ext.YMin
    print 'xmax is: ', ext.XMax
    print 'ymax is: ', ext.YMax
    minX = ext.XMin
    minY = ext.YMin

    # now look for next polygon description
    curPolygon = polygons.next()

進一步閱讀請點擊這里

您的原始代碼非常接近。 您只需要移動並重新調整for循環即可在光標(而不是shapefile)上進行迭代。 我還建議在游標對象上使用with ,這樣就無需管理它。

import arcpy # csv
from arcpy import env

print "Creating and defining variables."
env.workspace = r"C:\Users\Desktop\data"
env.overwriteOutput = 1

theme = 'interestAreas.shp' 
with arcpy.da.SearchCursor(theme, ['SHAPE@']) as Curs:
    for i in Curs: # iterate through the rows in the cursor object
        polygon = i[0]
        ext = polygon.extent

        # Find min X, Y and max X, Y for each polygon and write it to the file:
        print 'xmin is: ', ext.XMin
        print 'ymin is: ', ext.YMin
        print 'xmax is: ', ext.XMax
        print 'ymax is: ', ext.YMax
        minX = ext.XMin
        minY = ext.YMin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM