0

How do i use the pyproj module to take a file from a path and just change it automatically?

The file also includes multiple rows of data. It would probably need to run a loop on all the coordinates and just change them?

I've added this code into the question based on your suggestions.

import os,shutil
import json
from pyproj import Proj,transform


#Create Desktop Folder
path= os.path.expanduser('~/Desktop/NAD83_to_WGS84')
path2=os.path.expanduser(path+'/EPSG_4326.json')
#Any file path for original 2263 file
original_2263= "C:\path\EPSG_2263.json"


#Creates new folder
def newpath(path_input):
     if not os.path.exists(path_input):
         os.makedirs(path)

 #copies original 2263 into new folder.
 def oldintonew():
     config=shutil.copy(original_2263,path)

 #Makes a second copy
 def secondtime():
    config=shutil.copy(original_2263,path2)


 p_web=Proj(init='EPSG:4326')

 with open (path2) as src:
     fc_in=json.load(src)

 # Define dictionary representation of output feature collection
 fc_out = {'features': [],
      'type': 'FeatureCollection'}

 # Iterate through each feature of the feature collection
 for feature in fc_in['features']:
     feature_out = feature.copy()
     new_coords = []
    # Project/transform coordinate pairs of each ring
     # (iteration required in case geometry type is MultiPolygon, or 
 there are holes)
     for ring in feature['geometry']['coordinates']:
         x2, y2 = p_web(*zip(*ring))
         new_coords.append(zip(x2, y2))
     # Append transformed coordinates to output feature
     feature_out['geometry']['coordinates'] = new_coords
     # Append feature to output featureCollection
     fc_out['features'].append(feature_out)

 print(fc_out)





 newpath(path)
 oldintonew()
 secondtime()

Now I get an error message "TypeError:zip() argument after* must be an iterable, not float"

user35131
  • 1,105
  • 6
  • 18

1 Answers1

0

I suggest using geopandas, which will make this task far simpler, especially if it contains several sets of data.

import geopandas as gpd

my_gdf = gpd.read_file('path/to/geo.json')
my_gdf = my_gdf.to_crs({'init': 'epsg:4326'})

If you want to store it as a JSON again, you can do the following:

my_gdf.to_file('path/to/out.json', driver="GeoJSON", encoding='utf-8')
bexi
  • 1,186
  • 5
  • 9
  • This looks so convenient and wish i could try it out. For some reason i can't download geopandas or its dependencies. I'm on some restriction it seems at my office. There is no way to do it through pyproj? – user35131 Jul 29 '19 at 15:17
  • Have you tried both `conda` and `pip`? In case you can't use geopandas, I think [this](https://gis.stackexchange.com/a/248205/146303) is the answer you are looking for – bexi Jul 29 '19 at 16:29