如何在 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 和循环 for 或 while 来完成?
或者如何遍历多边形的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
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 和循环 for 或 while 来完成? 或者如何遍历多边形的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