<jupyter_start><jupyter_text>Insert Cursor Demo- Shows how to use an insert cursor- Goal: Take a table (csv file) and make it spatial!<jupyter_code>import os
from arcpy import da
from arcpy import env
import arcpy
import pandas as pd
# Monument data from a github site. 
df = pd.read_csv("https://raw.githubusercontent.com/mike-n-7/ML4/master/data/monuments.csv")
df.head()<jupyter_output><empty_output><jupyter_text>Create a Feature Class Check if the Feature Class Exists- recall: When creating feature classes, spatial tables not exist<jupyter_code>fc = os.path.join(env.scratchGDB, "monument")
if arcpy.Exists(fc):
    arcpy.management.Delete(fc)<jupyter_output><empty_output><jupyter_text>Create Spatial Reference<jupyter_code>sr = arcpy.SpatialReference(4326)<jupyter_output><empty_output><jupyter_text>Create The Feature Class<jupyter_code>fc = arcpy.management.CreateFeatureclass(out_name=os.path.basename(fc), 
                                         out_path=os.path.dirname(fc), 
                                         geometry_type="POINT", 
                                         spatial_reference=sr)[0]
print(fc)<jupyter_output>C:\Users\lando\AppData\Local\Temp\scratch.gdb\monument<jupyter_text>Add Fields to the Feature Class- Use the `arcpy.management.AddField()`<jupyter_code>arcpy.management.AddField(in_table=fc, field_name="NOM", field_type="TEXT")
arcpy.management.AddField(in_table=fc, field_name="LONG", field_type="DOUBLE")
arcpy.management.AddField(in_table=fc, field_name="LAT", field_type="DOUBLE")
fields = [fld.name for fld in arcpy.ListFields(fc) \
          if fld.type != "Geometry"]
fields<jupyter_output><empty_output><jupyter_text>Load the Data- Use an Insert Cursor to add the information<jupyter_code>fields = [fld.name for fld in arcpy.ListFields(fc) \
          if fld.type not in  ["Geometry", "OID"]]
fields += ['Shape@']
fields<jupyter_output><empty_output><jupyter_text>**A Sample of the Data**<jupyter_code>dict(zip(df.columns.tolist(), df.values[0].tolist()))<jupyter_output><empty_output><jupyter_text>Create the Cursor Object- Use the `with` pattern to create a cursor- For each row in the Data Frame, **insert** a new row- Finally clean up the cursor and release the lock    + `del` ensure the lock is removed (though should not be needed)<jupyter_code>with da.InsertCursor(fc, fields) as icur:
    for row in df.values:
        pt = arcpy.Point(row[-2], row[-1])
        ptgeom = arcpy.PointGeometry(pt, sr)
        icur.insertRow([row[1], row[-2], row[-1], ptgeom])
        
del icur<jupyter_output><empty_output><jupyter_text>Display the Added Rows<jupyter_code>arcpy.management.GetCount(fc)[0]<jupyter_output><empty_output><jupyter_text>Map It<jupyter_code>from arcgis.gis import GIS
gis = GIS()
m1 = gis.map()
m1.center = (45.4944962453396,-73.53701983501136)
m1
m1.zoom = 12
from arcgis.features import GeoAccessor, GeoSeriesAccessor
sdf = pd.DataFrame.spatial.from_featureclass(fc)
sdf.head()
sdf.spatial.plot(m1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1211314475873544, 'fraction_numerical': 0.017304492512479203, 'mean_word_length': 4.780769230769231, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '253135', 'license': 'Apache-2.0', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1035, 'n_words': 220, 'path': 'lecture/Lesson 05/Lesson 05 Insert Cursor with Building Geometries Example.ipynb', 'repo_name': 'shaheen19/Adv_Py_Scripting_for_GIS_Course', 'size': '3005'}