1

I have a CSV file with number of records that have WKT Point geometry. I am bit lost on how to insert to this into a new point layer.

Sample data from CSV file from GeometryItem Field:

  • POINT (-13174097.3521 4006999.38409)
  • POINT (-13172281.9726 4006836.9554)
  • POINT (-13172215.0902 4009803.66756)
  • POINT (-13173987.4738 4011721.75923)

arcpy.env.overwriteOutput

out_path = r'D:\My Documents\Metro_RTSO\GISData\Metro_RTSO.gdb'
arcpy.env.workspace = out_path
sr = arcpy.SpatialReference(3395)
print sr.name
newFC= 'ITS_20171024_CCTVs'

if arcpy.Exists(newFC):
    print newFC + " Exists, Deleting"
    arcpy.Delete_management(newFC)
    arcpy.CreateFeatureclass_management(out_path, newFC, 'POINT', spatial_reference=sr)

else:
     arcpy.CreateFeatureclass_management(out_path, newFC, 'POINT', spatial_reference=sr)

with open(r'D:\My Documents\Metro_RTSO\GISData\ITS Inventory\ITS_20171024_CCTVs.csv', 'r') as f:
     reader = csv.DictReader(f)
     insertCursor = arcpy.da.InsertCursor(newFC, ['SHAPE@WKT'])
     Lines = 0
     records = 0
     for row in reader:
         Lines += 1
         if row["GeometryItem"] != None:
            records +=1
            ##print row["GeometryItem"]
            print arcpy.FromWKT(row["GeometryItem"],sr)
            new_pt = arcpy.Point(arcpy.FromWKT(row["GeometryItem"]))
            insertCursor.insertRow(new_pt)
     del insertCursor
     print records, Lines

print 'finished'
  • 1
    If you review the documentation, it has examples of how inserts work. You've missed the part where the inserted row must be an array. Since you convert the WKT to Point, you shouldn't have an @WKT on the cursor. The correct object to insert is PointGeometry, though Point will work – Vince Oct 26 '17 at 23:08
  • Change to: insertCursor = arcpy.da.InsertCursor(newFC, ['SHAPE@']) and insertCursor.insertRow(arcpy.FromWKT(row["GeometryItem"])). This fixed the problem. Though I couldn't get ['SHAPE@WTK'] to work with insertCursor.insertRow(row["GeometryItem"]). – aconti74 Oct 26 '17 at 23:56
1

I got it to work by changing my code to the following though never figure out how use the insert cursor with the SHAPE@WKT:

import arcpy
import csv

def main():

    arcpy.env.overwriteOutput

    out_path = r'D:\My Documents\Metro_RTSO\GISData\Metro_RTSO.gdb'
    arcpy.env.workspace = out_path
    sr = arcpy.SpatialReference(3395)

    newFC= 'ITS_20171024_CCTVs'

    if arcpy.Exists(newFC):
        print newFC + " Exists, Deleting"
        arcpy.Delete_management(newFC)
        arcpy.CreateFeatureclass_management(out_path, newFC, 'POINT', spatial_reference=sr)

    else:
        arcpy.CreateFeatureclass_management(out_path, newFC, 'POINT', spatial_reference=sr)

    with open(r'D:\My Documents\Metro_RTSO\GISData\ITS Inventory\2017.10.24 ITS Inventory Export - CCTVs.csv', 'r') as f:
         reader = csv.DictReader(f)
         insertCursor = arcpy.da.InsertCursor(newFC, ['SHAPE@'])
         Lines = 0
         records = 0
         for row in reader:
             Lines += 1
             if row["GeometryItem"] != '':
                records +=1
                print row["GeometryItem"]
                insertCursor.insertRow(arcpy.FromWKT(row["GeometryItem"]))

         del insertCursor
         print records, Lines

     print 'finished'

if __name__ == '__main__':
    main()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.