Incorrect parameter for geometry.project_as(spatial_reference)?

904
4
05-27-2018 05:56 PM
JohnSolly2
New Contributor

I am trying to project a Geometry object, but every time I use the function, it returns `None`...What I am I doing wrong? I am trying to change the spatial reference from WGS84, 'wkid':4326, to Web Mercator, 'wkid':3857'.

I've tried

ESRI_GEOMETRY.project_as(spatial_reference={'wkid': 3857})
ESRI_GEOMETRY.project_as(spatial_reference=3857)
ESRI_GEOMETRY.project_as(spatial_reference="3857")

The documentation simply says:

ArgumentDescription
spatial_referenceRequired SpatialReference. The new spatial reference. This can be a SpatialReference object or the coordinate system name.

What is the coordinate system name? Am I supposed to be putting in the words "Web Mercator" ? That seems wrong? I went ahead and made a spatial reference object and passed that in and it still returns "None"

SPATIAL REFERENCE = SpatialReference(iterable={"wkid":3857})

ESRI_GEOMETRY.project_as(spatial_reference=SPATIAL_REFERENCE)

Any help would be appreciated!

See the API doc - > arcgis.geometry module — arcgis 1.4.1 documentation 

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

None is being returned suggests that the input geometry doesn't have a defined coordinate system

0 Kudos
by Anonymous User
Not applicable

Do you have a Geometry Engine installed in your Python Environment? The Python API uses Arcpy OR Shapely. If neither are installed, then many of the geometry operations return None. FWIW, the project_as function seems to only work if you Arcpy installed.

JoshuaBixby
MVP Esteemed Contributor

Given that the ArcGIS API for Python can work with web-based and multiple local geometry libraries, results aren't always as they seem at first glance.  It is helpful to use 29.12. inspect — Inspect live objects — Python 3.6.5 documentation to understand which geometry libraries are being used, or trying to be used, and whether the result is expected.

To understand why Aaron Pulver‌ made the statement about Geometry.project_as() only working with ArcPy:

>>> import arcgis
>>> import inspect
>>> print(inspect.getsource(arcgis.geometry.Geometry.project_as))
    def project_as(self, spatial_reference, transformation_name=None):
        """
        Projects a geometry and optionally applies a geotransformation.


        Parameter:
         :spatial_reference: - The new spatial reference. This can be a
          SpatialReference object or the coordinate system name.
         :transformation_name: - The geotransformation name.
        """
        from six import string_types, integer_types
        if HASARCPY:
            if isinstance(spatial_reference, SpatialReference):
                spatial_reference = spatial_reference.as_arcpy
            elif isinstance(spatial_reference, arcpy.SpatialReference):
                spatial_reference = spatial_reference
            elif isinstance(spatial_reference, integer_types):
                spatial_reference = arcpy.SpatialReference(spatial_reference)
            elif isinstance(spatial_reference, string_types):
                spatial_reference = arcpy.SpatialReference(
                    text=spatial_reference)
            else:
                raise ValueError("Invalid spatial reference object.")
            return Geometry(self.as_arcpy.projectAs(spatial_reference=spatial_reference,
                                                    transformation_name=transformation_name))
        return None

>>>

Basically, if ArcPy is not installed, it doesn't matter what you pass for the arguments because they are never being looked at, the function simply returns None immediately.

JohnSolly4
Esri Contributor

Thank you everyone. All makes sense now!

0 Kudos