Select to view content in your preferred language

How to create a feature in a feature class with ArcPy

4813
7
04-23-2018 09:53 AM
JoseSanchez
Frequent Contributor

Hello everyone,

How can I create a point feature in a feature class with ArcPy the same way I do it with vb.net

I need to convert this vb.net code into Python code.

Dim workspaceEdit As IWorkspaceEdit = CType(myTargetIWorkspace, IWorkspaceEdit)

'' Start an edit session. An undo/redo stack isn't necessary in this case.

workspaceEdit.StartEditing(False)

''/ Start an edit operation.

workspaceEdit.StartEditOperation()

Dim feature As IFeature = pFeatureClass.CreateFeature()

feature.Shape = pPoint

feature.Value(feature.Fields.FindField("ID")) = IncidentID

feature.Value(feature.Fields.FindField("ADDRESS")) = Address

feature.Value(feature.Fields.FindField("XCOORD")) = pPoint.X

feature.Value(feature.Fields.FindField("YCOORD")) = pPoint.Y

feature.Value(feature.Fields.FindField("PROBDESC")) = ProblemDescription

feature.Value(feature.Fields.FindField("REQSTDATE")) = DateTime.Parse(RequestedDate.ToString)

feature.Store()

''// Save the edit operation. To cancel an edit operation, the AbortEditOperation

''// method can be used.

workspaceEdit.StopEditOperation()

''// Stop the edit session. The saveEdits parameter indicates the edit session

''// will be committed.

workspaceEdit.StopEditing(True)

0 Kudos
7 Replies
JoeBorgione
MVP Emeritus

Does this help?  Create Feature Class—Help | ArcGIS Desktop 

That should just about do it....
0 Kudos
JoseSanchez
Frequent Contributor

I am looking for a sample code to create a record in the feature class, not a feature class.

0 Kudos
JoeBorgione
MVP Emeritus

Oops.  My bad.  Sorry about that!

how about this:  https://www.e-education.psu.edu/geog485/node/142 

insert cursors:  InsertCursor—Help | ArcGIS Desktop 

That should just about do it....
DanPatterson_Retired
MVP Emeritus

Then you want an UpdateCursor or an InsertCursor and you can follow some of the code samples in the arcpy section of the help files... for example

Writing Geometries....

There are loads of other examples, and even on geonet, code samples abound and I am sure that the exact snippet has been written to suit your case, but the arcpy help is a good reference location 

0 Kudos
JoseSanchez
Frequent Contributor

An exact snippet!!!   this is what I am looking for  🙂

0 Kudos
JoeBorgione
MVP Emeritus

I thought the links above provided samples.  Here is some code I use to create a rectangular polygon based on screen map extent.  Note that it's only an 'in memory' feature class that I use and toss....

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
frameExtent = df.extent
#
XMAX = frameExtent.XMax
XMIN = frameExtent.XMin
YMAX = frameExtent.YMax
YMIN = frameExtent.YMin
#
pnt1 = arcpy.Point(XMIN, YMIN)
pnt2 = arcpy.Point(XMIN, YMAX)
pnt3 = arcpy.Point(XMAX, YMAX)
pnt4 = arcpy.Point(XMAX, YMIN)
#
array = arcpy.Array()
array.add(pnt1)
array.add(pnt2)
array.add(pnt3)
array.add(pnt4)
array.add(pnt1)
#
Mypolygon = arcpy.Polygon(array)
arcpy.CopyFeatures_management(Mypolygon,"in_memory\\ExtentPoly")
in_poly_layer = arcpy.mapping.ListLayers(mxd,"ExtentPoly",df)[0]
poly_symbology_layer = arcpy.mapping.Layer(r"C:\LayerFiles\EmptyRedPolygon.lyr")
arcpy.ApplySymbologyFromLayer_management(in_poly_layer,poly_symbology_layer)
That should just about do it....
DanPatterson_Retired
MVP Emeritus

Perhaps the link isn't exactly what you need... just replace what isn't needed with stuff in the example.

import arcpy
fc = "c:/data/gdb.gdb/roads"
cursor = arcpy.da.InsertCursor(fc, ["SHAPE@"])
array = arcpy.Array([arcpy.Point(5997611.48964, 2069897.7022),
                     arcpy.Point(5997577.46097, 2069905.81145)])
spatial_reference = arcpy.SpatialReference(4326)
polyline = arcpy.Polyline(array, spatial_reference)

cursor.insertRow([polyline])

There are other examples in the link I sent, just like the one above