Select to view content in your preferred language

Transform XY coordinate to WGS84 lat, long in a spatially enabled dataframe

4143
6
09-17-2021 05:44 PM
markjones6
Emerging Contributor

Hi,

As part of my python script, I have a spatially enabled data frame with ~1000 points in OSGB (EPSG:27700) X,Y which i would like to convert to WGS84 lat, long.  Is there a way, using the ArcGIS API for Python, to transform the OSGB XY coordinates to Lat/Long?  Or to transform the SHAPE field of the SDF from OSGB to WGS84?

 

Thanks in advance!

0 Kudos
6 Replies
David_Brooks
MVP Regular Contributor

@markjones6 is there any reason why you can't just use arcpy.

arcpy.management.Project(in_dataset, out_dataset)

to repoject from OSGB to WGS84? 


David
..Maps with no limits..
0 Kudos
markjones6
Emerging Contributor

Is it possible to use arcpy.management.Project whilst keeping all the data as a SDF?  My input data only exists in a SDF and I'd like to keep the output as a SDF to perform some additional processing before exporting out to a feature class.

0 Kudos
MehdiPira1
Esri Contributor

@markjones6,

The following script projects an SDF into a WGS84 lat long SDF:

 

from arcgis import GIS
gis = GIS('AGOL or Portal url', 'username', 'password')
# let's say you create an SDF from a feature layer first
item = gis.content.search("title: featureLyr")[0]
flayer = item.layers[0]
fset = flayer.query()
# project using WGS84 epsg code in the feature set
sdf_WGS84 = flayer.query(out_sr=4326).sdf
sdf_WGS84.head()

 

 Now if you check the SHAPE you should see the coordinates in lat and long format.

MehdiPira1_1-1632102830560.png

------------------------------------------------------------------------------------------------------------------------------------

Please give a like if you find it helpful. If it answered your question please accept as solution.

 

 

markjones6
Emerging Contributor

This is really interesting @MehdiPira1 , thank you!  

Is it possible to do something similar where the SDF doesn't exist as a feature layer? Basically I would like to work with my data only in my notebook as i have a number of data analysis to do before I publish out a "final" dataset to a feature layer. 

I was building on the work in this post - ArcGIS API for Python - geometry.project as below, but it would be great rather than transform the coordinates in this way, we could transform the SHAPE to lat/long without the need for an "existing" dataset or feature layer.

input_geoms = [{"x": -17568824.55, "y": 2428377.35}, {"x": -17568456.88, "y": 2428431.352}]
result = project(geometries=input_geoms, in_sr=3857, out_sr=4326)
result

 

 

0 Kudos
MehdiPira1
Esri Contributor

@markjones6 ,

Then the code snippet below is the way to follow:

 

from arcgis import geometry

# access the SHAPE field in your SDF and create a list
shape_list=[row.rings[0] for row in rows.SHAPE]
# do the projection on the list
for shape in shape_list:
    result = geometry.project(geometries=shape, in_sr=3857, out_sr=4326)
    print(result)

 

Bear in mind that this is based on the format below:

MehdiPira1_1-1632188447031.png

if you couldn't get it to work, I need to see a sample of your SDF to check the format.

 

 

0 Kudos
markjones6
Emerging Contributor

Hi, sorry for taking a while to reply, but thank you for your advice, it led me to a solution that's worked for my purposes.

What I did was to create a dictionary from my x,y columns and use the following to transform from OSGB to WGS84:

gis = GIS()
wgs84_coords_dict = project(geometries = osgb_coords_dict, in_sr=27700, out_sr=4326, transformation=1314)

 This creates a new coordinate dictionary - wgs84_coords_dict, which can then by merged with the remainder of the dataframe.

0 Kudos