Select to view content in your preferred language

Re-project geometry's spatialReference

3511
5
Jump to solution
05-20-2021 10:33 AM
MingHome
Regular Contributor

Dear All,

I am writing codes to add more features to an agol hosted point layer using arcgis API for Python. It is a seemingly straightforward task, but I encountered problem to deal with geometry's spatial reference.

the coordinates of my source points and target agol layer are in deferent spatial references system, source is in WJID 4326 and the target is in WKID 3857. Well, I have two questions:

First, could I add feature(s) with different SR to  an Agol featureLayer (using featureLayer.edit_features(adds=newFeatureDict). I thought that it could not (I am an old GIS guy). However, the input of new feature has the option for define SR for each feature. It is not mentioned in the documents "the new feature must have the same sr with the target featureLayer. This made me wondering if there is a "internal on the fly transformation" for the method. I tried and the new points landed in African instead to Ohio!

Second, How to use the arcgis.geometry.project to re-project geometry from one sr to another. in the documentation, there is a very simple and straightforward module (arcgis.geometry.project) dedicated for this purpose. But it did not work and returns a error message. I even tried using the example copied from the documentation. it returns the sample error.  

from arcgis.geometry import project            
input_geom = [{"x": -17568824.55, "y": 2428377.35}, {"x": -17568456.88, "y": 2428431.352}]
result = project(geometries=input_geom, in_sr=3857, out_sr=4326)

Exception: 'NoneType' object has no attribute '_tools'

I called ESRI tech support and as usaul, I am still waiting ...... Please help!

Ming

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

I hate to be this guy, but when I run your code, I get a successful result.

jcarlson_0-1621532627216.png

I haven't modified anything, as you can see.

Where are you running this code?

Also, if the source is a service, you can specify the output SR in the query function, which may help here.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
MingHome
Regular Contributor

I am glad tht you are the guy and you ran it! At least and module should work!

I ran it in PyCharm and i tried it again. It seems that it need one to login arcgis.gis t run. as soon I added logged in, it works now:

from arcgis.geometry import project
from arcgis.gis import GIS

gis = GIS(url="https://www.arcgis.com", username="me", password="that")
input_geom = [{"x": -17568824.55, "y": 2428377.35}, {"x": -17568456.88, "y": 2428431.352}]
result = project(geometries=input_geom, in_sr=3857, out_sr=4326)
print(result)

 

I will run it with my data now!

Thanks!

 

Ming 

View solution in original post

5 Replies
jcarlson
MVP Esteemed Contributor

I hate to be this guy, but when I run your code, I get a successful result.

jcarlson_0-1621532627216.png

I haven't modified anything, as you can see.

Where are you running this code?

Also, if the source is a service, you can specify the output SR in the query function, which may help here.

- Josh Carlson
Kendall County GIS
0 Kudos
MingHome
Regular Contributor

I am glad tht you are the guy and you ran it! At least and module should work!

I ran it in PyCharm and i tried it again. It seems that it need one to login arcgis.gis t run. as soon I added logged in, it works now:

from arcgis.geometry import project
from arcgis.gis import GIS

gis = GIS(url="https://www.arcgis.com", username="me", password="that")
input_geom = [{"x": -17568824.55, "y": 2428377.35}, {"x": -17568456.88, "y": 2428431.352}]
result = project(geometries=input_geom, in_sr=3857, out_sr=4326)
print(result)

 

I will run it with my data now!

Thanks!

 

Ming 

DarrenSmith
Regular Contributor

Hi Josh, do you have an example of how to " specify the output SR in the query function", or can you point me to it in the documentation. I can't find it anywhere 😞
Thanks,

Darren

0 Kudos
BlakeTerhune
MVP Regular Contributor

Hi @DarrenSmith, is this what you're looking for?

arcgis.features module | ArcGIS API for Python

The query method of a FeatureLayer has an out_sr parameter.

0 Kudos
DarrenSmith
Regular Contributor

Thanks @BlakeTerhune. Yes, I eventually figured it out back in October, but I forgot to post an update. Thanks for taking the time to reply.

0 Kudos