How do I compare spatial references of extents?

1042
7
Jump to solution
01-03-2019 02:17 PM
KylePerri
Esri Contributor

I am passing an extent into my script tool and I want to check to make sure it is WGS84 and if it isn't I convert it using projectAs; however, when it is WGS84 if I project it the extent's xmin, xmax, ymin, ymax gets messed up. How can I compare them so I don't project it if it's already in decimal degrees? Here is what I have, but it doesn't seem to work: 

boundingBox = None
WGS84 = SpatialReference(4326)
# Project our extent to GPS lat long if it is not already
if extent and extent.spatialReference is not WGS84:
    boundingBox = extent.projectAs(WGS84)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Did you try:

boundingBox = None
WGS84 = SpatialReference(4326)
# Project our extent to GPS lat long if it is not already
if extent and extent.spatialReference == WGS84:
    boundingBox = extent.projectAs(WGS84)‍‍‍‍‍

 

Your original code used the is operator, which tests for object identity and not equality.  A quick check shows that Esri has implemented equality equivalence with the Spatial Reference class:

>>> SR1 = arcpy.SpatialReference(4326)
>>> SR2 = arcpy.SpatialReference(4326)
>>> 
>>> SR1 == SR2
True
>>> SR1 is SR2
False
‍‍‍‍‍‍‍

View solution in original post

7 Replies
DanPatterson_Retired
MVP Emeritus

can you put in values that you are testing.  the code snippet fails since 'extent' isn't defined

KylePerri
Esri Contributor

Extent is a parameter in the script tool, but I've been using the current extent of my map.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then my examples should help if it is an extent object.  If it is a string representation of the extent, you may have some parsing to do to get the numeric values

0 Kudos
KylePerri
Esri Contributor

I ended up doing this: 

if extent and extent.spatialReference.linearUnitName == 'Meter':
    boundingBox = extent.projectAs(SpatialReference(4326))

It seems to work.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Did you try:

boundingBox = None
WGS84 = SpatialReference(4326)
# Project our extent to GPS lat long if it is not already
if extent and extent.spatialReference == WGS84:
    boundingBox = extent.projectAs(WGS84)‍‍‍‍‍

 

Your original code used the is operator, which tests for object identity and not equality.  A quick check shows that Esri has implemented equality equivalence with the Spatial Reference class:

>>> SR1 = arcpy.SpatialReference(4326)
>>> SR2 = arcpy.SpatialReference(4326)
>>> 
>>> SR1 == SR2
True
>>> SR1 is SR2
False
‍‍‍‍‍‍‍
JoeBorgione
MVP Emeritus

Not sure if these links will help or not:

https://community.esri.com/thread/226543-how-to-output-the-values-of-bounding-box-in-spatial-index-u... 

projectAs help 

I don't have any experience with projectAs, but I think if you could get the xy's out of the given feature class and then compare their values to a set (or subset) of known values, you should be able to determine if they are DD or something else.

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

An example.... one file in geographic and one in projected coordinates...

in_fc = r"C:\....\arcpytools_demo.gdb\Can_0_big_3"
desc = arcpy.Describe(in_fc)
ext = desc.extent
ext.spatialReference.name
'GCS_WGS_1984'

in_fc2 = r"C:\Git_Dan\a_Data\testdata.gdb\polygon"
desc2 = arcpy.Describe(in_fc2)
ext2 = desc2.extent
ext2.spatialReference.name
'NAD_1983_CSRS_MTM_9'


# ----- now do the comparisons ----
ext.spatialReference.name  # ---- 'GCS_WGS_1984'
ext.lowerLeft
<Point (-141.00686645507812, 41.67692565917997, #, #)>
ext.upperRight
<Point (-52.61888885498041, 83.11042022705101, #, #)>

ext2.spatialReference.name  # ---- 'NAD_1983_CSRS_MTM_9'
ext2.lowerLeft
<Point (300000.0, 5000000.0, #, #)>
ext2.upperRight
<Point (300020.0, 5000020.0, #, #)>

# ---- now other properties and test parameters ----
ext.contains(ext2)
True

ext2.contains(ext)
False

ext.overlaps(ext2)
False

ext.within(ext2)
False

ext2.within(ext)
True

# ----- from the help
dir(ext)
['JSON', 'MMax', 'MMin', 'XMax', 'XMin', 'YMax', 'YMin', 'ZMax', 'ZMin',.... snip...
 ', 'contains', 'crosses', 'depth', 'disjoint', 'equals', 'geohash', 'geohashCovers',
 'geohashNeighbors', 'height', 'lowerLeft', 'lowerRight', 'overlaps', 'polygon',
 'projectAs', 'spatialReference', 'touches', 'upperLeft', 'upperRight', 'width',
 'within']