我有一个json文件,它有一个2263投影,我想用pyproj把它改成4326?

2024-04-27 05:49:18 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用pyproj模块从路径获取文件并自动更改它?你知道吗

该文件还包含多行数据。它可能需要在所有坐标上运行一个循环,然后改变它们?你知道吗

根据你的建议,我在问题中添加了这个代码。你知道吗

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()

现在我收到一条错误信息“类型错误:zip()*后面的参数必须是iterable,而不是float


Tags: topathinimportjsonnewosout
1条回答
网友
1楼 · 发布于 2024-04-27 05:49:18

我建议使用geopandas,这将使此任务更简单,特别是当它包含多组数据时。你知道吗

import geopandas as gpd

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

如果要再次将其存储为JSON,可以执行以下操作:

my_gdf.to_file('path/to/out.json', driver="GeoJSON", encoding='utf-8')

相关问题 更多 >