问题
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"
回答1:
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')
来源:https://stackoverflow.com/questions/57255368/i-have-a-json-file-that-has-a-2263-projection-and-i-want-to-change-it-to-4326-us