Python API project

1035
3
Jump to solution
07-19-2021 12:14 AM
MeToo2
by
New Contributor

Could someone please provide an example of how to use this python function. I am unable to get it to work with a transformation. Ideally from Web Mercator to GDA lat long, Web Mercator  to WGS84 lat long and Web Mercator  to GDA94 MGA56

transformations = geometry.find_transformation(
in_sr=from_SR,
out_sr=toSR,
#extent_of_interest=self.in_extent
)

result = geometry.project(geometries = input_geom, in_sr = from_SR, out_sr = toSR, transform = transformations)

toSR= '4326' #WGS84 or 
toSR= '28356' #MGA 56

from_SR comes from the data and is 

spatialReference': {'wkid': 102100, 'latestWkid': 3857} I plug in 3857

0 Kudos
1 Solution

Accepted Solutions
MeToo2
by
New Contributor

Hi Hamish, thanks for your response. setting out the selected_transformation was a great help. Also clouding matters was a weird corruption error for my global constant for srout (in my code). This initially this was a string and I swear blind was working previously. I changed this to a int (as you had it in your code, but was still being corrupted by time of use). I had to cast this as an int in my function and it now works.

View solution in original post

0 Kudos
3 Replies
Tim_McGinnes
Occasional Contributor III

So the first issue is that you should provide wkid's as integers not strings and the second is that find_transformation returns a list of transforms in JSON, but the project function just wants the wkid for the transform.

Here's some code with outputs showing all the projections you were after. It's a bit more complicated than it needs to be because it is getting the transformation using the from and to SR, then programatically extracting the wkid for the transform. It's way easier just to have a cheatsheet with all your wkid's and directly enter the ones you need.

from arcgis.gis import GIS
from arcgis.geometry import Point, project, find_transformation
gis = GIS('home')

#Create a point in web mercator coordinates
ptwebm = Point({"x" : 17041106.13569748, "y" : -3183137.706970891})
srwebm = 3857

#Project point from Web Mercator to WGS84 (no transformation required)
srout = 4326
result = project(geometries = [ptwebm], in_sr = srwebm, out_sr = srout)
print("Projected Point: " + str(result))

 

Projected Point: [{'x': 153.08286100000004, 'y': -27.476550999999997, 'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}]

 

#Project point from Web Mercator to GDA94
srout = 4283
transforms=find_transformation(in_sr = srwebm, out_sr = srout)
print("Transforms: " + str(transforms))
srtransform=transforms['transformations'][0]['geoTransforms'][0]['latestWkid']
print("Selected Transform Wkid: " + str(srtransform))
result = project(geometries = [ptwebm], in_sr = srwebm, out_sr = srout, transformation = srtransform)
print("Projected Point: " + str(result))

 

Transforms: {'transformations': [{'geoTransforms': [{'wkid': 8050, 'latestWkid': 1150, 'transformForward': False, 'name': 'GDA_1994_To_WGS_1984'}]}]}


Selected Transform Wkid: 1150


Projected Point: [{'x': 153.08286100000004, 'y': -27.476550999227037, 'spatialReference': {'wkid': 4283, 'latestWkid': 4283}}]

 

#Project point from Web Mercator to MGA Zone 56
srout = 28356
transforms=find_transformation(in_sr = srwebm, out_sr = srout)
print("Transforms: " + str(transforms))
srtransform=transforms['transformations'][0]['geoTransforms'][0]['latestWkid']
print("Selected Transform Wkid: " + str(srtransform))
result = project(geometries = [ptwebm], in_sr = srwebm, out_sr = srout, transformation = srtransform)
print("Projected Point: " + str(result))

 

Transforms: {'transformations': [{'geoTransforms': [{'wkid': 8050, 'latestWkid': 1150, 'transformForward': False, 'name': 'GDA_1994_To_WGS_1984'}]}]}


Selected Transform Wkid: 1150


Projected Point: [{'x': 508186.1329199932, 'y': 6960777.826612913, 'spatialReference': {'wkid': 28356, 'latestWkid': 28356}}]

 

0 Kudos
HamishMorton
Esri Contributor

Hi,

Does the following help at all?

from arcgis import geometry
from arcgis.gis import GIS

gis = GIS("home")

point = [{"x": 12797393.0236, 
          "y": -3693437.2067}, 
         {"x": 12303304.0728, 
          "y": -3681207.2822}]

transformations_list = geometry.find_transformation(in_sr = 102100,
                                                    out_sr = 28356)

print(transformations_list)

selected_transform = transformations_list['transformations'][0]['geoTransforms'][0]['wkid']

result = geometry.project(geometries = point,
                          in_sr = 102100,
                          out_sr = 28356,
                          transformation = selected_transform)

print(result)
Hamish
MeToo2
by
New Contributor

Hi Hamish, thanks for your response. setting out the selected_transformation was a great help. Also clouding matters was a weird corruption error for my global constant for srout (in my code). This initially this was a string and I swear blind was working previously. I changed this to a int (as you had it in your code, but was still being corrupted by time of use). I had to cast this as an int in my function and it now works.

0 Kudos