0

I have one shapefile with XY coordinates in meters. Now, I want to develop one script in Python which converts meters to decimal degrees.

Can you help me to develop this?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user876307
  • 191
  • 3
  • 9

4 Answers4

4

Sounds like you are looking to project your shapefile. Look at Project (Data Management) which:

Projects spatial data from one coordinate system to another.

It has examples of how to do it using Python.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Sasa Ivetic
  • 5,190
  • 23
  • 22
1

It would really help if you would write what do you want to do exactly. You can use GDAL to do this or call a web service if you would find one. For example you can try using this http://www.opencts.org/guidedwcts.php in your python code.

If you want to write it completely on your own, convert this java code to python. There is also some theory on what really happens there.

Glorfindel
  • 1,096
  • 2
  • 9
  • 14
Mykolas Simutis
  • 776
  • 1
  • 6
  • 15
1

The easiest way to do it is using GDAL command line calls from Python.

So, using the line from above (ogr2ogr ...) along with a GDAL install (see Installing GDAL with Python on windows?) you can sort out a method which will do it for you.

The more comprehensive way is to find a shapefile importing tool and then using pyproj to do it natively in Python. I would try the former for a script, and the latter if you want something embedded in a portable application.

Alex Leith
  • 13,453
  • 30
  • 69
0

This should give you a good starting point:

# Import necessary modules
import shapefile
from pyproj import Proj, transform

Read in the shapefile

sf = shapefile.Reader("path/to/shapefile.shp")

Define the projection for the shapefile

inProj = Proj(init="epsg:3857")

Define the projection for decimal degrees

outProj = Proj(init="epsg:4326")

Loop through each feature in the shapefile

for feature in sf.shapeRecords(): # Get the coordinates of the feature coords = feature.shape.points

# Convert the coordinates from meters to decimal degrees
coords_dd = transform(inProj, outProj, coords[0], coords[1])

This code uses the pyproj module to convert the coordinates from meters to decimal degrees. It loops through each feature in the shapefile and converts the coordinates using the transform() function.

Viv
  • 675
  • 5
  • 21