다음은 Python을 사용하여 공간 참조, 필드 속성, 필드 값 등과 같은 다른 정보 비트 중에서 ESRI shapefile 위도 및 경도 좌표에 액세스하는 방법입니다. 아래 코드는 다각형과 점에만 작동합니다 (폴리 라인 코드 작성에 익숙하지 않기 때문에). 나는 기본적으로 ArcGIS Desktop Help 9.3에 흩어져있는 코드를 모아서 내 코드 중 하나를 추가하고 하나의 함수로 묶었습니다. ArcGIS 9.3으로 작성되었습니다. 다각형 셰이프 파일 또는 포인트 셰이프 파일을 전달할 수 있어야하며 로직이 그에 따라 지시합니다.
def get_shapefile( shape_file ):
# Import native arcgisscripting module
import arcgisscripting
# Create the geoprocessor object
gp = arcgisscripting.create(9.3)
# Identify the geometry field
desc = gp.Describe( shape_file )
shapefieldname = desc.ShapeFieldName
# Get shapefile Name
print
print 'Shapefile Name: ', desc.Name
# Get the spatial reference
spatial_reference = desc.SpatialReference.Name
print 'Spatial Reference: ', spatial_reference
print
# Create search cursor
rows = gp.SearchCursor( shape_file )
row = rows.Next()
# Enter while loop for each feature/row
while row:
# Create the geometry object
feat = row.GetValue(shapefieldname)
print '*' * 30
print
print 'Geometry related Information'
print
# Get Geometry Type
geometry_Type = feat.Type
print 'Geometry Type: ', geometry_Type
# Get the area of the feature
geometry_Area = feat.Area
print 'geometry_Area; ', geometry_Area
# Get the centroid for the feature
geometry_Centroid = feat.Centroid
print 'geometry_Centroid:', geometry_Centroid
# Get the extent for the feature
geometry_Extent = feat.Extent
print 'geometry_extent: ', geometry_Extent
print
print 'Get Attribute Table Information'
# Get all the fields for the feature class
fields = desc.Fields
total_number_of_fields = len( fields )
print 'Total number of fields: ', total_number_of_fields
print
print 'List attribute table related information:'
print
field_num_cntr = 0
# Loop through all the fields in the feature class
for field in fields:
print '*'*5, field_num_cntr, '*'*5
print
print 'field Type: ', field.Type
print 'Scale: ', str(field.Scale)
print 'Precision: ', str(field.Precision)
print field.Name, '=> ', row.GetValue( field.Name )
print
field_num_cntr += 1
if geometry_Type == 'polygon':
# Variable to keep track of how many multipart polygons are in
# featureclass
partnum = 0
# Count the number of points in the current multipart feature
partcount = feat.PartCount
print
print 'Number of polygons in feature class: ', partcount
print
# Enter while loop for each part in the feature (if a singlepart feature
# this will occur only once)
while partnum < partcount:
# Print the part number
print "Part ", str(partnum), "of", partcount, ":"
print
part = feat.GetPart(partnum)
pnt = part.Next()
pntcount = 0
# Enter while loop for each vertex
while pnt:
# Print x,y coordinates of current point
print 'X coord:', pnt.x, 'Y coord:', pnt.y
pnt = part.Next()
pntcount += 1
# If pnt is null, either the part is finished or there is an interior ring
if not pnt:
pnt = part.Next()
if pnt:
print "Interior Ring:"
partnum += 1
print
print 'Number of coordinates in feature class: ', pntcount - 1
print
elif geometry_Type == 'point':
feat = row.GetValue(shapefieldname)
# Get coords
pnt = feat.GetPart()
# Print x,y coordinates of current point object
print 'X coord:', pnt.x, 'Y coord:', pnt.y
row = rows.Next()
your_shapefile = 'Path\To\Your\Shapefile.shp'
get_shapefile( your_shapefile )