Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Geoprocessing with Python: Writing multipart geometries

2 views
Skip to first unread message

MoonChild

unread,
Feb 10, 2009, 10:45:33 PM2/10/09
to
I have read everywhere that a multipart geometry in the ESRI world
(and Python) should be treated as an array of parts, which parts are
themselves arrays of points. I build that easily, and my array of
points (parts) works fine if they are stored in a single-part geometry
feature. But putting all those parts in an array and trying to save it
does NOT work in any way. I keep getting empty geometries. Does anyone
know why this happens or if there is a workaround other than using a
shapelib from another vendor?

Niklas Norrthon

unread,
Feb 11, 2009, 7:08:50 AM2/11/09
to

This should work: (unless google screws up my indentation)

import arcgisscripting
gp = arcgisscripting.create()

geometry = [[(10, 10),
(10, 20),
(20, 20),
(20, 10),
],
[(25, 25),
(50, 50),
(50, 25),
],
]

workspace = r'c:\tmp'
output_fc = r'multi.shp'

def main():
gp.Workspace = workspace
gp.CreateFeatureClass(workspace, output_fc, 'POLYGON')

part_array = gp.CreateObject('Array')
point_array = gp.CreateObject('Array')
point = gp.CreateObject('Point')

cur = gp.InsertCursor(output_fc)
row = cur.NewRow()

part_array.RemoveAll()
for part in geometry:
# first loop indentation
point_array.RemoveAll()
for x, y in part:
# second loop indentation
point.X = x
point.Y = y
point_array.Add(point)
# second loop ends here

# close the loop, repeat first point
x, y = part[0]
point.X = x
point.Y = y
point_array.Add(point)
print 'Adding %d points' % point_array.Count
part_array.Add(point_array)

# first loop ends here

print 'Adding %d parts' % part_array.Count
row.Shape = part_array
cur.InsertRow(row)


if __name__ == '__main__':
main()


/Niklas Norrthon

0 new messages