Performing Feature to Point (no advanced license) using ArcPy?
Here is an arcpy soloution:
import os, sys, arcpy
InFC = sys.argv[1]
OutFC = sys.argv[2]
#split the output into directory and name
Folder = os.path.dirname(OutFC)
Name = os.path.basename(OutFC)
# Get the existins spatial reference as it's going to match
desc = arcpy.Describe(InFC)
SR = desc.spatialReference
# create or append
if not arcpy.Exists(OutFC):
arcpy.CreateFeatureclass_management(Folder,Name,"POINT",spatial_reference = SR)
# open up the 'writer'
with arcpy.da.InsertCursor(OutFC,"SHAPE@XY") as iCur:
# open up the 'reader'
with arcpy.da.SearchCursor(InFC,"SHAPE@") as sCur:
# loop through each polygon in the InFC
for sRow in sCur:
cent = sRow[0].centroid # get the centroid
iCur.insertRow([(cent.X,cent.Y)])# write it to the new feature class
This creates a new feature class, opens an insert cursor, loops through each geometry in the InFC and writes the centroid property to the output.. note that multi-part geometries will not have a centroid per part but rather a single centroid for the whole geometry.. it's not much more difficult to loop through the parts - let's keep this example fairly simple though to show the basics.
This should do the trick:
import arcpy
import os
def polysToPoints(in_polys, out_points):
"""converts polygons to centroids
in_polys -- input polygons
out_points -- output points
"""
ws, name = os.path.split(out_points)
sr = arcpy.Describe(in_polys).spatialReference
arcpy.management.CreateFeatureclass(ws, name, 'POINT', template=in_polys, spatial_reference=sr)
# populate records
fields = ['SHAPE@'] + [f.name for f in arcpy.ListFields(out_points) if f.type not in ('OID', 'Geometry')]
with arcpy.da.InsertCursor(out_points, fields) as irows:
with arcpy.da.SearchCursor(in_polys, fields) as rows:
for r in rows:
irows.insertRow((arcpy.PointGeometry(r[0].centroid),) + r[1:])
return out_points
if __name__ == '__main__':
polys = r'C:\path_to_your\polys.shp'
points = r'C:\path_to_your\points.shp'
polysToPoints(polys, points)
shapelib library is ideal for this task.
No depedecies, just a simple module to create shapefiles:
import shapefile
coord_list = ((20,40), (0,0), )
w = shapefile.Writer(shapefile.POINT)
w.field('ID_FIELD','C','40')
for id,(x,y) in enumerate(coord_list):
w.record(ID_FIELD=id)
w.point(x,y)
w.save("file\to\save")
it's very simple you might want to add the projection file afterwards (manually).
Check the manual on additional info