arcpy.da.InsertCursor - all features are inserted at the same location

2036
5
03-29-2021 06:03 PM
MaximeDemers
Occasional Contributor III

Hi,

I am using a arcpy to insert rows from a feature class into another but I realize that the features in the destination are loosing their location. They are all positionned at the location of the first one.

Curiously, if I am using the SHAPE@ token for the geometry field, the features location are correct but if I use SHAPE@XY or SHAPE@JSON they are all at the location of the first feature. Also, the problem does not occur if I create the destination feature class manually with ArcMap, but only when creating it using arcpy.CreateFeatureClass_management like in the code below.

The shape type is point and the feature class is in a non-versioned dataset in 3857 (web mercator). This is a geodatabase enterprise (v10.6) with a postgresql (v9.6) database. I am using arcpy 64bits. The destination feature class has attchments enabled and it seems to be the cause of the problem (see Edit 2 below)

 

import os, arcpy

WORKSPACE = os.path.join(r"\\sgoappwp01", "Script", "Connections", "sgopubwt02 - PostgrSQL.sde")
INPUT_FC = os.path.join(WORKSPACE, "CollecteEncombrants", "CollecteEncombrants_Formulaire")
OUTPUT_FC = os.path.join(WORKSPACE, "CollecteEncombrants", "CollecteEncombrants_Terrain")

arcpy.CreateFeatureclass_management(
  out_path = os.path.join(WORKSPACE, "CollecteEncombrants"),
  out_name = "CollecteEncombrants_Terrain",
  geometry_type = "POINT",
  spatial_reference = arcpy.SpatialReference(3857),
  has_m = "DISABLED",
  has_z = "DISABLED"
)

edit = arcpy.da.Editor(WORKSPACE)
edit.startEditing(False, False)
edit.startOperation()

rows = []
with arcpy.da.SearchCursor(INPUT_FC, ["SHAPE@JSON"]) as sCursor:
    for row in sCursor:
        rows.append(row)

with arcpy.da.InsertCursor(OUTPUT_FC, ["SHAPE@JSON"]) as iCursor:
    for row in rows:
        iCursor.insertRow(row)

edit.stopOperation()
edit.stopEditing(True)

 

Any idea what could be wrong?

EDIT:  here some printed rows before and after the insert

 

#some rows just before being inserted

with arcpy.da.InsertCursor(OUTPUT_FC, ["SHAPE@JSON"]) as iCursor:
  for row in rows:
  print
  iCursor.insertRow(row)

(u'{"x":-7994810.0683999993,"y":5685131.9157000035,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-8007290.4016999993,"y":5685999.8158999979,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-8005971.6404999997,"y":5685563.5265000015,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-8003287.4274000004,"y":5684056.8937999979,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)

#rows after being inserted
with arcpy.da.SearchCursor(OUTPUT_FC, ["SHAPE@JSON"]) as sCursor:
  for row in sCursor:
  print(row)

(u'{"x":-7994810.0683999993,"y":5685131.9157000035,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-7994810.0683999993,"y":5685131.9157000035,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-7994810.0683999993,"y":5685131.9157000035,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)
(u'{"x":-7994810.0683999993,"y":5685131.9157000035,"spatialReference":{"wkid":102100,"latestWkid":3857}}',)

 

EDIT 2:

I think I have found the source of the problem. It seems it is related with the attachments.

The destination feature class has attachment enabled. If I remove the attachments the insert cursor is working as expected with SHAPE@JSON token. If I enable the attachment again, the problem is reappearing.

I still wonder why...

Tags (2)
0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

Can you print a few rows off using the options so we can see what is being returned compared to what is expected


... sort of retired...
0 Kudos
MaximeDemers
Occasional Contributor III

Hi!

That's the first thing I verified to debug, but everything seems correct with the inserted rows. You can see my edited post

 

 

 

0 Kudos
mody_buchbinder
Occasional Contributor III

Hi 

I am not sure you can use SHAPE@JSON in insert cursor even the docs does not say it.

You assume the software translate automatically the json to the geometry binary in the insert, I am not sure it happened in real life.

If it is point then SHAPE@XY might work .

Is there any reason not to use SHAPE@

Have fun

Mody

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

What if you try the same workflow but using file geodatabase?  Knowing whether it is EGDB related or a larger issue would be a good step.

0 Kudos
MaximeDemers
Occasional Contributor III

Hi! 

Very good suggestion but meanwhile I think I have found the source of the problem. It seems it is related with the attachments.

The destination feature class has attachment enabled. If I remove the attachments the insert cursor is working as expected with SHAPE@JSON token. If I enable the attachment again, the problem is reappearing.

I still wonder why...

0 Kudos