How to Fix 'TypeError: transform() missing 1 required positional argument: 'y'' in PyProj
If you're encountering the 'TypeError: transform() missing 1 required positional argument: 'y'' error while using the pyproj.transform() function, it's likely because you're not providing the 'x' and 'y' coordinates as separate arguments. The transform() function expects individual 'x' and 'y' values for the point you want to transform.
Here's the corrected Python code demonstrating the proper usage of pyproj.transform():
import pyproj
from shapely.geometry import Point
# Define the original point with its coordinates
x = 116.4074
y = 39.9042
point = Point(x, y)
# Define the original CRS (e.g., WGS84, EPSG:4326)
original_crs = pyproj.Proj('EPSG:4326')
# Define the desired CRS (e.g., UTM Zone 50N, EPSG:32650)
desired_crs = pyproj.Proj('EPSG:32650')
# Transform the point to the desired CRS
transformed_x, transformed_y = pyproj.transform(original_crs, desired_crs, x, y)
transformed_point = Point(transformed_x, transformed_y)
# Print the transformed point
print(transformed_point)
Explanation:
- Import necessary libraries: We import
pyprojfor coordinate transformations andshapely.geometry.Pointto represent points. - Define coordinates and point: We define the 'x' and 'y' coordinates and create a
Pointobject. - Define CRSs: We define the original and desired coordinate reference systems using their EPSG codes.
- Perform transformation: We use
pyproj.transform()with separate 'x' and 'y' arguments to get the transformed coordinates. - Create transformed point: We create a new
Pointobject using the transformed coordinates.
This corrected code should resolve the 'TypeError' and accurately transform your point to the desired coordinate reference system.
原文地址: https://www.cveoy.top/t/topic/mBo 著作权归作者所有。请勿转载和采集!