1

I'm trying to write a script that reads geometry for each row in feature class, then create a new feature class and insert into it that geometry. It has to work with ArcGIS 10.0 so I have to use arcpy.InsertCursor. But I have a problem, because in the new feature class there is the same number of rows as in input but every row has the same geometry, from last row of previous feature. In 10.2, even using arcpy.InsertCursor, everything is working fine.

import arcpy
arcpy.env.workspace
infile=arcpy.GetParameterAsText(0)
outfc="przekroje_oblicz_hydroo.shp"
outpath="C:\\Users\\Bartek\\Desktop\\temp"
shapeName = arcpy.Describe(infile).shapeFieldName

linie=[]
km=[]
ID_HYDRO_R="12334"
#ver=arcpy.GetInstallInfo()['Version']
ver="10.0"

if ver=="10.0":
    cursor = arcpy.SearchCursor(infile)
    for row in cursor:
        linie.append(row.getValue(shapeName))
        km.append(row.getValue("Km"))    

else:
    for row in arcpy.da.SearchCursor(infile, ["SHAPE@","Km"]):
        linie.append(row[0])
        km.append(row[1])


PUWG92 = "PROJCS['PUWG_1992',GEOGCS['GCS_ETRS_1989',DATUM['D_ETRS_1989',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['false_easting',500000.0],PARAMETER['false_northing',-5300000.0],PARAMETER['central_meridian',19.0],PARAMETER['scale_factor',0.9993],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]"
fc=arcpy.CreateFeatureclass_management(outpath, outfc, "POLYLINE", "", "", "ENABLED", PUWG92)
#fc=arcpy.CreateFeatureclass_management(outpath, outfc, "POINT", "", "", "ENABLED", PUWG92)
arcpy.AddField_management(fc, "ID_HYDRO_R", "STRING", "", "", "22", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(fc, "KM_PKT", "DOUBLE", "6", "3", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(fc, "ZW_10", "DOUBLE", "7", "2", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(fc, "ZW_1", "DOUBLE", "7", "2", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(fc, "ZW_02", "DOUBLE", "7", "2", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(fc, "ZW_WZ", "DOUBLE", "7", "2", "", "", "NULLABLE", "NON_REQUIRED", "")

if ver=="10.0":
    k=0
    cur = arcpy.InsertCursor(fc)
    for i in linie:
        feat = cur.newRow()
        feat.Shape = i
        feat.ID_HYDRO_R=ID_HYDRO_R
        feat.KM_PKT=((float(km[k])/1000))
        feat.ZW_10=11
        feat.ZW_1=12
        feat.ZW_02=12
        feat.ZW_WZ=-7777
        cur.insertRow(feat)
        k+=1

else:
    cursor = arcpy.da.InsertCursor(fc, ["SHAPE@","KM_PKT"])
    k=0
    for i in linie:
        kma=(float(km[k])/1000)
        #arcpy.AddMessage
        cursor.insertRow([i,kma])
        k+=1


del cursor, cur

EDIT:

I managed to rewrtie geometry by modyfying searchcursor to read vertex into 'arcpy.Array', then create 'arcpy.Polyline' and add it to list linie. But it is much slower solution.

cursor = arcpy.SearchCursor(infile)
for row in cursor:
    feat=row.getValue(shapeName)
    k=0
    punkty_lista=arcpy.Array()
    for part in feat:
        for pnt in feat.getPart(k):
            punkty_lista.add(pnt)
    linia=arcpy.Polyline(punkty_lista)
    linie.append(linia)
    punkty_lista.removeAll()
    k=+1
  • ArcGIS 10.0 has been in Retired support status for nearly three years, so this isn't really even an issue for Esri Tech Support. Are you sure there isn't an ancient Python aspect to this issue as well? – Vince Oct 23 '18 at 17:08
  • May not be the source of your problem but fc in your code is actually a RESULT object. So if you called it res then typically you get the FeatureClass path as fc = res.getOutput(0). – Hornbydd Oct 23 '18 at 22:32
  • Have you put a breakpoint at for i in linie to see if i is actually changing on each iteration? Or you could put a breakpoint even a bit before that to see what your linie list contains after your for row in cursor line. Not sure it works for 10.0 but you could try changing that one to row[0] also to see if that works instead of getValue() – MKF Oct 26 '18 at 14:57
  • @Hornbydd: Nothing has changed. When i print the object list it looks like they are all different. – Bartek Blatkiewicz Oct 27 '18 at 15:23

Your Answer

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

Browse other questions tagged or ask your own question.