我有一个具有2263投影的json文件,我想使用pyproj将其更改为4326? [英] I have a json file that has a 2263 projection and i want to change it to 4326 using pyproj?
问题描述
我如何使用pyproj模块从路径中获取文件并自动对其进行更改?
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()
现在,我收到一条错误消息"*之后的TypeError:zip()参数必须是可迭代的,而不是浮点数"
Now I get an error message "TypeError:zip() argument after* must be an iterable, not float"
推荐答案
我建议使用geopandas
,这将使此任务更加简单,尤其是当它包含多组数据时.
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'})
如果要再次将其存储为JSON,可以执行以下操作:
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')
这篇关于我有一个具有2263投影的json文件,我想使用pyproj将其更改为4326?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!