Select to view content in your preferred language

Reprojecting an Extent object in a Python Toolbox

217
4
12-18-2024 07:34 PM
LewisTrotter
Regular Contributor

Hello,

I am wondering how to reproject an arcpy.Extent object. I have a Python script toolboxes with a single GPExtent parameter. In the execute block, I have the following code:

ext = parameters[0].value 
srs = arcpy.SpatialReference(4326) 
ext.projectAs(srs)

When I run this, I get an error stating "RuntimeError: Object: CreateObject error creating spatial reference". Am I doing something wrong? Oddly, the above code runs fine in a notebook.

Thanks in advance!

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

Extent—ArcGIS Pro | Documentation

What is parameter 0?  use an AddMessage to report it.  Perhaps the extent object is non-existent or not in the correct form


... sort of retired...
0 Kudos
LewisTrotter
Regular Contributor

Thanks Dan,

It is an Extent object, I can pluck the XMin, XMax et al. from it no problem. It also appears to have the correct .spatialReference.

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

You might want to have a look at this link

Python Extent Calculations

it uses the extent object to make a geometry object, project it, then get the extent... seems to contradict the projectAs in the help though if the source and destination spatial references are known and correct


... sort of retired...
0 Kudos
HaydenWelch
MVP

You might need to assign a transformation, according to the docstring for Extent.projectAs():

HaydenWelch_0-1734704026606.png

If your projection requires a transformation, you can use ListTransformations  to get a list of valid transformations and pass those to the call:

from arcpy import Extent, SpatialReference, ListTransformations

ext: Extent = parameters[0].value 
srs = SpatialReference(4326)

transformation: str = ListTransformations(ext.spatialReference, srs, extent=ext)[0]

ext.projectAs(srs, transformation_name=transformation)

 

0 Kudos