Skip to content

Instantly share code, notes, and snippets.

@GIS-Luke
Created February 10, 2020 23:42
Show Gist options
  • Save GIS-Luke/a68ff1caa2143dda5bec90f0fc66127c to your computer and use it in GitHub Desktop.
Save GIS-Luke/a68ff1caa2143dda5bec90f0fc66127c to your computer and use it in GitHub Desktop.
Convert features to points without an ArcGIS Advanced license
"""
Convert features to points without an ArcGIS Advanced license.
This example truncates an existing feature class and then
inserts the created rows from the source feature class.
"""
import arcpy as ap
# list field objects to get the name property
fld_objs = ap.ListFields(in_path_fc)
# loop through the field objects and append their names to the list
# excepting the ones which are known to cause issues
bad_names = ['Shape.STArea()', 'Shape.STLength()', 'SHAPE.area', 'SHAPE.len',
'SHAPE.fid']
fld_names = [fld_obj.name for fld_obj in fld_objs
if fld_obj.name not in bad_names]
# make some lists of the field names necessary to create a point feature class
in_flds = ['SHAPE@' if x == 'SHAPE' else x for x in fld_names]
out_flds = ['SHAPE@XY' if x == 'SHAPE' else x for x in fld_names]
# Truncate the destination featureclass, so it is prepared for population
ap.TruncateTable_management(out_path_fc)
# build the SearchCursor so we have something data to convert
in_cur = ap.da.SearchCursor((in_path_fc), in_flds)
# build the InsertCursor
out_cur = ap.da.InsertCursor(out_path_fc, out_flds)
# shape_pos used to rebuild a feature with a centroid in the shape
# instead of a more complex geomtry
shape_pos = fld_names.index('SHAPE')
for row in in_cur:
# build an outrow constructed of row, replacing the geometry
# with it's centroid
outrow = (row[:shape_pos] + tuple([row[shape_pos].centroid]) +
row[shape_pos + 1:])
# insert the outrow to commit it to the feature class
out_cur.insertRow(outrow)
del in_cur
del out_cur
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment