Add Spatial Reference Python Call Zoom Issue

84
2
2 weeks ago
ARO
by
New Contributor

I'm working on an automated script to 1) import a .OBJ file into ArcGIS Pro, 2) Move the generated feature layer to a specified location [for this example, Paris, France] then 3) Recalculate the spatial index to correct the zoom-to depth. I'm currently using ArcGIS Pro 3.2.2  and for testing purposes, I'm working out of the Python Notebook. I've attached the zip file of the .OBJ file I'm working with for replicability. 

The first two portions have been successful.

1) Import .OBJ File to ArcGIS Pro

ARO_0-1713804628732.png

 

# Import the .OBJ file as Feature Class
arcpy.ddd.Import3DFiles(
    in_files=r"D:\models\gravel\compressed_gravel.obj",
    out_featureClass=r"C:\Users\mwylie\Documents\ArcGIS\Projects\zoom-issues\zoom-issues.gdb\gravel_Import3DFiles",
    root_per_feature="ONE_ROOT_ONE_FEATURE",
    spatial_reference='PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]];-20037700 -30241100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision',
    y_is_up="Z_IS_UP",
    file_suffix="*",
    in_featureClass=None,
    symbol_field=None
)

 

The OBJ file successfully loads as a 3D layer in my local scene. Since there's no geospatial data attached to the .OBJ file, it defaults to (0, 0).

2) Set coordinates/spatial index and move to coordinates:

ARO_1-1713804821949.png

 

# Paris Coordinates
longitude = 2.344636
latitude = 48.855364
new_location = (longitude, latitude)
# Set Spatial Reference
spatref = arcpy.SpatialReference(4326)
feature_class = "gravel_Import3DFiles"
# Move feature to coordinates
with arcpy.da.UpdateCursor(feature_class, ["SHAPE@XY"], spatial_reference=spatref) as cursor:
    for row in cursor:
        # Update the cursor with the new location
        cursor.updateRow([new_location])

 

I set the coordinates to (2.344636, 48.855364) [Paris, France] and move the object successfully to that location. The location won't reflect automatically; you'll need to right-click on the gravel_Import3DFiles feature layer -> Properties -> Cache -> Clear Cache, then the object will move. Right-click the feature layer again, then click Zoom To Layer, and you'll get this extreme zoom out. The zoom can be fixed in two ways:

  1. Right-click the gravel_Import3DFiles -> Properties -> Indexes -> Spatial Index -> Recalculate. This will fix the spatial index and correct the zoom. This can't be done programmatically, from what I've researched.
  2. Go to Geoprocessing -> Data Management Tools -> Indexes -> Add Spatial Index. Under Import Features, select gravel_Import3DFiles feature. Click run and it'll fix the spatial index and correct the zoom. This can be achieved programmatically (in theory).

3) Recalculate Spatial Index

 

# Correct spatial index
arcpy.management.AddSpatialIndex(
    in_features="gravel_Import3DFiles",
    spatial_grid_1=0,
    spatial_grid_2=0,
    spatial_grid_3=0
)

 

This script is a direct copy from the Add Spatial Index tool (instead of clicking Run, click the dropdown arrow and click Copy Python Command). After running this, right-click the gravel_Import3DFiles layer and select Zoom To Layer and nothing happens. However, if you save the project and re-open the project, the zoom will be corrected.

I've tried recalculating the feature class extent programmatically, toggling the feature visibility, clearing the feature cache, clearing the ArcGIS Pro cache, removing the layer and adding it back in.. essentially trying to replicate closing ArcGIS Pro without closing it. It doesn't make sense that the Geoprocessing tool version of Add Spatial Index works, but when using in the Python Notebook, it doesn't work.

Here's the full script:

 

# Import the .OBJ file as Feature Class
arcpy.ddd.Import3DFiles(
    in_files=r"D:\models\gravel\compressed_gravel.obj",
    out_featureClass=r"C:\Users\mwylie\Documents\ArcGIS\Projects\zoom-issues\zoom-issues.gdb\gravel_Import3DFiles",
    root_per_feature="ONE_ROOT_ONE_FEATURE",
    spatial_reference='PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]];-20037700 -30241100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision',
    y_is_up="Z_IS_UP",
    file_suffix="*",
    in_featureClass=None,
    symbol_field=None
)

# Paris Coordinates
longitude = 2.344636
latitude = 48.855364
new_location = (longitude, latitude)

# Set Spatial Reference
spatref = arcpy.SpatialReference(4326)
feature_class = "gravel_Import3DFiles"

# Move feature to coordinates
with arcpy.da.UpdateCursor(feature_class, ["SHAPE@XY"], spatial_reference=spatref) as cursor:
    for row in cursor:
        # Update the cursor with the new location
        cursor.updateRow([new_location])

# Correct spatial index
arcpy.management.AddSpatialIndex(
    in_features="gravel_Import3DFiles",
    spatial_grid_1=0,
    spatial_grid_2=0,
    spatial_grid_3=0
)

 

 

0 Kudos
2 Replies
TonyAlmeida
Occasional Contributor II

I think the issue is with Pro. I have a script that update features from another feature class and it won't reflect the change/update, until I close out of the project or refresh the database by right click on the database in the List by Data Source on the table of contents and select refresh. It's SUPER ANNOYING!

0 Kudos
ARO
by
New Contributor

I'm figuring that it has something to do with a disconnect between strictly data manipulation in the Python environment and the UI. Since the .NET API has more control over the UI, I'm now attempting to update the coordinates directly using 'GeometryEngine.Instance.Move', but that has issues of its own. I need to calculate the distance between the center point of the featureLayer and the target coordinate to get the x/y/z offsets, but that's not going so well ATM.

Esri should just have the Python implementation of the geoprocessing tool update the UI as though it's being referenced directly.

0 Kudos